@@ -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 + t i t l e = " ( [ ^ " ] + ) " /
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
420459class EditorHotkeyHelper {
421460 constructor ( plugin ) {
0 commit comments