Skip to content

Commit

Permalink
Fix implicit_header_references with duplicate headings.
Browse files Browse the repository at this point in the history
Documentation says that when more than one heading has the same text,
an implicit reference `[Heading text][]` refers to the first one.
Previously pandoc linked to the last one instead. This patch
makes pandoc conform to the documented behavior.

See #8300.
  • Loading branch information
jgm committed Sep 14, 2022
1 parent eb03cad commit d79f838
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,9 @@ registerImplicitHeader raw attr@(ident, _, _)
| T.null raw = return ()
| otherwise = do
let key = toKey $ "[" <> raw <> "]"
updateState $ \s ->
s { stateHeaderKeys = M.insert key (("#" <> ident,""), attr)
updateState $ \s -> -- don't override existing headers
s { stateHeaderKeys = M.insertWith (\_new old -> old)
key (("#" <> ident,""), attr)
(stateHeaderKeys s) }

--
Expand Down
6 changes: 3 additions & 3 deletions test/markdown-reader-more.native
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ Pandoc
, Link
( "" , [] , [] )
[ Str "My" , Space , Str "header" ]
( "#my-header-1" , "" )
( "#my-header" , "" )
, Str "."
]
, Para
Expand All @@ -367,7 +367,7 @@ Pandoc
, Space
, Str "to"
, Space
, Link ( "" , [] , [] ) [ Str "it" ] ( "#my-header-1" , "" )
, Link ( "" , [] , [] ) [ Str "it" ] ( "#my-header" , "" )
, Str "."
]
, Para
Expand All @@ -378,7 +378,7 @@ Pandoc
, Link
( "" , [] , [] )
[ Str "case" , Space , Str "insensitive" ]
( "#my-header-1" , "" )
( "#my-header" , "" )
, Str "."
]
, Para
Expand Down

0 comments on commit d79f838

Please sign in to comment.