mkdocs-katex-ssr 0.1.0__tar.gz → 1.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,126 @@
1
+ Metadata-Version: 2.4
2
+ Name: mkdocs-katex-ssr
3
+ Version: 1.0.3
4
+ Summary: A MkDocs plugin for server-side rendering of KaTeX math.
5
+ Author-email: RainPPR <2125773894@qq.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://raineblog.dpdns.org/mkdocs-katex-ssr/
8
+ Project-URL: Documentation, https://raineblog.dpdns.org/mkdocs-katex-ssr/configuration/
9
+ Project-URL: Repository, https://github.com/raineblog/mkdocs-katex-ssr
10
+ Project-URL: Issues, https://github.com/raineblog/mkdocs-katex-ssr/issues
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: mkdocs>=1.1.0
14
+ Requires-Dist: beautifulsoup4>=4.9.0
15
+ Dynamic: license-file
16
+
17
+ # MkDocs KaTeX SSR Plugin
18
+
19
+ A MkDocs plugin that renders LaTeX math on the server side using [KaTeX](https://katex.org/), offering faster load times, layout stability, and optional offline support.
20
+
21
+ > [!NOTE]
22
+ > **Maintenance Philosophy**: This project is self-maintained. I welcome bug reports and corrections, but please note that I may not have the bandwidth to accept significant feature requests or major functional additions. Pull requests for bug fixes are appreciated.
23
+ >
24
+ > *This project's code and documentation were generated by Antigravity (Google DeepMind) and verified by the maintainer.*
25
+
26
+ ## Why Server-Side Rendering (SSR)?
27
+
28
+ Traditional client-side rendering relies on JavaScript in the browser to convert LaTeX strings (e.g., `$\sqrt{a^2 + b^2}$`) into HTML. This can lead to:
29
+
30
+ - **Layout Shift**: Content jumps as math loads.
31
+ - **Slower Performance**: The browser must download and parse the KaTeX library before rendering.
32
+ - **Dependency**: Requires client-side JavaScript execution.
33
+
34
+ **MkDocs KaTeX SSR** pre-renders all math during the `mkdocs build` process using a local Node.js process. The resulting HTML contains ready-to-view markup.
35
+
36
+ ## Features
37
+
38
+ - **High Performance**: Uses a persistent Node.js process to render equations efficiently without spawning a new process for every item.
39
+ - **Offline Support**: Optional "Offline Mode" copies all necessary CSS, fonts, and scripts to your site directory, removing external CDN dependencies.
40
+ - **Smart Asset Management**: Separate configuration for server-side processing scripts (like `mhchem`) and client-side interactive scripts (like `copy-tex`).
41
+ - **Clean Output**: Aggressive warning suppression for a quieter build log.
42
+
43
+ ## Installation
44
+
45
+ ### Prerequisites
46
+
47
+ - **Node.js**: Must be installed and available in your system PATH.
48
+ - **Python**: 3.8+
49
+
50
+ ### Install Plugin
51
+
52
+ ```bash
53
+ pip install mkdocs-katex-ssr
54
+ ```
55
+
56
+ ## Configuration
57
+
58
+ Add the plugin to your `mkdocs.yml`:
59
+
60
+ ```yaml
61
+ markdown_extensions:
62
+ - pymdownx.arithmatex:
63
+ generic: true
64
+
65
+ plugins:
66
+ - katex-ssr:
67
+ # --- Basic Configuration ---
68
+ katex_dist: "https://cdn.jsdelivr.net/npm/katex@latest/dist/"
69
+ add_katex_css: true
70
+ katex_css_filename: "katex-swap.min.css" # Use swap version for better font-display behavior
71
+
72
+ # --- Script Loading ---
73
+ # Scripts needed for rendering (Server-Side). e.g., chemical formulas.
74
+ # These are NOT sent to the browser.
75
+ ssr_contribs:
76
+ - mhchem
77
+
78
+ # Scripts needed for interaction (Client-Side). e.g., clipboard copy.
79
+ # These ARE injected into the HTML.
80
+ client_scripts:
81
+ - copy-tex
82
+
83
+ # --- KaTeX Options ---
84
+ katex_options:
85
+ macros:
86
+ "\\RR": "\\mathbb{R}"
87
+ ```
88
+
89
+ ### Offline Mode (Self-Contained)
90
+
91
+ If you need your documentation to work without an internet connection (or just want to host everything yourself), enable `embed_assets`.
92
+
93
+ ```yaml
94
+ plugins:
95
+ - katex-ssr:
96
+ embed_assets: true
97
+ # You can specify a local path if auto-detection fails
98
+ # katex_dist: "./node_modules/katex/dist/"
99
+ ```
100
+
101
+ **What this does:**
102
+
103
+ 1. **Copies Files**: It copies `katex.min.css` (or your chosen filename), the `fonts/` directory, and any specified `client_scripts` from your local `node_modules` to `site/assets/katex`.
104
+ 2. **Relative Linking**: It updates all HTML citations to point to these local files using correct relative paths (e.g., `../assets/katex/katex.min.css`).
105
+
106
+ ## Advanced Configuration Options
107
+
108
+ | Option | Type | Default | Description |
109
+ | :--- | :--- | :--- | :--- |
110
+ | `katex_dist` | str | jsDelivr | Base URL for CDN, or local file path to KaTeX distribution. |
111
+ | `add_katex_css` | bool | `true` | Whether to inject the CSS link tag. |
112
+ | `katex_css_filename` | str | `katex.min.css` | The specific CSS file to load. `katex-swap.min.css` is recommended. |
113
+ | `embed_assets` | bool | `false` | If true, copies assets to output dir and links locally. |
114
+ | `ssr_contribs` | list | `[]` | List of KaTeX `contrib` libraries to load in the Node.js renderer (e.g., `mhchem`). |
115
+ | `client_scripts` | list | `[]` | List of KaTeX `contrib` libraries to load in the browser (e.g., `copy-tex`). |
116
+ | `copy_assets_to` | str | `assets/katex` | Destination directory for copied assets (relative to site_dir). |
117
+ | `katex_options` | dict | `{}` | Standard options passed to `katex.renderToString` (macros, etc.). |
118
+
119
+ ## Troubleshooting
120
+
121
+ - **`katex` module not found**: Ensure `npm install katex` (or `pnpm`/`yarn`) has been run in your project root, or specific the path via `katex_dist`.
122
+ - **Node.js error**: The plugin requires `node` to be in your PATH. On Windows, ensure you can run `node --version` in your terminal.
123
+
124
+ ## License
125
+
126
+ This project is licensed under the MIT License.
@@ -167,7 +167,7 @@ class KatexSsrPlugin(BasePlugin):
167
167
  soup = BeautifulSoup(output, 'html.parser')
168
168
  math_elements = soup.find_all(class_='arithmatex')
169
169
  for el in math_elements:
170
- content = el.get_text().strip()
170
+ content = el.get_text(strip=True)
171
171
  display_mode = False
172
172
 
173
173
  if content.startswith('\\(') and content.endswith('\\)'):
@@ -184,9 +184,10 @@ class KatexSsrPlugin(BasePlugin):
184
184
  latex = content
185
185
 
186
186
  rendered_html = self._render_latex(latex, display_mode)
187
- if rendered_html:
188
- new_soup = BeautifulSoup(rendered_html, 'html.parser')
189
- el.replace_with(new_soup)
187
+ new_soup = BeautifulSoup(rendered_html, 'html.parser')
188
+
189
+ el.clear()
190
+ el.append(new_soup)
190
191
 
191
192
  # Assets Injection
192
193
  css_file = self.config['katex_css_filename']
@@ -0,0 +1,126 @@
1
+ Metadata-Version: 2.4
2
+ Name: mkdocs-katex-ssr
3
+ Version: 1.0.3
4
+ Summary: A MkDocs plugin for server-side rendering of KaTeX math.
5
+ Author-email: RainPPR <2125773894@qq.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://raineblog.dpdns.org/mkdocs-katex-ssr/
8
+ Project-URL: Documentation, https://raineblog.dpdns.org/mkdocs-katex-ssr/configuration/
9
+ Project-URL: Repository, https://github.com/raineblog/mkdocs-katex-ssr
10
+ Project-URL: Issues, https://github.com/raineblog/mkdocs-katex-ssr/issues
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: mkdocs>=1.1.0
14
+ Requires-Dist: beautifulsoup4>=4.9.0
15
+ Dynamic: license-file
16
+
17
+ # MkDocs KaTeX SSR Plugin
18
+
19
+ A MkDocs plugin that renders LaTeX math on the server side using [KaTeX](https://katex.org/), offering faster load times, layout stability, and optional offline support.
20
+
21
+ > [!NOTE]
22
+ > **Maintenance Philosophy**: This project is self-maintained. I welcome bug reports and corrections, but please note that I may not have the bandwidth to accept significant feature requests or major functional additions. Pull requests for bug fixes are appreciated.
23
+ >
24
+ > *This project's code and documentation were generated by Antigravity (Google DeepMind) and verified by the maintainer.*
25
+
26
+ ## Why Server-Side Rendering (SSR)?
27
+
28
+ Traditional client-side rendering relies on JavaScript in the browser to convert LaTeX strings (e.g., `$\sqrt{a^2 + b^2}$`) into HTML. This can lead to:
29
+
30
+ - **Layout Shift**: Content jumps as math loads.
31
+ - **Slower Performance**: The browser must download and parse the KaTeX library before rendering.
32
+ - **Dependency**: Requires client-side JavaScript execution.
33
+
34
+ **MkDocs KaTeX SSR** pre-renders all math during the `mkdocs build` process using a local Node.js process. The resulting HTML contains ready-to-view markup.
35
+
36
+ ## Features
37
+
38
+ - **High Performance**: Uses a persistent Node.js process to render equations efficiently without spawning a new process for every item.
39
+ - **Offline Support**: Optional "Offline Mode" copies all necessary CSS, fonts, and scripts to your site directory, removing external CDN dependencies.
40
+ - **Smart Asset Management**: Separate configuration for server-side processing scripts (like `mhchem`) and client-side interactive scripts (like `copy-tex`).
41
+ - **Clean Output**: Aggressive warning suppression for a quieter build log.
42
+
43
+ ## Installation
44
+
45
+ ### Prerequisites
46
+
47
+ - **Node.js**: Must be installed and available in your system PATH.
48
+ - **Python**: 3.8+
49
+
50
+ ### Install Plugin
51
+
52
+ ```bash
53
+ pip install mkdocs-katex-ssr
54
+ ```
55
+
56
+ ## Configuration
57
+
58
+ Add the plugin to your `mkdocs.yml`:
59
+
60
+ ```yaml
61
+ markdown_extensions:
62
+ - pymdownx.arithmatex:
63
+ generic: true
64
+
65
+ plugins:
66
+ - katex-ssr:
67
+ # --- Basic Configuration ---
68
+ katex_dist: "https://cdn.jsdelivr.net/npm/katex@latest/dist/"
69
+ add_katex_css: true
70
+ katex_css_filename: "katex-swap.min.css" # Use swap version for better font-display behavior
71
+
72
+ # --- Script Loading ---
73
+ # Scripts needed for rendering (Server-Side). e.g., chemical formulas.
74
+ # These are NOT sent to the browser.
75
+ ssr_contribs:
76
+ - mhchem
77
+
78
+ # Scripts needed for interaction (Client-Side). e.g., clipboard copy.
79
+ # These ARE injected into the HTML.
80
+ client_scripts:
81
+ - copy-tex
82
+
83
+ # --- KaTeX Options ---
84
+ katex_options:
85
+ macros:
86
+ "\\RR": "\\mathbb{R}"
87
+ ```
88
+
89
+ ### Offline Mode (Self-Contained)
90
+
91
+ If you need your documentation to work without an internet connection (or just want to host everything yourself), enable `embed_assets`.
92
+
93
+ ```yaml
94
+ plugins:
95
+ - katex-ssr:
96
+ embed_assets: true
97
+ # You can specify a local path if auto-detection fails
98
+ # katex_dist: "./node_modules/katex/dist/"
99
+ ```
100
+
101
+ **What this does:**
102
+
103
+ 1. **Copies Files**: It copies `katex.min.css` (or your chosen filename), the `fonts/` directory, and any specified `client_scripts` from your local `node_modules` to `site/assets/katex`.
104
+ 2. **Relative Linking**: It updates all HTML citations to point to these local files using correct relative paths (e.g., `../assets/katex/katex.min.css`).
105
+
106
+ ## Advanced Configuration Options
107
+
108
+ | Option | Type | Default | Description |
109
+ | :--- | :--- | :--- | :--- |
110
+ | `katex_dist` | str | jsDelivr | Base URL for CDN, or local file path to KaTeX distribution. |
111
+ | `add_katex_css` | bool | `true` | Whether to inject the CSS link tag. |
112
+ | `katex_css_filename` | str | `katex.min.css` | The specific CSS file to load. `katex-swap.min.css` is recommended. |
113
+ | `embed_assets` | bool | `false` | If true, copies assets to output dir and links locally. |
114
+ | `ssr_contribs` | list | `[]` | List of KaTeX `contrib` libraries to load in the Node.js renderer (e.g., `mhchem`). |
115
+ | `client_scripts` | list | `[]` | List of KaTeX `contrib` libraries to load in the browser (e.g., `copy-tex`). |
116
+ | `copy_assets_to` | str | `assets/katex` | Destination directory for copied assets (relative to site_dir). |
117
+ | `katex_options` | dict | `{}` | Standard options passed to `katex.renderToString` (macros, etc.). |
118
+
119
+ ## Troubleshooting
120
+
121
+ - **`katex` module not found**: Ensure `npm install katex` (or `pnpm`/`yarn`) has been run in your project root, or specific the path via `katex_dist`.
122
+ - **Node.js error**: The plugin requires `node` to be in your PATH. On Windows, ensure you can run `node --version` in your terminal.
123
+
124
+ ## License
125
+
126
+ This project is licensed under the MIT License.
@@ -4,17 +4,25 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mkdocs-katex-ssr"
7
- version = "0.1.0"
7
+ version = "1.0.3"
8
8
  description = "A MkDocs plugin for server-side rendering of KaTeX math."
9
+ readme = "README.md"
9
10
  authors = [
10
- {name = "Your Name", email = "your.email@example.com"}
11
+ {name = "RainPPR", email = "2125773894@qq.com"}
11
12
  ]
12
- license = {text = "MIT"}
13
+ license = "MIT"
14
+ license-files = ["LICEN[CS]E*"]
13
15
  dependencies = [
14
16
  "mkdocs>=1.1.0",
15
17
  "beautifulsoup4>=4.9.0",
16
18
  ]
17
19
 
20
+ [project.urls]
21
+ Homepage = "https://raineblog.dpdns.org/mkdocs-katex-ssr/"
22
+ Documentation = "https://raineblog.dpdns.org/mkdocs-katex-ssr/configuration/"
23
+ Repository = "https://github.com/raineblog/mkdocs-katex-ssr"
24
+ Issues = "https://github.com/raineblog/mkdocs-katex-ssr/issues"
25
+
18
26
  [project.entry-points."mkdocs.plugins"]
19
27
  katex-ssr = "mkdocs_katex_ssr.plugin:KatexSsrPlugin"
20
28
 
@@ -1,10 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mkdocs-katex-ssr
3
- Version: 0.1.0
4
- Summary: A MkDocs plugin for server-side rendering of KaTeX math.
5
- Author-email: Your Name <your.email@example.com>
6
- License: MIT
7
- License-File: LICENSE
8
- Requires-Dist: mkdocs>=1.1.0
9
- Requires-Dist: beautifulsoup4>=4.9.0
10
- Dynamic: license-file
@@ -1,10 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mkdocs-katex-ssr
3
- Version: 0.1.0
4
- Summary: A MkDocs plugin for server-side rendering of KaTeX math.
5
- Author-email: Your Name <your.email@example.com>
6
- License: MIT
7
- License-File: LICENSE
8
- Requires-Dist: mkdocs>=1.1.0
9
- Requires-Dist: beautifulsoup4>=4.9.0
10
- Dynamic: license-file