Org-mode publishing - Adding disqus code


Table of Contents

1 How to add disqus code to static pages generated with Emacs org mode?

One option is to use org-mode MACRO REPLACEMENTS

Another option is to install a filter function.

In this example I will use a filter function.

2 Generate a template to include in the pages you want to display disqus comments:

I will call this template

here is my template

<div id="disqus_thread"></div>

var disqus_config = function () { = 'http://{{my_site_domain}}{{my_site_baseurl}}{{my_page_url}}';  //  page's canonical URL = '{{my_site_baseurl}}{{my_page_url}}'; //  page's unique identifier variable

(function() {  // DON'T EDIT BELOW THIS LINE
   var d = document, s = d.createElement('script');

   s.src = '//{{my_disqus_shortname}}';

   s.setAttribute('data-timestamp', +new Date());
   (d.head || d.body).appendChild(s);


<noscript>Please enable JavaScript to view the <a href="" rel="nofollow">comments powered by Disqus.</a></noscript>


Note that inside the template there are 4 variables that will be replaced by our org-mode filter:





Also note that we tell org-mode that the template is a literal html by enclosing the content between



3 Include the template in the pages where you want disqus comments to appear:

Now that we have a template, use the following code in your org-mode files to insert the template where you want the disqus comments block to appear:


4 Install the org-mode filter function that will replace the variables:

Evaluate the following code inside emacs to set variables and install the filter function. The values of the variables will be replaced inside the disqus included template. The filter function simply takes the content as input, and returns the content after replacing the values.

ATTENTION: remember to replace site-domain, site-baseurl and disqus-shortname with your own values.

(setq site-domain "" )
;; configure site-baseurl to "/directory/" if the site is inside a subdirectory.  Otherwise set it to  "/"
(setq site-baseurl "/notes/" )
;; your disqus name
(setq disqus-shortname "snarvaez-com-ar-libertad")

(defun basename (path)
  (file-name-nondirectory (directory-file-name path)))

;; function to simply replace a regular expression in the output
(defun my-final-filter(output backend info)
  ;; inside org exports,  file variable containts a string with the full path of the output file
  (setq page-url  (basename file) )
  (setq output  (replace-regexp-in-string  "{{my_site_domain}}" site-domain output ))
  (setq output  (replace-regexp-in-string  "{{my_site_baseurl}}" site-baseurl output ))
  (setq output  (replace-regexp-in-string  "{{my_page_url}}" page-url  output ))
  (setq output (replace-regexp-in-string  "{{my_disqus_shortname}}" disqus-shortname  output ))

(setq org-export-filter-final-output-functions  '(my-final-filter) )

5 Finally build the project and generate the output html files

Publishing a whole project in org-mode requires setting some org-mode variables. We can create an elisp configuration file per project. For example this is my build.el configuration file. When I want to build the project, I open the file build.el in emacs, and execute the buffer with M-x eval-buffer

To understand how to configure publishing projects with org-mode, please read the tutorial here:

Publishing Org-mode files to HTML

ATTENTION: Remember to replace :base-directory and :publishing-directory according to your local project path

(require 'ox-publish)
(setq org-publish-project-alist

       ;; ... add all the components here
         :base-directory "~/c_workspace/sen-blog/snarvaez_com_ar_notes/org_notes/"
         :base-extension "org"
         :publishing-directory "~/c_workspace/sen-blog/snarvaez_com_ar_notes/_site/"
         :recursive t
         :publishing-function org-html-publish-to-html
         :headline-levels 4             ; Just the default for this project.
         :auto-preamble t
         :auto-sitemap t                ; Generate automagically...
         :sitemap-filename ""  ; ... call it (it's the default)...
         :sitemap-title "Sitemap"         ; ... with title 'Sitemap'.
         :language "en"
         :html-head  "<link rel='stylesheet' type='text/css' href='style.css' />"
         :html-head-extra "<meta name='viewport' content='width=device-width, initial-scale=1.0' />"
         :with-author "Sebastian Emilio Narvaez"
         :with-email nil
         ;; :html-preamble  my-preamble-format
         :exports code
         ;; :html-postamble my-postamble-format
         :htmlized-source t    ;; htmlized-source     non-nil means, publish htmlized source

         :base-directory "~/c_workspace/sen-blog/snarvaez_com_ar_notes/org_notes/"
         :base-extension "html\\|css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
         :publishing-directory "~/c_workspace/sen-blog/snarvaez_com_ar_notes/_site/"
         :recursive t
         :publishing-function org-publish-attachment

        ("org" :components ("org-notes" "org-static"))


(org-publish-project "org")

In this example, I keep my org-notes inside the folder:


and the final site will be saved inside the folder:


Remember to adjust according to your local folders.

The folder _site will contain the html files ready to be uploaded to a hosting server.

Finally, you can use a tool like rsync to syncrhonize the contents of your local folder with the remote hosting server.

This is my sync script,


rsync  -av  -e "ssh -l the_user -i /home/the_user/.ssh/id_rsa"    ./_site/    the_user@the_domain:/home/vhosts/my_site/public_html/notes/

Author: Sebastian Emilio Narvaez

Created: 2019-05-22 Wed 17:54

Emacs 25.2.2 (Org mode 8.2.10)