Skip to content

Commit

Permalink
improve rendering of automatic links
Browse files Browse the repository at this point in the history
Do not render the URL of automatic links twice.

Signed-off-by: Ruben Jenster <r.jenster@drachenfels.de>
  • Loading branch information
r10r committed Nov 30, 2022
1 parent e5bd727 commit 66e0792
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions md2man/roff.go
@@ -1,6 +1,7 @@
package md2man

import (
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -109,11 +110,16 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
out(w, strongCloseTag)
}
case blackfriday.Link:
if !entering {
// Hyphens in a link must be escaped to avoid word-wrap in the rendered man page.
escapedLink := strings.ReplaceAll(string(node.LinkData.Destination), "-", "\\-")
out(w, linkTag+escapedLink+linkCloseTag)
// Don't render the link text for automatic links, because this
// will only duplicate the URL in the roff output.
// See https://daringfireball.net/projects/markdown/syntax#autolink
if !bytes.Equal(node.LinkData.Destination, node.FirstChild.Literal) {
out(w, string(node.FirstChild.Literal))
}
// Hyphens in a link must be escaped to avoid word-wrap in the rendered man page.
escapedLink := strings.ReplaceAll(string(node.LinkData.Destination), "-", "\\-")
out(w, linkTag+escapedLink+linkCloseTag)
walkAction = blackfriday.SkipChildren
case blackfriday.Image:
// ignore images
walkAction = blackfriday.SkipChildren
Expand Down
2 changes: 2 additions & 0 deletions md2man/roff_test.go
Expand Up @@ -338,6 +338,8 @@ func TestLinks(t *testing.T) {
".nh\n\n.PP\nSee docs\n\\[la]https://docs.docker.com/\\[ra] for\nmore\n",
"See [docs](https://docs-foo.docker.com/) for\nmore",
".nh\n\n.PP\nSee docs\n\\[la]https://docs\\-foo.docker.com/\\[ra] for\nmore\n",
"See <https://docs-foo.docker.com/> for\nmore",
".nh\n\n.PP\nSee \n\\[la]https://docs\\-foo.docker.com/\\[ra] for\nmore\n",
}
doTestsInline(t, tests)
}
Expand Down

0 comments on commit 66e0792

Please sign in to comment.