【运维】部署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 绑定域名。
相关推荐
分布式存储与RustFS6 小时前
群晖 NAS 被 C3RB3R 锁了:本地备份一起被加密,RustFS 对象锁为什么删不掉
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
真好啊又活了一天6 小时前
服务器和虚机配置IPV6
运维·服务器·github
zixu_star7 小时前
定时任务是什么?从日常自动化到 AI 应用,软件如何自动执行任务?
运维·人工智能·自动化
AR-26710-7 小时前
Linux Day33——定时任务+lvm
linux·运维
润滑卫士8 小时前
液压系统工作粘度窗口等级选型:泵允许粘度范围与油温-粘度匹配计算
运维
每月一号准时摆烂10 小时前
服务器运维笔记(一)——使用shell编写实时查看BMC功率的脚本
运维·服务器·笔记
时间售卖铺11 小时前
Chrony与SSH实战:精准时间同步与安全远程登录
运维·安全·云原生·ssh
微订校园外卖系统12 小时前
校园代取快递系统选型:现成系统与定制开发的技术核对项
大数据·运维·人工智能·外卖系统·本地生活·微订
QXWZ_IA12 小时前
管道GIS测绘如何实现自动化成图:三种成图方式怎么选
运维·人工智能·安全·自动化
一条破秋裤12 小时前
【1112】Linux中断【整理】
linux·运维·服务器