md updates

This commit is contained in:
Ignacio Ballesteros
2026-02-14 17:21:33 +01:00
parent 3738d1d7d8
commit 23aa2f4dc4
3 changed files with 110 additions and 10 deletions

View File

@@ -74,9 +74,9 @@ def process_wikilinks(content: str) -> str:
def process_citations(content: str, resolver) -> str:
"""Process org-mode citations and resolve them.
Handles:
Handles (after pandoc conversion, brackets may be escaped):
- cite:key
- [cite:@key]
- [cite:@key] or \[cite:@key\]
- [[cite:key][description]]
"""
if resolver is None:
@@ -86,18 +86,19 @@ def process_citations(content: str, resolver) -> str:
cite_key = match.group(1)
return resolver.resolve(cite_key)
# cite:key pattern
content = re.sub(r"cite:([a-zA-Z0-9_-]+)", replace_cite, content)
# cite:key pattern (not preceded by @, to avoid double-matching)
content = re.sub(r"(?<!@)cite:([a-zA-Z0-9_-]+)", replace_cite, content)
# [cite:@key] pattern
content = re.sub(r"\[cite:@([a-zA-Z0-9_-]+)\]", replace_cite, content)
# [cite:@key] pattern (escaped or not after pandoc)
# Pandoc escapes [ ] as \[ \]
content = re.sub(r"\\?\[cite:@([a-zA-Z0-9_-]+)\\?\]", replace_cite, content)
# [[cite:key][description]] - use resolved link but keep description context
def replace_cite_with_desc(match) -> str:
cite_key = match.group(1)
# [[cite:key][description]] - after pandoc becomes [description](cite:key)
def replace_cite_link(match) -> str:
cite_key = match.group(2)
return resolver.resolve(cite_key)
content = re.sub(r"\[\[cite:([a-zA-Z0-9_-]+)\]\[[^\]]*\]\]", replace_cite_with_desc, content)
content = re.sub(r"\[([^\]]+)\]\(cite:([a-zA-Z0-9_-]+)\)", replace_cite_link, content)
return content