Fix admonitions without style (#797)

pull/770/head
onespaceman 2024-03-07 12:52:04 -05:00 committed by GitHub
parent adf7d2e785
commit 2705fff52e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 5 deletions

View File

@ -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({