html: Support links with image descriptions

Turns out Org mode supports image links natively and we don't have to go out of
spec!

From https://orgmode.org/manual/Images-in-HTML-export.html:

[...] if the description part of the Org link is itself another link, such as
‘file:’ or ‘http:’ URL pointing to an image, the HTML export back-end in-lines
this image and links to [...]
This commit is contained in:
Niklas Fasching 2020-06-28 21:00:35 +02:00
parent c536adf6e9
commit d85768891c
5 changed files with 33 additions and 10 deletions

View file

@ -341,16 +341,26 @@ func (w *HTMLWriter) WriteRegularLink(l RegularLink) {
if prefix := w.document.Links[l.Protocol]; prefix != "" {
url = html.EscapeString(prefix) + strings.TrimPrefix(url, l.Protocol+":")
}
description := url
if l.Description != nil {
description = w.WriteNodesAsString(l.Description...)
}
switch l.Kind() {
case "image":
w.WriteString(fmt.Sprintf(`<img src="%s" alt="%s" title="%s" />`, url, description, description))
if l.Description == nil {
w.WriteString(fmt.Sprintf(`<img src="%s" alt="%s" title="%s" />`, url, url, url))
} else {
description := strings.TrimPrefix(String(l.Description), "file:")
w.WriteString(fmt.Sprintf(`<a href="%s"><img src="%s" alt="%s" /></a>`, url, description, description))
}
case "video":
w.WriteString(fmt.Sprintf(`<video src="%s" title="%s">%s</video>`, url, description, description))
if l.Description == nil {
w.WriteString(fmt.Sprintf(`<video src="%s" title="%s">%s</video>`, url, url, url))
} else {
description := strings.TrimPrefix(String(l.Description), "file:")
w.WriteString(fmt.Sprintf(`<a href="%s"><video src="%s" title="%s"></video></a>`, url, description, description))
}
default:
description := url
if l.Description != nil {
description = w.WriteNodesAsString(l.Description...)
}
w.WriteString(fmt.Sprintf(`<a href="%s">%s</a>`, url, description))
}
}

View file

@ -383,6 +383,14 @@ func isValidPostChar(r rune) bool {
func isValidBorderChar(r rune) bool { return !unicode.IsSpace(r) }
func (l RegularLink) Kind() string {
description := String(l.Description)
descProtocol, descExt := strings.SplitN(description, ":", 2)[0], path.Ext(description)
if ok := descProtocol == "file" || descProtocol == "http" || descProtocol == "https"; ok && imageExtensionRegexp.MatchString(descExt) {
return "image"
} else if ok && videoExtensionRegexp.MatchString(descExt) {
return "video"
}
if p := l.Protocol; l.Description != nil || (p != "" && p != "file" && p != "http" && p != "https") {
return "regular"
}

View file

@ -76,6 +76,9 @@ not emphasized/</p>
<p>regular link to https (image) <img src="https://placekitten.com/200/200#.png" alt="https://placekitten.com/200/200#.png" title="https://placekitten.com/200/200#.png" /></p>
</li>
<li>
<p>regular link with image as description <a href="https://placekitten.com"><img src="https://placekitten.com/200/200#.png" alt="https://placekitten.com/200/200#.png" /></a></p>
</li>
<li>
<p>regular link enclosed in [] [<a href="https://www.example.com">https://www.example.com</a>] [<a href="https://www.example.com">example.com</a>]</p>
</li>
<li>

View file

@ -28,8 +28,9 @@
4. regular link to a file (video) [[my-video.mp4]]
5. regular link to http (image) [[http://placekitten.com/200/200#.png]]
6. regular link to https (image) [[https://placekitten.com/200/200#.png]]
7. regular link enclosed in [] [[[https://www.example.com]]] [[[https://www.example.com][example.com]]]
8. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com
7. regular link with image as description [[https://placekitten.com][https://placekitten.com/200/200#.png]]
8. regular link enclosed in [] [[[https://www.example.com]]] [[[https://www.example.com][example.com]]]
9. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com
- timestamps
- <2019-01-06>
- <2019-01-06 Sun>

View file

@ -28,8 +28,9 @@
4. regular link to a file (video) [[my-video.mp4]]
5. regular link to http (image) [[http://placekitten.com/200/200#.png]]
6. regular link to https (image) [[https://placekitten.com/200/200#.png]]
7. regular link enclosed in [] [[[https://www.example.com]]] [[[https://www.example.com][example.com]]]
8. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com
7. regular link with image as description [[https://placekitten.com][https://placekitten.com/200/200#.png]]
8. regular link enclosed in [] [[[https://www.example.com]]] [[[https://www.example.com][example.com]]]
9. auto link, i.e. not inside =\[[square brackets]\]= https://www.example.com
- timestamps
- <2019-01-06 Sun>
- <2019-01-06 Sun>