Skip to content

Commit 82440aa

Browse files
authored
Merge pull request #1184 from DoubleW2w/feature/add-codeblock-title
Feat(code_block_title): 添加代码块标题插件及相关设置
2 parents 07c485c + 0891679 commit 82440aa

7 files changed

Lines changed: 53 additions & 1 deletion

File tree

plugin/fence_enhance/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class FenceEnhancePlugin extends BasePlugin {
3232
if (this.config.HIGHLIGHT_BY_LANGUAGE) {
3333
new HighlightHelper(this).process()
3434
}
35+
if (this.config.ENABLE_CODE_TITLE) {
36+
codeTitle(this)
37+
}
3538
if (this.config.ENABLE_LANGUAGE_FOLD) {
3639
await foldLanguage(this)
3740
}
@@ -416,6 +419,42 @@ const visibleTabs = ({ utils }) => {
416419
utils.insertStyleFile("fence-enhance-visible-tabs", "./plugin/fence_enhance/resource/visible-tabs.css")
417420
}
418421

422+
const codeTitle = ({ utils }) => {
423+
const className = "plugin-code-title"
424+
const REGEX = /.+?\s+title="([^"]+)"/
425+
426+
const rerender = (cid) => {
427+
if (!cid) return
428+
const fence = document.querySelector(`.md-fences[cid="${cid}"]`)
429+
if (!fence) return
430+
431+
let barEl = fence.querySelector(`.${className}`)
432+
const title = fence.getAttribute("lang")?.match(REGEX)?.[1]
433+
if (!title) {
434+
barEl?.remove()
435+
} else {
436+
if (!barEl) {
437+
barEl = document.createElement("div")
438+
barEl.className = className
439+
fence.prepend(barEl)
440+
}
441+
barEl.textContent = title
442+
}
443+
}
444+
445+
utils.insertStyle("plugin-fence-enhance-code-title",
446+
`.md-fences .${className} {
447+
padding: 6px 14px;
448+
border-bottom: 1px solid var(--code-title-border, #d0d0d0);
449+
margin-bottom: 6px;
450+
color: var(--code-title-text, currentColor);
451+
user-select: none;
452+
}`)
453+
454+
utils.eventHub.addEventListener(utils.eventHub.eventType.afterAddCodeBlock, cid => rerender(cid))
455+
utils.eventHub.addEventListener(utils.eventHub.eventType.afterUpdateCodeBlockLang, ([node] = []) => rerender(node?.cid))
456+
}
457+
419458
// doc: https://codemirror.net/5/doc/manual.html
420459
class EditorHotkeyHelper {
421460
constructor(plugin) {

plugin/global/locales/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,14 @@
715715
"$label.HIGHLIGHT_LINE_COLOR_ON_HOVER": "Hover Background Color",
716716
"$label.HIGHLIGHT_ON_FOCUS": "Highlight on Focus",
717717
"$label.HIGHLIGHT_LINE_COLOR_ON_FOCUS": "Focus Background Color",
718+
"$label.ENABLE_CODE_TITLE": "Code Title",
718719
"$title.buttonGeneral": "Button",
719720
"$title.functionButtons": "Function Buttons",
720721
"$title.buttonHotkeys": "Code Block Hotkeys",
721722
"$title.lineHighlighting": "Line Highlighting",
722723
"$tooltip.viewCodeMirrorKeymapsManual": "CodeMirror keymaps reference",
723-
"$tooltip.viewVitePressLineHighlighting": "VitePress syntax reference",
724+
"$tooltip.viewVitePressLineHighlighting": "Syntax reference",
725+
"$tooltip.viewVuePressCodeTitle": "Syntax reference",
724726
"$tooltip.viewFocusLineHighlightingEffect": "Preview effect",
725727
"$tooltip.viewSideBySideEffect": "Preview effect",
726728
"$tooltip.viewVisibleTabsEffect": "Preview effect",

plugin/global/locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,14 @@
715715
"$label.HIGHLIGHT_LINE_COLOR_ON_HOVER": "高亮背景色",
716716
"$label.HIGHLIGHT_ON_FOCUS": "光标聚焦高亮",
717717
"$label.HIGHLIGHT_LINE_COLOR_ON_FOCUS": "高亮背景色",
718+
"$label.ENABLE_CODE_TITLE": "代码标题",
718719
"$title.buttonGeneral": "按钮",
719720
"$title.functionButtons": "功能按钮",
720721
"$title.buttonHotkeys": "代码块快捷键",
721722
"$title.lineHighlighting": "行高亮",
722723
"$tooltip.viewCodeMirrorKeymapsManual": "CodeMirror 风格的快捷键",
723724
"$tooltip.viewVitePressLineHighlighting": "行高亮语法",
725+
"$tooltip.viewVuePressCodeTitle": "代码标题语法",
724726
"$tooltip.viewFocusLineHighlightingEffect": "查看效果",
725727
"$tooltip.viewSideBySideEffect": "查看效果",
726728
"$tooltip.viewVisibleTabsEffect": "查看效果",

plugin/global/locales/zh-TW.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,14 @@
715715
"$label.HIGHLIGHT_LINE_COLOR_ON_HOVER": "高亮背景色",
716716
"$label.HIGHLIGHT_ON_FOCUS": "游標聚焦高亮",
717717
"$label.HIGHLIGHT_LINE_COLOR_ON_FOCUS": "高亮背景色",
718+
"$label.ENABLE_CODE_TITLE": "程式碼區塊標題",
718719
"$title.buttonGeneral": "按鈕",
719720
"$title.functionButtons": "功能按鈕",
720721
"$title.buttonHotkeys": "程式碼區塊快捷鍵",
721722
"$title.lineHighlighting": "行高亮",
722723
"$tooltip.viewCodeMirrorKeymapsManual": "CodeMirror 風格的快捷鍵",
723724
"$tooltip.viewVitePressLineHighlighting": "行高亮語法",
725+
"$tooltip.viewVuePressCodeTitle": "程式碼區塊標題語法",
724726
"$tooltip.viewFocusLineHighlightingEffect": "查看效果",
725727
"$tooltip.viewSideBySideEffect": "查看效果",
726728
"$tooltip.viewVisibleTabsEffect": "查看效果",

plugin/global/settings/settings.default.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,11 @@ HIGHLIGHT_ON_FOCUS = false
12301230
# Highlight line background color on cursor focus
12311231
HIGHLIGHT_LINE_COLOR_ON_FOCUS = "rgba(142, 150, 170, 0.14)"
12321232

1233+
# =========== [ Code Title ] ===========
1234+
# Enable code title
1235+
# See: https://vuepress.vuejs.org/guide/markdown.html#code-blocks
1236+
ENABLE_CODE_TITLE = false
1237+
12331238
# =========== [ Advanced ] ===========
12341239
# Auto-align wrapped lines: Maintain proper indentation for long, wrapped text lines. Note: High rendering performance cost
12351240
# See: https://github.com/codemirror/codemirror5/issues/2085

plugin/preferences/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = (plugin) => {
1717
viewMarkdownlintRules: openUrl("https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md"),
1818
viewCodeMirrorKeymapsManual: openUrl("https://codemirror.net/5/doc/manual.html#keymaps"),
1919
viewVitePressLineHighlighting: openUrl(`https://vitepress.dev/${i18n.locale === "zh-CN" ? "zh/" : ""}guide/markdown#line-highlighting-in-code-blocks`),
20+
viewVuePressCodeTitle: openUrl("https://vuepress.vuejs.org/guide/markdown.html#code-title"),
2021
viewFocusLineHighlightingEffect: openUrl("https://codemirror.net/5/demo/activeline.html"),
2122
viewAbcVisualOptionsManual: openUrl("https://docs.abcjs.net/visual/render-abc-options.html"),
2223
viewVisibleTabsEffect: openUrl("https://codemirror.net/5/demo/visibletabs.html"),

plugin/preferences/schemas.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ const schema_fence_enhance = () => [
590590
Switch("HIGHLIGHT_ON_HOVER"),
591591
Text("HIGHLIGHT_LINE_COLOR_ON_HOVER").ShowIf(When.true("HIGHLIGHT_ON_HOVER")),
592592
),
593+
Switch("ENABLE_CODE_TITLE").ActionTooltip("viewVuePressCodeTitle"),
593594
Group("advanced",
594595
Switch("SIDE_BY_SIDE_VIEW").Tooltip("stylisticConfusion").ActionTooltip("viewSideBySideEffect"),
595596
Switch("VISIBLE_TABS").ActionTooltip("viewVisibleTabsEffect"),

0 commit comments

Comments
 (0)