Files
plainleaf/client/codemirror/attribute.ts
T
2026-01-08 14:05:26 +01:00

43 lines
1.1 KiB
TypeScript

import { syntaxTree } from "@codemirror/language";
import { Decoration } from "@codemirror/view";
import { decoratorStateField } from "./util.ts";
export function attributePlugin() {
return decoratorStateField((state) => {
const widgets: any[] = [];
syntaxTree(state).iterate({
enter: (node) => {
if (node.type.name !== "Attribute") {
return;
}
const attributeText = state.sliceDoc(node.from, node.to);
// attribute text will have a format of [hell: bla bla bla]
const attributeName = attributeText.slice(
1,
attributeText.indexOf(":"),
);
const attributeValue = attributeText.slice(
attributeText.indexOf(":") + 1,
attributeText.length - 1,
).trim();
// Wrap the tag in html anchor element
widgets.push(
Decoration.mark({
tagName: "span",
class: "sb-attribute",
attributes: {
[`data-${attributeName}`]: attributeValue,
},
}).range(node.from, node.to),
);
},
});
return Decoration.set(widgets, true);
});
}