MichaelHatherly / CommonMark.jl

A CommonMark-compliant Markdown parser for Julia.
Other
87 stars 11 forks source link

Disabling LinkRule (but not ImageRule) does not disable link parsing #47

Open mortenpi opened 2 years ago

mortenpi commented 2 years ago

I believe this is because the inline_rules for [ and ] that go into p.inline_parser.inline_parsers are identical for LinkRule and ImageRule (except that ImageRule also triggers on !):

https://github.com/MichaelHatherly/CommonMark.jl/blob/287e39ae128e80a6647d2d9762bf22040adffea1/src/parsers/inlines/links.jl#L271-L275

My understanding of the parser is that parse_open_bracket will trigger anyway, because once it has been added to p.inline_parser.inline_parsers, it doesn't know whether it was added by LinkRule or ImageRule.

For an MWE, setting up the parser as follows to remove LinkRule (and it's associated inline_rules):

using CommonMark
p = Parser()
rules = filter(r -> !isa(r, LinkRule), p.rules)
disable!(p, copy(p.rules))
enable!(p, rules)
n = p("![a](b)\n\n[x](y)")

The resulting AST still has the Link nodes:

julia> n.first_child.first_child.t
CommonMark.Image("b", "")

julia> n.first_child.nxt.first_child.t
CommonMark.Link("y", "")
mortenpi commented 2 years ago

To be fair, I am not sure whether it even makes sense to disable one and not another. But if it doesn't, maybe it would make sense to merge them into a single rule?