-
Notifications
You must be signed in to change notification settings - Fork 8
👌 IMPROVE: Alternative proposal for allowing colon fences #48
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
Open
brunobeltran
wants to merge
2
commits into
executablebooks:master
Choose a base branch
from
brunobeltran:bruno/colon-fence-for-upstream
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−68
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from markdown_it import MarkdownIt | ||
| import mdformat.plugins | ||
| from mdformat.renderer import RenderContext, RenderTreeNode | ||
| from mdit_py_plugins.colon_fence import colon_fence_plugin | ||
| from mdit_py_plugins.dollarmath import dollarmath_plugin | ||
| from mdit_py_plugins.myst_blocks import myst_block_plugin | ||
| from mdit_py_plugins.myst_role import myst_role_plugin | ||
|
|
@@ -17,23 +18,16 @@ | |
|
|
||
|
|
||
| def update_mdit(mdit: MarkdownIt) -> None: | ||
| # Enable mdformat-tables plugin | ||
| tables_plugin = mdformat.plugins.PARSER_EXTENSIONS["tables"] | ||
| if tables_plugin not in mdit.options["parser_extension"]: | ||
| mdit.options["parser_extension"].append(tables_plugin) | ||
| tables_plugin.update_mdit(mdit) | ||
|
|
||
| # Enable mdformat-front-matters plugin | ||
| front_matters_plugin = mdformat.plugins.PARSER_EXTENSIONS["front_matters"] | ||
| if front_matters_plugin not in mdit.options["parser_extension"]: | ||
| mdit.options["parser_extension"].append(front_matters_plugin) | ||
| front_matters_plugin.update_mdit(mdit) | ||
|
|
||
| # Enable mdformat-footnote plugin | ||
| footnote_plugin = mdformat.plugins.PARSER_EXTENSIONS["footnote"] | ||
| if footnote_plugin not in mdit.options["parser_extension"]: | ||
| mdit.options["parser_extension"].append(footnote_plugin) | ||
| footnote_plugin.update_mdit(mdit) | ||
| plugins_to_enable = [ | ||
| "tables", | ||
| "front_matters", | ||
| "footnote", | ||
| ] | ||
| for plugin_name in plugins_to_enable: | ||
| plugin = mdformat.plugins.PARSER_EXTENSIONS[plugin_name] | ||
| if plugin not in mdit.options["parser_extension"]: | ||
| mdit.options["parser_extension"].append(plugin) | ||
| plugin.update_mdit(mdit) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 🌟 |
||
|
|
||
| # Enable MyST role markdown-it extension | ||
| mdit.use(myst_role_plugin) | ||
|
|
@@ -45,6 +39,9 @@ def update_mdit(mdit: MarkdownIt) -> None: | |
| # Enable dollarmath markdown-it extension | ||
| mdit.use(dollarmath_plugin) | ||
|
|
||
| # Enable support for the colon fence syntax | ||
| mdit.use(colon_fence_plugin) | ||
|
|
||
| # Trick `mdformat`s AST validation by removing HTML rendering of code | ||
| # blocks and fences. Directives are parsed as code fences and we | ||
| # modify them in ways that don't break MyST AST but do break | ||
|
|
@@ -117,7 +114,6 @@ def _escape_paragraph(text: str, node: RenderTreeNode, context: RenderContext) - | |
| lines = text.split("\n") | ||
|
|
||
| for i in range(len(lines)): | ||
|
|
||
| # Three or more "+" chars are interpreted as a block break. Escape them. | ||
| space_removed = lines[i].replace(" ", "") | ||
| if space_removed.startswith("+++"): | ||
|
|
@@ -144,15 +140,17 @@ def _escape_text(text: str, node: RenderTreeNode, context: RenderContext) -> str | |
| return text | ||
|
|
||
|
|
||
| CHANGES_AST = True | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this strictly necessary? Turning this on can introduce a lot of risk -- I'm assuming this is required to deal with the recursion? |
||
| RENDERERS = { | ||
| "blockquote": _math_block_safe_blockquote_renderer, | ||
| "colon_fence": fence, | ||
| "fence": fence, | ||
| "myst_role": _role_renderer, | ||
| "myst_line_comment": _comment_renderer, | ||
| "myst_block_break": _blockbreak_renderer, | ||
| "myst_target": _target_renderer, | ||
| "math_inline": _math_inline_renderer, | ||
| "math_block_label": _math_block_label_renderer, | ||
| "math_block": _math_block_renderer, | ||
| "fence": fence, | ||
| } | ||
| POSTPROCESSORS = {"paragraph": _escape_paragraph, "text": _escape_text} | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried about this the performance here and would prefer to have native support like regex parsing (proposed in #36), but directive content should be small and is something we could revisit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bigger issue is that I don't think any other plugin uses mdformat recursively like this, so it could lead to unexpected behavior