Skip to content

Commit

Permalink
Never transform links to autolinks
Browse files Browse the repository at this point in the history
This fixes one bug raised in avh4#339
that was causing links to modules like [Module](Module) being
transformed to invalid autolinks like <Module>.
  • Loading branch information
rlefevre committed Dec 2, 2019
1 parent 3b2a0fd commit 9412b83
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
13 changes: 6 additions & 7 deletions markdown/Cheapskate/Inlines.hs
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,14 @@ pAutolink = do
| otherwise -> fail "Unknown contents of <>"

autoLink :: Text -> Inlines
autoLink t = singleton $ Link (toInlines t) (Url t) (T.empty)
where toInlines t' = case parse pToInlines t' of
Right r -> r
Left e -> error $ "autolink: " ++ show e
pToInlines = mconcat <$> many strOrEntity
autoLink t =
case parse pToInlines t of
Right _ -> singleton $ Autolink t
Left e -> error $ "autolink: " ++ show e
where pToInlines = mconcat <$> many strOrEntity
strOrEntity = ((singleton . Str) <$> takeWhile1 (/='&'))
<|> pEntity
<|> ((singleton . Str) <$> string "&")

emailLink :: Text -> Inlines
emailLink t = singleton $ Link (singleton $ Str t)
(Url $ "mailto:" <> t) (T.empty)
emailLink t = singleton $ Autolink ("mailto:" <> t)
1 change: 1 addition & 0 deletions markdown/Cheapskate/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ data Inline = Str Text
| Strong Inlines
| Code Text
| Link Inlines LinkTarget {- URL -} Text {- title -}
| Autolink Text
| Image Inlines Text {- URL -} Text {- title -}
| Entity Text
| RawHtml Text
Expand Down
22 changes: 12 additions & 10 deletions src/ElmFormat/Render/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,25 @@ formatMarkdownInline fixSpecialChars inline =
Code text ->
"`" ++ Text.unpack text ++ "`" -- TODO: escape backticks

Autolink url ->
let
url' = Text.unpack url
in
if fixSpecialChars
then "<" ++ url' ++ ">"
else url'

Link inlines (Url url) title ->
let
text = fold $ fmap (formatMarkdownInline False) $ inlines

title' = Text.unpack title
url' = Text.unpack url
in
if text == url' && title' == ""
then
if fixSpecialChars
then "<" ++ url' ++ ">"
else url'
else
"[" ++ text
++ "](" ++ Text.unpack url
++ (if title' == "" then "" else " \"" ++ title' ++ "\"")
++ ")"
"[" ++ text
++ "](" ++ Text.unpack url
++ (if title' == "" then "" else " \"" ++ title' ++ "\"")
++ ")"

Link inlines (Ref ref) _ ->
let
Expand Down
24 changes: 24 additions & 0 deletions tests/test-files/good/Elm-0.19/AllSyntax/Doc.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module AllSyntax.Doc exposing (links)

{-| Documentation
docs links
-}


{-| Markdown links
Links:
- Link to [Module](Module).
Autolinks:
- <https://host.domain>
- <host.domain>
- <mailto:me@domain>
-}
links =
[]

0 comments on commit 9412b83

Please sign in to comment.