【运维】部署MKDocs

部署MKDocs

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

1 使用MKDocs创建笔记

创建仓库,安装 Material for MkDocsmkdocs-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 绑定域名。
相关推荐
阿里云大数据AI技术9 分钟前
ES Serverless 8.17王牌发布:向量检索「火力全开」,智能扩缩「秒级响应」!
大数据·运维·serverless
wanhengidc12 分钟前
服务器中日志分析的作用都有哪些
运维·服务器
Mikhail_G42 分钟前
Python应用变量与数据类型
大数据·运维·开发语言·python·数据分析
曹瑞曹瑞43 分钟前
VMware导入vmdk文件
linux·运维·服务器
十年磨一剑~1 小时前
centos查看开启关闭防火墙状态
linux·运维·centos
无效的名字2 小时前
向日葵远程控制debian无法进入控制画面的解决方法
运维·debian
藥瓿亭2 小时前
K8S认证|CKS题库+答案| 7. Dockerfile 检测
运维·ubuntu·docker·云原生·容器·kubernetes·cks
搬码临时工2 小时前
如何把本地服务器变成公网服务器?内网ip网址转换到外网连接访问
运维·服务器·网络·tcp/ip·智能路由器·远程工作·访问公司内网
Guheyunyi3 小时前
监测预警系统重塑隧道安全新范式
大数据·运维·人工智能·科技·安全
知更鸟呆呆4 小时前
【Linux操作系统】基础开发工具(yum、vim、gcc/g++)
linux·运维·vim