Closed GuutBoy closed 11 months ago
Note in regular text this issue does not seem to be present. I have only seen this for headings.
Just to clarify, it looks like it isn't the Markdown
widget itself that's crashing, but rather the Tree
used for the table of contents, which is why this issues only occurs with headings.
Hey @GuutBoy thanks for the issue and @TomJGooding for the headstart on this.
@GuutBoy could you help me understand better how/why something like [[/test.md]]
might end up at the end of a markdown heading?
And what would be your expected output?
Would the [[/test.md]]
just disappear from the output markdown document?
I also don't think I followed your mention of wikilinks/markdown extensions.
Maybe the focus on wikilinks is not so important. The point is that any string like [/test]
(i.e., something than resembles a unclosed Rich tag) in a Markdown heading will cause Markdown widget to crash. It does not matter if this string is in the beginning, end or anywhere else in the heading. (In fact as TomJGooding points out it seems to be the Tree widget that is really causing the issue).
I just mentioned wikilinks because this is how I noticed the issue because wikilinks are likely to trigger this issue. The use of wikilinks is a common extension to Markdown. WikiLinks is a different way to make links than the regular syntax for links in Markdown. A wikilink surrounds the link path in double brackets like so [[link]]
. If the path of you link starts with a /
you trigger this issue (something that would be common if you are linking to local files). Ideally, wikilinks would be treated as regular Markdown links. But at least it would be good widgets did not just crash on these types of strings
Why would someone have wikilinks in a heading? Well, why not?
Btw. I noticed that if the I make a regular markdown link with the text starting in a /
the issue is not triggered. Like so [/test](/test.md)
.
Thanks for the additional context, @GuutBoy. PR 3697 will take care of the crashing table of contents and when that's merged, it will close this issue.
Feel free to open a new issue if you'd like to request support for wikilink-style tags/links. (This is not an endorsement for such a feature request!)
Thanks to the parser_factory
parameter in the Markdown
widget, you could potentially create a custom markdown-it-py parser to handle WikiLinks.
I've managed to hack together a quick example after a couple of hours looking into markdown-it-py plugins. This isn't a working example as I don't actually understand how WikiLinks should work, but hopefully gives an idea what is possible.
import re
from markdown_it import MarkdownIt
from markdown_it.rules_inline import StateInline
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Markdown
WIKILINK_PATTERN = re.compile(r"\[\[([^\[\]\|\:]+)\]\]")
def wikilinks_plugin(md: MarkdownIt) -> None:
md.inline.ruler.before("link", "wikilink", _wikilink_rule)
def _wikilink_rule(state: StateInline, silent: bool) -> bool:
start = state.pos
match = WIKILINK_PATTERN.match(state.src[start:])
if not match:
return False
href = match.group(1)
if not silent:
token = state.push("link_open", "a", 1)
token.attrSet("href", href)
token = state.push("text", "", 0)
token.content = href
token = state.push("link_close", "a", -1)
state.pos = start + match.end()
return True
def parser_factory() -> MarkdownIt:
parser = MarkdownIt("gfm-like")
parser.use(wikilinks_plugin)
return parser
WIKILINK_MD = """\
# WikiLinks Example
This is an example of a [Markdown link](https://github.com) in some text.
This is an example of a [[WikiLink]] in some text.
"""
class WikiLinksMarkdownApp(App):
def compose(self) -> ComposeResult:
yield Markdown(
markdown=WIKILINK_MD,
parser_factory=parser_factory,
)
@on(Markdown.LinkClicked)
def on_markdown_link_clicked(self, event: Markdown.LinkClicked) -> None:
self.notify(event.href)
if __name__ == "__main__":
app = WikiLinksMarkdownApp()
app.run()
Don't forget to star the repository!
Follow @textualizeio for Textual updates.
The Markdown Widget crashes if there is a closing rich tag in a heading. I.e., if there is a string like
[/some_text]
in a markdown heading. This can become problematic when using Markdown extensions to WikiLinks and having something linke[[/test.md]]
in a heading.Note in regular text this issue does not seem to be present. I have only seen this for headings.
Example
Running this
will produce this error: