Skip to content

Commit d76223c

Browse files
committed
fix: Fix non-rendered Markdown in documents
1 parent 5b4e24b commit d76223c

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

packages/dsw-document-worker/dsw/document_worker/model/utils.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,44 @@ def extendMarkdown(self, md):
4141

4242
class DSWMarkdownProcessor(markdown.preprocessors.Preprocessor):
4343
LI_RE = re.compile(r'^[ ]*((\d+\.)|[*+-])[ ]+.*')
44+
# Opening of a fenced code block, mirroring the `fenced_code` extension:
45+
# a run of at least three backticks or tildes at the start of the line.
46+
FENCE_RE = re.compile(r'^(?P<fence>`{3,}|~{3,})')
4447

4548
def __init__(self, md):
4649
super().__init__(md)
4750

51+
def _find_fence_close(self, lines, start, fence):
52+
# `fenced_code` closes on the exact same marker (same character and
53+
# length), optionally followed by trailing spaces.
54+
for index in range(start, len(lines)):
55+
if lines[index].rstrip(' ') == fence:
56+
return index
57+
return None
58+
4859
def run(self, lines):
4960
prev_li = False
5061
new_lines = []
62+
index = 0
63+
64+
while index < len(lines):
65+
line = lines[index]
66+
67+
# Copy complete fenced code blocks verbatim so that list-like or
68+
# backslash-terminated code lines are not rewritten. A block counts
69+
# only when a matching closing fence exists later, exactly as the
70+
# `fenced_code` extension requires; an unterminated fence is treated
71+
# as ordinary text.
72+
fence_match = self.FENCE_RE.match(line)
73+
if fence_match is not None:
74+
fence = fence_match.group('fence')
75+
close = self._find_fence_close(lines, index + 1, fence)
76+
if close is not None:
77+
new_lines.extend(lines[index:close + 1])
78+
prev_li = False
79+
index = close + 1
80+
continue
5181

52-
for line in lines:
5382
# Add line break before the first list item
5483
if self.LI_RE.match(line):
5584
if not prev_li:
@@ -62,9 +91,11 @@ def run(self, lines):
6291
_line = line.rstrip('\\')
6392
if line[-1:] == '\\' and (len(line) - len(_line)) % 2 == 1:
6493
new_lines.append(f'{line[:-1]} ')
94+
index += 1
6595
continue
6696

6797
new_lines.append(line)
98+
index += 1
6899

69100
return new_lines
70101

@@ -76,5 +107,11 @@ def render_markdown(md_text: str):
76107
text=md_text,
77108
extensions=[
78109
DSWMarkdownExt(),
110+
'fenced_code',
111+
'pymdownx.tilde',
79112
],
113+
extension_configs={
114+
# only enable ~~strikethrough~~, keep single ~tilde~ literal
115+
'pymdownx.tilde': {'subscript': False},
116+
},
80117
))

packages/dsw-document-worker/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies = [
2727
"panflute", # required by the pandoc-docx-pagebreak addon
2828
"pathvalidate",
2929
"pluggy",
30+
"pymdown-extensions",
3031
"python-dateutil",
3132
"python-slugify",
3233
"rdflib",

uv.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)