handle links correctly

This commit is contained in:
Yann Esposito (Yogsototh) 2019-07-06 17:18:13 +02:00
parent 218305dbfe
commit a3775b87db
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
2 changed files with 117 additions and 98 deletions

View file

@ -2,5 +2,5 @@
#+begin_archive #+begin_archive
@@html:<li>@@ @@html:<span class="archive-item"><span class="archive-date">@@ Jul 05, 2019 @@html:</span>@@ [[file:posts/2019-07-04-org-publish.org][org-publish]] @@html:</span>@@ @@html:</li>@@ @@html:<li>@@ @@html:<span class="archive-item"><span class="archive-date">@@ jul 06, 2019 @@html:</span>@@ [[file:posts/2019-07-04-org-publish.org][Static blog with org-mode]] @@html:</span>@@ @@html:</li>@@
#+end_archive #+end_archive

View file

@ -1,4 +1,4 @@
* Code magic :noexport: * Code magic :noexport:
#+TITLE: her.esy.fun #+TITLE: her.esy.fun
#+AUTHOR: Yann Esposito #+AUTHOR: Yann Esposito
#+EMAIL: yann.esposito@gmail.com #+EMAIL: yann.esposito@gmail.com
@ -9,116 +9,132 @@
#+CREATOR: Spacemacs, org-mode (Emacs 26.1, Org mode 9.2.3) #+CREATOR: Spacemacs, org-mode (Emacs 26.1, Org mode 9.2.3)
#+OPTIONS: H:6 #+OPTIONS: H:6
#+begin_src elisp :results none #+begin_src elisp :results none
(require 'org) (require 'org)
(require 'ox-publish) (require 'ox-publish)
(require 'ox-html) (require 'ox-html)
(require 'org-element) (require 'org-element)
(require 'ox-rss) (require 'ox-rss)
(defun org-blog-prepare (project-plist) (defun org-blog-prepare (project-plist)
"With help from `https://github.com/howardabrams/dot-files'. "With help from `https://github.com/howardabrams/dot-files'.
Touch `index.org' to rebuilt it. Touch `index.org' to rebuilt it.
Argument `PROJECT-PLIST' contains information about the current project." Argument `PROJECT-PLIST' contains information about the current project."
(let* ((base-directory (plist-get project-plist :base-directory)) (let* ((base-directory (plist-get project-plist :base-directory))
(buffer (find-file-noselect (expand-file-name "index.org" base-directory) t))) (buffer (find-file-noselect (expand-file-name "index.org" base-directory) t)))
(with-current-buffer buffer (with-current-buffer buffer
(set-buffer-modified-p t) (set-buffer-modified-p t)
(save-buffer 0)) (save-buffer 0))
(kill-buffer buffer))) (kill-buffer buffer)))
(defvar org-blog-head (defvar org-blog-head
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/assets/css/minimalist.css\"/> "<link rel=\"stylesheet\" type=\"text/css\" href=\"/assets/css/minimalist.css\"/>
<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/favicon.ico\">") <link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/favicon.ico\">")
(defun org-blog-preamble (_plist) (defun org-blog-preamble (_plist)
"Pre-amble for whole blog." "Pre-amble for whole blog."
"<strong><code>Programing Heresy</code></strong>") "<strong><code>Programing Heresy</code></strong>")
(defun org-blog-postamble (_plist) (defun org-blog-postamble (_plist)
"Post-amble for whole blog." "Post-amble for whole blog."
"<hr/> "<hr/>
<a href=\"/\">Home</a> - <a href=\"/\">Home</a> -
<a href=\"/archive.html\">Posts</a>") <a href=\"/archive.html\">Posts</a>")
(defun org-blog-sitemap-format-entry (entry _style project) (defun org-blog-sitemap-format-entry (entry _style project)
"Return string for each ENTRY in PROJECT." "Return string for each ENTRY in PROJECT."
(when (s-starts-with-p "posts/" entry) (when (s-starts-with-p "posts/" entry)
(format "@@html:<span class=\"archive-item\"><span class=\"archive-date\">@@ %s @@html:</span>@@ [[file:%s][%s]] @@html:</span>@@" (format "@@html:<span class=\"archive-item\"><span class=\"archive-date\">@@ %s @@html:</span>@@ [[file:%s][%s]] @@html:</span>@@"
(format-time-string "%h %d, %Y" (format-time-string "%h %d, %Y"
(org-publish-find-date entry project)) (org-publish-find-date entry project))
entry entry
(org-publish-find-title entry project)))) (org-publish-find-title entry project))))
(defun org-blog-sitemap-function (title list) (defun org-blog-sitemap-function (title list)
"Return sitemap using TITLE and LIST returned by `org-blog-sitemap-format-entry'." "Return sitemap using TITLE and LIST returned by `org-blog-sitemap-format-entry'."
(concat "#+TITLE: " title "\n\n" (concat "#+TITLE: " title "\n\n"
"\n#+begin_archive\n" "\n#+begin_archive\n"
(mapconcat (lambda (li) (mapconcat (lambda (li)
(format "@@html:<li>@@ %s @@html:</li>@@" (car li))) (format "@@html:<li>@@ %s @@html:</li>@@" (car li)))
(seq-filter #'car (cdr list)) (seq-filter #'car (cdr list))
"\n") "\n")
"\n#+end_archive\n")) "\n#+end_archive\n"))
(setq base-dir (concat (projectile-project-root) "src")) (setq base-dir (concat (projectile-project-root) "src"))
(setq publish-dir (concat (projectile-project-root) "_site")) (setq publish-dir (concat (projectile-project-root) "_site"))
(setq assets-dir (concat base-dir "/assets")) (setq assets-dir (concat base-dir "/assets"))
(setq publish-assets-dir (concat publish-dir "/assets")) (setq publish-assets-dir (concat publish-dir "/assets"))
(setq rss-dir base-dir) (setq rss-dir base-dir)
(setq publish-rss-dir publish-dir) (setq publish-rss-dir publish-dir)
(setq org-publish-project-alist (setq domainname "https://her.esy.fun")
`(("orgfiles" (setq org-publish-project-alist
:base-directory ,base-dir `(("orgfiles"
:exclude ".*drafts/.*" :base-directory ,base-dir
:base-extension "org" :exclude ".*drafts/.*"
:base-extension "org"
:publishing-directory ,publish-dir :publishing-directory ,publish-dir
:recursive t :recursive t
:preparation-function org-blog-prepare :preparation-function org-blog-prepare
:publishing-function org-html-publish-to-html :publishing-function org-html-publish-to-html
:with-toc nil :with-toc nil
:with-title t :with-title t
:with-date t :with-date t
:section-numbers nil :section-numbers nil
:html-doctype "html5" :html-doctype "html5"
:html-html5-fancy t :html-html5-fancy t
:html-head-include-default-style nil :html-head-include-default-style nil
:html-head-include-scripts nil :html-head-include-scripts nil
:htmlized-source t :htmlized-source t
:html-head-extra ,org-blog-head :html-head-extra ,org-blog-head
:html-preamble org-blog-preamble :html-preamble org-blog-preamble
:html-postamble org-blog-postamble :html-postamble org-blog-postamble
:auto-sitemap t :auto-sitemap t
:sitemap-filename "archive.org" :sitemap-filename "archive.org"
:sitemap-title "Blog Posts" :sitemap-title "Blog Posts"
:sitemap-style list :sitemap-style list
:sitemap-sort-files anti-chronologically :sitemap-sort-files anti-chronologically
:sitemap-format-entry org-blog-sitemap-format-entry :sitemap-format-entry org-blog-sitemap-format-entry
:sitemap-function org-blog-sitemap-function) :sitemap-function org-blog-sitemap-function)
("assets" ("assets"
:base-directory ,assets-dir :base-directory ,assets-dir
:base-extension ".*" :base-extension ".*"
:publishing-directory ,publish-assets-dir :publishing-directory ,publish-assets-dir
:publishing-function org-publish-attachment :publishing-function org-publish-attachment
:recursive t) :recursive t)
("rss" ("rss"
:base-directory ,rss-dir :base-directory ,rss-dir
:base-extension "org" :base-extension "org"
:html-link-home "https://her.esy.fun/" :html-link-home ,domainname
:html-link-use-abs-url t :html-link-use-abs-url t
:rss-extension "xml" :rss-extension "xml"
:publishing-directory ,publish-rss-dir :publishing-directory ,publish-rss-dir
:publishing-function (org-rss-publish-to-rss) :publishing-function (org-rss-publish-to-rss)
:exclude ".*" :exclude ".*"
:include ("archive.org") :include ("archive.org")
:section-numbers nil :section-numbers nil
:table-of-contents nil) :table-of-contents nil)
("blog" :components ("orgfiles" "assets" "rss"))))
;; add target=_blank and rel="noopener noreferrer" to all links by default
(defun my-org-export-add-target-blank-to-http-links (text backend info)
"Add target=\"_blank\" to external links."
(when (and
(org-export-derived-backend-p backend 'html)
(string-match "href=\"http[^\"]+" text)
(not (string-match "target=\"" text))
(not (string-match (concat "href=\"" domainname "[^\"]*") text)))
(string-match "<a " text)
(replace-match "<a target=\"_blank\" rel=\"noopener noreferrer\" " nil nil text)))
(add-to-list 'org-export-filter-link-functions
'my-org-export-add-target-blank-to-http-links)
("blog" :components ("orgfiles" "assets" "rss"))))
#+end_src #+end_src
* Welcome! * Welcome!
@ -129,3 +145,6 @@ This is a new take on my personal blog.
With a lot more minimalism in mind. With a lot more minimalism in mind.
With [[http://orgmode.org][org-mode]] With [[http://orgmode.org][org-mode]]
TEST