用 Org 导出 Hexo 可以识别的 Markdown 文件 : )

org
elisp
evil

#1

ORG=>HEXO’s MD

:jack_o_lantern: 更新 2019-02-19:

已提取成单独插件,仓库地址: jk-org2md-hexo

已上传到 Melpa (镜像尚未同步),后续可以通过以下方式使用:

M-x package-install jk-org2md-hexo

添加 (requrie 'jk-org2md-hexo) 到配置文件即可。


:dark_sunglasses: 依赖插件

ox-gfm


:sweat_smile: 自定义的两个函数

jk/insert-date

(defun jk/insert-date ()
  "Insert current date."
  (interactive)
  (insert "#+DATE: ")
  ;; (org-time-stamp t)
  (insert (format-time-string "%Y/%m/%d %T"))
  (insert "\n\n"))

jk/md-export

(defun jk/md-export ()
  "Create front-matter of GFM-md.
Note that it just adapts to a title including most five words."
  (interactive)
  (save-buffer)				;; Save current buffer
  (setq fname (car (split-string (buffer-name) "\\."))) ;; if current buffer is "hello-world.org", FNAME will be "hello-world"
  (setq fdir "../_posts/")				;; Set export dir
  (setq fnamed (concat fdir fname ".md"))			;; "hello-world.md"
  (setq fnamel (split-string fname "-"))		;; Now, fnamel will be ("hello" "world")
  ;; Split the fnamel and join them with <space>, now ftitle will be "hello world"
  (setq ftitle (concat "---\ntitle: "
		       (car fnamel) " "
		       (car (cdr fnamel)) " "
		       (car (cdr (cdr fnamel))) " "
		       (car (cdr (cdr (cdr fnamel)))) " "
		       (car (cdr (cdr (cdr (cdr fnamel))))) "\n"))
  (write-region ftitle nil fnamed) ;; Add ftitle to a file named "hello-world.md"
  ;; Append date like "data: 2019/02/10 10:47:56" to "hello-world.md"
  (beginning-of-buffer)
  (forward-char 8)
  (let (p1 p2)
    (setq p1 (point))
    (end-of-line)
    (setq p2 (point))
    (append-to-file "date: " nil fnamed)
    (append-to-file p1 p2 fnamed)
    (append-to-file "\nupdated: " nil fnamed)
    (append-to-file (format-time-string "%Y/%m/%d %T") nil fnamed)
    (append-to-file "\n---\n\n" nil fnamed))
  (org-gfm-export-as-markdown)			 ;; Invoke gfm plugin to open a relative .md buffer
  (replace-string ".." "")			 ;; ! Just for hexo-blog's special img-show format...
  (append-to-file nil t fnamed)	 ;; Append the contents of "hello-world.md" to "hello-world.md" again
  (kill-this-buffer)
  (switch-window-then-maximize)) ;; Kill the "hello-world.md" generate by GFM plugin to keep you stay in current .org file

:butterfly: 举个例子:

用 Emacs 打开 get-things-done.org 文件下,该文件位于 Hexo 文件夹 xxx/source/_orgs 下。

如我的电脑中该文件位于 C:\Users\Jack Liu\Blog\source\_orgs\get-things-done.org

先后执行: jk/insert-datejk/md-export 两个函数,便可在相应的 _posts 文件夹下,生成 get-things-done.md 文件。

如我的电脑中该生成文件位于 C:\Users\Jack Liu\Blog\source\_posts\get-things-done.md

文件头部格式如下:

---
title: get things done  
date: 2018/06/09 12:46:29
updated: 2019/02/10 12:46:52
---
...
...

效果如下:

:rofl: 图片也可以完美展示哦。

有兴趣的话,可以试着在 jk/md-export 中找出实现图片显示的代码哦,权当娱乐。


:surfing_man: 源码及 Demo :


后续不定期更新,如果你遇到什么问题,可以发邮件给我 loveminimal@163.com