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:
@ -128,9 +128,9 @@
|
||||
/* 代码块基础样式 */
|
||||
.post-detail-body pre {
|
||||
position: relative;
|
||||
background: #f3f4f6;
|
||||
background: #1a2332;
|
||||
color: #e2e8f0;
|
||||
padding: 12px 16px;
|
||||
padding-top: 36px;
|
||||
border-radius: 8px;
|
||||
overflow-x: auto;
|
||||
font-size: 14px;
|
||||
@ -138,19 +138,21 @@
|
||||
font-family: 'Menlo', 'Consolas', 'Monaco', 'Liberation Mono', monospace;
|
||||
}
|
||||
|
||||
/* 通过 JS 动态注入语言标签,因为 CSS attr() 无法从 class 中提取 */
|
||||
.code-lang-label {
|
||||
/* 代码块语言标签(JS 动态注入) */
|
||||
.post-detail-body pre .code-lang-label {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
padding: 8px 16px !important;
|
||||
background: #e5e7eb;
|
||||
color: #6b7280;
|
||||
padding: 4px 16px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: #94a3b8;
|
||||
background: #2c3e50;
|
||||
border-radius: 8px 8px 0 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
|
||||
pointer-events: none;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.post-detail-body pre code {
|
||||
@ -158,6 +160,8 @@
|
||||
border: none !important;
|
||||
padding: 0 !important;
|
||||
font-size: inherit !important;
|
||||
white-space: pre;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.post-detail-body code {
|
||||
@ -167,6 +171,43 @@
|
||||
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 {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
|
||||
@ -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