pandoc 轉化md to org時出現的問題

每個標題下面會增加三行:

* 2022-06-08 星期三
:PROPERTIES:
:CUSTOM_ID: 星期三-1
:END:

轉換用的命令是:

pandoc -f markdown -t org -o newfile.org original-file.markdown

請問如何在轉換時,不要這三行。或者這三行怎樣能批量去掉呢?

還出現一個問題,中英文一起的文章,中英文中間會多一個空格。

pandoc -t org file.md | sed -E "/^[[:space:]]+:/ d" > file.org

用 sed 手动把 org 里初始是冒号的行删掉,这有可能删除原 md 中以冒号作为行首的内容,但 md 里几乎不会出现这种写法

参考pandoc - in org-mode, a function to delete all properties drawers? - Emacs Stack Exchange

1 个赞

謝謝,如果用elisp語句呢,原來的代碼是:

  (defun markdown-md-convert-buffer-to-org ()
    "Convert the current buffer's content from markdown to orgmode format and save it with the current buffer's file name but with .org extension."
    (interactive)
    (shell-command-on-region (point-min) (point-max)
                             (format "pandoc -f markdown -t org -o %s"
                                     (concat (file-name-sans-extension (buffer-file-name)) ".org"))))
 (defun markdown-md-convert-buffer-to-org () 
   "Convert the current buffer's content from markdown to orgmode format and save it with the current buffer's file name but with .org extension."
    (interactive)
    (let ((filename (file-name-sans-extension (buffer-file-name))))
      (shell-command-on-region (point-min) (point-max)
                               (format "pandoc -t org %s.md | sed -E '/^[[:space:]]+:/ d' > %s.org" filename filename))))

将当前 buffer 转成同名的 org 文件,另外分享一个在 dired 模式下转换的函数,可以 mark 后批量转:

(require 'seq)
(defun dired-md-to-org ()
  (interactive)
  (let
      ((shell-command "pandoc -t org `?`.md | sed -E '/^[[:space:]]+:/ d' > `?`.org"))
    (setq marked-filepathes (dired-get-marked-files))
    (setq marked-filenames (mapcar 'file-name-nondirectory marked-filepathes))
    (setq marked-source-filenames
          (seq-filter
           (lambda (x) (equal "md" (file-name-extension x))) marked-filenames))
    (setq marked-source-names (mapcar 'file-name-sans-extension marked-source-filenames))
    (dired-do-shell-command shell-command nil marked-source-names))) 

1 个赞

將[[:space:]]+刪除掉,可以刪除掉多餘生成的行,查了一下正則,/^[[:space:]]+表示:前面還有字符,如果以:開頭,不需要加space了,謝謝,謝謝

這些是sed和shell的知識了,電腦有太多知識了。