MichaelHatherly / CommonMark.jl

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

Duplicate rules when enabling/disabling rules #45

Closed mortenpi closed 2 years ago

mortenpi commented 2 years ago

The enable! / disable! functions seem to duplicate the rules. I presume this is not intended?

If you disable! a rule, it does remove it, but it seems to duplicate every other rule:

julia> using CommonMark

julia> p = Parser()
Parser(Node(CommonMark.Document))

julia> disable!(p, LinkRule())
Parser(Node(CommonMark.Document))

julia> p.rules
36-element Vector{Any}:
 BlockQuoteRule()                    <-------------------------
 AtxHeadingRule()
 FencedCodeBlockRule()
 HtmlBlockRule()
 SetextHeadingRule()
 ThematicBreakRule()
 ListItemRule()
 IndentedCodeBlockRule()
 AutolinkRule()
 InlineCodeRule()
 AsteriskEmphasisRule()
 UnderscoreEmphasisRule()
 CommonMark.BackslashEscapeRule()
 HtmlInlineRule()
 HtmlEntityRule()
 ImageRule()
 CommonMark.TextRule()
 CommonMark.NewlineRule()
 BlockQuoteRule()                    <-------------------------
 AtxHeadingRule()
 FencedCodeBlockRule()
 HtmlBlockRule()
 SetextHeadingRule()
 ThematicBreakRule()
 ListItemRule()
 IndentedCodeBlockRule()
 AutolinkRule()
 InlineCodeRule()
 AsteriskEmphasisRule()
 UnderscoreEmphasisRule()
 CommonMark.BackslashEscapeRule()
 HtmlInlineRule()
 HtmlEntityRule()
 ImageRule()
 CommonMark.TextRule()
 CommonMark.NewlineRule()

Similarly, enable!-ing a rule that is already enabled, will duplicate it in the list, rather than overriding it:

julia> using CommonMark

julia> p = Parser()
Parser(Node(CommonMark.Document))

julia> enable!(p, LinkRule())
Parser(Node(CommonMark.Document))

julia> p.rules
20-element Vector{Any}:
 BlockQuoteRule()
 AtxHeadingRule()
 FencedCodeBlockRule()
 HtmlBlockRule()
 SetextHeadingRule()
 ThematicBreakRule()
 ListItemRule()
 IndentedCodeBlockRule()
 AutolinkRule()
 InlineCodeRule()
 AsteriskEmphasisRule()
 UnderscoreEmphasisRule()
 CommonMark.BackslashEscapeRule()
 HtmlInlineRule()
 HtmlEntityRule()
 LinkRule()                    <------------------
 ImageRule()
 CommonMark.TextRule()
 CommonMark.NewlineRule()
 LinkRule()                    <------------------

For context: I am working on an approach for implementing the invalid reference link callback that would work via an option to LinkRule, so I want to replace the vanilla LinkRule with one that has the callback as a field. Right now, if I enable! it, the original LinkRule still exists and takes precedence, if I understand correctly what is going on.

MichaelHatherly commented 2 years ago

Yes, looks like a bug to me.