Skip to content

Commit

Permalink
Encode link URIs before rendering into Gemtext
Browse files Browse the repository at this point in the history
This makes gmnhg encode link destinations before rendering them into
Gemtext according to RFC 3986. This particularly fixes spaces in links.

Invalid URIs will skipped from rendering entirely.

Fixes #49.
  • Loading branch information
tdemin committed Jan 13, 2022
1 parent 62d762a commit 5f755e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/renderer/link.go
Expand Up @@ -18,6 +18,7 @@ package renderer
import (
"fmt"
"io"
"net/url"

"github.com/gomarkdown/markdown/ast"
)
Expand All @@ -27,8 +28,13 @@ func (r Renderer) link(w io.Writer, node *ast.Link, entering bool) {
if node.Footnote != nil {
fmt.Fprintf(w, "[^%d]: %s", node.NoteID, extractText(node.Footnote))
} else {
uri, err := url.Parse(string(node.Destination))
if err != nil {
// TODO: should we skip links with invalid URIs?
return
}
w.Write(linkPrefix)
w.Write(node.Destination)
w.Write([]byte(uri.String()))
w.Write(space)
r.text(w, node, true)
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/links.gmi
Expand Up @@ -26,6 +26,11 @@ Other container elements can contain inline links as well. For instance, this is

=> https://xmpp.org/extensions/xep-0384.html XEP-0384

Links will get encoded according to RFC 3986, like this sample link to nowhere. The other sample link to somewhere on GitHub will not get transformed: sample.

=> /URI%20with%20spaces link
=> https://github.com:443/request+with+characters%20 sample

## Footnotes

gmnhg supports footnotes, written like this[^1]. Footnotes can use any references, including alphanumeric ones[^2]; alphanumeric references will be replaced with numeric IDs on render.
Expand Down
6 changes: 6 additions & 0 deletions testdata/links.md
Expand Up @@ -32,6 +32,12 @@ this is an example of a link inside a blockquote:
> OTR has significant usability drawbacks for inter-client mobility.
> [XEP-0384](https://xmpp.org/extensions/xep-0384.html)
Links will get encoded according to RFC 3986, like this sample
[link](/URI with spaces) to nowhere. The other sample link to
somewhere on GitHub will not get transformed: [sample][elsewhere].

[elsewhere]: https://github.com:443/request+with+characters%20

## Footnotes

gmnhg supports footnotes, written like this[^1]. Footnotes can use any
Expand Down

0 comments on commit 5f755e4

Please sign in to comment.