RobertDober / earmark_parser

The Markdown to AST part of Earmark.
Apache License 2.0
68 stars 26 forks source link

Failed to parse footnote when footnote contains no whitespace #99

Closed c4710n closed 2 years ago

c4710n commented 2 years ago

Version

1.4.23

Reproduce

Example code:

markdown = """
A line with[^1] two references[^2]

[^1]: oneline-without-whitespace
[^2]: - Footnote 2.1
"""

EarmarkParser.as_ast(markdown, footnotes: true)

Outputs:

{:error,
 [
   {"p", [],
    [
      "A line with[^1] two references",
      {"a",
       [
         {"href", "#fn:2"},
         {"id", "fnref:2"},
         {"class", "footnote"},
         {"title", "see footnote"}
       ], ["2"], %{}}
    ], %{}},
   {"div", [{"class", "footnotes"}],
    [
      {"hr", [], [], %{}},
      {"ol", [],
       [
         {"li", [{"id", "fn:2"}],
          [
            {"a",
             [
               {"class", "reversefootnote"},
               {"href", "#fnref:2"},
               {"title", "return to article"}
             ], ["↩"], %{}},
            {"ul", [], [{"li", [], ["Footnote 2.1"], %{}}], %{}}
          ], %{}}
       ], %{}}
    ], %{}}
 ], [{:error, 1, "footnote 1 undefined, reference to it ignored"}]}

What about footnote with whitespace?

Everything is fine. For example:

markdown = """
A line with[^1] two references[^2]

[^1]: oneline without whitespace
[^2]: - Footnote 2.1
"""

EarmarkParser.as_ast(markdown, footnotes: true)

Outputs:

{:ok,
 [
   {"p", [],
    [
      "A line with",
      {"a",
       [
         {"href", "#fn:1"},
         {"id", "fnref:1"},
         {"class", "footnote"},
         {"title", "see footnote"}
       ], ["1"], %{}},
      " two references",
      {"a",
       [
         {"href", "#fn:2"},
         {"id", "fnref:2"},
         {"class", "footnote"},
         {"title", "see footnote"}
       ], ["2"], %{}}
    ], %{}},
   {"div", [{"class", "footnotes"}],
    [
      {"hr", [], [], %{}},
      {"ol", [],
       [
         {"li", [{"id", "fn:1"}],
          [
            {"a",
             [
               {"class", "reversefootnote"},
               {"href", "#fnref:1"},
               {"title", "return to article"}
             ], ["↩"], %{}},
            {"p", [], ["oneline without whitespace"], %{}}
          ], %{}},
         {"li", [{"id", "fn:2"}],
          [
            {"a",
             [
               {"class", "reversefootnote"},
               {"href", "#fnref:2"},
               {"title", "return to article"}
             ], ["↩"], %{}},
            {"ul", [], [{"li", [], ["Footnote 2.1"], %{}}], %{}}
          ], %{}}
       ], %{}}
    ], %{}}
 ], []}
RobertDober commented 2 years ago

@c4710n thank you for that report

c4710n commented 2 years ago

Thanks, Robert.

RobertDober commented 2 years ago

My pleasure: 1.4.24 with the fix is available