-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Mermaid.js syntax via fenced code blocks.
Closes kiwitcms/Kiwi#3116 if you type ```mermaid <diagram code goes here> ``` this will be rendered as a PNG image via the https://mermaid.ink service and the image will be automatically injected into the resulting HTML once markdown is rendered.
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
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
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,52 @@ | ||
# Copyright (c) 2024 Alexander Todorov <atodorov@otb.bg> | ||
# | ||
# Licensed under GNU Affero General Public License v3 or later (AGPLv3+) | ||
# https://www.gnu.org/licenses/agpl-3.0.html | ||
|
||
# pylint: disable=too-few-public-methods | ||
|
||
import base64 | ||
import markdown | ||
|
||
|
||
def mermaid2url(src_code): | ||
""" | ||
Convert a Mermaid source code into a viewable URL where the image | ||
is generated by the Mermaid.Ink Web Service! | ||
""" | ||
as_base64 = base64.b64encode(src_code.encode("utf8")) | ||
as_base64 = as_base64.decode("ascii") | ||
return f"https://mermaid.ink/img/{as_base64}?type=png" | ||
|
||
|
||
class MermaidPreprocessor(markdown.preprocessors.Preprocessor): | ||
def run(self, lines): | ||
new_lines = [] | ||
diagram_src = [] | ||
diagram_started = False | ||
|
||
for line in lines: | ||
if line.lower().find("```mermaid") > -1: | ||
diagram_started = True | ||
diagram_src = [] | ||
elif diagram_started and line.lower().strip() == "```": | ||
diagram_started = False | ||
|
||
image_url = mermaid2url("\n".join(diagram_src)) | ||
new_lines.append(f"![diagram.png]({image_url})") | ||
elif diagram_started: | ||
diagram_src.append(line) | ||
else: | ||
new_lines.append(line) | ||
|
||
return new_lines | ||
|
||
|
||
class KiwiTCMSExtension(markdown.extensions.Extension): | ||
def extendMarkdown(self, md): | ||
# Insert a preprocessor before ReferencePreprocessor | ||
md.preprocessors.register(MermaidPreprocessor(md), 'mermaid', 35) | ||
|
||
|
||
def makeExtension(**kwargs): # pylint: disable=invalid-name | ||
return KiwiTCMSExtension(**kwargs) |
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