Skip to content

Commit

Permalink
fix: Make cross-post linking for subtree-based flow work once again
Browse files Browse the repository at this point in the history
Also fix a test post subtree at wrong indentation level
  - An EXPORT_FILE_NAME subtree was nested inside another
    EXPORT_FILE_NAME subtree. Post subtrees are not supposed to be
    organized like that.
  • Loading branch information
kaushalmodi committed Feb 18, 2022
1 parent 513cca4 commit 54b213a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 74 deletions.
116 changes: 62 additions & 54 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ This variable needs to be non-nil for the support of
cross-subtree Org internal links when using the subtree-based
export flow.")

(defconst org-hugo--preprocessed-buffer-dummy-file-suffix ".pre-processed.org"
"Dummy suffix (including file extension) for pre-processed buffers.
Dummy Org file paths are created in
`org-hugo--get-pre-processed-buffer' by appending this variable
to the link targets out of the current subtree scope.")


;;; Obsoletions

Expand Down Expand Up @@ -2097,34 +2104,18 @@ Some heading\", \"#some_custom_id\".
If the search fails, return nil."
;; (message "[get-link-search-location DBG] org-file: %S" org-file)
;; (message "[get-link-search-location DBG] search-str: %S" search-str)
(let ((buffer (get-file-buffer org-file))) ;nil if `org-file' buffer is not already open
(let ((buffer (get-file-buffer org-file)) ;nil if `org-file' buffer is not already open
elem)
(unless (file-exists-p org-file)
(error "[org-hugo--search-and-get-element] Unable to open Org file `%s'" org-file))
(with-current-buffer (or buffer (find-file-noselect org-file))
(org-export-get-environment) ;Eval #+bind keywords, etc.
;; Below code for `save-position-maybe' and `search' is taken
;; from the `org-open-file' function.
(let* ((save-position-maybe
(let ((old-buffer (current-buffer))
(old-pos (point))
(old-mode major-mode))
(lambda ()
(and (derived-mode-p 'org-mode)
(eq old-mode 'org-mode)
(or (not (eq old-buffer (current-buffer)))
(not (eq old-pos (point))))
(org-mark-ring-push old-pos old-buffer)))))
elem)
(condition-case err
(org-link-search search-str)
;; Save position before error-ing out so user
;; can easily move back to the original buffer.
(error (funcall save-position-maybe)
(error (nth 1 err))))
(setq elem (org-element-at-point))
;; (message "[get-link-search-location DBG] elem: %S" elem)

(unless buffer ;Kill the buffer if it wasn't open already
(kill-buffer (current-buffer)))
elem))))
(org-link-search search-str) ;This is extracted from the `org-open-file' function.
(setq elem (org-element-at-point))
;; (message "[get-link-search-location DBG] elem: %S" elem)
(unless buffer ;Kill the buffer if it wasn't open already
(kill-buffer (current-buffer))))
elem))

(defun org-hugo-link (link desc info)
"Convert LINK to Markdown format.
Expand Down Expand Up @@ -2432,26 +2423,43 @@ and rewrite link paths to make blogging more seamless."
(;; Remove the "file://" prefix.
(string= type "file")
;; (message "[org-hugo-link DBG] raw-path: %s" raw-path)
(let ((path1 (replace-regexp-in-string "\\`file://" "" raw-path)))
(if (string= ".org" (downcase (file-name-extension path1 ".")))
(let ((link-search-str
;; If raw-link is "./foo.org::#bar",
;; set `link-search-str' to
;; "#bar".
(when (string-match ".*\\.org::\\(.*\\)" raw-link)
(match-string-no-properties 1 raw-link)))
(anchor ""))
;; (message "[org-hugo-link DBG] link-search-str: %s" link-search-str)
(when link-search-str
(let ((matched-elem
(org-hugo--search-and-get-element raw-path link-search-str)))
(when matched-elem
(setq anchor (format "#%s" (org-hugo--get-anchor matched-elem info))))))
;; (message "[org-hugo-link DBG] link search anchor: %S" anchor)
(format "{{< relref \"%s%s\" >}}"
(file-name-sans-extension (file-name-nondirectory path1))
anchor))
(org-hugo--attachment-rewrite-maybe path1 info))))
(let* ((path1 (replace-regexp-in-string "\\`file://" "" raw-path))
(path-lc (downcase path1)))
(cond
(;; foo.org, foo.org::* Heading, foo.org::#custom_id
(string= ".org" (file-name-extension path-lc "."))
(let ((ref "")
(anchor ""))
(if (string-suffix-p org-hugo--preprocessed-buffer-dummy-file-suffix path-lc)
(progn
(setq ref (string-remove-suffix
org-hugo--preprocessed-buffer-dummy-file-suffix
(file-name-nondirectory path1)))
;; Dummy Org file paths created in
;; `org-hugo--get-pre-processed-buffer'
;; For dummy Org file paths, we are
;; limiting to only "#" style search
;; strings.
(when (string-match ".*\\.org::\\(#.*\\)" raw-link)
(setq anchor (match-string-no-properties 1 raw-link))))
;; Regular Org file paths.
(setq ref (file-name-sans-extension (file-name-nondirectory path1)))
(let ((link-search-str
;; If raw-link is "./foo.org::#bar",
;; set `link-search-str' to
;; "#bar".
(when (string-match ".*\\.org::\\(.*\\)" raw-link)
(match-string-no-properties 1 raw-link))))
;; (message "[org-hugo-link DBG] link-search-str: %s" link-search-str)
(when link-search-str
(let ((matched-elem
(org-hugo--search-and-get-element raw-path link-search-str)))
(when matched-elem
(setq anchor (format "#%s" (org-hugo--get-anchor matched-elem info))))))))
;; (message "[org-hugo-link DBG] link search anchor: %S" anchor)
(format "{{< relref \"%s%s\" >}}" ref anchor)))
(t ;; attachments like foo.png
(org-hugo--attachment-rewrite-maybe path1 info)))))
(t
raw-path)))
(link-param-str (org-string-nw-p (org-trim link-param-str))))
Expand Down Expand Up @@ -4385,24 +4393,24 @@ links."
;; link should point to the file (without
;; anchor).
((org-element-property :EXPORT_FILE_NAME destination)
(concat destination-path ".org"))
;; Hugo only supports anchors to headings, so
;; if a "fuzzy" type link points to anything
;; else than a heading, it should point to
;; the file.
(concat destination-path org-hugo--preprocessed-buffer-dummy-file-suffix))
;; Hugo only supports anchors to headings,
;; so if a "fuzzy" type link points to
;; anything else than a heading, it should
;; point to the file.
((and (string= type "fuzzy")
(not (string-prefix-p "*" raw-link)))
(concat destination-path ".org"))
(concat destination-path org-hugo--preprocessed-buffer-dummy-file-suffix))
;; In "custom-id" type links, the raw-link
;; matches the anchor of the destination.
((string= type "custom-id")
(concat destination-path ".org::" raw-link))
(concat destination-path org-hugo--preprocessed-buffer-dummy-file-suffix "::" raw-link))
;; In "id" and "fuzzy" type links, the anchor
;; of the destination is derived from the
;; :CUSTOM_ID property or the title.
(t
(let ((anchor (org-hugo--get-anchor destination info)))
(concat destination-path ".org::#" anchor)))))
(concat destination-path org-hugo--preprocessed-buffer-dummy-file-suffix "::#" anchor)))))
;; If the link destination is a heading and if
;; user hasn't set the link description, set the
;; description to the destination heading title.
Expand Down
12 changes: 6 additions & 6 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -3649,9 +3649,9 @@ to a heading with a *:CUSTOM_ID* property will be resolved to the
appropriate location in the linked file. Links to headings and
links to targets will be resolved to the containing file.

