Prechádzať zdrojové kódy

【文档】更新脚本设计与工作流程文档

- 新增脚本与主程序设计分析文档(scripts.md)
- 新增自动化发布工作流文档(workflow.md)
- 更新 README.md 和 README_EN.md 中的说明和链接
- 在 release.yml 中添加 Pages 部署相关步骤
PJ568 1 rok pred
rodič
commit
5dbcbc9aed
6 zmenil súbory, kde vykonal 470 pridanie a 48 odobranie
  1. 21 0
      .github/workflows/release.yml
  2. 5 5
      README.md
  3. 45 42
      README_EN.md
  4. 223 0
      doc/scripts.md
  5. 175 0
      doc/workflow.md
  6. 1 1
      index.html

+ 21 - 0
.github/workflows/release.yml

@@ -76,6 +76,15 @@ jobs:
         cp -v index.min.html ../release/
         cp -v index.allinone.html ../release/
         cp -v index.allinone.min.html ../release/
+        # Misc
+        cp -v LICENSE ../release/
+        cp -v README.md ../release/
+        cp -v README_EN.md ../release/
+        cp -v CONTRIBUTING.md ../release/
+        cp -v CNAME ../release/
+        cp -rv doc ../release/
+        cp -rv scripts ../release/
+        cp -rv src ../release/
       shell: bash
       working-directory: ${{ github.action_path }}
 
@@ -87,6 +96,18 @@ jobs:
         tag: "v${{ steps.version-check.outputs.version }}"
         artifacts: "../release/index.html,../release/index.min.html,../release/index.allinone.html,../release/index.allinone.min.html"
 
+    - name: Setup Pages
+      uses: actions/configure-pages@v4
+
+    - name: Upload artifact
+      uses: actions/upload-pages-artifact@v3
+      with:
+        path: '../release'
+
+    - name: Deploy to GitHub Pages
+      id: deployment
+      uses: actions/deploy-pages@v4
+
     - name: Delete tag
       if: (cancelled() || failure()) && steps.push-tag.outputs.tag_name != ''
       run: |

+ 5 - 5
README.md

@@ -18,10 +18,10 @@
 
 | 文件名 | 说明 |
 | :----- | :--- |
-| index.html | 默认 HTML 入口文件,使用 CDN 引入依赖。 |
-| index.min.html | 压缩后的 HTML 入口文件,使用 CDN 引入依赖。 |
-| index.allinone.html | 全合一 HTML 入口文件,默认包含所有可被脚本下载的依赖。 |
-| index.allinone.min.html | 压缩后的全合一 HTML 入口文件,默认包含所有可被脚本下载的依赖。 |
+| [index.html](index.html) | 默认 HTML 入口文件,使用 CDN 引入依赖。 |
+| [index.min.html](index.min.html) | 压缩后的 HTML 入口文件,使用 CDN 引入依赖。 |
+| [index.allinone.html](index.allinone.html) | 全合一 HTML 入口文件,默认包含所有可被脚本下载的依赖。 |
+| [index.allinone.min.html](index.allinone.min.html) | 压缩后的全合一 HTML 入口文件,默认包含所有可被脚本下载的依赖。 |
 
 ### 手动部署
 
@@ -49,7 +49,7 @@
 ## 代码或内容贡献
 
 欢迎每一位对本项目感兴趣的朋友贡献代码和内容。
-请参阅[程序逻辑](doc/logic.md)。
+请参阅[程序逻辑](doc/logic.md)、[脚本与主程序设计分析](doc/scripts.md)和[工作流程](doc/workflow.md),并确保你的贡献符合项目规范。
 
 较为简单的提交贡献方法可查阅[贡献文档](CONTRIBUTING.md)以了解相关信息。
 

+ 45 - 42
README_EN.md

@@ -6,31 +6,31 @@ This project provides a low-dependency, one-click deployment solution for render
 
 ## Features
 
