feat: 代码块语法高亮 + 语言选择 + 样式统一
- 引入 @tiptap/extension-code-block-lowlight + lowlight 实现代码语法高亮 - 工具栏新增语言下拉选择器,光标在代码块内时显示,支持 27 种常用语言 - 默认语言 text,高亮主题适配 MetaLab 深蓝背景 (#1a2332) - 代码块语言标签用 JS 动态注入 DOM 元素(避免 ::before 在 overflow 容器中失效) - 详情页同步代码块高亮和语言标签显示 - 编辑器与详情页样式统一:代码块暗色背景、引用蓝色边框、链接 accent 蓝 - sanitizePolicy 放行 data-language 属性和 hljs-* class - 修复 import map 避免 keyed plugin 重复实例 - 修复详情页代码块开头空白行(移除多余的 padding-top) - 修复语法错误(多余的闭合括号)
This commit is contained in:
@ -2,6 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"metazone.cc/metalab/internal/common"
|
"metazone.cc/metalab/internal/common"
|
||||||
"metazone.cc/metalab/internal/config"
|
"metazone.cc/metalab/internal/config"
|
||||||
@ -12,11 +13,13 @@ import (
|
|||||||
|
|
||||||
// sanitizePolicy Tiptap 输出 HTML 的消毒策略
|
// sanitizePolicy Tiptap 输出 HTML 的消毒策略
|
||||||
// 前端 WYSIWYG 编辑器输出 HTML,由 bluemonday UGC 策略消毒
|
// 前端 WYSIWYG 编辑器输出 HTML,由 bluemonday UGC 策略消毒
|
||||||
// 显式放行 code/pre 的 class 属性以保留代码块样式
|
// 放行 code/pre 的 class/data-language 属性以保留代码高亮和语言标签
|
||||||
// 放行 img 的 src/alt 属性以保留图片
|
// 放行 span 的 class 属性以保留 highlight.js 的 hljs-* 高亮
|
||||||
|
// 放行 img 的 src/alt/title 属性以保留图片
|
||||||
var sanitizePolicy = func() *bluemonday.Policy {
|
var sanitizePolicy = func() *bluemonday.Policy {
|
||||||
p := bluemonday.UGCPolicy()
|
p := bluemonday.UGCPolicy()
|
||||||
p.AllowAttrs("class").OnElements("code", "pre")
|
p.AllowAttrs("class", "data-language").OnElements("code", "pre")
|
||||||
|
p.AllowAttrs("class").Matching(regexp.MustCompile("^hljs-")).OnElements("span")
|
||||||
p.AllowAttrs("src", "alt", "title").OnElements("img")
|
p.AllowAttrs("src", "alt", "title").OnElements("img")
|
||||||
return p
|
return p
|
||||||
}()
|
}()
|
||||||
|
|||||||
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
/* 引用 */
|
/* 引用 */
|
||||||
.tiptap blockquote {
|
.tiptap blockquote {
|
||||||
border-left: 3px solid #e5e7eb;
|
border-left: 3px solid #3498db;
|
||||||
padding-left: 1em;
|
padding-left: 1em;
|
||||||
margin: 0.5em 0;
|
margin: 0.5em 0;
|
||||||
color: #6b7280;
|
color: #6b7280;
|
||||||
@ -43,16 +43,17 @@
|
|||||||
|
|
||||||
/* 代码块 */
|
/* 代码块 */
|
||||||
.tiptap pre {
|
.tiptap pre {
|
||||||
background: #1f2937;
|
background: #1a2332;
|
||||||
color: #f3f4f6;
|
color: #e2e8f0;
|
||||||
padding: 12px 16px;
|
padding: 12px 16px;
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
margin: 0.5em 0;
|
margin: 0.5em 0;
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
.tiptap pre code { background: transparent; padding: 0; color: inherit; font-size: inherit; }
|
.tiptap pre code { background: transparent; padding: 0; color: inherit; font-size: inherit; white-space: pre; display: block; }
|
||||||
.tiptap code {
|
.tiptap code {
|
||||||
background: #f3f4f6;
|
background: #f3f4f6;
|
||||||
padding: 2px 5px;
|
padding: 2px 5px;
|
||||||
@ -60,6 +61,59 @@
|
|||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
font-family: 'SF Mono', 'Menlo', 'Monaco', monospace;
|
font-family: 'SF Mono', 'Menlo', 'Monaco', monospace;
|
||||||
}
|
}
|
||||||
|
/* 代码块语言标签 */
|
||||||
|
.tiptap pre .code-lang-label {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
padding: 4px 16px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #94a3b8;
|
||||||
|
background: #2c3e50;
|
||||||
|
border-radius: 6px 6px 0 0;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
|
||||||
|
pointer-events: none;
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* highlight.js 语法高亮主题 — 基于 One Dark,适配 MetaLab 深蓝背景 */
|
||||||
|
.tiptap pre code .hljs-keyword,
|
||||||
|
.tiptap pre code .hljs-selector-tag,
|
||||||
|
.tiptap pre code .hljs-literal,
|
||||||
|
.tiptap pre code .hljs-section,
|
||||||
|
.tiptap pre code .hljs-link { color: #c792ea; }
|
||||||
|
.tiptap pre code .hljs-string,
|
||||||
|
.tiptap pre code .hljs-title,
|
||||||
|
.tiptap pre code .hljs-name,
|
||||||
|
.tiptap pre code .hljs-type,
|
||||||
|
.tiptap pre code .hljs-attribute,
|
||||||
|
.tiptap pre code .hljs-symbol,
|
||||||
|
.tiptap pre code .hljs-bullet,
|
||||||
|
.tiptap pre code .hljs-addition,
|
||||||
|
.tiptap pre code .hljs-variable,
|
||||||
|
.tiptap pre code .hljs-template-tag,
|
||||||
|
.tiptap pre code .hljs-template-variable { color: #c3e88d; }
|
||||||
|
.tiptap pre code .hljs-comment,
|
||||||
|
.tiptap pre code .hljs-quote,
|
||||||
|
.tiptap pre code .hljs-deletion,
|
||||||
|
.tiptap pre code .hljs-meta { color: #676e95; }
|
||||||
|
.tiptap pre code .hljs-number,
|
||||||
|
.tiptap pre code .hljs-regexp,
|
||||||
|
.tiptap pre code .hljs-built_in { color: #f78c6c; }
|
||||||
|
.tiptap pre code .hljs-function .hljs-title,
|
||||||
|
.tiptap pre code .hljs-doctag,
|
||||||
|
.tiptap pre code .hljs-formula { color: #82aaff; }
|
||||||
|
.tiptap pre code .hljs-tag,
|
||||||
|
.tiptap pre code .hljs-subst { color: #f07178; }
|
||||||
|
.tiptap pre code .hljs-emphasis { font-style: italic; }
|
||||||
|
.tiptap pre code .hljs-strong { font-weight: bold; }
|
||||||
|
.tiptap pre code .hljs-attr,
|
||||||
|
.tiptap pre code .hljs-selector-attr,
|
||||||
|
.tiptap pre code .hljs-selector-class,
|
||||||
|
.tiptap pre code .hljs-selector-id,
|
||||||
|
.tiptap pre code .hljs-selector-pseudo { color: #ffcb6b; }
|
||||||
|
|
||||||
/* 水平线 */
|
/* 水平线 */
|
||||||
.tiptap hr { border: none; border-top: 1px solid #e5e7eb; margin: 1em 0; }
|
.tiptap hr { border: none; border-top: 1px solid #e5e7eb; margin: 1em 0; }
|
||||||
@ -68,7 +122,7 @@
|
|||||||
.tiptap img { max-width: 100%; height: auto; border-radius: 6px; }
|
.tiptap img { max-width: 100%; height: auto; border-radius: 6px; }
|
||||||
|
|
||||||
/* 链接 */
|
/* 链接 */
|
||||||
.tiptap a { color: #6366f1; text-decoration: underline; }
|
.tiptap a { color: #3498db; text-decoration: underline; }
|
||||||
|
|
||||||
/* Placeholder */
|
/* Placeholder */
|
||||||
.tiptap p.is-editor-empty:first-child::before {
|
.tiptap p.is-editor-empty:first-child::before {
|
||||||
@ -104,7 +158,21 @@
|
|||||||
transition: background 0.15s;
|
transition: background 0.15s;
|
||||||
}
|
}
|
||||||
.editor-toolbar button:hover { background: #e5e7eb; }
|
.editor-toolbar button:hover { background: #e5e7eb; }
|
||||||
.editor-toolbar button.is-active { background: #1f2937; color: #fff; }
|
.editor-toolbar button.is-active { background: #1a2332; color: #fff; }
|
||||||
|
.code-lang-select {
|
||||||
|
height: 30px;
|
||||||
|
padding: 0 6px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
border-radius: 4px;
|
||||||
|
background: transparent;
|
||||||
|
color: #374151;
|
||||||
|
font-size: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-family: inherit;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
.code-lang-select:hover { border-color: #e5e7eb; }
|
||||||
|
.code-lang-select:focus { border-color: #3498db; }
|
||||||
.editor-toolbar .toolbar-divider {
|
.editor-toolbar .toolbar-divider {
|
||||||
width: 1px;
|
width: 1px;
|
||||||
background: #e5e7eb;
|
background: #e5e7eb;
|
||||||
@ -143,6 +211,35 @@
|
|||||||
<button type="button" data-action="blockquote" title="引用">"</button>
|
<button type="button" data-action="blockquote" title="引用">"</button>
|
||||||
<span class="toolbar-divider"></span>
|
<span class="toolbar-divider"></span>
|
||||||
<button type="button" data-action="codeBlock" title="代码块">{ }</button>
|
<button type="button" data-action="codeBlock" title="代码块">{ }</button>
|
||||||
|
<select id="codeLangSelect" class="code-lang-select" style="display:none" title="选择语言">
|
||||||
|
<option value="text">text</option>
|
||||||
|
<option value="javascript">JavaScript</option>
|
||||||
|
<option value="typescript">TypeScript</option>
|
||||||
|
<option value="python">Python</option>
|
||||||
|
<option value="go">Go</option>
|
||||||
|
<option value="rust">Rust</option>
|
||||||
|
<option value="java">Java</option>
|
||||||
|
<option value="cpp">C++</option>
|
||||||
|
<option value="c">C</option>
|
||||||
|
<option value="csharp">C#</option>
|
||||||
|
<option value="ruby">Ruby</option>
|
||||||
|
<option value="php">PHP</option>
|
||||||
|
<option value="swift">Swift</option>
|
||||||
|
<option value="kotlin">Kotlin</option>
|
||||||
|
<option value="sql">SQL</option>
|
||||||
|
<option value="bash">Bash</option>
|
||||||
|
<option value="json">JSON</option>
|
||||||
|
<option value="yaml">YAML</option>
|
||||||
|
<option value="xml">XML</option>
|
||||||
|
<option value="css">CSS</option>
|
||||||
|
<option value="scss">SCSS</option>
|
||||||
|
<option value="html">HTML</option>
|
||||||
|
<option value="markdown">Markdown</option>
|
||||||
|
<option value="shell">Shell</option>
|
||||||
|
<option value="dockerfile">Dockerfile</option>
|
||||||
|
<option value="nginx">Nginx</option>
|
||||||
|
<option value="diff">Diff</option>
|
||||||
|
</select>
|
||||||
<button type="button" data-action="horizontalRule" title="分隔线">—</button>
|
<button type="button" data-action="horizontalRule" title="分隔线">—</button>
|
||||||
<span class="toolbar-divider"></span>
|
<span class="toolbar-divider"></span>
|
||||||
<button type="button" data-action="undo" title="撤销">↩</button>
|
<button type="button" data-action="undo" title="撤销">↩</button>
|
||||||
@ -170,6 +267,19 @@
|
|||||||
|
|
||||||
{{template "layout/footer.html" .}}
|
{{template "layout/footer.html" .}}
|
||||||
|
|
||||||
|
<script type="importmap">
|
||||||
|
{
|
||||||
|
"imports": {
|
||||||
|
"@tiptap/core": "https://esm.sh/@tiptap/core@3.13.0",
|
||||||
|
"@tiptap/starter-kit": "https://esm.sh/@tiptap/starter-kit@3.13.0",
|
||||||
|
"@tiptap/extension-placeholder": "https://esm.sh/@tiptap/extension-placeholder@3.13.0",
|
||||||
|
"@tiptap/extension-image": "https://esm.sh/@tiptap/extension-image@3.13.0",
|
||||||
|
"@tiptap/extension-code-block-lowlight": "https://esm.sh/@tiptap/extension-code-block-lowlight@3.13.0",
|
||||||
|
"lowlight": "https://esm.sh/lowlight@3.3.0",
|
||||||
|
"highlight.js/lib/languages/": "https://esm.sh/highlight.js@11.11.0/es/languages/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</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>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@ -47,6 +47,20 @@
|
|||||||
|
|
||||||
<script src="/static/js/common.js"></script>
|
<script src="/static/js/common.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
// ---- 代码块语言标签注入 ----
|
||||||
|
(function() {
|
||||||
|
document.querySelectorAll('.post-detail-body pre').forEach(function(pre) {
|
||||||
|
var lang = pre.getAttribute('data-language');
|
||||||
|
if (lang) {
|
||||||
|
var label = document.createElement('div');
|
||||||
|
label.className = 'code-lang-label';
|
||||||
|
label.textContent = lang;
|
||||||
|
pre.insertBefore(label, pre.firstChild);
|
||||||
|
pre.style.paddingTop = '32px';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var btn = document.getElementById('submitAuditBtn');
|
var btn = document.getElementById('submitAuditBtn');
|
||||||
if (!btn) return;
|
if (!btn) return;
|
||||||
|
|||||||
@ -128,9 +128,9 @@
|
|||||||
/* 代码块基础样式 */
|
/* 代码块基础样式 */
|
||||||
.post-detail-body pre {
|
.post-detail-body pre {
|
||||||
position: relative;
|
position: relative;
|
||||||
background: #f3f4f6;
|
background: #1a2332;
|
||||||
|
color: #e2e8f0;
|
||||||
padding: 12px 16px;
|
padding: 12px 16px;
|
||||||
padding-top: 36px;
|
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
@ -138,19 +138,21 @@
|
|||||||
font-family: 'Menlo', 'Consolas', 'Monaco', 'Liberation Mono', monospace;
|
font-family: 'Menlo', 'Consolas', 'Monaco', 'Liberation Mono', monospace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 通过 JS 动态注入语言标签,因为 CSS attr() 无法从 class 中提取 */
|
/* 代码块语言标签(JS 动态注入) */
|
||||||
.code-lang-label {
|
.post-detail-body pre .code-lang-label {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
right: 0;
|
right: 0;
|
||||||
padding: 8px 16px !important;
|
padding: 4px 16px;
|
||||||
background: #e5e7eb;
|
|
||||||
color: #6b7280;
|
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
|
color: #94a3b8;
|
||||||
|
background: #2c3e50;
|
||||||
border-radius: 8px 8px 0 0;
|
border-radius: 8px 8px 0 0;
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
|
||||||
|
pointer-events: none;
|
||||||
|
z-index: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-detail-body pre code {
|
.post-detail-body pre code {
|
||||||
@ -158,6 +160,8 @@
|
|||||||
border: none !important;
|
border: none !important;
|
||||||
padding: 0 !important;
|
padding: 0 !important;
|
||||||
font-size: inherit !important;
|
font-size: inherit !important;
|
||||||
|
white-space: pre;
|
||||||
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.post-detail-body code {
|
.post-detail-body code {
|
||||||
@ -167,6 +171,43 @@
|
|||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* highlight.js 语法高亮 — 适配 MetaLab 深蓝背景 */
|
||||||
|
.post-detail-body pre code .hljs-keyword,
|
||||||
|
.post-detail-body pre code .hljs-selector-tag,
|
||||||
|
.post-detail-body pre code .hljs-literal,
|
||||||
|
.post-detail-body pre code .hljs-section,
|
||||||
|
.post-detail-body pre code .hljs-link { color: #c792ea; }
|
||||||
|
.post-detail-body pre code .hljs-string,
|
||||||
|
.post-detail-body pre code .hljs-title,
|
||||||
|
.post-detail-body pre code .hljs-name,
|
||||||
|
.post-detail-body pre code .hljs-type,
|
||||||
|
.post-detail-body pre code .hljs-attribute,
|
||||||
|
.post-detail-body pre code .hljs-symbol,
|
||||||
|
.post-detail-body pre code .hljs-bullet,
|
||||||
|
.post-detail-body pre code .hljs-addition,
|
||||||
|
.post-detail-body pre code .hljs-variable,
|
||||||
|
.post-detail-body pre code .hljs-template-tag,
|
||||||
|
.post-detail-body pre code .hljs-template-variable { color: #c3e88d; }
|
||||||
|
.post-detail-body pre code .hljs-comment,
|
||||||
|
.post-detail-body pre code .hljs-quote,
|
||||||
|
.post-detail-body pre code .hljs-deletion,
|
||||||
|
.post-detail-body pre code .hljs-meta { color: #676e95; }
|
||||||
|
.post-detail-body pre code .hljs-number,
|
||||||
|
.post-detail-body pre code .hljs-regexp,
|
||||||
|
.post-detail-body pre code .hljs-built_in { color: #f78c6c; }
|
||||||
|
.post-detail-body pre code .hljs-function .hljs-title,
|
||||||
|
.post-detail-body pre code .hljs-doctag,
|
||||||
|
.post-detail-body pre code .hljs-formula { color: #82aaff; }
|
||||||
|
.post-detail-body pre code .hljs-tag,
|
||||||
|
.post-detail-body pre code .hljs-subst { color: #f07178; }
|
||||||
|
.post-detail-body pre code .hljs-emphasis { font-style: italic; }
|
||||||
|
.post-detail-body pre code .hljs-strong { font-weight: bold; }
|
||||||
|
.post-detail-body pre code .hljs-attr,
|
||||||
|
.post-detail-body pre code .hljs-selector-attr,
|
||||||
|
.post-detail-body pre code .hljs-selector-class,
|
||||||
|
.post-detail-body pre code .hljs-selector-id,
|
||||||
|
.post-detail-body pre code .hljs-selector-pseudo { color: #ffcb6b; }
|
||||||
|
|
||||||
.post-detail-body img {
|
.post-detail-body img {
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
height: auto;
|
height: auto;
|
||||||
|
|||||||
@ -6,11 +6,12 @@
|
|||||||
*
|
*
|
||||||
* 安全模型:Tiptap → HTML(富文本)→ 后端 bluemonday 消毒 → 存储
|
* 安全模型:Tiptap → HTML(富文本)→ 后端 bluemonday 消毒 → 存储
|
||||||
*/
|
*/
|
||||||
import { Editor } from 'https://esm.sh/@tiptap/core@3.13.0'
|
import { Editor } from '@tiptap/core'
|
||||||
import StarterKit from 'https://esm.sh/@tiptap/starter-kit@3.13.0'
|
import StarterKit from '@tiptap/starter-kit'
|
||||||
import Placeholder from 'https://esm.sh/@tiptap/extension-placeholder@3.13.0'
|
import Placeholder from '@tiptap/extension-placeholder'
|
||||||
import Link from 'https://esm.sh/@tiptap/extension-link@3.13.0'
|
import Image from '@tiptap/extension-image'
|
||||||
import Image from 'https://esm.sh/@tiptap/extension-image@3.13.0'
|
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
|
||||||
|
import { common, createLowlight } from 'lowlight'
|
||||||
|
|
||||||
// ---- 状态 ----
|
// ---- 状态 ----
|
||||||
let editor = null
|
let editor = null
|
||||||
@ -34,14 +35,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
extensions: [
|
extensions: [
|
||||||
StarterKit.configure({
|
StarterKit.configure({
|
||||||
heading: { levels: [1, 2, 3] },
|
heading: { levels: [1, 2, 3] },
|
||||||
|
link: {
|
||||||
|
openOnClick: false,
|
||||||
|
HTMLAttributes: { rel: 'noopener noreferrer', target: '_blank' },
|
||||||
|
},
|
||||||
|
codeBlock: false,
|
||||||
|
}),
|
||||||
|
CodeBlockLowlight.configure({
|
||||||
|
lowlight: createLowlight(common),
|
||||||
|
defaultLanguage: null,
|
||||||
}),
|
}),
|
||||||
Placeholder.configure({
|
Placeholder.configure({
|
||||||
placeholder: '在此输入正文...',
|
placeholder: '在此输入正文...',
|
||||||
}),
|
}),
|
||||||
Link.configure({
|
|
||||||
openOnClick: false,
|
|
||||||
HTMLAttributes: { rel: 'noopener noreferrer', target: '_blank' },
|
|
||||||
}),
|
|
||||||
Image.configure({
|
Image.configure({
|
||||||
inline: true,
|
inline: true,
|
||||||
}),
|
}),
|
||||||
@ -131,6 +137,55 @@ function setupToolbar() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// ---- 代码块语言选择器 ----
|
||||||
|
const langSelect = document.getElementById('codeLangSelect')
|
||||||
|
if (langSelect) {
|
||||||
|
// 当光标在代码块内时显示语言选择器,移出时隐藏
|
||||||
|
editor.on('selectionUpdate', () => {
|
||||||
|
if (editor.isActive('codeBlock')) {
|
||||||
|
langSelect.style.display = ''
|
||||||
|
const attrs = editor.getAttributes('codeBlock')
|
||||||
|
langSelect.value = attrs.language || ''
|
||||||
|
} else {
|
||||||
|
langSelect.style.display = 'none'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// 语言切换
|
||||||
|
langSelect.addEventListener('change', () => {
|
||||||
|
const lang = langSelect.value
|
||||||
|
editor.chain().focus().updateAttributes('codeBlock', { language: lang || null }).run()
|
||||||
|
editor.commands.focus()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- 代码块语言标签注入 ----
|
||||||
|
// 为每个 <pre> 注入语言标签 DOM 元素(因为 CSS ::before 在 overflow:auto 容器中不可靠)
|
||||||
|
function injectCodeLangLabels() {
|
||||||
|
const editorEl = document.querySelector('#editor-container .tiptap')
|
||||||
|
if (!editorEl) return
|
||||||
|
editorEl.querySelectorAll('pre').forEach(pre => {
|
||||||
|
// 移除旧标签
|
||||||
|
const old = pre.querySelector('.code-lang-label')
|
||||||
|
if (old) old.remove()
|
||||||
|
|
||||||
|
const lang = pre.getAttribute('data-language')
|
||||||
|
if (lang) {
|
||||||
|
const label = document.createElement('div')
|
||||||
|
label.className = 'code-lang-label'
|
||||||
|
label.textContent = lang
|
||||||
|
pre.insertBefore(label, pre.firstChild)
|
||||||
|
pre.style.paddingTop = '32px'
|
||||||
|
} else {
|
||||||
|
pre.style.paddingTop = ''
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
editor.on('update', injectCodeLangLabels)
|
||||||
|
// 初始注入
|
||||||
|
setTimeout(injectCodeLangLabels, 50)
|
||||||
|
|
||||||
editor.on('selectionUpdate', updateActive)
|
editor.on('selectionUpdate', updateActive)
|
||||||
editor.on('update', updateActive)
|
editor.on('update', updateActive)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user