From d63c59b3265ea5a0f9a01f34dacaea6e392803bc Mon Sep 17 00:00:00 2001 From: Mitesh Ashar Date: Tue, 13 Dec 2022 20:46:17 +0530 Subject: [PATCH] Markdown-it-py plugin to add language-none class code fences without a language. Always load Prismjs styles. Consequent changes in JS flow and tests. --- funnel/assets/js/utils/prism.js | 5 +++-- funnel/utils/markdown/base.py | 7 ++++++- funnel/utils/markdown/mdit_plugins/__init__.py | 1 + funnel/utils/markdown/mdit_plugins/fence_ext.py | 13 +++++++++++++ tests/unit/utils/markdown/data/code.toml | 4 ++-- 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 funnel/utils/markdown/mdit_plugins/fence_ext.py diff --git a/funnel/assets/js/utils/prism.js b/funnel/assets/js/utils/prism.js index e4a385a2b..2410ef847 100644 --- a/funnel/assets/js/utils/prism.js +++ b/funnel/assets/js/utils/prism.js @@ -15,7 +15,8 @@ const PrismEmbed = { ]; let asset = 0; const loadPrismStyle = () => { - $('head').append($(``)); + if (!$(`link[href*="${CDN_CSS}"]`).length) + $('head').append($(``)); }; const loadPrismScript = () => { $.ajax({ @@ -41,8 +42,8 @@ const PrismEmbed = { } }); }; + loadPrismStyle(); if (!window.Prism) { - loadPrismStyle(); loadPrismScript(); } else this.activatePrism(); }, diff --git a/funnel/utils/markdown/base.py b/funnel/utils/markdown/base.py index dc28989a9..5b9d3cfe2 100644 --- a/funnel/utils/markdown/base.py +++ b/funnel/utils/markdown/base.py @@ -27,6 +27,7 @@ from .mdit_plugins import ( # toc_plugin, del_plugin, embeds_plugin, + fence_extend_plugin, ins_plugin, mark_plugin, sub_plugin, @@ -170,11 +171,14 @@ def render(self, text: Optional[str]) -> Optional[Markup]: MarkdownPlugin('markmap', embeds_plugin, {'name': 'markmap'}) MarkdownPlugin('vega-lite', embeds_plugin, {'name': 'vega-lite'}) MarkdownPlugin('mermaid', embeds_plugin, {'name': 'mermaid'}) +MarkdownPlugin('fence_ext', fence_extend_plugin) # MarkdownPlugin('toc', toc_plugin) # --- Markdown configurations ---------------------------------------------------------- -MarkdownConfig(name='basic', options_update={'html': False, 'breaks': True}) +MarkdownConfig( + name='basic', options_update={'html': False, 'breaks': True}, plugins={'fence_ext'} +) MarkdownConfig( name='document', preset='gfm-like', @@ -196,6 +200,7 @@ def render(self, text: Optional[str]) -> Optional[Markup]: 'markmap', 'vega-lite', 'mermaid', + 'fence_ext', # 'toc', }, enable_rules={'smartquotes'}, diff --git a/funnel/utils/markdown/mdit_plugins/__init__.py b/funnel/utils/markdown/mdit_plugins/__init__.py index 815bb5c7b..72a5a6343 100644 --- a/funnel/utils/markdown/mdit_plugins/__init__.py +++ b/funnel/utils/markdown/mdit_plugins/__init__.py @@ -3,6 +3,7 @@ from .del_tag import * from .embeds import * +from .fence_ext import * from .ins_tag import * from .mark_tag import * from .sub_tag import * diff --git a/funnel/utils/markdown/mdit_plugins/fence_ext.py b/funnel/utils/markdown/mdit_plugins/fence_ext.py new file mode 100644 index 000000000..6ff144176 --- /dev/null +++ b/funnel/utils/markdown/mdit_plugins/fence_ext.py @@ -0,0 +1,13 @@ +"""Markdown-it-py plugin to add language-none class to code fence tokens.""" + +from markdown_it import MarkdownIt +from markdown_it.renderer import RendererHTML + + +def fence_extend_plugin(md: MarkdownIt, **opts) -> None: + def fence(self, tokens, idx, options, env): + output = RendererHTML.fence(self, tokens, idx, options, env) + output = output.replace('
', '
')
+        return output
+
+    md.add_render_rule('fence', fence)
diff --git a/tests/unit/utils/markdown/data/code.toml b/tests/unit/utils/markdown/data/code.toml
index 1ed8a4c3f..64ab0e2b9 100644
--- a/tests/unit/utils/markdown/data/code.toml
+++ b/tests/unit/utils/markdown/data/code.toml
@@ -72,7 +72,7 @@ line 2 of code
 line 3 of code
 

Block code "fences"

-
Sample text here...
+
Sample text here...
 It is a sample text that has multiple lines
 

Syntax highlighting

@@ -120,7 +120,7 @@ line 2 of code line 3 of code

Block code “fences”

-
Sample text here...
+
Sample text here...
 It is a sample text that has multiple lines
 

Syntax highlighting