generated from executablebooks/mdformat-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
fix(#54): add 4-space indented deflists #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5218c55
fix(#54): add 4-space indented deflists
KyleKing 19ff403
fix: finish implementation and unit tests
KyleKing 0d987b4
ci: correct type error
KyleKing d5cf739
build: update test dependencies
KyleKing db8ac33
refactor: back to Latin
KyleKing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,111 @@ | ||
"""Material Definition Lists. | ||
|
||
Based on | ||
[mdformat-deflist](https://github.com/executablebooks/mdformat-deflist/blob/bbcf9ed4f80847db58b6f932ed95e2c7a6c49ae5/mdformat_deflist/plugin.py), | ||
but modified for mkdocs-material conventions. | ||
|
||
Example: | ||
```md | ||
`Lorem ipsum dolor sit amet` | ||
|
||
: Sed sagittis eleifend rutrum. Donec vitae suscipit est. Nullam tempus | ||
tellus non sem sollicitudin, quis rutrum leo facilisis. | ||
|
||
`Cras arcu libero` | ||
|
||
: Aliquam metus eros, pretium sed nulla venenatis, faucibus auctor ex. Proin | ||
ut eros sed sapien ullamcorper consequat. Nunc ligula ante. | ||
|
||
Duis mollis est eget nibh volutpat, fermentum aliquet dui mollis. | ||
Name vulputate tincidunt fringilla. | ||
Nullam dignissim ultrices urna non auctor. | ||
``` | ||
|
||
Docs: | ||
<https://squidfunk.github.io/mkdocs-material/reference/lists/#using-definition-lists> | ||
|
||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from markdown_it import MarkdownIt | ||
from mdit_py_plugins.deflist import deflist_plugin | ||
|
||
if TYPE_CHECKING: | ||
from markdown_it import MarkdownIt | ||
from mdformat.renderer import RenderContext, RenderTreeNode | ||
from mdformat.renderer.typing import Render | ||
|
||
|
||
def material_deflist_plugin(md: MarkdownIt) -> None: | ||
"""Add mkdocs-material definition list support to markdown-it parser.""" | ||
md.use(deflist_plugin) | ||
|
||
|
||
def make_render_children(separator: str) -> Render: | ||
"""Create a renderer that joins child nodes with a separator.""" | ||
|
||
def render_children( | ||
node: RenderTreeNode, | ||
context: RenderContext, | ||
) -> str: | ||
return separator.join(child.render(context) for child in node.children) | ||
|
||
return render_children | ||
|
||
|
||
def render_material_definition_list( | ||
node: RenderTreeNode, | ||
context: RenderContext, | ||
) -> str: | ||
"""Render Material Definition List.""" | ||
return make_render_children("\n")(node, context) | ||
|
||
|
||
def render_material_definition_term( | ||
node: RenderTreeNode, | ||
context: RenderContext, | ||
) -> str: | ||
"""Render Material Definition Term.""" | ||
return make_render_children("\n")(node, context) | ||
|
||
|
||
def render_material_definition_body( | ||
node: RenderTreeNode, | ||
context: RenderContext, | ||
) -> str: | ||
"""Render the definition body.""" | ||
tight_list = all( | ||
child.type != "paragraph" or child.hidden for child in node.children | ||
) | ||
marker = ": " # FYI: increased for material | ||
indent_width = len(marker) | ||
context.env["indent_width"] += indent_width | ||
try: | ||
text = make_render_children("\n\n")(node, context) | ||
lines = text.splitlines() | ||
if not lines: | ||
return ":" | ||
indented_lines = [f"{marker}{lines[0]}"] + [ | ||
f"{' ' * indent_width}{line}" if line else "" for line in lines[1:] | ||
] | ||
joined_lines = ("" if tight_list else "\n") + "\n".join(indented_lines) | ||
next_sibling = node.next_sibling | ||
return joined_lines + ( | ||
"\n" if (next_sibling and next_sibling.type == "dt") else "" | ||
) | ||
finally: | ||
context.env["indent_width"] -= indent_width | ||
|
||
|
||
def escape_deflist( | ||
text: str, | ||
node: RenderTreeNode, # noqa: ARG001 | ||
context: RenderContext, # noqa: ARG001 | ||
) -> str: | ||
"""Escape line starting ":" which would otherwise be parsed as a definition list.""" | ||
return "\n".join( | ||
"\\" + line if line.startswith(":") else line for line in text.split("\n") | ||
) |
This file contains hidden or 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 hidden or 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 hidden or 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,179 @@ | ||
Pandoc (with slightly changed indents): | ||
. | ||
paragraph | ||
|
||
Term 1 | ||
|
||
: Definition 1 | ||
|
||
Term 2 with *inline markup* | ||
|
||
: Definition 2 | ||
|
||
{ some code, part of Definition 2 } | ||
|
||
Third paragraph of definition 2. | ||
|
||
paragraph | ||
. | ||
paragraph | ||
|
||
Term 1 | ||
|
||
: Definition 1 | ||
|
||
Term 2 with *inline markup* | ||
|
||
: Definition 2 | ||
|
||
``` | ||
{ some code, part of Definition 2 } | ||
``` | ||
|
||
Third paragraph of definition 2. | ||
|
||
paragraph | ||
. | ||
|
||
Pandoc 2: | ||
. | ||
Term 1 | ||
|
||
: Definition | ||
with lazy continuation. | ||
|
||
Second paragraph of the definition. | ||
. | ||
Term 1 | ||
|
||
: Definition | ||
with lazy continuation. | ||
|
||
Second paragraph of the definition. | ||
. | ||
|
||
Pandoc 3 | ||
. | ||
paragraph | ||
|
||
Term 1 | ||
~ Definition 1 | ||
|
||
Term 2 | ||
~ Definition 2a | ||
~ Definition 2b | ||
|
||
paragraph | ||
. | ||
paragraph | ||
|
||
Term 1 | ||
: Definition 1 | ||
|
||
Term 2 | ||
: Definition 2a | ||
: Definition 2b | ||
|
||
paragraph | ||
. | ||
|
||
Spaces after a colon: | ||
. | ||
Term 1 | ||
: paragraph | ||
|
||
Term 2 | ||
: code block | ||
. | ||
Term 1 | ||
: paragraph | ||
|
||
Term 2 | ||
: ``` | ||
code block | ||
``` | ||
. | ||
|
||
List is tight, only if all dts are tight: | ||
. | ||
Term 1 | ||
: foo | ||
: bar | ||
|
||
Term 2 | ||
: foo | ||
|
||
: bar | ||
. | ||
Term 1 | ||
|
||
: foo | ||
|
||
: bar | ||
|
||
Term 2 | ||
|
||
: foo | ||
|
||
: bar | ||
. | ||
|
||
|
||
Regression test (first paragraphs shouldn't be tight): | ||
. | ||
Term 1 | ||
: foo | ||
|
||
bar | ||
|
||
Term 2 | ||
: foo | ||
. | ||
Term 1 | ||
|
||
: foo | ||
|
||
bar | ||
|
||
Term 2 | ||
|
||
: foo | ||
. | ||
|
||
Nested definition lists: | ||
|
||
. | ||
test | ||
: foo | ||
: bar | ||
: baz | ||
: bar | ||
: foo | ||
. | ||
test | ||
: foo | ||
: bar | ||
: baz | ||
: bar | ||
: foo | ||
. | ||
|
||
Regression test (blockquote inside deflist) | ||
. | ||
foo | ||
: > bar | ||
: baz | ||
. | ||
foo | ||
: > bar | ||
: baz | ||
. | ||
|
||
Escaped deflist | ||
. | ||
Term 1 | ||
\: Definition | ||
. | ||
Term 1 | ||
\: Definition | ||
. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.