MichaelHatherly / CommonMark.jl

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

Clash between TableRule and IndentedCodeBlockRule #25

Closed tlienart closed 3 years ago

tlienart commented 3 years ago

Example

using CommonMark
CM = CommonMark

cm_parser = CM.Parser()

enable!(cm_parser, CM.TableRule())
disable!(cm_parser, CM.IndentedCodeBlockRule())

s = """
    tables:

    | abc | def |
    | --- | ----|
    | 0 | 1|

    end
    """
cm_parser(s)

Stacktrace

ERROR: KeyError: key '\0' not found
Stacktrace:
 [1] getindex
   @ ./dict.jl:481 [inlined]
 [2] incorporate_line(parser::Parser, ln::String)
   @ CommonMark ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:322
 [3] parse(parser::Parser, my_input::IOBuffer; kws::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ CommonMark ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:440
 [4] parse
   @ ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:427 [inlined]
 [5] #_#21
   @ ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:451 [inlined]
 [6] Parser
   @ ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:451 [inlined]
 [7] #_#20
   @ ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:450 [inlined]
 [8] (::Parser)(text::String)
   @ CommonMark ~/.julia/packages/CommonMark/VdZT9/src/parsers/blocks.jl:450
 [9] top-level scope
   @ none:1

removing the disable! with IndentedCodeBlockRule avoids this but I do want to disable that rule (as per https://github.com/MichaelHatherly/CommonMark.jl/issues/1#issuecomment-735990126). Any suggestions?

Thanks

MichaelHatherly commented 3 years ago

The error it's giving is saying that there isn't a default parsing function to handle block parsing when nothing else matches. Have you tried adding the SkipIndented rule in the linked comment? You need to have a rule that handles the empty string, which is normally handled by IndentedCodeBlockRule.

julia> struct SkipIndented end;

julia> CommonMark.block_rule(::SkipIndented) = CommonMark.Rule((p, c) -> 0, 8, "");

julia> p = Parser();

julia> enable!(p, (TableRule(), SkipIndented()));

julia> disable!(p, IndentedCodeBlockRule());

julia> s = """
           tables:

           | abc | def |
           | --- | ----|
           | 0 | 1|

           end
           """;

julia> p(s)
 tables:

 ┏━━━━━┯━━━━━┓
 ┃ abc │ def ┃
 ┠─────┼─────┨
 ┃ 0   │ 1   ┃
 ┗━━━━━┷━━━━━┛
 end
tlienart commented 3 years ago

apologies this is on me with the --- block that was getting caught inside the table as a hrule on my side blocking CM from seeing the full table and crashing as a result.