From 2705fff52eeec39312cb4437686fd00e6fc24d49 Mon Sep 17 00:00:00 2001 From: onespaceman Date: Thu, 7 Mar 2024 12:52:04 -0500 Subject: [PATCH] Fix admonitions without style (#797) --- web/cm_plugins/admonition.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/web/cm_plugins/admonition.ts b/web/cm_plugins/admonition.ts index efd08463..2c8c804a 100644 --- a/web/cm_plugins/admonition.ts +++ b/web/cm_plugins/admonition.ts @@ -40,7 +40,7 @@ class AdmonitionIconWidget extends WidgetType { type AdmonitionFields = { preSpaces: string; admonitionType: string; - postSyntax: string + postSyntax: string; postSpaces: string; admonitionTitle: string; admonitionContent: string; @@ -89,6 +89,13 @@ export function admonitionPlugin(editor: Client) { return decoratorStateField((state: EditorState) => { const widgets: any[] = []; + // Get admonition styles from stylesheets + const allStyles = [...document.styleSheets] + .map((styleSheet) => { + return [...styleSheet.cssRules].map((rule) => rule.cssText).join(""); + }).filter(Boolean).join("\n"); + const admonitionStyles = allStyles.match(/(?<=admonition=").*?(?=")/g); + syntaxTree(state).iterate({ enter: (node: SyntaxNodeRef) => { const { type, from, to } = node; @@ -105,7 +112,12 @@ export function admonitionPlugin(editor: Client) { return; } - const { preSpaces, admonitionType, postSyntax, postSpaces } = extractedFields; + const { preSpaces, admonitionType, postSyntax, postSpaces } = + extractedFields; + + if (!admonitionStyles?.includes(admonitionType)) { + return; + } // A blockquote is actually rendered as many divs, one per line. // We need to keep track of the `from` offsets here, so we can attach css @@ -122,12 +134,11 @@ export function admonitionPlugin(editor: Client) { // icon further down. const iconRange = { from: from + 1, - to: from + preSpaces.length + 2 + admonitionType.length + postSyntax.length + + to: from + preSpaces.length + 2 + admonitionType.length + + postSyntax.length + postSpaces.length + 1, }; - const classes = ["sb-admonition"]; - // The first div is the title, attach title css class widgets.push( Decoration.line({