- [[file:link-destination.org::#external-target][Link to CUSTOM_ID]]
- [[file:link-destination.org::*external-target][Link to a heading]]
- [[file:link-destination.org::external-target][Link to a target]]
- [[file:issues/issue-556.org::#heading-xyz][Link to CUSTOM_ID]]
- [[file:issues/issue-556.org::* Heading 3][Link to a heading]]
- [[file:issues/issue-556.org::paragraph-2][Link to an Org Target]]
*** Internal links :internal_links:
Internal links point to targets in the current subtree that will be
exported to the same Hugo post as the link source. To handle links to
Expand Down Expand Up @@ -3693,19 +3693,19 @@ resolved to the containing post.
:ID: 8e65ff86-3f9a-48ef-9b43-751a2e8a9372
:END:
<<internal target link>>
*** Link destination
** Link destination
:PROPERTIES:
:CUSTOM_ID: link-destination
:EXPORT_FILE_NAME: link-destination
:ID: 1e5e0bcd-caea-40ad-a75b-e488634c2678
:END:
**** External target
*** External target
:PROPERTIES:
:CUSTOM_ID: external-target
:ID: de0df718-f9b4-4449-bb0a-eb4402fa5fcb
:END:
<<external-target>>
**** External target with *bold* and /italic/
*** External target with *bold* and /italic/
** Url encoding in links :escaping:url:encoding:
:PROPERTIES:
:EXPORT_FILE_NAME: url-encoding-in-links
Expand Down
5 changes: 5 additions & 0 deletions test/site/content-org/issues/issue-556.org
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ This heading's anchor will be derived off the ~ID~.
* %
This heading's anchor will be derived from /md5/ of the title as it is
not alphanumeric.
* Org Target
paragraph one

<<paragraph-2>>
paragraph two

* Local Variables :ARCHIVE:noexport:
#+bind: org-hugo-anchor-functions (org-hugo-get-custom-id org-hugo-get-id org-hugo-get-heading-slug org-hugo-get-md5)
Expand Down
8 changes: 8 additions & 0 deletions test/site/content/issues/issue-556.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ This heading's anchor will be derived off the `ID`.

This heading's anchor will be derived from _md5_ of the title as it is
not alphanumeric.


## Org Target {#org-target}

paragraph one

<span class="org-target" id="org-target--paragraph-2"></span>
paragraph two
17 changes: 3 additions & 14 deletions test/site/content/posts/links-outside-the-same-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ to a heading with a **:CUSTOM_ID** property will be resolved to the
appropriate location in the linked file. Links to headings and
links to targets will be resolved to the containing file.

- [Link to CUSTOM_ID]({{< relref "link-destination#external-target" >}})
- [Link to a heading]({{< relref "link-destination" >}})
- [Link to a target]({{< relref "link-destination" >}})
- [Link to CUSTOM_ID]({{< relref "issue-556#heading-xyz" >}})
- [Link to a heading]({{< relref "issue-556#heading-abc" >}})
- [Link to an Org Target]({{< relref "issue-556#d41d8c" >}})


## Internal links <span class="tag"><span class="internal_links">internal-links</span></span> {#internal-links}
Expand Down Expand Up @@ -69,14 +69,3 @@ resolved to the containing post.
## Internal target {#internal-target}

<span class="org-target" id="org-target--internal-target-link"></span>


## Link destination {#link-destination}


### External target {#external-target}

<span class="org-target" id="org-target--external-target"></span>


### External target with **bold** and _italic_ {#external-target-with-bold-and-italic}

0 comments on commit 54b213a

Please sign in to comment.