-- **Minimal dependencies**: Only requires a single HTML file to render Markdown web pages.
-- **Code highlighting**: Supports syntax highlighting for code blocks in Markdown.
-- **Markdown support**: Renders Markdown files to HTML.
-- **Mermaid support**: Renders Mermaid diagrams in Markdown.
-- **Multi-language support**: Provides machine translation between languages.
-- **Dark mode**: Supports light, dark, and system theme modes.
-- **Pjax support**: Enables page navigation without full refresh.
+- **Low Dependencies**: Only one HTML file is required to render Markdown to web pages.
+- **Syntax Highlighting**: Render code blocks in Markdown.
+- **Markdown Support**: Convert Markdown files to HTML.
+- **Mermaid Support**: Render Mermaid diagrams in Markdown.
+- **Multi-language Support**: Machine translation between multiple languages.
+- **Dark Mode**: Support light, dark, and system default themes.
+- **Pjax Support**: Page navigation without full reload.
 
 ## Usage
 
-| Filename | Description |
-| :----- | :--- |
-| index.html | Default HTML entry file using CDN dependencies. |
-| index.min.html | Minified HTML entry file using CDN dependencies. |
-| index.allinone.html | All-in-one HTML entry file with embedded dependencies which can be downloaded by script. |
-| index.allinone.min.html | Minified all-in-one HTML entry file with embedded dependencies which can be downloaded by script. |
+| File Name | Description |
+| :-------- | :---------- |
+| [index.html](index.html) | Default HTML entry file using CDN dependencies |
+| [index.min.html](index.min.html) | Minified HTML entry file using CDN dependencies |
+| [index.allinone.html](index.allinone.html) | All-in-one HTML entry file with embedded dependencies |
+| [index.allinone.min.html](index.allinone.min.html) | Minified all-in-one HTML entry file with embedded dependencies |
 
 ### Manual Deployment
 
