generated from executablebooks/mdformat-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial admon plugin attempt (#1)
- Loading branch information
Showing
7 changed files
with
268 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,6 @@ repos: | |
- flake8-bugbear>=21.3.2 | ||
- flake8-builtins>=1.5.3 | ||
- flake8-comprehensions>=3.4.0 | ||
args: [ | ||
--ignore=E501 | ||
] |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import textwrap | ||
from typing import Mapping | ||
|
||
from markdown_it import MarkdownIt | ||
from mdformat.renderer import RenderContext, RenderTreeNode | ||
from mdformat.renderer.typing import Render | ||
from mdit_py_plugins.admon import admon_plugin | ||
|
||
|
||
def update_mdit(mdit: MarkdownIt) -> None: | ||
"""Update the parser,""" | ||
mdit.use(admon_plugin) | ||
|
||
|
||
def _render_admon(node: RenderTreeNode, context: RenderContext) -> str: | ||
"""Render a `RenderTreeNode` of type `admonition`. | ||
Primarily based on: | ||
- https://github.com/executablebooks/mdformat-footnote/blob/80852fc20cfba7fd0330b9ac7a1a4df983542942/mdformat_footnote/plugin.py#LL24-L40C29 | ||
And based on: | ||
- https://github.com/hukkin/mdformat-gfm/blob/cf316a121b6cf35cbff7b0ad6e171f287803f8cb/src/mdformat_gfm/plugin.py | ||
- https://github.com/hukkin/mdformat-toc/blob/42624b6f3468da4f793ec7425da872b030714774/mdformat_toc/plugin.py | ||
- https://github.com/executablebooks/mdformat-footnote/blob/80852fc20cfba7fd0330b9ac7a1a4df983542942/mdformat_footnote/plugin.py | ||
""" | ||
separator = "\n\n" # TODO: Is this configurable? | ||
indent = " " * 4 # FIXME: Is this configurable? | ||
title = node.info.strip() | ||
body = f"{node.markup} {title}{separator}" | ||
with context.indented(len(indent)): # Modifies context.env['indent_width'] | ||
elements = [child.render(context) for child in node.children[1:]] | ||
body += textwrap.indent(separator.join(elements), indent) | ||
return body | ||
|
||
|
||
# A mapping from syntax tree node type to a function that renders it. | ||
# This can be used to overwrite renderer functions of existing syntax | ||
# or add support for new syntax. | ||
RENDERERS: Mapping[str, Render] = {"admonition": _render_admon} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,227 @@ | ||
a test | ||
|
||
Simple admonition | ||
. | ||
!!! note | ||
*content* | ||
. | ||
!!! note | ||
*content* | ||
. | ||
|
||
|
||
Could contain block elements too | ||
. | ||
!!! note | ||
### heading | ||
|
||
----------- | ||
|
||
. | ||
!!! note | ||
### heading | ||
|
||
______________________________________________________________________ | ||
|
||
. | ||
|
||
|
||
Shows custom title | ||
. | ||
!!! note Custom title | ||
|
||
Some text | ||
|
||
. | ||
!!! note Custom title | ||
|
||
Some text | ||
|
||
. | ||
|
||
|
||
Shows no title | ||
. | ||
!!! note "" | ||
Some text | ||
|
||
. | ||
This is the input Markdown test, | ||
then below add the expected output. | ||
!!! note "" | ||
Some text | ||
|
||
. | ||
|
||
|
||
Closes block after 2 empty lines | ||
. | ||
!!! note | ||
Some text | ||
|
||
|
||
A code block | ||
. | ||
!!! note | ||
Some text | ||
|
||
|
||
A code block | ||
. | ||
|
||
|
||
Nested blocks | ||
. | ||
This is the input Markdown test, | ||
then below add the expected output. | ||
!!! note | ||
!!! note | ||
Some text | ||
|
||
code block | ||
. | ||
!!! note | ||
!!! note | ||
Some text | ||
|
||
another test | ||
code block | ||
. | ||
Some *markdown* | ||
|
||
- a | ||
- b | ||
* c | ||
|
||
Consecutive admonitions | ||
. | ||
Some *markdown* | ||
!!! note | ||
|
||
- a | ||
- b | ||
!!! warning | ||
. | ||
!!! note | ||
|
||
* c | ||
!!! warning | ||
. | ||
|
||
|
||
Marker may be indented up to 3 chars | ||
. | ||
!!! note | ||
content | ||
. | ||
!!! note | ||
content | ||
. | ||
|
||
|
||
But that's a code block | ||
. | ||
!!! note | ||
content | ||
. | ||
!!! note | ||
content | ||
. | ||
|
||
|
||
Some more indent checks | ||
. | ||
!!! note | ||
not a code block | ||
|
||
code block | ||
. | ||
!!! note | ||
not a code block | ||
|
||
code block | ||
. | ||
|
||
|
||
Type could be adjacent to marker | ||
. | ||
!!!note | ||
xxx | ||
|
||
. | ||
!!!note | ||
xxx | ||
|
||
. | ||
|
||
|
||
Type could be adjacent to marker and content may be shifted up to 3 chars | ||
. | ||
!!!note | ||
xxx | ||
|
||
. | ||
!!!note | ||
xxx | ||
|
||
. | ||
|
||
|
||
Or several spaces apart | ||
. | ||
!!! note | ||
xxx | ||
. | ||
!!! note | ||
xxx | ||
. | ||
|
||
|
||
Admonitions self-close at the end of the document | ||
. | ||
!!! note | ||
xxx | ||
. | ||
!!! note | ||
xxx | ||
. | ||
|
||
|
||
They could be nested in lists | ||
. | ||
- !!! note | ||
- a | ||
- b | ||
- !!! warning | ||
- c | ||
- d | ||
. | ||
- !!! note | ||
- a | ||
- b | ||
- !!! warning | ||
- c | ||
- d | ||
. | ||
|
||
|
||
Or in blockquotes | ||
. | ||
> !!! note | ||
> xxx | ||
> > yyy | ||
> zzz | ||
> | ||
. | ||
> !!! note | ||
> xxx | ||
> > yyy | ||
> zzz | ||
> | ||
. | ||
|
||
|
||
Renders unknown admonition type | ||
. | ||
!!! unknown title | ||
content | ||
. | ||
!!! unknown title | ||
content | ||
. | ||
|
||
|
||
Does not render | ||
. | ||
!!! | ||
content | ||
. | ||
!!! | ||
content | ||
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters