├── .gitignore ├── example.org ├── README.org └── org-newtab-link.el /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.elc 3 | -------------------------------------------------------------------------------- /example.org: -------------------------------------------------------------------------------- 1 | #+html_link_newtab: t 2 | 3 | * Chapter1 4 | 5 | [[https://example.com/]] and [[https://example.org/]] will open in a new tabs. 6 | 7 | #+attr_html: :target _self 8 | [[https://example.com/]] will open in the current tab because the target attribute is explicitly specified. [[https://example.org/]] will open in a new tab because attr_html does not affect the second link in the paragraph. 9 | 10 | [[*Chapter1][Chapter1]] will open in the current tab. 11 | 12 | #+attr_html: :target _blank :rel noopener 13 | [[*Chapter1][Chapter1]] will open in a new tab. 14 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | Open org-mode links exported as HTML in a new tab. 2 | 3 | * Usage 4 | 5 | #+begin_src elisp 6 | (with-eval-after-load "org" 7 | (require 'org-newtab-link)) 8 | #+end_src 9 | 10 | In org-mode: 11 | 12 | #+begin_src org 13 | ,#+html_link_newtab: t 14 | ,* Chapter1 15 | 16 | [​[https://example.com/]] and [​[https://example.org/]] will open in a new tabs. 17 | 18 | ,#+attr_html: :target _self 19 | [​[https://example.com/]] will open in the current tab because the target attribute is explicitly specified. [​[https://example.org/]] will open in a new tab because attr_html does not affect the second link in the paragraph . 20 | 21 | [​[*Chapter1][Chapter1]] will open in the current tab. 22 | 23 | #+attr_html: :target _blank :rel noopener 24 | [[*Chapter1][Chapter1]] will open in a new tab. 25 | #+end_src 26 | 27 | The variable ~org-newtab-link-enabled~ can be a file-local variable or a directory-local variable. 28 | -------------------------------------------------------------------------------- /org-newtab-link.el: -------------------------------------------------------------------------------- 1 | ;;; org-newtab-link.el --- Open Org Link in New Tab -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2021 AKIYAMA Kouhei 4 | 5 | ;; Author: AKIYAMA Kouhei 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; 24 | 25 | ;;; Code: 26 | 27 | (eval-when-compile 28 | (require 'ox)) 29 | 30 | ;;;; Filter Function 31 | 32 | (defun org-newtab-link-filter (s backend _info) 33 | (if (and 34 | ;;@todo Support 35 | (with-no-warnings ;; lazy load ox.el 36 | (org-export-derived-backend-p backend 'html)) ;; html only 37 | (not (string-match "\\`[^>]* target=\"" s)) ;; has no target= 38 | (not (string-match "\\`[^>]* rel=\"" s)) ;; has no rel= 39 | (string-match "\\`[^>]* href=\"[^#]" s) ;;not internal link 40 | (string-match "\\` (:filter-link . ()) 76 | (when (null filter-link) 77 | (push (setq filter-link (list :filter-link)) 78 | (org-export-backend-filters backend))) 79 | ;; (:filter-link . function) => (:filter-link . (function)) 80 | (when (functionp (cdr filter-link)) 81 | (setcdr filter-link (list (cdr filter-link)))) 82 | ;; Add my filter function 83 | (when (not (memq 'org-newtab-link-filter-opt (cdr filter-link))) 84 | (push 'org-newtab-link-filter-opt (cdr filter-link)))))) 85 | 86 | (with-eval-after-load "ox-html" 87 | (org-newtab-link-modify-html-backend)) 88 | 89 | 90 | (provide 'org-newtab-link) 91 | ;;; org-newtab-link.el ends here 92 | --------------------------------------------------------------------------------