fix: 改用 importmap + esm.sh 解决 CodeMirror 多实例冲突
- 将 import 改回 bare specifier,通过 importmap 指向 esm.sh - esm.sh 在同一构建上下文中能识别共享依赖,确保 @codemirror/state 单例 - 消除 "Unrecognized extension value" instanceof 检查错误
This commit is contained in:
@ -10,14 +10,14 @@ func SecurityHeaders() gin.HandlerFunc {
|
|||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// Content-Security-Policy
|
// Content-Security-Policy
|
||||||
// script-src: 本站 + esm.sh CDN (CodeMirror 6 ESM 模块)
|
// script-src: 本站 + esm.sh CDN (CodeMirror 6 ESM 模块)
|
||||||
// connect-src: 本站 (Markdown 预览等 fetch 请求)
|
// connect-src: 本站 + esm.sh (source map 请求)
|
||||||
// img-src: 本站 + data: URI (头像裁切) + blob: (粘贴图片)
|
// img-src: 本站 + data: URI (头像裁切) + blob: (粘贴图片)
|
||||||
c.Header("Content-Security-Policy",
|
c.Header("Content-Security-Policy",
|
||||||
"default-src 'self'; "+
|
"default-src 'self'; "+
|
||||||
"script-src 'self' 'unsafe-inline' https://esm.sh; "+
|
"script-src 'self' 'unsafe-inline' https://esm.sh; "+
|
||||||
"style-src 'self' 'unsafe-inline'; "+
|
"style-src 'self' 'unsafe-inline'; "+
|
||||||
"img-src 'self' data: blob:; "+
|
"img-src 'self' data: blob:; "+
|
||||||
"connect-src 'self'")
|
"connect-src 'self' https://esm.sh")
|
||||||
|
|
||||||
// 禁止 MIME 类型嗅探
|
// 禁止 MIME 类型嗅探
|
||||||
c.Header("X-Content-Type-Options", "nosniff")
|
c.Header("X-Content-Type-Options", "nosniff")
|
||||||
|
|||||||
@ -124,6 +124,27 @@
|
|||||||
|
|
||||||
{{template "layout/footer.html" .}}
|
{{template "layout/footer.html" .}}
|
||||||
|
|
||||||
{{/* CodeMirror 6 (MIT License, via esm.sh bundle) */}}
|
{{/* CodeMirror 6 importmap — 所有 @codemirror/@lezer 指向 esm.sh 同一构建树,共享依赖单例 */}}
|
||||||
|
<script type="importmap">
|
||||||
|
{
|
||||||
|
"imports": {
|
||||||
|
"@codemirror/state": "https://esm.sh/@codemirror/state@6.5.2",
|
||||||
|
"@codemirror/view": "https://esm.sh/@codemirror/view@6.36.4",
|
||||||
|
"@codemirror/commands": "https://esm.sh/@codemirror/commands@6.8.1",
|
||||||
|
"@codemirror/language": "https://esm.sh/@codemirror/language@6.11.0",
|
||||||
|
"@codemirror/lang-markdown": "https://esm.sh/@codemirror/lang-markdown@6.3.2",
|
||||||
|
"@codemirror/language-data": "https://esm.sh/@codemirror/language-data@6.5.1",
|
||||||
|
"@codemirror/autocomplete": "https://esm.sh/@codemirror/autocomplete@6.18.6",
|
||||||
|
"@codemirror/search": "https://esm.sh/@codemirror/search@6.5.10",
|
||||||
|
"@lezer/common": "https://esm.sh/@lezer/common@1.2.3",
|
||||||
|
"@lezer/highlight": "https://esm.sh/@lezer/highlight@1.2.1",
|
||||||
|
"@lezer/lr": "https://esm.sh/@lezer/lr@1.4.2",
|
||||||
|
"@lezer/markdown": "https://esm.sh/@lezer/markdown@1.4.0",
|
||||||
|
"crelt": "https://esm.sh/crelt@1.0.6",
|
||||||
|
"style-mod": "https://esm.sh/style-mod@4.1.2",
|
||||||
|
"w3c-keyname": "https://esm.sh/w3c-keyname@2.2.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
<script src="/static/js/common.js"></script>
|
<script src="/static/js/common.js"></script>
|
||||||
<script type="module" src="/static/js/editor.js"></script>
|
<script type="module" src="/static/js/editor.js"></script>
|
||||||
|
|||||||
@ -1,28 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
* MetaLab Markdown Editor — CodeMirror 6 版
|
* MetaLab Markdown Editor — CodeMirror 6 版
|
||||||
*
|
*
|
||||||
* 功能:
|
* 依赖通过 importmap 指向 esm.sh,所有 @codemirror/@lezer 共享同一构建树,
|
||||||
* - CodeMirror 6 编辑器,Markdown 语法高亮
|
* 避免多实例导致的 "Unrecognized extension value" 错误。
|
||||||
* - 工具栏:加粗/斜体/删除线/行内代码/标题/链接/图片/引用/代码块/列表/表格/分割线
|
|
||||||
* - 快捷键:Ctrl+B/I/K/` Shift+I Tab Shift+Tab Ctrl+S Ctrl+Shift+P F11
|
|
||||||
* - 粘贴图片 / 拖拽上传 / 进度反馈
|
|
||||||
* - 分屏实时预览(Ctrl+Shift+P 切换)
|
|
||||||
* - 代码块语言下拉选择器
|
|
||||||
* - 自动保存草稿(localStorage + 后端)
|
|
||||||
* - 字数/行数/阅读时间统计
|
|
||||||
* - 全屏编辑模式(F11)
|
|
||||||
* - 滚动同步(分屏模式下)
|
|
||||||
*
|
|
||||||
* CodeMirror 6 通过 esm.sh bundle 加载,所有依赖自动解析。
|
|
||||||
*/
|
*/
|
||||||
import { EditorState } from 'https://esm.sh/@codemirror/state@6.5.2';
|
import { EditorState } from '@codemirror/state';
|
||||||
import { EditorView, keymap, lineNumbers, highlightActiveLine, drawSelection } from 'https://esm.sh/@codemirror/view@6.36.4';
|
import { EditorView, keymap, lineNumbers, highlightActiveLine, drawSelection } from '@codemirror/view';
|
||||||
import { defaultKeymap, history, historyKeymap, indentWithTab } from 'https://esm.sh/@codemirror/commands@6.8.1';
|
import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
|
||||||
import { markdown, markdownLanguage } from 'https://esm.sh/@codemirror/lang-markdown@6.3.2';
|
import { markdown, markdownLanguage } from '@codemirror/lang-markdown';
|
||||||
import { languages } from 'https://esm.sh/@codemirror/language-data@6.5.1';
|
import { languages } from '@codemirror/language-data';
|
||||||
import { syntaxHighlighting, defaultHighlightStyle } from 'https://esm.sh/@codemirror/language@6.11.0';
|
import { syntaxHighlighting, defaultHighlightStyle } from '@codemirror/language';
|
||||||
import { searchKeymap } from 'https://esm.sh/@codemirror/search@6.5.10';
|
import { searchKeymap } from '@codemirror/search';
|
||||||
import { autocompletion } from 'https://esm.sh/@codemirror/autocomplete@6.18.6';
|
import { autocompletion } from '@codemirror/autocomplete';
|
||||||
|
|
||||||
/* ================================================================
|
/* ================================================================
|
||||||
DOM 引用
|
DOM 引用
|
||||||
|
|||||||
Reference in New Issue
Block a user