4

I'm using the JSON produced by org-export-json to display it in a virtual DOM (the output is an array like [ element_type, properties{}, children[]... ] very suitable for this).

Being not very fluent in Lisp, I'm wondering how to modify this exporter and instead of the entire file, to append only relevant tags or TODO keywords, along with the directives (the header).

Flint
  • 292
  • 2
  • 10

1 Answers1

5

Here is a lightly adapted version of the export code you were using. The strategy I used to get selective export is to make a temporary org-file using org-map-entries and the match argument for it to only copy headings that match the criteria, and then to export the temp file.

(require 'json)

(defun org-export-json-buffer ()
  (interactive)
  (let* ((tree (org-element-parse-buffer 'object nil)))
    (org-element-map tree (append org-element-all-elements
                                  org-element-all-objects '(plain-text))
      (lambda (x)
        (if (org-element-property :parent x)
            (org-element-put-property x :parent "none"))
        (if (org-element-property :structure x)
            (org-element-put-property x :structure "none"))))
    (with-current-buffer (get-buffer-create "*ox-json*")
      (erase-buffer)
      (insert (json-encode tree))
      (json-pretty-print-buffer))
    (switch-to-buffer (get-buffer-create "*ox-json*"))))

(defun org-to-json (query)
  "Export headings that match QUERY to json."
  (org-map-entries
   (lambda ()
     (org-copy-subtree)
     (with-current-buffer (get-buffer-create "-t-")
       (yank)))
   query)
  (with-current-buffer "-t-" (org-export-json-buffer))
  (kill-buffer "-t-"))

(org-to-json "TODO=\"DONE\"") ; filter out headings marked DONE
(org-to-json "important") ; filter out headings tagged important
John Kitchin
  • 11,555
  • 1
  • 19
  • 41