Skip to content

Commit 87d7d98

Browse files
authored
fix(CodeBlock): apply syntax highlighting on initial render (#902)
1 parent e26239d commit 87d7d98

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

demo/stories/markdown/markup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Sample text here...
252252
253253
Syntax highlighting
254254
255-
\`\`\` js
255+
\`\`\`javascript
256256
var foo = function (bar) {
257257
return bar++;
258258
};

src/extensions/markdown/CodeBlock/CodeBlockHighlight/CodeBlockHighlight.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {Plugin, PluginKey} from 'prosemirror-state';
77
import type {Step} from 'prosemirror-transform';
88
// @ts-ignore // TODO: fix cjs build
99
import {findChildrenByType} from 'prosemirror-utils';
10-
import {Decoration, DecorationSet} from 'prosemirror-view';
10+
import {Decoration, DecorationSet, type EditorView} from 'prosemirror-view';
1111

1212
import type {ExtensionAuto} from '../../../../core';
1313
import {capitalize} from '../../../../lodash';
@@ -64,6 +64,7 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
6464

6565
builder.addPlugin(() => {
6666
let modulesLoaded = false;
67+
let view: EditorView | null = null;
6768

6869
const selectItems: LangSelectItem[] = [];
6970
const mapping: Record<string, string> = {};
@@ -90,6 +91,8 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
9091
}
9192
}
9293
}
94+
95+
view?.dispatch(view.state.tr.setMeta(key, {modulesLoaded}));
9396
}
9497
});
9598
return getDecorations(state.doc);
@@ -99,6 +102,10 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
99102
return DecorationSet.empty;
100103
}
101104

105+
if (tr.getMeta(key)?.modulesLoaded) {
106+
return getDecorations(tr.doc);
107+
}
108+
102109
if (tr.docChanged) {
103110
const oldNodeName = oldState.selection.$head.parent.type.name;
104111
const newNodeName = newState.selection.$head.parent.type.name;
@@ -143,13 +150,15 @@ export const CodeBlockHighlight: ExtensionAuto<CodeBlockHighlightOptions> = (bui
143150
return decos.map(tr.mapping, tr.doc);
144151
},
145152
},
146-
view: (view) =>
147-
codeLangSelectTooltipViewCreator(
153+
view: (v) => {
154+
view = v;
155+
return codeLangSelectTooltipViewCreator(
148156
view,
149157
selectItems,
150158
mapping,
151159
Boolean(opts.lineNumbers?.enabled),
152-
),
160+
);
161+
},
153162
props: {
154163
decorations: (state) => {
155164
return key.getState(state);
4.7 KB
Loading
4.28 KB
Loading

0 commit comments

Comments
 (0)