Skip to content

Commit 53922a3

Browse files
committed
fix #24
1 parent 502350e commit 53922a3

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/codemirror/Cm6_ViewPlugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function createCm6Plugin(plugin: ShikiPlugin) {
129129
}
130130

131131
if (props.has('HyperMD-codeblock-end')) {
132-
if (state.length > 0) {
132+
if (state.length > 0 && lang !== '') {
133133
const start = state[0].from;
134134
const end = state[state.length - 1].to;
135135

@@ -202,6 +202,10 @@ export function createCm6Plugin(plugin: ShikiPlugin) {
202202
* @param content
203203
*/
204204
buildDecorations(from: number, to: number, language: string, content: string): Range<Decoration>[] {
205+
if (language === '') {
206+
return [];
207+
}
208+
205209
const highlight = plugin.highlighter.getHighlightTokens(content, language);
206210

207211
if (!highlight) {

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,27 @@ export default class ShikiPlugin extends Plugin {
8484
this.registerMarkdownCodeBlockProcessor(
8585
language,
8686
async (source, el, ctx) => {
87+
// @ts-expect-error
88+
const isReadingMode = ctx.containerEl.hasClass('markdown-preview-section') || ctx.containerEl.hasClass('markdown-preview-view');
89+
// this seems to indicate whether we are in the pdf export mode
90+
// sadly there is no section info in this mode
91+
// thus we can't check if the codeblock is at the start of the note and thus frontmatter
92+
// const isPdfExport = ctx.displayMode === true;
93+
8794
// this is so that we leave the hidden frontmatter code block in reading mode alone
88-
if (language === 'yaml' && ctx.frontmatter) {
95+
if (language === 'yaml' && isReadingMode && ctx.frontmatter) {
8996
const sectionInfo = ctx.getSectionInfo(el);
97+
9098
if (sectionInfo && sectionInfo.lineStart === 0) {
91-
el.addClass('shiki-hide-in-reading-mode');
99+
return;
92100
}
93101
}
94102

95103
const codeBlock = new CodeBlock(this, el, source, language, ctx);
96104

97105
ctx.addChild(codeBlock);
98106
},
99-
-1,
107+
1000,
100108
);
101109
} catch (e) {
102110
console.warn(`Failed to register code block processor for ${language}`, e);

0 commit comments

Comments
 (0)