Build Website with Org Mode

配置

build-site.el

;; Set the package installation directory so that packages aren't stored in the
;; ~/.emacs.d/elpa path.
(require 'package)
(setq package-user-dir (expand-file-name "./.packages"))
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
                         ("elpa" . "https://elpa.gnu.org/packages/")))

;; Initialize the package system
(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))

;; Install dependencies
(package-install 'htmlize)

;; Load the publishing system
(require 'ox-publish)

;; Keep publishing state inside the workspace so local and CI builds are repeatable.
(setq org-publish-timestamp-directory
      (file-name-as-directory (expand-file-name "./.org-timestamps")))

;; Customize the HTML output
(setq org-html-validation-link nil            ;; Don't show validation link
      org-html-head-include-scripts nil       ;; Use our own scripts
      org-html-head-include-default-style nil ;; Use our own styles
      org-html-head (concat
                     "<link rel=\"stylesheet\" href=\"https://cdn.simplecss.org/simple.min.css\" />"
                     "<link rel=\"stylesheet\" href=\"/assets/css/site.css\" />"))

;; Shared header for every exported page; page titles remain in metadata.
(setq org-html-preamble
      (concat
       "<header class=\"site-header\">"
       "<a class=\"site-brand\" href=\"/\">Cupppa Lab</a>"
       "<nav class=\"site-nav\" aria-label=\"主导航\">"
       "<a href=\"/\">首页</a>"
       "<a href=\"/posts/index.html\">文章</a>"
       "<a href=\"/categories/index.html\">分类</a>"
       "<a href=\"/about.html\">关于</a>"
       "</nav></header>"))

;; Define the publishing projects
(setq org-publish-project-alist
      (list
       (list "org-site:main"
             :recursive t
             :base-directory "./org"
             :publishing-directory "./public_html"
             :publishing-function 'org-html-publish-to-html
             :with-title nil            ;; Body headings identify the page
             :with-author nil           ;; Don't include author name
             :with-creator t            ;; Include Emacs and Org versions in footer
             :with-toc nil              ;; Include a table of contents
             :section-numbers nil       ;; Don't include section numbers
             :time-stamp-file nil)      ;; Don't include time stamp in file
       (list "org-site:assets"
             :recursive t
             :base-directory "./org/assets"
             :publishing-directory "./public_html/assets"
             :base-extension "css\\|js\\|png\\|jpg\\|jpeg\\|gif\\|svg\\|webp\\|pdf"
             :publishing-function 'org-publish-attachment)
       (list "org-site"
             :components '("org-site:main" "org-site:assets"))))

;; Generate the site output
(org-publish-project "org-site" t)

(message "Build complete!")

build.sh

#!/bin/sh
emacs -Q --script build-site.el

.github/workflows/publish.yml

name: Publish to GitHub Pages

on:
  push:
    branches:
      - master

concurrency:
  group: publish-master
  cancel-in-progress: false

permissions:
  contents: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v7

      - name: Install Emacs
        run: sudo apt-get update && sudo apt-get install --no-install-recommends emacs-nox --yes

      - name: Cache Emacs packages
        uses: actions/cache@v6
        with:
          path: .packages
          key: emacs-packages-${{ runner.os }}-${{ hashFiles('build-site.el') }}
          restore-keys: |
            emacs-packages-${{ runner.os }}-

      - name: Build the site
        run: ./build.sh

      - name: Install CSS checker
        run: npm ci --ignore-scripts --no-audit --no-fund

      - name: Check generated site
        run: ./scripts/check-site.sh

      - name: Publish generated content to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@v4.9.0
        with:
          branch: gh-pages
          folder: public_html

发布前检查

首次使用或检查依赖更新后运行 =npm ci=。构建后检查页面生成、静态资源复制以及 CSS 语法;失败时停止发布。

npm ci --ignore-scripts --no-audit --no-fund
./build.sh
./scripts/check-site.sh

检查实现以仓库中的 check-site.sh 和 check-css.cjs 为准,不在正文重复维护完整脚本。

Date: 2024-03-09

Emacs 30.2 (Org mode 9.7.11)