diff --git a/.changes/only-live-preview.md b/.changes/only-live-preview.md new file mode 100644 index 0000000..dafd751 --- /dev/null +++ b/.changes/only-live-preview.md @@ -0,0 +1,5 @@ +--- +"obsidian-css-inlay-colors": patch:fix +--- + +Disable the plugin in source mode diff --git a/src/live.ts b/src/live.ts index 8120445..de645bc 100644 --- a/src/live.ts +++ b/src/live.ts @@ -32,7 +32,7 @@ class CSSColorInlayWidget extends WidgetType { } } -export function inlayExtension() { +export const inlayExtension = () => { return ViewPlugin.fromClass( class { decorations: DecorationSet = Decoration.none @@ -57,7 +57,11 @@ export function inlayExtension() { ) } -function createColorWidgets(view: EditorView) { +const createColorWidgets = (view: EditorView) => { + // Only create widgets in live preview mode + if (!view.dom.parentElement?.classList.contains('is-live-preview')) + return Decoration.none + const widgets: Range[] = [] for (const { from, to } of view.visibleRanges) { @@ -69,7 +73,11 @@ function createColorWidgets(view: EditorView) { const color = view.state.sliceDoc(node.from, node.to) // Not a valid color - if (parse(color) === undefined) return + try { + if (parse(color) === undefined) return + } catch { + return + } const deco = Decoration.widget({ side: 1, diff --git a/src/preview.ts b/src/preview.ts index ff22c5a..5057183 100644 --- a/src/preview.ts +++ b/src/preview.ts @@ -1,11 +1,15 @@ import { parse } from 'culori' -export function inlayPostProcessor(el: HTMLElement) { +export const inlayPostProcessor = (el: HTMLElement) => { for (const code of el.findAll('code')) { const color = code.innerText.trim() // Not a valid color - if (parse(color) === undefined) return + try { + if (parse(color) === undefined) return + } catch { + return + } code.createSpan({ prepend: true,