Skip to content

Commit

Permalink
Support internal linking to images by their block names
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Nov 8, 2017
1 parent 59acb59 commit cf4bcd0
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ox-hugo.el
Expand Up @@ -1054,9 +1054,15 @@ and rewrite link paths to make blogging more seamless."
;; case.
(grand-parent (when (eq parent-type 'link)
(org-export-get-parent parent)))
(attr (if grand-parent
(org-export-read-attribute :attr_html grand-parent)
(org-export-read-attribute :attr_html parent)))
(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
(caption (org-string-nw-p
Expand Down Expand Up @@ -1087,7 +1093,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 "{{<figure %s>}}" (org-trim figure-param-str))))
(format "%s{{<figure %s>}}" label (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
37 changes: 37 additions & 0 deletions test/site/content-org/all-posts.org
Expand Up @@ -1608,6 +1608,43 @@ link./
#+INCLUDE: "./all-posts.org::#lorem-ipsum" :only-contents t

*Here we refer to table [[tab__simple]].*
*** Links to images :noexport:
:PROPERTIES:
:EXPORT_FILE_NAME: links-to-images
:END:
*Note*: This test is marked as =noexport= because the Org generated
link IDs are random and thus would fail the tests. So let this test
serve just as an example and not as a part of the automated test
suite.

To export this test, you'll need to temporarily remove the =noexport=
tag.
-----
From [[http://orgmode.org/manual/Images-and-tables.html][(org) Images and tables]],

*Note*: When targeting a =#+NAME= keyword, =#+CAPTION= keyword is
mandatory in order to get proper numbering for images and images.

#+BEGIN_SRC org
,#+CAPTION: Org Logo
,#+NAME: img__org_logo
[[/images/org-mode-unicorn-logo.png]]
Here we refer to image [[img__org_logo]].
#+END_SRC

will output below (/lorem-ipsum/ added to increase page content so
that the link jump is evident):

#+CAPTION: Org Logo
#+NAME: img__org_logo
[[/images/org-mode-unicorn-logo.png]]

/Scroll to the end of the below 'lorem-ipsum' block to find the test
link./

#+INCLUDE: "./all-posts.org::#lorem-ipsum" :only-contents t

*Here we refer to image [[img__org_logo]].*
* Equations :equations:
** Inline equations
:PROPERTIES:
Expand Down
90 changes: 90 additions & 0 deletions test/site/content/posts/links-to-images.md
@@ -0,0 +1,90 @@
+++
title = "Links to images"
tags = ["links", "internal-links"]
draft = false
+++

**Note**: This test is marked as `noexport` because the Org generated
link IDs are random and thus would fail the tests. So let this test
serve just as an example and not as a part of the automated test
suite.

To export this test, you'll need to temporarily remove the `noexport`
tag.

---

From [(org) Images and tables](http://orgmode.org/manual/Images-and-tables.html),

**Note**: When targeting a `#+NAME` keyword, `#+CAPTION` keyword is
mandatory in order to get proper numbering for images and images.

```org
#+CAPTION: Org Logo
#+NAME: img__org_logo
[[/images/org-mode-unicorn-logo.png]]
Here we refer to image [[img__org_logo]].
```

will output below (_lorem-ipsum_ added to increase page content so
that the link jump is evident):

<a id="org22e708b"></a>
{{<figure src="/images/org-mode-unicorn-logo.png" caption="Org Logo">}}

_Scroll to the end of the below 'lorem-ipsum' block to find the test
link._

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque et
quam metus. Etiam in iaculis mi, sit amet pretium magna. Donec ut dui
mi. Maecenas pharetra sapien nunc, ut mollis enim aliquam quis. Nam at
ultricies metus. Nulla tempor augue in vestibulum tristique. Phasellus
volutpat pharetra metus quis suscipit. Morbi maximus sem dolor, id
accumsan ipsum commodo non.

Fusce quam ligula, gravida ac dui venenatis, bibendum commodo
lorem. Duis id elit turpis. Integer sed diam nibh. Donec tempus
lacinia odio, a laoreet velit dictum id. Suspendisse efficitur euismod
purus et porttitor. Aliquam sit amet tellus mauris. Mauris semper
dignissim nibh, faucibus vestibulum purus varius quis. Suspendisse
potenti. Cras at ligula sit amet nunc vehicula condimentum quis nec
est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
posuere cubilia Curae; Donec iaculis, neque sit amet maximus rhoncus,
nisl tortor convallis ante, ut mollis purus augue ut justo. Praesent
felis urna, volutpat sit amet posuere dictum, luctus quis nibh. Proin
et tristique ipsum, in aliquam ante.

Aenean eget ex mauris. Cras ut tempor quam. Curabitur eget nulla
laoreet, bibendum neque porta, tempus nulla. Ut tellus nisi, semper eu
ligula pretium, aliquam accumsan dolor. Nunc fermentum cursus arcu eu
suscipit. Nam dolor tellus, efficitur sed condimentum at, fringilla
eget nisi. Nulla luctus metus felis. Suspendisse potenti. Cras lacinia
orci nec dui sodales commodo. Donec tellus arcu, congue porta ultrices
non, pretium et sapien. Proin mattis risus dignissim feugiat
tristique. Donec nibh lorem, facilisis id posuere ut, varius ac
urna. Etiam ultrices dignissim mauris, quis venenatis ex semper ut.

Curabitur id fermentum erat, rhoncus scelerisque est. Sed pulvinar,
nulla sed sollicitudin scelerisque, ipsum erat sollicitudin dolor, ut
commodo arcu justo vel libero. Curabitur turpis dolor, fermentum ut
elit a, vehicula commodo nunc. Sed sit amet blandit nulla, quis
sodales massa. Donec lobortis, urna vel volutpat ullamcorper, mauris
est efficitur nulla, et suscipit velit dui at metus. Aliquam id sem
sed metus tristique scelerisque nec vitae odio. Phasellus a
pellentesque libero, vel convallis metus. Sed nec fringilla magna, non
varius ex. Sed interdum eleifend ligula, quis porta enim ultrices
a. Donec hendrerit diam ac elementum tincidunt.

Pellentesque eget nisl rhoncus, malesuada justo nec, suscipit
quam. Nam sodales mauris eu bibendum suscipit. Vivamus sodales dui
lorem, scelerisque pellentesque diam fermentum sed. Etiam fermentum
nisl id nisl blandit, sit amet semper erat ultricies. Nulla tincidunt
nulla metus, eu imperdiet lorem malesuada sagittis. Maecenas accumsan
risus sed ante eleifend, vitae pretium leo porta. Suspendisse vitae
eros vitae dui ornare condimentum id sit amet mauris. Etiam tincidunt
consequat risus, eu posuere mi. Donec ut nunc eu massa porttitor
suscipit nec nec neque. Suspendisse vitae tincidunt justo, sed
molestie velit. Nullam pellentesque convallis ante, vel posuere libero
blandit in.

**Here we refer to image [6](#org22e708b).**

0 comments on commit cf4bcd0

Please sign in to comment.