通过emacs的org-mode写文章

在Org mode下写Blog,有两种方式

直接在Blog所在的目录下写独立的org

在目录‘~/blog/gitblog/orgcontent/’下添加个demon.org文件,文件头中有如 下信息

#+HUGO_BASE_DIR: ../
#+HUGO_SECTION: ./posts
#+TITLE:       Org-mode&Hugo&ox-hugo写Blog
#+DATE:        <2019-10-19 Sat>
#+HUGO_CATEGORIES: Blog
#+HUGO_TAGS: Blog
#+OPTIONS: H:9

在jonural中写

通过如下命令,进入jonuarl中,填加一个分支

C-c c j

选择h,会提示新建一个Blog目录,之后可以在这个目录下写一个主题的Blog。 添加一个h选项,需要如下配置


;;---------------begin hugo------------------------
(with-eval-after-load 'ox
  (require 'ox-hugo))
;; org-hugo capture
(with-eval-after-load 'org-capture
  (defun org-hugo-new-subtree-post-capture-template ()
    "Returns `org-capture' template string for new Hugo post.
See `org-capture-templates' for more information."
    (let* (;; http://www.holgerschurig.de/en/emacs-blog-from-org-to-hugo/
           (date (format-time-string (org-time-stamp-format :long :inactive) (org-current-time)))
           (title (read-from-minibuffer "Post Title: ")) ;Prompt to enter the post title
           (fname (org-hugo-slug title)))
      (mapconcat #'identity
                 `(
                   ,(concat "** TODO " title "     :@随笔:")
                   ":PROPERTIES:"
                   ,(concat ":EXPORT_FILE_NAME: " fname)
                   ,(concat ":EXPORT_DATE: " date) ;Enter current date and time
                   ":END:"
                   "%?\n")          ;Place the cursor here finally
                 "\n")))

  (add-to-list 'org-capture-templates
               '("h"                ;`org-capture' binding + h
                 "Hugo post"
                 entry
                 ;; It is assumed that below file is present in `org-directory'
                 ;; and that it has a "Blog Ideas" heading. It can even be a
                 ;; symlink pointing to the actual location of all-posts.org!
                 (file+headline "~/blog/gitblog//orgcontent/0000head.org" "INBOX")
                 (function org-hugo-new-subtree-post-capture-template))))

ox-hugo导出md

emacs的org mode 加上ox-hugo可以方便的导出md文件。

把org文件导出成md

使用 ox-hugo 把 org 文件导出为 md 文件

方式一:手动导出整个文件

C-c C-e H h

或者导出目录树中的一个分支

C-c C-e H H

方式二:保存 org 文件时自动导出

在站点根目录新建 dir-locals.el 文件

vi .dir-locals.el
(("content-org/"
  . ((org-mode . ((eval . (org-hugo-auto-export-mode)))))))

注意第二中方式写的内容,有个Todo标签,如果不改成Done,导出的时候,md文 件上会标记为draft文件,hugo编译的时候,认为文章还没完成,如果不加-D参数,不会把这个md文 件编译成web。

完成的文件如下,需要把状态改为Done

\** DONE test                                                      :@随笔:ssh:
:PROPERTIES:
:EXPORT_FILE_NAME: test
:EXPORT_DATE: [2021-09-24 Fri 17:26]
:END:

编译hugo

在hugo的根目录下,执行以下命令

hugo server -D

其中-D参数表示Draft文件也编译。具体的其他选项操作,可以参考help。

有的时候,以上命令并没有生成静态文件。原因还没搞清楚。 直接用hugo命令编译。可以正常生成。

hugo

把生成好的public目录通过git传到github

初始化git仓库

cd public
git init
git remote add origin git@github.com:vritser/vritser.github.io.git

提交,推送

cd public
git init
git remote add origin git@github.com:vritser/vritser.github.io.git