Skip to content

Commit 87091d0

Browse files
committed
fix: markdown-enhancer CSS/hljs, add loadCSS/loadHighlightJS to plugin utils
- Framework: loadCSS for .css deps, skip hljs-* in manifest (plugin loads on-demand) - markdown-enhancer: call loadHighlightJS() before hljs lang packs - Expose loadCSS and loadHighlightJS in ChatRawPlugin.utils - Update Plugins README with utils docs and lib loading notes
1 parent 688522c commit 87091d0

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

Plugins/Plugin_market/markdown-enhancer/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@
163163
async function initExtraLanguages() {
164164
if (extraLangsLoaded || !window.hljs) return;
165165

166+
// Ensure hljs core is loaded first (app loads it lazily; plugin may run before that)
167+
const loadHLJS = ChatRawPlugin?.utils?.loadHighlightJS;
168+
if (loadHLJS) await loadHLJS();
169+
if (!window.hljs) return; // Cannot load lang packs without hljs core
170+
166171
const languages = [
167172
'typescript', 'go', 'rust', 'java', 'c', 'cpp', 'csharp',
168173
'ruby', 'php', 'swift', 'kotlin', 'sql', 'yaml', 'xml', 'shell'

Plugins/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ Helper functions for plugin developers:
474474
// Load external script
475475
await ChatRaw.utils.loadScript('https://cdn.example.com/lib.js');
476476

477+
// Load external CSS (e.g. for bundled lib styles)
478+
await ChatRaw.utils.loadCSS('/api/plugins/your-plugin/lib/styles.min.css');
479+
480+
// Ensure highlight.js core is loaded (call before loading hljs language packs)
481+
await ChatRaw.utils.loadHighlightJS();
482+
477483
// Show toast notification
478484
ChatRaw.utils.showToast('Operation completed', 'success');
479485

@@ -718,7 +724,7 @@ To distribute your plugin:
718724
719725
For plugins that need to work completely offline, you can bundle dependencies in the `lib/` directory.
720726
721-
**Important for plugin market**: If your plugin uses `lib/` files and will be installed from the market (GitHub URL), you **must** declare them in manifest `dependencies` using the format `/api/plugins/{plugin_id}/lib/{filename}`. Otherwise users get 404/MIME errors. Example:
727+
**Important for plugin market**: If your plugin uses `lib/` files and will be installed from the market (GitHub URL), you **must** declare them in manifest `dependencies` using the format `/api/plugins/{plugin_id}/lib/{filename}`. Otherwise users get 404/MIME errors. The framework loads `.css` files with `<link>` and `.js` files with `<script>`. Files named `hljs-*` are skipped (plugins load them on-demand after calling `ChatRaw.utils.loadHighlightJS()`). Example:
722728
723729
```json
724730
"dependencies": {
@@ -1418,6 +1424,12 @@ API 密钥安全存储在后端,会自动添加到请求中。
14181424
// 加载外部脚本
14191425
await ChatRaw.utils.loadScript('https://cdn.example.com/lib.js');
14201426

1427+
// 加载外部 CSS(如 bundled 的样式)
1428+
await ChatRaw.utils.loadCSS('/api/plugins/your-plugin/lib/styles.min.css');
1429+
1430+
// 确保 highlight.js 核心已加载(使用 hljs 语言包前需先调用)
1431+
await ChatRaw.utils.loadHighlightJS();
1432+
14211433
// 显示提示消息
14221434
ChatRaw.utils.showToast('操作完成', 'success');
14231435

RELEASE_v2.1.2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
### Markdown 渲染增强 (v1.0.1)
1212
- 添加 manifest dependencies,解决从市场安装时 KaTeX/Mermaid/lib 404
1313
- 安装时自动下载全部 18 个 lib 文件
14+
- 修复:CSS 依赖用 loadCSS 加载(不再误用 loadScript)
15+
- 修复:hljs 语言包在 hljs 核心加载后再加载,避免 "hljs is not defined"
1416

1517
### 插件设置
1618
- 修复 select 类型 options 为对象数组时的 Alpine x-for key 警告

backend/static/app.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,12 @@ function app() {
16231623
// Load external script dynamically
16241624
loadScript: (url) => appInstance.loadScript(url),
16251625

1626+
// Load external CSS dynamically
1627+
loadCSS: (url) => appInstance.loadCSS(url),
1628+
1629+
// Ensure highlight.js is loaded (for plugins that use hljs language packs)
1630+
loadHighlightJS: () => typeof loadHighlightJS === 'function' ? loadHighlightJS() : Promise.resolve(),
1631+
16261632
// Show toast notification
16271633
showToast: (message, type = '') => appInstance.showToast(message, type),
16281634

@@ -1900,14 +1906,25 @@ function app() {
19001906
for (const [name, url] of Object.entries(plugin.dependencies)) {
19011907
if (!this.loadedPluginDeps[name]) {
19021908
try {
1903-
await this.loadScript(url);
1904-
// Common library detection patterns
1905-
if (name === 'xlsx' && window.XLSX) {
1906-
this.loadedPluginDeps[name] = window.XLSX;
1907-
} else if (name === 'pdfjs' && window.pdfjsLib) {
1908-
this.loadedPluginDeps[name] = window.pdfjsLib;
1909-
} else if (window[name]) {
1910-
this.loadedPluginDeps[name] = window[name];
1909+
// CSS must use loadCSS, not loadScript
1910+
if (typeof url === 'string' && url.endsWith('.css')) {
1911+
await this.loadCSS(url);
1912+
this.loadedPluginDeps[name] = true;
1913+
} else if (name.startsWith('hljs-')) {
1914+
// hljs language packs loaded on-demand by plugin (need hljs core first)
1915+
continue;
1916+
} else {
1917+
await this.loadScript(url);
1918+
// Common library detection patterns
1919+
if (name === 'xlsx' && window.XLSX) {
1920+
this.loadedPluginDeps[name] = window.XLSX;
1921+
} else if (name === 'pdfjs' && window.pdfjsLib) {
1922+
this.loadedPluginDeps[name] = window.pdfjsLib;
1923+
} else if (window[name]) {
1924+
this.loadedPluginDeps[name] = window[name];
1925+
} else {
1926+
this.loadedPluginDeps[name] = true;
1927+
}
19111928
}
19121929
} catch (depError) {
19131930
console.error(`[Plugin] Failed to load dependency ${name} for ${plugin.id}:`, depError);
@@ -1950,6 +1967,22 @@ function app() {
19501967
});
19511968
},
19521969

1970+
loadCSS(url) {
1971+
return new Promise((resolve, reject) => {
1972+
const existing = document.querySelector(`link[href="${url}"]`);
1973+
if (existing) {
1974+
resolve();
1975+
return;
1976+
}
1977+
const link = document.createElement('link');
1978+
link.rel = 'stylesheet';
1979+
link.href = url;
1980+
link.onload = resolve;
1981+
link.onerror = () => reject(new Error(`Failed to load CSS: ${url}`));
1982+
document.head.appendChild(link);
1983+
});
1984+
},
1985+
19531986
// Register a hook handler
19541987
registerHook(hookName, handler) {
19551988
if (!this.pluginHooks[hookName]) {

backend/static/app.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)