Skip to content

Commit

Permalink
feat: Target links starting with "." don't get org-target-- prefix
Browse files Browse the repository at this point in the history
- `org-blackfriday--valid-html-anchor-name` is updated so that the
returned anchor names never begin or end with "-"
  • Loading branch information
kaushalmodi committed Feb 17, 2022
1 parent 94553ae commit 719589d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
45 changes: 36 additions & 9 deletions ox-blackfriday.el
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,14 @@ Else return an empty string."
;;;; Convert string to a valid anchor name
(defun org-blackfriday--valid-html-anchor-name (str)
"Turn STR into a valid HTML anchor name.
Replaces invalid characters with \"-\"."
Replaces invalid characters with \"-\". The returned anchor name
will also never begin or end with \"-\".
"
(or (and (stringp str)
(replace-regexp-in-string "[^a-zA-Z0-9_-.]" "-" str))
(string-trim
(replace-regexp-in-string "[^a-zA-Z0-9_-.]" "-" str)
"-"))
""))

;; Return HTML span tags for link targets.
Expand Down Expand Up @@ -1441,16 +1446,38 @@ contextual information."
blank-line-before-table tbl table-post))))

;;;; Target
(defun org-blackfriday--get-target-anchor (target)
"Get HTML anchor for TARGET element.
By default, the returned anchor string is the HTML sanitized
target name (`:value' property of TARGET element) with a prefix
returned by `org-blackfriday--get-ref-prefix'.
If the anchor string begins with \".\", the returned anchor
string is just the HTML sanitized target name without that \".\".
TARGET NAME ANCHOR
abc org-target--abc
abc def org-target--abc-def
.abc abc"
(let ((target-name (org-element-property :value target))
(verbatim-target-prefix ".") ;This needs to be non-alpha-numeric, and not an Org-recognized link prefix like "#"
(prefix ""))
(unless (string-prefix-p verbatim-target-prefix target-name)
(setq prefix (org-blackfriday--get-ref-prefix 'target)))
;; Below function will auto-remove the `verbatim-target-prefix' if
;; present.
(setq target-name (org-blackfriday--valid-html-anchor-name target-name))
(format "%s%s" prefix target-name)))

(defun org-blackfriday-target (target _contents _info)
"Transcode a TARGET object from Org to HTML.
CONTENTS is nil."
(let* ((prefix (org-blackfriday--get-ref-prefix 'target))
(ref (format "%s%s"
prefix
(org-element-property :value target)))
(attr (format " class=\"%s\" id=\"%s\""
(string-remove-suffix "--" prefix)
(org-blackfriday--valid-html-anchor-name ref))))
(let* ((class (string-remove-suffix "--"
(org-blackfriday--get-ref-prefix 'target)))
(anchor (org-blackfriday--get-target-anchor target))
(attr (format " class=\"%s\" id=\"%s\"" class anchor)))
(org-blackfriday--link-target attr)))

;;;; Verse Block
Expand Down
5 changes: 1 addition & 4 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -2198,10 +2198,7 @@ and rewrite link paths to make blogging more seamless."
(org-export-get-reference destination info))))
;; Ref to a <<target>>.
((eq (org-element-type destination) 'target)
(format "%s%s"
(org-blackfriday--get-ref-prefix (org-element-type destination))
;; If the target is <<xyz>>, `raw-path' will be "xyz".
(org-blackfriday--valid-html-anchor-name raw-path)))
(org-blackfriday--get-target-anchor destination))
;; Ref to all other link destinations.
(t
(org-export-get-reference destination info)))))
Expand Down
9 changes: 8 additions & 1 deletion test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -3536,7 +3536,7 @@ Once that is evaluated, links like these will export fine i.e. no
:CUSTOM_ID: link-heading-2
:END:
- Link to [[#link-heading-1][Heading 1]]
*** Links to Org targets
*** Links to Org Targets
:PROPERTIES:
:EXPORT_FILE_NAME: links-to-org-targets
:END:
Expand All @@ -3560,6 +3560,13 @@ link./
#+include: "./misc/common.org::#lorem-ipsum" :only-contents t

*Here we refer to item [[target]].*
**** Using Org targets as verbatim anchors
paragraph 1

<<.paragraph-2>>
paragraph 2

[[.paragraph-2][Link to paragraph 2]]
*** Links to source blocks :src_block:
:PROPERTIES:
:EXPORT_FILE_NAME: links-to-source-blocks
Expand Down
12 changes: 11 additions & 1 deletion test/site/content/posts/links-to-org-targets.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
title = "Links to Org targets"
title = "Links to Org Targets"
tags = ["links", "internal-links"]
draft = false
+++
Expand Down Expand Up @@ -74,3 +74,13 @@ molestie velit. Nullam pellentesque convallis ante, vel posuere libero
blandit in.

**Here we refer to item [2](#org-target--target).**


## Using Org targets as verbatim anchors {#using-org-targets-as-verbatim-anchors}

paragraph 1

<span class="org-target" id="paragraph-2"></span>
paragraph 2

[Link to paragraph 2](#paragraph-2)

0 comments on commit 719589d

Please sign in to comment.