29. Stripping .md from internal links
Code forges—like Codeberg, GitLab, and GitHub—will display source files in Markdown format formatted as HTML. Markdown-based editors like Obsidian and Markdown viewers like MDViewer don’t even require the conversion step.
When displaying Markdown like this, links between documents will work more or
less as expected. They need to be relative URL references, and they need to link
to the actual file name. It follows they will end with .md (or .markdown).
So to link to the post about omitting .html, I want .md if I want
it to work in one of the above-mentioned Markdown viewers, but .html to
work with Mismiy.
How Mismiy can help
The change to the mismiy command is pretty simple: when processing relative links,
replace a final .md or .markdown with .html or nothing (in the --omit-dot.html case).
The one edge case is what if the author of a post wants to link to Markdown file
as Markdown source rather than as the derived HTML. The workaround for that
can be to use an absolute URL.
Now I can write the link like so in the Markdown file:
… to link to the [post about omitting `.html`], I want `.md` if I want …
[post about omitting `.html`]: 2025-12-23-omit-dot-html.md
There is a complication.
The complication
I forgot that although Mismiy munges the URLs of tag and index-page links, it does
not munge links created with […] in the Markdown! These are still .html
regardless of the flag. So my vague assumption that all we need to do is add
another check to the munging function is wrong.
The Markdown library we use, mistletoe, has an API for overriding the renderer used to convert its abstract syntax tree to HTML, described in its Developer’s Guide. This should be more reliable than trying to use yet more regexes to find links!
Changing this very simple method definition:
def body_html(self):
"""The body of the entry, formatted as HTML fragment."""
return mistletoe.markdown(self.body)
… with something like this:
def body_html(self, omit_dot_html=False) -> str:
"""The body of the entry, formatted as HTML fragment."""
with MismiyRenderer(omit_dot_html=omit_dot_html) as renderer:
return renderer.render(Document(self.body))
The class definition MismiyRenderer is pretty simple, since it can lean on mistletoe’s definition.
It just applies the same munged routine we already created for tag and index
links.
The munger needs to know to leave absolute URLs alone. Otherwise the Obsidian URL, which happens to be https://obsidian.md, would be broken!
Posts on similar topics