diff --git a/ox-hugo.el b/ox-hugo.el index 76887de..5f046e9 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -1077,28 +1077,55 @@ and rewrite link paths to make blogging more seamless." ((equal type "radio") contents) (t - (let ((path (cond - ((member type '("http" "https" "ftp")) - (concat type ":" raw-path)) - (;; Do not add the "file://" prefix if the raw-path - ;; is in the Hugo "static" dir. - (and (string= type "file") - (let ((static-dir (file-truename - (concat - (file-name-as-directory (plist-get info :hugo-base-dir)) - "static/"))) - (raw-path-true (file-truename raw-path))) - (string-match-p (regexp-quote static-dir) raw-path-true))) - (let* ((path1 (org-export-file-uri (funcall link-org-files-as-md raw-path))) - (path1 (replace-regexp-in-string "\\`file://" "" path1))) - (org-hugo--attachment-rewrite-maybe path1 info))) - (t - raw-path)))) + (let* (attributes-plist + attributes + (path (cond + ((member type '("http" "https" "ftp")) + ;; Taken from ox-html.el -- Extract attributes + ;; from parent's paragraph. HACK: Only do this + ;; for the first link in parent (inner image link + ;; for inline images). This is needed as long as + ;; attributes cannot be set on a per link basis. + (setq attributes-plist + (let ((parent (org-export-get-parent-element link))) + (and (eq (org-element-map parent 'link #'identity info :first-match) link) + (org-export-read-attribute :attr_html parent)))) + (setq attributes + (let ((attr (org-html--make-attribute-string attributes-plist))) + (when (org-string-nw-p attr) + (concat " " attr)))) + ;; (message "[ox-hugo-link DBG] attributes: %s" attributes) + (concat type ":" raw-path)) + (;; Do not add the "file://" prefix if the raw-path + ;; is in the Hugo "static" dir. + (and (string= type "file") + (let ((static-dir (file-truename + (concat + (file-name-as-directory (plist-get info :hugo-base-dir)) + "static/"))) + (raw-path-true (file-truename raw-path))) + (string-match-p (regexp-quote static-dir) raw-path-true))) + (let* ((path1 (org-export-file-uri (funcall link-org-files-as-md raw-path))) + (path1 (replace-regexp-in-string "\\`file://" "" path1))) + (org-hugo--attachment-rewrite-maybe path1 info))) + (t + raw-path)))) (if contents (progn ;; (message "[ox-hugo DBG org-hugo-link: contents=%s path=%s" contents path) - (format "[%s](%s)" contents path)) - (format "<%s>" path))))))) + (if attributes + (format "%s" + (org-html-encode-plain-text path) + attributes + (org-link-unescape contents)) + (format "[%s](%s)" contents path))) + (if attributes + (let ((path (org-html-encode-plain-text path))) + (format "%s" + path + attributes + (org-link-unescape path))) + (format "<%s>" path)))))))) ;;;;; Helpers (defun org-hugo--attachment-rewrite-maybe (path info) diff --git a/test/site/content-org/all-posts.org b/test/site/content-org/all-posts.org index 7ea2d7f..a10df8a 100644 --- a/test/site/content-org/all-posts.org +++ b/test/site/content-org/all-posts.org @@ -1291,6 +1291,22 @@ not leak into the post's front-matter. :CUSTOM_ID: link-heading-2 :END: - Link to [[#link-heading-1][Heading 1]] +** Links with target attribute +:PROPERTIES: +:EXPORT_FILE_NAME: links-with-target-attribute +:EXPORT_HUGO_BLACKFRIDAY: :hrefTargetBlank nil +:END: +#+ATTR_HTML: :target _blank :rel noopener +[[http://orgmode.org/manual/Hyperlinks.html][This link (to Hyperlinks chapter in Org manual)]] will open in a new tab +as it is annotated with ~target="_blank"~. + +#+ATTR_HTML: :target _self +[[http://orgmode.org/manual/Hyperlinks.html][Here's the same link]] but with ~target="_self"~ annotation. So +clicking it will open that link in this same tab! + +[[http://orgmode.org/manual/Hyperlinks.html][Here's the same link again]], but this time there is no =#+ATTR_HTML= +annotation. So the behavior will depend on the browser (typically an +external link will open in a new tab automatically). * Equations :equations: ** Inline equations :PROPERTIES: diff --git a/test/site/content/posts/links-with-target-attribute.md b/test/site/content/posts/links-with-target-attribute.md new file mode 100644 index 0000000..69d6af9 --- /dev/null +++ b/test/site/content/posts/links-with-target-attribute.md @@ -0,0 +1,17 @@ ++++ +title = "Links with target attribute" +tags = ["links"] +draft = false +[blackfriday] + hrefTargetBlank = false ++++ + +This link (to Hyperlinks chapter in Org manual) will open in a new tab +as it is annotated with `target="_blank"`. + +Here's the same link but with `target="_self"` annotation. So +clicking it will open that link in this same tab! + +[Here's the same link again](http://orgmode.org/manual/Hyperlinks.html), but this time there is no `#+ATTR_HTML` +annotation. So the behavior will depend on the browser (typically an +external link will open in a new tab automatically).