import re
REPO_BLOB = "https://github.com/powersemmi/ruststream/blob/main/"
FENCE = re.compile(r"^```(?P<info>[^\n`]*)\n(?P<body>.*?)^```$", re.M | re.S)
INCLUDE = re.compile(r'--8<--\s+"(?P<path>[^":]+)(?::[^"]*)?"')
_linked_paths: set[str] = set()
def on_page_markdown(markdown, **_kwargs):
def add_title(match):
info, body = match.group("info"), match.group("body")
include = INCLUDE.search(body)
if include is None or "title=" in info:
return match.group(0)
path = include.group("path")
_linked_paths.add(path)
return f'```{info} title="{path}"\n{body}```'
return FENCE.sub(add_title, markdown)
def on_page_content(html, **_kwargs):
for path in _linked_paths:
html = html.replace(
f'<span class="filename">{path}</span>',
f'<span class="filename"><a href="{REPO_BLOB}{path}">{path}</a></span>',
)
return html