Function: markdown-ts-table-export-table

markdown-ts-table-export-table is an interactive and byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts-table-export-table &optional DISPLAY FORMAT)

Documentation

Export the Markdown table at point to CSV in a buffer.

If DISPLAY is non-nil, or with a prefix argument, display the export buffer after exporting. FORMAT is one of the symbols csv for comma-separated values aka CSV, or tsv for tab-separated values aka TSV. If any other value, use csv. If TSV, replace any tabs within a field with a space. Do not export the table's header delimiter row. If point is not at a table, do nothing.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-table-export-table (&optional display format)
  "Export the Markdown table at point to CSV in a buffer.
If DISPLAY is non-nil, or with a prefix argument, display the export
buffer after exporting.
FORMAT is one of the symbols `csv' for comma-separated values aka CSV,
or `tsv' for tab-separated values aka TSV.  If any other value, use
`csv'.  If TSV, replace any tabs within a field with a space.
Do not export the table's header delimiter row.
If point is not at a table, do nothing."
  (interactive "P")
  (markdown-ts--barf-if-not-mode 'markdown-ts-table-export-table)
  (when-let* ((at-table (markdown-ts-at-table-p nil t))
              (table (cdr at-table))
              (rows (treesit-node-children table 'named))
              (header (treesit-search-subtree table "\\`pipe_table_header\\'"))
              (delimiter-row (treesit-search-subtree
                              table "\\`pipe_table_delimiter_row\\'"))
              (export-buffer (get-buffer-create
                              markdown-ts-table-export-buffer)))
    (with-current-buffer export-buffer
      (special-mode)
      (let ((inhibit-read-only t))
        (widen)
        (erase-buffer)
        (dolist (row rows)
          (unless (equal row delimiter-row)
            (let* ((elts (treesit-node-children row 'named))
                   (len (length elts)))
              (dotimes (x len)
                (let ((s (string-trim (treesit-node-text (nth x elts)
                                                         'no-property))))
                  (pcase format
                    ('tsv
                     (insert (replace-regexp-in-string "\t" "\s" s))
                     (when (< x (1- len))
                       (insert "\t")))
                    (_
                     (insert
                      (if (string-match "[\",]" s)
                          (concat "\""
                                  (mapconcat
                                   'identity (split-string s "\"") "\"\"")
                                  "\"")
                        s))
                     (when (< x (1- len))
                       (insert ","))))))
              (insert "\n"))))
        (goto-char (point-min))))
    (when display
      (display-buffer export-buffer))))