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).


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 ()
  (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*")
      (insert (json-encode tree))
    (switch-to-buffer (get-buffer-create "*ox-json*"))))

(defun org-to-json (query)
  "Export headings that match QUERY to json."
   (lambda ()
     (with-current-buffer (get-buffer-create "-t-")
  (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

Your Answer

