【运维】部署MKDocs

部署MKDocs

obsidian 记录笔记,通过 mkdocs 私有化部署。

1 使用MKDocs创建笔记

创建仓库,安装 Material for MkDocs 和 mkdocs-minify-plugin

bash 复制代码
mkdir tmp
cd tmp
git init

pip install mkdocs-material
pip install mkdocs-minify-plugin

mkdocs new .

2 搭建写作环境

  1. 打开 Obsidian
  2. 打开本地仓库,选择 tmp/docs
  3. 安装常用插件,搭建写作环境

插件根据个人喜好,我用来做工作笔记,核心插件只保留了 大纲 、命令面板 、模板。

白板、关系图谱、双链等等我都用不到,组织文件和搜索通过插件实现。

必须安装 MAKE.md 或者 obsidian-bartender 或者其他可以自定义顺序的插件。

  • Git 备份数据
  • Number Headings 文档标题编号
  • Commander 隐藏各种不需要的图标和功能
  • Editing ToolBar 增加一些快捷键
  • MAKE.md 组织文件、搜索

3 辅助脚本

3.1 配置MKDocs

就是参考 mkdocs-material的官网配置ymal。配置比较长就不贴了放在 gist。

3.2 自定义文章顺序

mkdocs 直接部署文章顺序无法自定义,通过解析 MAKE.md 或者 obsidian-bartender 的数据实现自定义顺序。下边这段是解析 mkdocs 的,如果使用 obsidian-bartender 可以看 gist。

python 复制代码
import os
import yaml
import json
import sqlite3

db_name = '.space/context.mdb'
base_directory = './docs/'
ignore_directory = ["Todo"]
order_yaml_path = 'script/refactoring_directory.yml'
mkodcs_yml = 'mkdocs.yml'

def get_context_bt_db(path):
    conn = sqlite3.connect(path + db_name)
    cursor = conn.cursor()
    cursor.execute("SELECT File FROM files")
    file_list = [row[0] for row in cursor.fetchall()]
    conn.close()
    return file_list

def get_page_tree(relative_path=""):
    page_tree = []
    order_tree = []
    order_tree = get_context_bt_db(base_directory + relative_path)
    for key in order_tree:
        if key in ignore_directory:
            break
        if key.endswith('.md'):
            page_tree.append(key) 
        else:
            subtree = get_page_tree(key+"/")
            if subtree:
                page_tree.append({key.split('/')[-1]: subtree})
    return page_tree

page_tree = {"nav": get_page_tree()}
print(page_tree)

with open(order_yaml_path, 'w', encoding='utf-8-sig') as yaml_file:
    yaml.dump(page_tree, yaml_file, default_flow_style=False, allow_unicode=True)

output_lines = []
with open(mkodcs_yml, 'r', encoding='utf-8-sig') as file:
    delete_mode = False
    for line in file:
        if 'nav:' in line:
            delete_mode = True
        if not delete_mode:
            output_lines.append(line)
        if delete_mode and not line.strip():
            delete_mode = False

with open(order_yaml_path, 'r', encoding='utf-8-sig') as file:
    delete_mode = False
    for line in file:
        output_lines.append(line)

with open(mkodcs_yml, 'w', encoding='utf-8-sig') as file:
    file.writelines(output_lines)

3.3 github 自动部署

  1. 创建 github 仓库
  2. 将本地仓库链接到 github
  3. 服务器 pull 仓库,并放置部署脚本 /home/build.sh
bash 复制代码
#!/bin/sh
cd /home/MyBlog
git pull
mkdocs build
  1. 仓库配置 github-actions
yml 复制代码
name: Deploy

on:
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:   
    - name: executing remote ssh commands to develop
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.DEPLOY_HOST }}
        username: ${{ secrets.DEPLOY_USER }}
        password: ${{ secrets.DEPLOY_PASSWORD }}
        port: 22
        script:  sh /home/build.sh

我是手动执行 actions,使用的 workflow_dispatch,自动部署的话绑定到对应分支:

yaml 复制代码
on:
  push:
    branches:
      - dev
  1. mkdocs 编译后是静态网站(同级目录的 site),用宝塔或者 nginx 绑定域名。
相关推荐
qeen8712 分钟前
【Linux】线程的基本认识及与进程的关系、对比
linux·运维
Smoothcloud润云1 小时前
GPU服务器租用训练缓存小文件与inode耗尽排查实战
运维·服务器·人工智能·缓存·ai·gpu算力
Lsetea1 小时前
DNS-01 委托有 CNAME 却查不到 TXT?用 CertbotX 核对签发前和挑战中的记录
运维·https·ssl·dns·acme
Decay__3 小时前
集群安装部署实战 —— kubeadm 搭建 3 控制面 + 3 工作节点高可用集群
运维·云原生·容器·kubernetes·k8s
llilian_164 小时前
单北斗信创 NTP 服务器自主可控溯源要求
运维·服务器·功能测试·嵌入式硬件·51单片机·软件工程
华允物联-HUAIOT4 小时前
断线不可怕,可怕的是“假在线”:串口服务器的四道自愈防线
运维·服务器
大今野4 小时前
技能六 · Linux(CentOS 7)操作系统章节习题
linux·运维·centos·专转本
ynchyong4 小时前
PERC H330 Mini 挂 SATA 盘----DEL服务器
运维·服务器·del
tqs_123454 小时前
Agent治理与自动化评测体系
运维·人工智能·自动化·ai agent
yangmu32034 小时前
AI 辅助 EtherCAT 调试日志分析:从底层逻辑到工程实战
运维