Skip to content

Commit

Permalink
Make auto_identifiers work as advertised.
Browse files Browse the repository at this point in the history
Replace emojis with aliases.
  • Loading branch information
jgm committed Jun 17, 2024
1 parent 097ba1f commit b7f0ca3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion commonmark-extensions/commonmark-extensions.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ library
, network-uri
, commonmark >= 0.2.4.1 && < 0.3
-- for extensions:
, emojis >= 0.1 && < 0.2
, emojis >= 0.1.4 && < 0.2
exposed-modules:
Commonmark.Extensions
Commonmark.Extensions.Smart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Char (isSpace, isAlphaNum, isAscii, isMark,
import Data.Dynamic
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Text.Emoji as Emoji
import Text.Parsec

autoIdentifiersSpec :: (Monad m, IsBlock il bl, IsInline il, ToPlainText il)
Expand Down Expand Up @@ -49,7 +50,8 @@ addId ascii bd
Nothing -> do
contents <- runInlineParser
(removeIndent . mconcat . reverse . blockLines $ bd)
let ident = makeIdentifier ascii (toPlainText contents)
let ident = makeIdentifier ascii
(Emoji.replaceEmojis emojiToAlias $ toPlainText contents)
counterMap <- counters <$> getState
let key = "identifier:" <> ident
cnt <- case M.lookup key counterMap of
Expand All @@ -64,6 +66,10 @@ addId ascii bd
Just _ -> return $! bd
| otherwise = return $! bd

emojiToAlias :: T.Text -> [T.Text] -> T.Text
emojiToAlias t [] = t
emojiToAlias _ (a:_) = a

makeIdentifier :: Bool -> T.Text -> T.Text
makeIdentifier ascii = toIdent . T.toLower
where
Expand Down
8 changes: 8 additions & 0 deletions commonmark-extensions/test/auto_identifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ Hi
<h1 id="hi-2">Hi</h1>
<h1 id="hi-3">Hi</h1>
````````````````````````````````

Emojis should be replaced with aliases:

```
# Introduction ❤️💯
.
<h1 id="introduction-heart-100">Introduction ❤️💯</h1>
```

0 comments on commit b7f0ca3

Please sign in to comment.