Fix admonitions without style (#797)
parent
adf7d2e785
commit
2705fff52e
|
@ -40,7 +40,7 @@ class AdmonitionIconWidget extends WidgetType {
|
||||||
type AdmonitionFields = {
|
type AdmonitionFields = {
|
||||||
preSpaces: string;
|
preSpaces: string;
|
||||||
admonitionType: string;
|
admonitionType: string;
|
||||||
postSyntax: string
|
postSyntax: string;
|
||||||
postSpaces: string;
|
postSpaces: string;
|
||||||
admonitionTitle: string;
|
admonitionTitle: string;
|
||||||
admonitionContent: string;
|
admonitionContent: string;
|
||||||
|
@ -89,6 +89,13 @@ export function admonitionPlugin(editor: Client) {
|
||||||
return decoratorStateField((state: EditorState) => {
|
return decoratorStateField((state: EditorState) => {
|
||||||
const widgets: any[] = [];
|
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({
|
syntaxTree(state).iterate({
|
||||||
enter: (node: SyntaxNodeRef) => {
|
enter: (node: SyntaxNodeRef) => {
|
||||||
const { type, from, to } = node;
|
const { type, from, to } = node;
|
||||||
|
@ -105,7 +112,12 @@ export function admonitionPlugin(editor: Client) {
|
||||||
return;
|
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.
|
// 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
|
// 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.
|
// icon further down.
|
||||||
const iconRange = {
|
const iconRange = {
|
||||||
from: from + 1,
|
from: from + 1,
|
||||||
to: from + preSpaces.length + 2 + admonitionType.length + postSyntax.length +
|
to: from + preSpaces.length + 2 + admonitionType.length +
|
||||||
|
postSyntax.length +
|
||||||
postSpaces.length + 1,
|
postSpaces.length + 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
const classes = ["sb-admonition"];
|
|
||||||
|
|
||||||
// The first div is the title, attach title css class
|
// The first div is the title, attach title css class
|
||||||
widgets.push(
|
widgets.push(
|
||||||
Decoration.line({
|
Decoration.line({
|
||||||
|
|
Loading…
Reference in New Issue