目录

我的 org-roam 文章导出到 hugo 博客

目录

我定义了一个标记 #+EXPORT_TO_HUGO: publicBlog , 所有需要导出的文件,都会在前 20 行中声明此标记。

1
(setq export-to-hugo-flag "#+EXPORT_TO_HUGO: publicBlog") ;; 需要从org-roam 导出到 hugo 的标识

同时自己写了一些命令,用来执行导出操作:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
;; org-roam-export-to-hugo-test-file 是测试文件的路径
(setq org-roam-export-to-hugo-test-file "xxx/xxx_test.org")
;; roam export to hugo
(defun tao/roam-export-to-hugo-all()
  "my function: exports all org-roam files to hugo. 导出所有的 org-roam 文件到 hugo(只会导出特定标记的原始文件)"
  (interactive)
  (message "[org-roam-to-hugo] all export to hugo begin")
  (dolist (f (org-roam-list-files))
    (tao/roam-export-to-hugo-by-file f)
    )
  (message "[org-roam-to-hugo] all export to hugo end")
  )

(defun tao/roam-export-to-hugo-by-file-debug()
  "my function: debug org-roam export to hugo by a test file;通过测试文件调试导出命令"
  (interactive)
  (tao/roam-export-to-hugo-by-file org-roam-export-to-hugo-test-file)
  )


(defun tao/roam-export-to-hugo-by-current-buffer()
  "my function: export current buffer. 导出当前 buffer 的 org-roam 到 hugo"
  (interactive)
  (tao/roam-export-to-hugo-by-file (buffer-file-name))
  )

(defun tao/roam-export-to-hugo-by-file(filepath)
  "my function: exports one org-roam file to hugo. 导出指定文件 org-roam 到 hugo"
  (message "[org-roam-to-hugo] begin to handle. current file is: %s" filepath)

  (setq previous-file-path (buffer-file-name))

  ;;(with-temp-buffer (insert-file-contents filepath)
  (with-current-buffer (find-file-noselect filepath)
    (setq buffer-line-number 0) ;; 已处理的行 数目
    (setq line-number-search-max 20) ;; 最多寻找20行
    (setq file-name (file-name-nondirectory filepath)) ;; 待处理的文件名称

    ;; 逐行读取,代码来源: https://emacs.stackexchange.com/questions/19518/is-there-an-idiomatic-way-of-reading-each-line-in-a-buffer-to-process-it-line-by
    (goto-char (point-min))

    (while (not (eobp))

      (setq buffer-line-number (+ buffer-line-number 1))

      (if (< buffer-line-number line-number-search-max)
          (let* ((line (buffer-substring (point) (progn (forward-line 1) (point)))))
            (when (string-equal (s-trim-right line) export-to-hugo-flag)
              (message "[org-roam-to-hugo] exporting: %s. line number: %s" file-name buffer-line-number)
              (org-hugo-export-wim-to-md)
              )
            )

        (progn
          (message "[org-roam-to-hugo] line-number > %s,quit" line-number-search-max)
          (goto-char (point-max))
          )
        )
      )
    (message "[org-roam-to-hugo] previous-file-path:%s" previous-file-path)
    (message "[org-roam-to-hugo] file-path:%s" filepath)
    (unless (string-equal (s-trim previous-file-path) (s-trim filepath))
      (kill-buffer)
      )
    )
  (message "[org-roam-to-hugo] end")
  )

测试文件 xxx_test.org 内容如下(为了避免和本文的标记重复,下文中行首都加了一个 !):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
!:PROPERTIES:
!:ID:       14910C1E-2E14-4942-B609-07ED267AF465
!:END:
!#+EXPORT_TO_HUGO: publicBlog
!#+OPTIONS: author:nil ^:{}
!#+HUGO_FRONT_MATTER_FORMAT: YAML
!#+HUGO_CUSTOM_FRONT_MATTER: :toc true
!#+HUGO_DRAFT: false
!
!#+TITLE: org-roam 导出到 hugo 测试
!#+HUGO_TITLE: org-roma-export-to-hugo-test
!#+EXPORT_FILE_NAME: org-roam-export-to-hugo-test
!#+HUGO_CATEGORIES:
!#+HUGO_TAGS:
!#+DATE: 2023-01-01
!
!** 以下为测试内容
!this is test
!
!~code test~
!#+begin_src emacs-lisp
!  (setq a 1)
!#+end_src
!
!#+begin_src go
!  fmt.println(1)
!#+end_src

测试内容结束