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:
@ -6,11 +6,12 @@
|
||||
*
|
||||
* 安全模型:Tiptap → HTML(富文本)→ 后端 bluemonday 消毒 → 存储
|
||||
*/
|
||||
import { Editor } from 'https://esm.sh/@tiptap/core@3.13.0'
|
||||
import StarterKit from 'https://esm.sh/@tiptap/starter-kit@3.13.0'
|
||||
import Placeholder from 'https://esm.sh/@tiptap/extension-placeholder@3.13.0'
|
||||
import Link from 'https://esm.sh/@tiptap/extension-link@3.13.0'
|
||||
import Image from 'https://esm.sh/@tiptap/extension-image@3.13.0'
|
||||
import { Editor } from '@tiptap/core'
|
||||
import StarterKit from '@tiptap/starter-kit'
|
||||
import Placeholder from '@tiptap/extension-placeholder'
|
||||
import Image from '@tiptap/extension-image'
|
||||
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
|
||||
import { common, createLowlight } from 'lowlight'
|
||||
|
||||
// ---- 状态 ----
|
||||
let editor = null
|
||||
@ -34,14 +35,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
extensions: [
|
||||
StarterKit.configure({
|
||||
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: '在此输入正文...',
|
||||
}),
|
||||
Link.configure({
|
||||
openOnClick: false,
|
||||
HTMLAttributes: { rel: 'noopener noreferrer', target: '_blank' },
|
||||
}),
|
||||
Image.configure({
|
||||
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('update', updateActive)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user