Skip to content

Commit

Permalink
fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
mProjectsCode committed Sep 20, 2024
1 parent 502350e commit 53922a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/codemirror/Cm6_ViewPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function createCm6Plugin(plugin: ShikiPlugin) {
}

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

Expand Down Expand Up @@ -202,6 +202,10 @@ export function createCm6Plugin(plugin: ShikiPlugin) {
* @param content
*/
buildDecorations(from: number, to: number, language: string, content: string): Range<Decoration>[] {
if (language === '') {
return [];
}

const highlight = plugin.highlighter.getHighlightTokens(content, language);

if (!highlight) {
Expand Down
14 changes: 11 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,27 @@ export default class ShikiPlugin extends Plugin {
this.registerMarkdownCodeBlockProcessor(
language,
async (source, el, ctx) => {
// @ts-expect-error
const isReadingMode = ctx.containerEl.hasClass('markdown-preview-section') || ctx.containerEl.hasClass('markdown-preview-view');
// this seems to indicate whether we are in the pdf export mode
// sadly there is no section info in this mode
// thus we can't check if the codeblock is at the start of the note and thus frontmatter
// const isPdfExport = ctx.displayMode === true;

// this is so that we leave the hidden frontmatter code block in reading mode alone
if (language === 'yaml' && ctx.frontmatter) {
if (language === 'yaml' && isReadingMode && ctx.frontmatter) {
const sectionInfo = ctx.getSectionInfo(el);

if (sectionInfo && sectionInfo.lineStart === 0) {
el.addClass('shiki-hide-in-reading-mode');
return;
}
}

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

ctx.addChild(codeBlock);
},
-1,
1000,
);
} catch (e) {
console.warn(`Failed to register code block processor for ${language}`, e);
Expand Down

0 comments on commit 53922a3

Please sign in to comment.