Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing case of nested link references by adding a container parse phase #85

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions spec-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@
184,
185,
186,
187,
188
],
"Links": [
Expand Down Expand Up @@ -917,6 +918,7 @@
184,
185,
186,
187,
188
],
"Links": [
Expand Down
50 changes: 38 additions & 12 deletions src/Markdown/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ parse input =

Ok v ->
-- then parse the inlines of each raw block
case parseAllInlines v of
case parseAllInlines (parseContainers v) of
Err e ->
-- NOTE these messages get an incorrect location,
-- because they are parsed outside of the main (raw block) parser context.
Expand All @@ -80,6 +80,35 @@ parse input =
Ok (List.filter isNotEmptyParagraph blocks)


parseContainers: State-> State
parseContainers state =
parseContainersHelp state.rawBlocks {linkReferenceDefinitions=state.linkReferenceDefinitions, rawBlocks = []}


parseContainersHelp : List RawBlock -> State -> State
parseContainersHelp unparsedRawBlocks parsedState =
case unparsedRawBlocks of
rawBlock :: rest ->
case rawBlock of
BlockQuote rawBlocks ->
case Advanced.run rawBlockParser rawBlocks of
Ok value ->
parseContainersHelp rest
{ linkReferenceDefinitions = parsedState.linkReferenceDefinitions ++ (parseContainers value).linkReferenceDefinitions
, rawBlocks = parsedState.rawBlocks ++[ (parseContainers value).rawBlocks|>ParsedBlockQuote]
}
Err e ->
-- TODO return this error
{linkReferenceDefinitions=[], rawBlocks= []}
_ ->
parseContainersHelp rest
{ linkReferenceDefinitions = parsedState.linkReferenceDefinitions
, rawBlocks = parsedState.rawBlocks ++ [rawBlock]
}
[] ->
parsedState


deadEndsToString : List (Advanced.DeadEnd String Parser.Problem) -> String
deadEndsToString deadEnds =
deadEnds
Expand Down Expand Up @@ -294,19 +323,16 @@ parseInlines linkReferences rawBlock =
EmptyBlock

BlockQuote rawBlocks ->
case Advanced.run rawBlockParser rawBlocks of
Ok value ->
case parseAllInlines value of
Ok parsedBlocks ->
Block.BlockQuote parsedBlocks
|> ParsedBlock

Err e ->
InlineProblem e
EmptyBlock

Err error ->
InlineProblem (Parser.Problem (deadEndsToString error))
ParsedBlockQuote rawBlocks->
case parseAllInlines {linkReferenceDefinitions = linkReferences, rawBlocks = rawBlocks} of
Ok parsedBlocks ->
Block.BlockQuote parsedBlocks
|> ParsedBlock

Err e ->
InlineProblem e
IndentedCodeBlock codeBlockBody ->
Block.CodeBlock { body = codeBlockBody, language = Nothing }
|> ParsedBlock
Expand Down
1 change: 1 addition & 0 deletions src/Markdown/RawBlock.elm
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ type RawBlock
| TableDelimiter Markdown.Table.TableDelimiterRow
| BlankLine
| BlockQuote String
| ParsedBlockQuote (List RawBlock)
| SetextLine SetextLevel String
2 changes: 1 addition & 1 deletion test-results/failing/CommonMark/HTML blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Should give output:
But instead was:

````````````html
ERROR Problem at row 1 Problem at row 2 Expecting symbol

````````````
## [Example 144](https://spec.commonmark.org/0.29/#example-144)

Expand Down
22 changes: 0 additions & 22 deletions test-results/failing/CommonMark/Link reference definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,3 @@ But instead was:
````````````html
<p>Foo<a href="/baz">bar</a></p>
````````````
## [Example 187](https://spec.commonmark.org/0.29/#example-187)

This markdown:

````````````markdown
[foo]

> [foo]: /url

````````````

Should give output:

````````````html
<p><a href="/url">foo</a></p><blockquote></blockquote>
````````````

But instead was:

````````````html
<p>[foo]</p><blockquote></blockquote>
````````````
2 changes: 1 addition & 1 deletion test-results/failing/GFM/HTML blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Should give output:
But instead was:

````````````html
ERROR Problem at row 1 Problem at row 2 Expecting symbol

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

````````````
## [Example 144](https://spec.commonmark.org/0.29/#example-144)

Expand Down
22 changes: 0 additions & 22 deletions test-results/failing/GFM/Link reference definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,3 @@ But instead was:
````````````html
<p>Foo<a href="/baz">bar</a></p>
````````````
## [Example 187](https://spec.commonmark.org/0.29/#example-187)

This markdown:

````````````markdown
[foo]

> [foo]: /url

````````````

Should give output:

````````````html
<p><a href="/url">foo</a></p><blockquote></blockquote>
````````````

But instead was:

````````````html
<p>[foo]</p><blockquote></blockquote>
````````````
22 changes: 22 additions & 0 deletions test-results/passing-CommonMark.md
Original file line number Diff line number Diff line change
Expand Up @@ -6624,6 +6624,28 @@ Gives this correct output:

````````````

### [Example 187](https://spec.commonmark.org/0.29/#example-187)

This markdown:


````````````markdown
[foo]

> [foo]: /url

````````````

Gives this correct output:


````````````html
<p><a href="/url">foo</a></p>
<blockquote>
</blockquote>

````````````

### [Example 188](https://spec.commonmark.org/0.29/#example-188)

This markdown:
Expand Down
22 changes: 22 additions & 0 deletions test-results/passing-GFM.md
Original file line number Diff line number Diff line change
Expand Up @@ -6624,6 +6624,28 @@ Gives this correct output:

````````````

### [Example 187](https://spec.commonmark.org/0.29/#example-187)

This markdown:


````````````markdown
[foo]

> [foo]: /url

````````````

Gives this correct output:


````````````html
<p><a href="/url">foo</a></p>
<blockquote>
</blockquote>

````````````

### [Example 188](https://spec.commonmark.org/0.29/#example-188)

This markdown:
Expand Down