mkdocs-katex-ssr 1.0.4__tar.gz → 1.0.5__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mkdocs-katex-ssr
3
- Version: 1.0.4
3
+ Version: 1.0.5
4
4
  Summary: A MkDocs plugin for server-side rendering of KaTeX math.
5
5
  Author-email: RainPPR <2125773894@qq.com>
6
6
  License-Expression: MIT
@@ -1,10 +1,11 @@
1
1
  import os
2
2
  import json
3
+ import sqlite3
4
+ import hashlib
3
5
  import subprocess
4
6
  import threading
5
7
  import warnings
6
8
  import logging
7
- import requests
8
9
  import shutil
9
10
  from mkdocs.plugins import BasePlugin
10
11
  from mkdocs.config import config_options
@@ -48,6 +49,8 @@ class KatexSsrPlugin(BasePlugin):
48
49
  self.lock = threading.Lock()
49
50
  self._asset_cache = {}
50
51
  self._local_dist_path = None
52
+ self.db_conn = None
53
+ self.db_path = None
51
54
 
52
55
  def _ensure_trailing_slash(self, path):
53
56
  if not path.endswith('/') and not path.endswith('\\'):
@@ -66,6 +69,21 @@ class KatexSsrPlugin(BasePlugin):
66
69
 
67
70
  project_dir = os.path.dirname(config['config_file_path'])
68
71
 
72
+ # Initialize Cache DB
73
+ try:
74
+ cache_dir = os.path.join(project_dir, '.cache', 'plugin', 'katex-ssr')
75
+ os.makedirs(cache_dir, exist_ok=True)
76
+ self.db_path = os.path.join(cache_dir, 'cache.db')
77
+ self.db_conn = sqlite3.connect(
78
+ self.db_path,
79
+ check_same_thread=False
80
+ )
81
+ with self.db_conn:
82
+ self.db_conn.execute('CREATE TABLE IF NOT EXISTS katex_cache (hash TEXT PRIMARY KEY, html TEXT)')
83
+ except Exception as e:
84
+ print(f"Warning: Failed to initialize KaTeX SSR cache: {e}")
85
+ self.db_conn = None
86
+
69
87
  # Merge legacy contrib_scripts into ssr_contribs if used
70
88
  if self.config['contrib_scripts']:
71
89
  # Append unique items
@@ -129,6 +147,22 @@ class KatexSsrPlugin(BasePlugin):
129
147
  return config
130
148
 
131
149
  def _render_latex(self, latex, display_mode=False):
150
+ # Check cache first
151
+ latex_trimmed = latex.strip()
152
+ cache_key = None
153
+ if self.db_conn:
154
+ try:
155
+ # Create a unique hash for the content AND display mode
156
+ content_to_hash = f"{latex_trimmed}::{display_mode}"
157
+ cache_key = hashlib.sha256(content_to_hash.encode('utf-8')).hexdigest()
158
+
159
+ cursor = self.db_conn.execute("SELECT html FROM katex_cache WHERE hash=?", (cache_key,))
160
+ row = cursor.fetchone()
161
+ if row:
162
+ return row[0]
163
+ except Exception as e:
164
+ print(f"Error reading cache: {e}")
165
+
132
166
  if not self.process:
133
167
  return None
134
168
 
@@ -150,7 +184,18 @@ class KatexSsrPlugin(BasePlugin):
150
184
 
151
185
  result = json.loads(response_line.decode('utf-8'))
152
186
  if result.get('status') == 'success':
153
- return result.get('html')
187
+ html = result.get('html')
188
+ # Save to cache
189
+ if self.db_conn and cache_key:
190
+ try:
191
+ with self.db_conn:
192
+ self.db_conn.execute(
193
+ "INSERT OR REPLACE INTO katex_cache (hash, html) VALUES (?, ?)",
194
+ (cache_key, html)
195
+ )
196
+ except Exception as e:
197
+ print(f"Error saving to cache: {e}")
198
+ return html
154
199
  else:
155
200
  print(f"KaTeX error: {result.get('message')}")
156
201
  except Exception as e:
@@ -234,6 +279,13 @@ class KatexSsrPlugin(BasePlugin):
234
279
  self.process.terminate()
235
280
  self.process.wait()
236
281
 
282
+ if self.db_conn:
283
+ try:
284
+ self.db_conn.close()
285
+ except:
286
+ pass
287
+ self.db_conn = None
288
+
237
289
  # Copy assets if requested
238
290
  if self.config['embed_assets'] and self._local_dist_path:
239
291
  dest_dir = os.path.join(config['site_dir'], self.config['copy_assets_to'])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mkdocs-katex-ssr
3
- Version: 1.0.4
3
+ Version: 1.0.5
4
4
  Summary: A MkDocs plugin for server-side rendering of KaTeX math.
5
5
  Author-email: RainPPR <2125773894@qq.com>
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mkdocs-katex-ssr"
7
- version = "1.0.4"
7
+ version = "1.0.5"
8
8
  description = "A MkDocs plugin for server-side rendering of KaTeX math."
9
9
  readme = "README.md"
10
10
  authors = [