Org-roam: Supporting export & publish features

Created on 22 Feb 2020  路  5Comments  路  Source: org-roam/org-roam

Brief Abstract

Add support for org-export and publishing

Long Description

So when you export/publish to whatever option you choose, example being html, take the backlink references with links and add them to a section at the bottom of the exported file. Similar to a cite or reference.

Proposed Implementation (if any)

Please check the following:

  • [x] No similar feature requests
enhancement

Most helpful comment

Fair enough :)

@nmartin84: Here's where I ended up, it works well for me if you'd like to use it as well. Should work for any export backend. Note that the second function is...functioning properly, but bad. I haven't figured out how to use org-export-filter-keyword-functions properly, so this is my hacky way of making this work. Since this isn't going into any package, I'm not going to bother figuring it out correctly haha. Just put #+roam: backlinks on a line on its own, and that's where it'll end up in the export.

(defun org-roam--backlinks-string (file-path)
  (org-roam--ensure-cache-built)
  (if-let
      (ht (gethash file-path org-roam-backward-links-cache))
      (-reduce-from
       (lambda (x file-from)
     (concat x (format " - [[file:%s][%s]]\n"
               file-from
               (org-roam--get-title-or-slug file-from))))
       "\n* Backlinks\n"
       (hash-table-keys ht))
    ""))

(defun org-roam--backlinks-export (backend)
  (replace-regexp "^\\#\\+[Rr][Oo][Aa][Mm]: backlinks"
          (org-roam--backlinks-string (buffer-file-name))))

(add-to-list 'org-export-before-processing-hook #'org-roam--backlinks-export)

All 5 comments

The first step for this would be #154, which I posted literally 7 minutes before you :)

Here's what I'll be using just to get the string that needs inserted somewhere:

(defun my/collect-backlinks-string (file)
  (org-roam--ensure-cache-built)
  (if-let
      ((backlinks (hash-table-keys (gethash file org-roam-backward-links-cache))))
      (-reduce-from
       (lambda (x file-from)
     (concat x (format "** [[file:%s][%s]]\n"
               file-from
               (org-roam--get-title-or-slug file-from))))
       "\n\n* Backlinks\n"
       backlinks)
    ""))

I'm trying to learn more elisp as I look into this so I can do it the right way, but I feel like I'll have a solution for this soon. I was just going to do it for my specific case, but it seems like the right solution for my self will end up solving this as well. If @jethrokuan is ok with having some export related code in org-roam, I'll open a PR once I've figured this out.

The string seems like an opinionated view of what should be exported. I think the cache structure is easy enough to read from, and any export code can be written explicitly in configs. At the moment I'd like to keep the core functionality small, so it's easier to iterate upon.

Fair enough :)

@nmartin84: Here's where I ended up, it works well for me if you'd like to use it as well. Should work for any export backend. Note that the second function is...functioning properly, but bad. I haven't figured out how to use org-export-filter-keyword-functions properly, so this is my hacky way of making this work. Since this isn't going into any package, I'm not going to bother figuring it out correctly haha. Just put #+roam: backlinks on a line on its own, and that's where it'll end up in the export.

(defun org-roam--backlinks-string (file-path)
  (org-roam--ensure-cache-built)
  (if-let
      (ht (gethash file-path org-roam-backward-links-cache))
      (-reduce-from
       (lambda (x file-from)
     (concat x (format " - [[file:%s][%s]]\n"
               file-from
               (org-roam--get-title-or-slug file-from))))
       "\n* Backlinks\n"
       (hash-table-keys ht))
    ""))

(defun org-roam--backlinks-export (backend)
  (replace-regexp "^\\#\\+[Rr][Oo][Aa][Mm]: backlinks"
          (org-roam--backlinks-string (buffer-file-name))))

(add-to-list 'org-export-before-processing-hook #'org-roam--backlinks-export)

@benide - thanks man, will test later. Appreciate it!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jason-morgan picture jason-morgan  路  5Comments

oatmealm picture oatmealm  路  4Comments

tmalsburg picture tmalsburg  路  6Comments

fkgruber picture fkgruber  路  4Comments

cantao picture cantao  路  5Comments