-1. Download the required HTML file from [Releases](https://github.com/PJ-568/MARKDOWN.HTML/releases) and place it in your website root directory;
-2. The HTML file will automatically render `index.md` in the same directory, or `README.md` if `index.md` doesn't exist.
+1. Download the required HTML file from the [Releases page](https://github.com/PJ-568/MARKDOWN.HTML/releases) and place it in your website directory;
+2. When accessed, the HTML file will automatically render `index.md` in the same directory. If `index.md` doesn't exist, it will render `README.md`.
 
-### Github Actions
+### GitHub Actions
 
-Compiles and deploys HTML files to specified directory. Replace `<version>` with actual version number.
+Compile and deploy HTML files to the target directory. Replace `<version>` with the actual version number.
 
 ```yaml
 - name: Deploy markdown.html
@@ -42,45 +42,48 @@ Compiles and deploys HTML files to specified directory. Replace `<version>` with
     use-all-in-one: 'false'
 ```
 
-## Notes
+## Additional Notes
 
-`This open-source project allows modifications but please retain original author information. If removal is necessary, please contact the author to avoid losing technical support.` See [License](#license) for details.
+`The open-source projects used in this work may be modified, but please retain the original author information. Contact the author if removal is necessary to avoid losing technical support.` For details, see [License](#license).
 
-## Contributions
+## Contributing
 
-We welcome contributions to code and content. Please refer to:
+We welcome contributions to this project. Please review:
 
-- [Logic Documentation](doc/logic.md)
-- [Contribution Guidelines](CONTRIBUTING.md)
+- [Program Logic](doc/logic.md)
+- [Script Design](doc/scripts.md)
+- [Workflow](doc/workflow.md)
+
+Ensure your contributions follow project specifications. For contribution guidelines, see [CONTRIBUTING.md](CONTRIBUTING.md).
 
 ## License
 
-This project is licensed under [CC BY-SA 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). For complete license information, see [`LICENSE`](LICENSE).
+This project is licensed under [CC BY-SA 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). The full license text is available in [`LICENSE`](LICENSE).
 
-### Acknowledgments
+### Acknowledgements
 
 #### Projects
 
-|Name|License|Links|
-|:-:|:--|:--|
-|tabler-icons|[MIT License](//mit-license.org)|[Github](https://github.com/tabler/tabler-icons), [Website](https://tabler.io/icons)|
-|highlightjs/highlight.js|[BSD-3-Clause license](https://github.com/highlightjs/highlight.js/raw/refs/heads/main/LICENSE)|[Github](https://github.com/highlightjs/highlight.js), [Website](https://highlightjs.org)|
-|markedjs/marked|[marked License](https://github.com/markedjs/marked/blob/master/LICENSE.md)|[Github](https://github.com/markedjs/marked), [Website](https://marked.js.org)|
-|mermaidjs/mermaid|[MIT License](https://github.com/mermaid-js/mermaid/blob/develop/LICENSE)|[Github](https://github.com/mermaid-js/mermaid), [Website](https://mermaid.js.org)|
-|translate.js|[Apache License Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)|[Github](https://github.com/xnx3/translate), [Gitee](https://gitee.com/mail_osc/translate), [Website](https://translate.zvo.cn)|
-|darkmode.js|[MIT license](//mit-license.org)|[Github](https://github.com/sandoche/Darkmode.js), [Website](https://darkmodejs.learn.uno)|
-|MoOx/pjax|[MIT license](//mit-license.org)|[Github](https://github.com/MoOx/pjax)|
-|instant.page|[MIT license](//mit-license.org)|[Github](https://github.com/instantpage/instant.page), [Website](https://instant.page)|
-|Maple Mono|[OFL-1.1 license](https://openfontlicense.org/open-font-license-official-text/)|[Github](https://github.com/subframe7536/maple-font), [Website](https://font.subf.dev)|
+| Name | License | Links |
+| :--- | :------ | :---- |
+| tabler-icons | [MIT](//mit-license.org) | [GitHub](https://github.com/tabler/tabler-icons), [Website](https://tabler.io/icons) |
+| highlight.js | [BSD-3-Clause](https://github.com/highlightjs/highlight.js/raw/main/LICENSE) | [GitHub](https://github.com/highlightjs/highlight.js), [Website](https://highlightjs.org) |
+| marked | [marked License](https://github.com/markedjs/marked/blob/master/LICENSE.md) | [GitHub](https://github.com/markedjs/marked), [Website](https://marked.js.org) |
+| mermaid | [MIT](https://github.com/mermaid-js/mermaid/blob/develop/LICENSE) | [GitHub](https://github.com/mermaid-js/mermaid), [Website](https://mermaid.js.org) |
+| translate.js | [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) | [GitHub](https://github.com/xnx3/translate), [Gitee](https://gitee.com/mail_osc/translate), [Website](https://translate.zvo.cn) |
+| darkmode.js | [MIT](//mit-license.org) | [GitHub](https://github.com/sandoche/Darkmode.js), [Website](https://darkmodejs.learn.uno) |
+| pjax | [MIT](//mit-license.org) | [GitHub](https://github.com/MoOx/pjax) |
+| instant.page | [MIT](//mit-license.org) | [GitHub](https://github.com/instantpage/instant.page), [Website](https://instant.page) |
+| Maple Mono | [OFL-1.1](https://openfontlicense.org) | [GitHub](https://github.com/subframe7536/maple-font), [Website](https://font.subf.dev) |
 
 #### Services
 
-|Icon|Name|
-|:-:|:--|
-|-|[360 Frontend CDN](https://cdn.baomitu.com/)|
-|![ByteDance Static CDN](https://cdn.bytedance.com/src/res/logo.svg)|[ByteDance Static CDN](https://cdn.bytedance.com/)|
-|![Chinese Web Font Project](https://chinese-font.netlify.app/favicon.ico)|[Chinese Web Font Project](https://chinese-font.netlify.app)|
+| Icon | Service |
+| :--- | :------ |
+| - | [360 Frontend CDN](https://cdn.baomitu.com/) |
+| ![ByteDance CDN](https://cdn.bytedance.com/src/res/logo.svg) | [ByteDance Static CDN](https://cdn.bytedance.com/) |
+| ![Chinese Fonts](https://chinese-font.netlify.app/favicon.ico) | [Chinese Web Fonts](https://chinese-font.netlify.app) |
 
-## Stars
+## Stargazers
 
 [![Stargazers over time](https://starchart.cc/PJ-568/MARKDOWN.HTML.svg?variant=adaptive)](https://starchart.cc/PJ-568/MARKDOWN.HTML)

+ 223 - 0
doc/scripts.md

@@ -0,0 +1,223 @@
+# 脚本与主程序功能设计文档
+
+## 1. 引言
+
+本文档详细说明项目中三个Bash脚本(`get-version.bash`, `is-newer-version.bash`, `release.bash`)和主程序(`src/main.rs`)的设计与功能。这些组件协同工作,实现版本管理、发布流程和HTML处理功能。
+
+```mermaid
+graph TD
+    A[get-version.bash] --> B[is-newer-version.bash]
+    B --> C[release.bash]
+    D[main.rs] --> E[minify_html]
+    D --> F[all_in_one]
+```
+
+## 2. 脚本功能说明
+
+### 2.1 get-version.bash
+
+从HTML文件中提取版本号的核心脚本。
+
+#### 功能设计
+
+1. **路径解析**:
+   - 自动检测脚本所在目录(`$SCRIPT_DIR`)
+   - 计算仓库根目录(`$REPO_DIR`)
+   - 支持自定义文件路径参数(`$1`)
+
+2. **多语言支持**:
+   - 通过`LANG`环境变量自动检测中英文
+   - `recho()`函数实现双语错误消息
+
+3. **版本提取逻辑**:
+
+```bash
+# 从index.html的<markdown-html>标签提取version属性
+VERSION=$(grep -oP '<markdown-html\s+version="\K[^"]+' "$FILE_PATH" | head -1)
+```
+
+4. **错误处理**:
+   - 文件不存在检测(`[ ! -f "$FILE_PATH" ]`)
+   - 版本号缺失检测(`[ -z "$VERSION" ]`)
+
+**使用示例**:
+
+```bash
+./scripts/get-version.bash # 默认检测index.html
+./scripts/get-version.bash path/to/custom.html # 自定义文件
+```
+
+### 2.2 is-newer-version.bash
+
+比较当前版本与Git标签,判断是否需要发布新版本。
+
+#### 核心逻辑
+
+1. **版本获取**:
+   - 调用`get-version.bash`获取当前版本
+   - 获取最新Git标签(`git tag -l 'v*'`)
+
+2. **版本比较算法**:
+
+```bash
+[ "$(printf "%s\n%s" "$CURRENT_VERSION" "$LATEST_VERSION" | sort -V | head -n1)" != "$CURRENT_VERSION" ]
+```
+
+- 使用`sort -V`进行版本号排序比较
+- 当前版本较新时输出版本号,否则输出"0"
+
+3. **边界处理**:
+   - 无标签时默认需要发布
+   - 自动去除标签的"v"前缀
+
+**输出说明**:
+
+- 输出版本号:需要发布新版本
+- 输出"0":已是最新版本
+
+### 2.3 release.bash
+
+交互式版本发布工具。
+
+#### 工作流程
+
+```mermaid
+sequenceDiagram
+    用户->>release.bash: 执行脚本
+    alt 带版本参数
+        release.bash->>release.bash: 验证版本格式
+    else 无参数
+        release.bash->>用户: 提示输入版本
+        loop 格式验证
+            用户->>release.bash: 输入版本
+            release.bash->>release.bash: 校验X.X.X格式
+        end
+    end
+    release.bash->>Git: 创建标签(v$version)
+    release.bash->>Git: 推送标签
+```
+
+#### 关键特性
+
+1. **参数解析**:支持`-v/--version`参数
+2. **版本验证**:正则校验`^[0-9]+\.[0-9]+\.[0-9]+$`格式
+3. **错误处理**:
+   - 仓库目录切换失败
+   - 标签创建/推送失败
+4. **多语言界面**:所有交互提示支持中英文
+
+## 3. 主程序分析(src/main.rs)
+
+HTML处理核心程序,提供压缩和内联功能。
+
+### 3.1 模块结构
+
+```rust
+pub fn minify_html(input_path: &str) -> Result<PathBuf> { ... }
+pub fn all_in_one(input_path: &str) -> Result<PathBuf> { ... }
+fn fetch_external_resource(url: &str) -> Result<String> { ... }
+```
+
+### 3.2 minify_html功能
+
+HTML压缩实现:
+
+1. **输出路径生成**:`{filename}.min.html`
+2. **压缩配置**:
+
+   ```rust
+   let mut cfg = Cfg::new();
+   cfg.minify_css = true;
+   cfg.minify_js = true;
+   cfg.keep_closing_tags = true;
+   // 其他配置项...
+   ```
+
+3. **压缩执行**:`minify(&code, &cfg)`
+
+### 3.3 all_in_one功能
+
+资源内联实现:
+
+1. **资源获取**:
+   - 通过`fetch_external_resource`下载CSS/JS
+   - 自定义User-Agent规避拦截
+2. **DOM处理**:
+   - 使用`scraper`库解析HTML
+   - 选择器定位`script[src]`和`link[rel=stylesheet]`
+3. **内联替换**:
+
+   ```rust
+   // 示例:脚本内联
+   let replacement = format!("<script>{}</script>", content);
+   processed_html.replace(&original, &replacement);
+   ```
+
+### 3.4 错误处理系统
+
+1. **自定义错误类型**:`HtmlProcessorError`
+2. **错误传播**:`Result<T, Box<dyn Error>>`统一处理
+3. **错误日志**:
+
+   ```rust
+   eprintln!("Error minifying HTML: {}", e);
+   ```
+
+### 3.5 主流程
+
+```rust
+fn main() {
+    let input_path = ...; // 获取输入文件
+    minify_html(&input_path)?; // 生成压缩版
+    let all_in_one_path = all_in_one(&input_path)?; // 生成内联版
+    minify_html(all_in_one_path.to_str().unwrap())?; // 压缩内联版
+}
+```
+
+## 4. 工作流程
+
+组件间的协同关系:
+
+```mermaid
+flowchart TB
+    subgraph 版本发布
+        A[get-version] --> B[is-newer-version]
+        B --> C[release]
+    end
+    subgraph HTML处理
+        D[main.rs] --> E[压缩]
+        D --> F[内联]
+    end
+    C -->|新版本| G[Git标签]
+    F -->|生成| H[allinone.html]
+    E -->|生成| I[min.html]
+```
+
+## 5. 使用示例
+
+### 5.1 完整发布流程
+
+```bash
+# 检查版本更新
+NEW_VER=$(./scripts/is-newer-version.bash)
+
+# 发布新版本
+if [ "$NEW_VER" != "0" ]; then
+  ./scripts/release.bash -v $NEW_VER
+fi
+```
+
+### 5.2 HTML处理
+
+```bash
+# 处理index.html
+cargo run -- index.html
+
+# 生成文件:
+# - index.min.html
+# - index.allinone.html
+# - index.allinone.min.html
+```
+
+---
+> 本文档最后更新于:2025年6月26日  

+ 175 - 0
doc/workflow.md

@@ -0,0 +1,175 @@
+# 自动化发布工作流文档
+
+## 1. 概述
+
+本工作流实现项目的自动化发布流程,包含版本检测、构建处理、发布创建和部署全流程。通过GitHub Actions触发,确保发布过程的一致性和可靠性。
+
+```mermaid
+graph LR
+    A[代码推送] --> B[版本检测]
+    B --> C{新版本?}
+    C -->|是| D[创建标签]
+    C -->|否| E[流程终止]
+    D --> F[构建处理]
+    F --> G[生成HTML]
+    G --> H[打包发布物]
+    H --> I[创建Release]
+    H --> J[部署到Pages]
+```
+
+## 2. 触发条件
+
+工作流在以下事件触发:
+
+- 推送到`master`分支
+- 手动触发(`workflow_dispatch`)
+
+## 3. 核心阶段详解
+
+### 3.1 版本检测阶段
+
+```yaml
+- name: Check version
+  id: version-check
+  run: |
+    chmod +x ./scripts/is-newer-version.bash
+    chmod +x ./scripts/get-version.bash
+    VERSION=$(./scripts/is-newer-version.bash)
+    echo "version=${VERSION}" >> $GITHUB_OUTPUT
+```
+
+**功能说明**:
+
+1. 赋予脚本执行权限
+2. 调用`is-newer-version.bash`检测是否需要发布
+3. 输出检测结果:
+   - 新版本:输出版本号
+   - 无更新:输出"0"
+
+### 3.2 标签创建阶段
+
+```yaml
+- name: Push tag
+  if: steps.version-check.outputs.version != '0'
+  run: |
+    tag="v${{ steps.version-check.outputs.version }}"
+    git config user.name "${GITHUB_ACTOR}"
+    git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
+    git tag -a "${tag}" -m "Release ${tag}"
+    git push origin "${tag}"
+```
+
+**关键操作**:
+
+- 自动生成vX.X.X格式标签
+- 配置Git用户信息
+- 创建并推送带注释的标签
+
+### 3.3 构建处理阶段
+
+```mermaid
+sequenceDiagram
+    构建系统->>Rust环境: 安装工具链
+    构建系统->>依赖缓存: 加载缓存
+    构建系统->>主程序: cargo build --release
+    构建系统->>HTML生成: cargo run --release -- index.html
+```
+
+**步骤说明**:
+
+1. **环境准备**:
+   - 设置Rust编译环境
+   - 缓存依赖加速构建
+2. **项目构建**:
+   - 发布模式编译(`cargo build --release`)
+3. **HTML生成**:
+   - 执行主程序处理index.html
+   - 产出四种HTML变体
+
+### 3.4 发布物打包
+
+```yaml
+- name: Copy files
+  run: |
+    mkdir -p ../release
+    cp -v index.html ../release/
+    cp -v index.min.html ../release/
+    cp -v index.allinone.html ../release/
+    cp -v index.allinone.min.html ../release/
+    # 附加文件
+    cp -v LICENSE ../release/
+    cp -v README*.md ../release/
+    cp -v CONTRIBUTING.md ../release/
+    cp -v CNAME ../release/
+    cp -rv doc ../release/
+    cp -rv scripts ../release/
+    cp -rv src ../release/
+```
+
+**发布物结构**:
+
+```plaintext
+release/
+├── index.html
+├── index.min.html
+├── index.allinone.html
+├── index.allinone.min.html
+├── LICENSE
+├── README.md
+├── README_EN.md
+├── CONTRIBUTING.md
+├── CNAME
+├── doc/
+├── scripts/
+└── src/
+```
+
+### 3.5 发布与部署
+
+```yaml
+- name: Create release
+  uses: ncipollo/release-action@v1
+  with:
+    generateReleaseNotes: true
+    tag: "v${{ steps.version-check.outputs.version }}"
+    artifacts: "../release/index.html,..."
+
+- name: Deploy to GitHub Pages
+  uses: actions/deploy-pages@v4
+```
+
+**双通道输出**:
+
+1. **GitHub Release**:
+   - 自动生成发布说明
+   - 包含所有HTML变体文件
+2. **GitHub Pages**:
+   - 自动部署整个release目录
+   - 即时生效的在线访问
+
+## 4. 错误处理机制
+
+```yaml
+- name: Delete tag
+  if: (cancelled() || failure()) && steps.push-tag.outputs.tag_name != ''
+  run: |
+    git push --delete origin "${{ steps.push-tag.outputs.tag_name }}"
+    git tag -d "${{ steps.push-tag.outputs.tag_name }}"
+```
+
+**回滚策略**:
+
+1. 当流程取消或失败时触发
+2. 自动删除已创建的Git标签
+3. 确保失败发布不会留下无效标签
+
+## 5. 环境配置
+
+| 配置项 | 说明 |
+|-------|------|
+| `runs-on: ubuntu-latest` | 使用最新Ubuntu运行环境 |
+| `permissions: write-all` | 授予标签推送和发布权限 |
+| `working-directory` | 统一指定操作目录 |
+
+---
+> 本文档最后更新于:2025年6月26日  

+ 1 - 1
index.html

@@ -1,7 +1,7 @@
 <!doctype html>
 
 <html>
-<markdown-html version="1.18.1" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
+<markdown-html version="1.18.2" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
   license="CC BY-SA 4.0 International"></markdown-html>
 
 <head>