replace links that have .md suffixes with clean links

to aid viewing the raw markdown source in e.g. a gitlab source browser,
or to aid navigation in vim with "gf" style commands to jump between
files, allow the markdown source to specify foo.md or whatever/index.md
explicitly, yet generate the clean URLs for linking in the HTML output

this assumes that nginx is serving "foo" with foo.html, and "bar/" with
bar/index.html

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
2025-09-18 14:19:48 -05:00
parent 7205bb2aa5
commit 424ec3621d
4 changed files with 41 additions and 0 deletions

View File

@@ -58,7 +58,18 @@ def parse_md(path: str, pages_root: str):
with open(absolute_path, 'r') as input_file:
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(input_file.name), tz=datetime.timezone.utc)
entry = input_file.read()
logger.debug("path '%s' read", absolute_path)
# remove .md extensions used for navigating in vim and replace them with
# the pattern we use for HTML output here
# foo/index.md -> foo/, foo/index.md#anchor -> foo/#anchor
# ../index.md -> ../, ../index.md#anchor -> ../#anchor
entry = re.sub(r'\[([^]]+)\]\(([^)]+)index.md(#[^)]*)?\)', r'[\1](\2\3)', entry)
# index.md -> ., index.md#anchor -> .#anchor
entry = re.sub(r'\[([^]]+)\]\(index.md(#[^)]*)?\)', r'[\1](.\2)', entry)
# bar.md -> bar, foo/bar.md -> foo/bar, bar.md#anchor -> bar#anchor
entry = re.sub(r'\[([^]]+)\]\(([^)]+).md(#[^)]*)?\)', r'[\1](\2\3)', entry)
md = init_md()
content = Markup(md.convert(entry)) # nosec B704
except (OSError, FileNotFoundError):