Skip to content

Commit 5d873e3

Browse files
committed
#162 - Fixed incorrect inline code color when using subscript
1 parent fbaa299 commit 5d873e3

2 files changed

Lines changed: 75 additions & 69 deletions

File tree

‎main.js‎

Lines changed: 57 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/EditorView/InlineCode.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import { InlineCodeRenderer } from "../ReadingView/InlineCodeRenderer";
1616

1717
export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings: CodeblockCustomizerSettings) {
1818
const inlineCodeViewPlugin = ViewPlugin.fromClass(class {
19-
decorations: DecorationSet;
19+
decorations: DecorationSet = Decoration.none;
20+
// separate set with higher precedence ==> text highlight spans are nested inside the syntax token spans and their text style wins (fix for #162)
21+
textHighlights: DecorationSet = Decoration.none;
2022
prevEnableSyntaxHighlight: boolean;
2123
prevUsePrismHighlight: boolean;
2224
prevDisableEditorSyntaxHighlight: boolean;
2325
prevPrismLoaded: boolean;
2426

2527
constructor(view: EditorView) {
26-
this.decorations = this.buildDecorations(view);
28+
this.buildDecorations(view);
2729
this.prevEnableSyntaxHighlight = settings.pluginSettings.inlineCode.enableSyntaxHighlight;
2830
this.prevUsePrismHighlight = settings.pluginSettings.codeblock.usePrismHighlight;
2931
this.prevDisableEditorSyntaxHighlight = settings.pluginSettings.common.disableEditorSyntaxHighlight;
@@ -38,19 +40,21 @@ export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings:
3840
this.prevUsePrismHighlight != settings.pluginSettings.codeblock.usePrismHighlight ||
3941
this.prevDisableEditorSyntaxHighlight != settings.pluginSettings.common.disableEditorSyntaxHighlight ||
4042
this.prevPrismLoaded != prismLoaded) {
41-
this.decorations = this.buildDecorations(update.view);
43+
this.buildDecorations(update.view);
4244
this.prevEnableSyntaxHighlight = settings.pluginSettings.inlineCode.enableSyntaxHighlight;
4345
this.prevUsePrismHighlight = settings.pluginSettings.codeblock.usePrismHighlight;
4446
this.prevDisableEditorSyntaxHighlight = settings.pluginSettings.common.disableEditorSyntaxHighlight;
4547
this.prevPrismLoaded = prismLoaded;
4648
}
4749
}
4850

49-
buildDecorations(view: EditorView): DecorationSet {
51+
buildDecorations(view: EditorView) {
52+
this.decorations = this.textHighlights = Decoration.none;
5053
if (!settings.pluginSettings.common.enableInSourceMode && isSourceMode(view.state))
51-
return Decoration.none;
54+
return;
5255

5356
const decorations: Array<Range<Decoration>> = [];
57+
const highlights: Array<Range<Decoration>> = [];
5458
// a line can belong to more than one visible range (CodeMirror splits them at bigger widgets, e.g. a rendered HTML inline code) ==> decorate each HTML inline code only once
5559
const decoratedHtmlInlineCodes = new Set<number>();
5660

@@ -62,7 +66,7 @@ export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings:
6266
return;
6367

6468
decorations.push(Decoration.mark({ class: "codeblock-customizer-inline-code-wrapper" }).range(node.from, node.to));
65-
decorateInlineCode(view, node, decorations);
69+
decorateInlineCode(view, node, decorations, highlights);
6670
},
6771
});
6872

@@ -74,18 +78,20 @@ export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings:
7478
decoratedHtmlInlineCodes.add(node.from);
7579

7680
decorations.push(Decoration.mark({ class: "codeblock-customizer-inline-code-wrapper codeblock-customizer-html-inline-code" }).range(node.from, node.to));
77-
const { codeFrom, hasIcon } = decorateInlineCode(view, node, decorations, node.parameters, true);
81+
const { codeFrom, hasIcon } = decorateInlineCode(view, node, decorations, highlights, node.parameters, true);
7882
// the content of the HTML element is not formatted as code by Obsidian. Only the visible code, so the icon (which replaces the prefix) is next to it, as for backticks
7983
if (codeFrom < node.to) {
8084
decorations.push(Decoration.mark({ class: `cm-inline-code${hasIcon ? ' codeblock-customizer-html-inline-code-after-icon' : ''}` }).range(codeFrom, node.to));
8185
}
8286
}
8387
}
8488
}
85-
return RangeSet.of(decorations, true);
89+
this.decorations = RangeSet.of(decorations, true);
90+
this.textHighlights = RangeSet.of(highlights, true);
8691
}
8792
}, {
88-
decorations: v => v.decorations,
93+
decorations: v => v.textHighlights,
94+
provide: p => EditorView.decorations.of(view => view.plugin(p)?.decorations ?? Decoration.none),
8995
eventHandlers: {
9096
// use mousedown instead of click (#185)
9197
mousedown: (event, view) => {
@@ -299,7 +305,7 @@ export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings:
299305
return result;
300306
}// findHtmlInlineCodesInRange
301307

302-
function decorateInlineCode(view: EditorView, node: { from: number, to: number }, decorations: Array<Range<Decoration>>, attributeParameters = '', isHtml = false): { codeFrom: number, hasIcon: boolean } {
308+
function decorateInlineCode(view: EditorView, node: { from: number, to: number }, decorations: Array<Range<Decoration>>, highlights: Array<Range<Decoration>>, attributeParameters = '', isHtml = false): { codeFrom: number, hasIcon: boolean } {
303309
const selection = view.state.selection.main;
304310
const inlineCodeText = view.state.sliceDoc(node.from, node.to);
305311
let match = inlineCodeText.match(INLINE_CODE_LANG_REGEX);
@@ -369,7 +375,7 @@ export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings:
369375

370376
if (!settings.pluginSettings.inlineCode.enableSyntaxHighlight || settings.pluginSettings.common.disableEditorSyntaxHighlight) {
371377
const prefixHidden = addPrefixDecoration(false);
372-
addTextHighlightDecorations(code, codeStartPos, inlineParams, decorations);
378+
addTextHighlightDecorations(code, codeStartPos, inlineParams, highlights);
373379
return { codeFrom: prefixHidden ? codeStartPos : node.from, hasIcon };
374380
}
375381

@@ -402,7 +408,7 @@ export function inlineCodeExtension(plugin: CodeBlockCustomizerPlugin, settings:
402408
}
403409
}
404410

405-
addTextHighlightDecorations(code, codeStartPos, inlineParams, decorations);
411+
addTextHighlightDecorations(code, codeStartPos, inlineParams, highlights);
406412
return { codeFrom: prefixHidden ? codeStartPos : node.from, hasIcon };
407413
}// decorateInlineCode
408414

0 commit comments

Comments
 (0)