Skip to content

Commit

Permalink
Fix clickable image links with #+NAME
Browse files Browse the repository at this point in the history
Earlier the a id tag was placed incorrectly.. fixed now.
  • Loading branch information
kaushalmodi committed Jan 15, 2018
1 parent 53cfa26 commit fef0ec5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
43 changes: 24 additions & 19 deletions ox-hugo.el
Expand Up @@ -1301,11 +1301,6 @@ and rewrite link paths to make blogging more seamless."
(useful-parent (if grand-parent
grand-parent
parent))
(label (let ((lbl (and (org-element-property :name useful-parent)
(org-export-get-reference useful-parent info))))
(if lbl
(format "<a id=\"%s\"></a>\n" lbl)
"")))
(attr (org-export-read-attribute :attr_html useful-parent))
;; Hugo `figure' shortcode named parameters
;; https://gohugo.io/content-management/shortcodes/#figure
Expand Down Expand Up @@ -1337,7 +1332,7 @@ and rewrite link paths to make blogging more seamless."
(format "%s=\"%s\" "
name val))))))
;; (message "[org-hugo-link DBG] figure params: %s" figure-param-str)
(format "%s{{<figure %s>}}" label (org-trim figure-param-str))))
(format "{{<figure %s>}}" (org-trim figure-param-str))))
((string= type "coderef")
(let ((ref (org-element-property :path link)))
(format (org-export-get-coderef-format ref contents)
Expand Down Expand Up @@ -1467,19 +1462,29 @@ INFO is a plist used as a communication channel."
"Transcode PARAGRAPH element into Hugo Markdown format.
CONTENTS is the paragraph contents. INFO is a plist used as a
communication channel."
(unless (org-hugo--plist-get-true-p info :hugo-preserve-filling)
(setq contents (concat (mapconcat 'identity (split-string contents) " ") "\n")))
;; Glue footnotes to the words before them using &nbsp; so that the
;; footnote reference does not end up on a new line by itself.
(setq contents (replace-regexp-in-string
;; "something FN" -> "something&nbsp;FN"
"[[:blank:]]+\\(\\[\\^[^]]+\\]\\)" "&nbsp;\\1"
(replace-regexp-in-string
;; "FN ." -> "FN."
"\\(\\[\\^[^]]+\\]\\)[[:blank:]]*\\([.]+\\)" "\\1\\2"
contents)))
;; (message "[org-hugo-paragraph DBG] para: %s" contents)
(org-md-paragraph paragraph contents info))
(let (;; The label is mainly for paragraphs that are standalone
;; images with #+NAME keyword.
(label (let ((lbl (and (org-element-property :name paragraph)
(org-export-get-reference paragraph info))))
(if lbl
(format "<a id=\"%s\"></a>\n" lbl)
"")))
ret)
(unless (org-hugo--plist-get-true-p info :hugo-preserve-filling)
(setq contents (concat (mapconcat 'identity (split-string contents) " ") "\n")))
;; Glue footnotes to the words before them using &nbsp; so that the
;; footnote reference does not end up on a new line by itself.
(setq contents (replace-regexp-in-string
;; "something FN" -> "something&nbsp;FN"
"[[:blank:]]+\\(\\[\\^[^]]+\\]\\)" "&nbsp;\\1"
(replace-regexp-in-string
;; "FN ." -> "FN."
"\\(\\[\\^[^]]+\\]\\)[[:blank:]]*\\([.]+\\)" "\\1\\2"
contents)))
;; (message "[org-hugo-paragraph DBG] para: %s" contents)
(setq ret (concat label
(org-md-paragraph paragraph contents info)))
ret))

;;;; Source Blocks
(defun org-hugo-src-block (src-block _contents info)
Expand Down
14 changes: 11 additions & 3 deletions test/site/content-org/all-posts.org
Expand Up @@ -66,10 +66,18 @@ tag and look the same size.
[[/images/org-mode-unicorn-logo.png][Click here to see the unicorn]]
*** Clickable image that opens the image (works!)
Click below image to jump to the unicorn image.
[[file:/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]
[[/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]

- NOTE :: =file:= has to be used in both Link and Description components
of the Org link.
- NOTE :: =file:= has to be used in the *Description component* of the
Org link.

-----

Here's the same link with =#+NAME= specified.. which should also be
clickable.

#+NAME: fig__unicorn
[[/images/org-mode-unicorn-logo.png][file:/images/org-mode-unicorn-logo.png]]
*** Link to image outside of standard Hugo =static= directory
[[../files-to-be-copied-to-static/static/images/copy-of-unicorn-logo.png]]

Expand Down
12 changes: 10 additions & 2 deletions test/site/content/posts/image-links.md
Expand Up @@ -47,8 +47,16 @@ tag and look the same size.
Click below image to jump to the unicorn image.
[{{<figure src="/images/org-mode-unicorn-logo.png">}}](/images/org-mode-unicorn-logo.png)

- **NOTE:** `file:` has to be used in both Link and Description components
of the Org link.
- **NOTE:** `file:` has to be used in the **Description component** of the
Org link.

---

Here's the same link with `#+NAME` specified.. which should also be
clickable.

<a id="orgf772560"></a>
[{{<figure src="/images/org-mode-unicorn-logo.png">}}](/images/org-mode-unicorn-logo.png)


## Link to image outside of standard Hugo `static` directory {#link-to-image-outside-of-standard-hugo-static-directory}
Expand Down

0 comments on commit fef0ec5

Please sign in to comment.