Skip to content

Commit

Permalink
Fix auto_identifiers_ascii extension.
Browse files Browse the repository at this point in the history
Omit non-ASCII characters that don't have an (unaccented)
ASCII equivalent.

Add tests to test suite.
  • Loading branch information
jgm committed Jun 17, 2024
1 parent b7f0ca3 commit a3baddd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ makeIdentifier ascii = toIdent . T.toLower
= fromchar c
| otherwise = mempty
fromchar c
| ascii
, not (isAscii c) = maybe mempty T.singleton $ M.lookup c asciiMap
| otherwise = T.singleton c
| ascii =
if isAscii c
then T.singleton c
else case M.lookup c asciiMap of
Nothing -> mempty
Just d -> T.singleton d
| otherwise = T.singleton c

asciiMap :: M.Map Char Char
asciiMap = M.fromList
Expand Down
13 changes: 10 additions & 3 deletions commonmark-extensions/test/auto_identifiers_ascii.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
The `auto_identifiers_ascii` extension is like
`auto_identifiers` but limits identifiers to ASCII.
Accented Latin characters are converted into the
closest ASCII equivalent.
closest ASCII equivalent. Other non-ASCII characters
are simply omitted.

```````````````````````````````` example
# Heading with ä
# Heading with ä and α
.
<h1 id="heading-with-a">Heading with &auml;</h1>
<h1 id="heading-with-a-and-">Heading with ä and α</h1>
````````````````````````````````

```````````````````````````````` example
# Heading with ❤️
.
<h1 id="heading-with-heart">Heading with ❤️</h1>
````````````````````````````````
1 change: 1 addition & 0 deletions commonmark-extensions/test/test-commonmark-extensions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ main = do
, ("test/bracketed_spans.md", bracketedSpanSpec)
, ("test/fenced_divs.md", fencedDivSpec)
, ("test/auto_identifiers.md", autoIdentifiersSpec <> attributesSpec)
, ("test/auto_identifiers_ascii.md", autoIdentifiersAsciiSpec <> attributesSpec)
, ("test/implicit_heading_references.md",
autoIdentifiersSpec <> attributesSpec <> implicitHeadingReferencesSpec)
, ("test/wikilinks_title_before_pipe.md", wikilinksSpec TitleBeforePipe)
Expand Down

0 comments on commit a3baddd

Please sign in to comment.