mdformat icon indicating copy to clipboard operation
mdformat copied to clipboard

Support Admonitions

Open KyleKing opened this issue 3 years ago • 4 comments

Describe the problem/need and solution

Problem

Right now, mdformat breaks indentation for admonitions. Below is a repeatable example based on the first snippet from the admonition documentation

~/Developer > pipx install mdformat-gfm --include-deps
  installed package mdformat-gfm 0.3.5, installed using Python 3.10.2
  These apps are now globally available
    - markdown-it
    - mdformat
done! ✨ 🌟 ✨
~/Developer > pbpaste > ad_test.md
~/Developer > bat ad_test.md
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ad_test.md
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ !!! type "optional explicit title within double quotes"
   2   │     Any number of other indented markdown elements.
   3   │
   4   │     This is the second paragraph.
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
~/Developer > mdformat ad_test.md
~/Developer > bat ad_test.md
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ad_test.md
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ !!! type "optional explicit title within double quotes"
   2   │ Any number of other indented markdown elements.
   3   │
   4   │ ```
   5   │ This is the second paragraph.
   6   │ ```
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Solution

admonitions are not part of the CommonMark specification and would be best handled by a plugin (i.e. mdformat-admonition)

I would be happy to try writing one, but I wanted to open an issue first so that I'm not duplicating effort. I saw that there was at least one other issue with admonition problems, but there didn't seem to be any updates since: #53 (https://github.com/executablebooks/mdformat/issues/53#issuecomment-928306196)

Benefit

Would be nice to have support for admonitions (particular for mkdocs which uses the markdown extension)

Guide for implementation

No response

Tasks and updates

No response

KyleKing avatar Feb 20 '22 21:02 KyleKing

Hey thanks for the issue!

Yeah this should be a plugin, and the plugin template you linked should be helpful. The first step, however, is a parser extension for mdformat's Markdown parser (markdown-it-py which is a Python port of the JavaScript markdown-it).

You'll find the existing parser extensions in this collection repository: https://github.com/executablebooks/mdit-py-plugins. I suggest making a PR there (although it's entirely possible that you keep in a separate repo too).

Luckily you don't have to start from scratch: there seems to be a JavaScript version of the parser extension which you can port to Python https://github.com/commenthol/markdown-it-admon (I dont know whether the JS implementation is any good though).

hukkin avatar Feb 20 '22 21:02 hukkin

Great! I've got a busy next few weeks, but I'll start on this once I have time!

KyleKing avatar Feb 21 '22 01:02 KyleKing

@KyleKing had any chance to work on this?

lorenzomorandini avatar Sep 02 '22 13:09 lorenzomorandini

Thanks for reminder. I had completed forgotten about this ticket. This is still an issue for me, but haven't had time to work on it

KyleKing avatar Sep 02 '22 18:09 KyleKing

@KyleKing I guess there is still no advance, isn't it?

lyz-code avatar Nov 24 '22 17:11 lyz-code

Yeah, no progress yet

I actually had these tickets open to try to get to it this week, but haven't had time yet with school and holidays here

KyleKing avatar Nov 24 '22 18:11 KyleKing

Submitted a PR! It is a Draft, but I'll finish up and mark it ready for review sometime tomorrow

KyleKing avatar Nov 26 '22 15:11 KyleKing

And I started the plugin for mdformat-admon here: https://github.com/KyleKing/mdformat-admon

KyleKing avatar Nov 27 '22 14:11 KyleKing

This ticket keeps accidentally closing...but I had some time to make progress today!

If anyone is more familiar with mdformat and can weigh in on my implementation, let me know:

https://github.com/KyleKing/mdformat-admon/blob/main/mdformat_admon/plugin.py

KyleKing avatar Dec 14 '22 04:12 KyleKing

After a long, busy year, I finally have a v0.0.1 release of the admonition plugin!

I will test it in a real world project and will revisit if formats other than !!! will be supported, but I think we can close this issue (third times the charm!) and track any issues around admonition support in: https://github.com/KyleKing/mdformat-admon/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc 🎉

KyleKing avatar Dec 15 '22 13:12 KyleKing