From 50562db72329af76ed07d448af936a7347efac20 Mon Sep 17 00:00:00 2001 From: valentine195 <38669521+valentine195@users.noreply.github.com> Date: Tue, 8 Feb 2022 10:18:33 -0500 Subject: [PATCH] fix: Fixes several issues with Live Preview MSDoc syntax rendering (closes #170) --- src/main.ts | 59 +++++++++------------- src/util/constants.ts | 114 +++++++++++++++++++++++++++++++----------- 2 files changed, 111 insertions(+), 62 deletions(-) diff --git a/src/main.ts b/src/main.ts index 6843c78..28f91b0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,5 @@ import { addIcon, - editorLivePreviewField, - requireApiVersion, - editorViewField, MarkdownPostProcessor, MarkdownPostProcessorContext, MarkdownPreviewRenderer, @@ -29,13 +26,8 @@ import { tokenClassNodeProp } from "@codemirror/stream-parser"; import { Range } from "@codemirror/rangeset"; import { StateEffect, StateField } from "@codemirror/state"; -import { - Admonition, - ObsidianAdmonitionPlugin, - ISettingsData, - AdmonitionIconDefinition -} from "./@types"; -import { getID, getMatches, getParametersFromSource } from "./util"; +import { Admonition, ISettingsData, AdmonitionIconDefinition } from "./@types"; +import { getID, getMatches, getParametersFromSource, MSDOCREGEX } from "./util"; import { ADMONITION_MAP, ADD_ADMONITION_COMMAND_ICON, @@ -362,8 +354,7 @@ export default class ObsidianAdmonition extends Plugin { const firstLine = text[section.lineStart]; if (!/^> \[!.+\]/.test(firstLine)) return; - let [, type, title, col] = - firstLine.match(/^> \[!(\w+)(?::[ ]?(.+))?\](x|\+|\-)?/) ?? []; + let [, type, title, col] = firstLine.match(MSDOCREGEX) ?? []; if ( !type || @@ -510,24 +501,27 @@ export default class ObsidianAdmonition extends Plugin { constructor(editor: EditorView) { this.editor = editor; } - + hash(token: TokenSpec) { + return `from${token.from}to${token.to}`; + } async compute(tokens: TokenSpec[]) { const admonition: Range[] = []; for (let token of tokens) { - let deco = this.cache[token.value]; + let deco = this.cache[this.hash(token)]; if (!deco) { - deco = this.cache[token.value] = Decoration.replace({ - inclusive: true, - widget: new AdmonitionWidget( - token.type, - token.title, - token.collapse, - token.value - ), - block: true, - from: token.from, - to: token.to - }); + deco = this.cache[this.hash(token)] = + Decoration.replace({ + inclusive: true, + widget: new AdmonitionWidget( + token.type, + token.title, + token.collapse, + token.value + ), + block: true, + from: token.from, + to: token.to + }); } admonition.push(deco.range(token.from, token.to)); } @@ -564,7 +558,6 @@ export default class ObsidianAdmonition extends Plugin { update(update: ViewUpdate) { if (update.heightChanged) return; if (!self.data.livePreviewMS) return; - if (!isLivePreview(update.view.state)) { if (this.source == false) { this.source = true; @@ -612,9 +605,8 @@ export default class ObsidianAdmonition extends Plugin { if (!/^> \[!.+\]/.test(line)) return; const [, type, title, col] = - line.match( - /^> \[!(\w+)(?:: (.+))?\](x|\+|\-)?/ - ) ?? []; + line.match(MSDOCREGEX) ?? []; + if (!type || !self.admonitions[type]) return; let collapse; switch (col) { @@ -644,13 +636,12 @@ export default class ObsidianAdmonition extends Plugin { const to = from + line.length + content.length + 1; - targetElements.push({ from, to, - value: content, - title, - type, + value: content?.trim(), + title: title?.trim(), + type: type?.trim(), collapse }); } diff --git a/src/util/constants.ts b/src/util/constants.ts index 25268a3..5e9f23c 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -6,6 +6,8 @@ export const ADD_COMMAND_NAME = Symbol("add-command"); export const REMOVE_ADMONITION_COMMAND_ICON = ``; export const REMOVE_COMMAND_NAME = Symbol("remove-command"); +export const MSDOCREGEX = /^> \[!(\w+)(?::[ ]?(.+)?)?\](x|\+|\-)?/; + export const ADMONITION_MAP: Record = { note: { type: "note", @@ -15,7 +17,9 @@ export const ADMONITION_MAP: Record = { name: "pencil-alt" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, seealso: { type: "note", @@ -25,7 +29,9 @@ export const ADMONITION_MAP: Record = { name: "pencil-alt" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, abstract: { type: "abstract", @@ -35,7 +41,9 @@ export const ADMONITION_MAP: Record = { name: "book" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, summary: { type: "abstract", @@ -45,7 +53,9 @@ export const ADMONITION_MAP: Record = { name: "book" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, tldr: { type: "abstract", @@ -55,7 +65,9 @@ export const ADMONITION_MAP: Record = { name: "book" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, info: { type: "info", @@ -65,7 +77,9 @@ export const ADMONITION_MAP: Record = { name: "info-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, todo: { type: "info", @@ -75,7 +89,9 @@ export const ADMONITION_MAP: Record = { name: "info-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, tip: { type: "tip", @@ -85,7 +101,9 @@ export const ADMONITION_MAP: Record = { name: "fire" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, hint: { type: "tip", @@ -95,7 +113,9 @@ export const ADMONITION_MAP: Record = { name: "fire" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, important: { type: "tip", @@ -105,7 +125,9 @@ export const ADMONITION_MAP: Record = { name: "fire" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, success: { type: "success", @@ -115,7 +137,9 @@ export const ADMONITION_MAP: Record = { name: "check-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, check: { type: "success", @@ -125,7 +149,9 @@ export const ADMONITION_MAP: Record = { name: "check-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, done: { type: "success", @@ -135,7 +161,9 @@ export const ADMONITION_MAP: Record = { name: "check-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, question: { type: "question", @@ -145,7 +173,9 @@ export const ADMONITION_MAP: Record = { name: "question-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, help: { type: "question", @@ -155,7 +185,9 @@ export const ADMONITION_MAP: Record = { name: "question-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, faq: { type: "question", @@ -165,7 +197,9 @@ export const ADMONITION_MAP: Record = { name: "question-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, warning: { type: "warning", @@ -175,7 +209,9 @@ export const ADMONITION_MAP: Record = { name: "exclamation-triangle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, caution: { type: "warning", @@ -185,7 +221,9 @@ export const ADMONITION_MAP: Record = { name: "exclamation-triangle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, attention: { type: "warning", @@ -195,7 +233,9 @@ export const ADMONITION_MAP: Record = { name: "exclamation-triangle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, failure: { type: "failure", @@ -205,7 +245,9 @@ export const ADMONITION_MAP: Record = { name: "times-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, fail: { type: "failure", @@ -215,7 +257,9 @@ export const ADMONITION_MAP: Record = { name: "times-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, missing: { type: "failure", @@ -225,7 +269,9 @@ export const ADMONITION_MAP: Record = { name: "times-circle" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, danger: { type: "danger", @@ -235,7 +281,9 @@ export const ADMONITION_MAP: Record = { name: "bolt" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, error: { type: "danger", @@ -245,7 +293,9 @@ export const ADMONITION_MAP: Record = { name: "bolt" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, bug: { type: "bug", @@ -255,7 +305,9 @@ export const ADMONITION_MAP: Record = { name: "bug" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, example: { type: "example", @@ -265,7 +317,9 @@ export const ADMONITION_MAP: Record = { name: "list-ol" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, quote: { type: "quote", @@ -275,7 +329,9 @@ export const ADMONITION_MAP: Record = { name: "quote-right" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false }, cite: { type: "quote", @@ -285,6 +341,8 @@ export const ADMONITION_MAP: Record = { name: "quote-right" }, command: false, - injectColor: true + injectColor: true, + noTitle: false, + copy: false } };