From d85768891c64f0d5d2c8ac32ca308d6f8aac7f76 Mon Sep 17 00:00:00 2001
From: Niklas Fasching
Date: Sun, 28 Jun 2020 21:00:35 +0200
Subject: [PATCH] html: Support links with image descriptions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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 [...]
---
org/html_writer.go | 22 ++++++++++++++++------
org/inline.go | 8 ++++++++
org/testdata/inline.html | 3 +++
org/testdata/inline.org | 5 +++--
org/testdata/inline.pretty_org | 5 +++--
5 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/org/html_writer.go b/org/html_writer.go
index 7c5c6d3..3333906 100644
--- a/org/html_writer.go
+++ b/org/html_writer.go
@@ -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(`
`, url, description, description))
+ if l.Description == nil {
+ w.WriteString(fmt.Sprintf(`
`, url, url, url))
+ } else {
+ description := strings.TrimPrefix(String(l.Description), "file:")
+ w.WriteString(fmt.Sprintf(`
`, url, description, description))
+ }
case "video":
- w.WriteString(fmt.Sprintf(``, url, description, description))
+ if l.Description == nil {
+ w.WriteString(fmt.Sprintf(``, url, url, url))
+ } else {
+ description := strings.TrimPrefix(String(l.Description), "file:")
+ w.WriteString(fmt.Sprintf(``, url, description, description))
+ }
default:
+ description := url
+ if l.Description != nil {
+ description = w.WriteNodesAsString(l.Description...)
+ }
w.WriteString(fmt.Sprintf(`%s`, url, description))
}
}
diff --git a/org/inline.go b/org/inline.go
index ede3355..e7a8f47 100644
--- a/org/inline.go
+++ b/org/inline.go
@@ -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"
}
diff --git a/org/testdata/inline.html b/org/testdata/inline.html
index f2fc773..0df8ec5 100644
--- a/org/testdata/inline.html
+++ b/org/testdata/inline.html
@@ -76,6 +76,9 @@ not emphasized/
regular link to https (image) 
+regular link with image as description 
+
+
regular link enclosed in [] [https://www.example.com] [example.com]
diff --git a/org/testdata/inline.org b/org/testdata/inline.org
index 188e38c..a81e24c 100644
--- a/org/testdata/inline.org
+++ b/org/testdata/inline.org
@@ -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>
diff --git a/org/testdata/inline.pretty_org b/org/testdata/inline.pretty_org
index 60fe598..acf70d6 100644
--- a/org/testdata/inline.pretty_org
+++ b/org/testdata/inline.pretty_org
@@ -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>