Skip to content

Commit 00909d8

Browse files
committed
Fixing issue with complex mermaid charts not rendering
1 parent 3f6addb commit 00909d8

File tree

5 files changed

+26
-65
lines changed

5 files changed

+26
-65
lines changed

Dockerfile

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
FROM python:3.12-alpine
22

3-
# Install required packages
4-
RUN apk add --no-cache bash
3+
RUN pip install --no-cache-dir markopolis==0.3.1
54

6-
# Install markopolis
7-
RUN pip install --no-cache-dir markopolis==0.3.0
8-
9-
# Set working directory
105
WORKDIR /app
116

12-
# Copy the .env file if it exists
13-
COPY .env* /app/
14-
15-
# Create a startup script
16-
RUN echo '#!/bin/bash\n\
17-
if [ -f /app/.env ]; then\n\
18-
export $(cat /app/.env | xargs)\n\
19-
fi\n\
20-
exec markopolis run --port 8080\n\
21-
' > /app/start.sh && chmod +x /app/start.sh
22-
237
EXPOSE 8080
248

25-
# Run the startup script when the container launches
26-
CMD ["/app/start.sh"]
9+
# Run markopolis when the container launches
10+
CMD ["markopolis", "run", "--port", "8080"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ version: '3.8'
5252
5353
services:
5454
markopolis:
55-
image: ghcr.io/rishikanthc/markopolis:0.3.0
55+
image: ghcr.io/rishikanthc/markopolis:0.3.1
5656
ports:
5757
- "8080:8080"
5858
environment:

markopolis/md_extensions.py

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import markdown
2-
import string
32
from markdown.inlinepatterns import InlineProcessor
43
import xml.etree.ElementTree as etree
54
from markdown.preprocessors import Preprocessor
@@ -116,60 +115,36 @@ def extendMarkdown(self, md):
116115
md.postprocessors.register(footnote_postprocessor, "footnote_postprocessor", 25)
117116

118117

119-
def strip_not_printable(text):
120-
"""Remove non-printable characters from a string."""
121-
return "".join(filter(lambda x: x in string.printable, text))
122-
123-
124-
# This regex matches the opening line for a mermaid code block in Obsidian's syntax.
125-
MermaidRegex = re.compile(r"^```[\ \t]*[Mm]ermaid[\ \t]*$")
126-
127-
128118
class MermaidPreprocessor(Preprocessor):
129119
def run(self, lines):
130120
new_lines = []
131-
in_mermaid_code = False
132-
is_mermaid = False
121+
is_mermaid_block = False
122+
mermaid_lines = []
133123

134124
for line in lines:
135-
if not in_mermaid_code:
136-
# Match the opening line of the mermaid code block
137-
if MermaidRegex.match(line):
138-
in_mermaid_code = True
139-
new_lines.append('<div class="mermaid">')
140-
is_mermaid = True
141-
else:
142-
new_lines.append(line)
125+
if line.strip() == "```mermaid":
126+
is_mermaid_block = True
127+
mermaid_lines = []
128+
elif line.strip() == "```" and is_mermaid_block:
129+
is_mermaid_block = False
130+
mermaid_content = "\n".join(mermaid_lines)
131+
mermaid_html = self.mermaid_to_html(mermaid_content)
132+
new_lines.append(mermaid_html)
133+
elif is_mermaid_block:
134+
mermaid_lines.append(line)
143135
else:
144-
# Match the closing line of the mermaid code block
145-
if line.strip() == "```":
146-
in_mermaid_code = False
147-
new_lines.append("</div>")
148-
new_lines.append("")
149-
else:
150-
# Strip non-printable characters and add the mermaid code lines
151-
new_lines.append(strip_not_printable(line).strip())
152-
153-
if is_mermaid:
154-
# Add the mermaid.js initialization script to the bottom of the document if any mermaid diagrams were found.
155-
new_lines.append("")
156-
new_lines.append("""<script>
157-
function initializeMermaid() {
158-
mermaid.initialize({startOnLoad: true});
159-
}
160-
if (document.readyState === "complete" || document.readyState === "interactive") {
161-
setTimeout(initializeMermaid, 1);
162-
} else {
163-
document.addEventListener("DOMContentLoaded", initializeMermaid);
164-
}
165-
</script>""")
136+
new_lines.append(line)
166137

167138
return new_lines
168139

140+
def mermaid_to_html(self, mermaid_content):
141+
escaped_content = mermaid_content.replace('"', "&quot;")
142+
return f'<div class="mermaid">{escaped_content}</div>'
143+
169144

170145
class MermaidExtension(Extension):
171146
def extendMarkdown(self, md):
172-
md.preprocessors.register(MermaidPreprocessor(md), "mermaid", 35)
147+
md.preprocessors.register(MermaidPreprocessor(md), "mermaid", 175)
173148

174149

175150
# Regular expression to match Obsidian callout syntax

markopolis/templates/page.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/dark.min.css"
2727
id="dark-theme" disabled>
2828
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
29-
<script src="/static/js/mermaid.min.js"></script>
3029
<script src="/static/js/callouts.js"></script>
30+
31+
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
32+
<script>mermaid.initialize({startOnLoad: true});</script>
3133
</head>
3234

3335
<body>

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "markopolis"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
description = "Self-hostable Obsidian Publish"
55
authors = ["Rishikanth Chandrasekaran <r3chandr@ucsd.edu>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)