Work on inline attributes

This commit is contained in:
Zef Hemel
2023-07-24 19:54:31 +02:00
parent a1c623b1f5
commit 2b494f263e
12 changed files with 211 additions and 16 deletions
+4
View File
@@ -18,3 +18,7 @@ export const DirectiveTag = Tag.define();
export const DirectiveStartTag = Tag.define();
export const DirectiveEndTag = Tag.define();
export const DirectiveProgramTag = Tag.define();
export const AttributeTag = Tag.define();
export const AttributeNameTag = Tag.define();
export const AttributeValueTag = Tag.define();
+13
View File
@@ -89,3 +89,16 @@ Deno.test("Test directive parser", () => {
tree = parse(lang, orderByExample);
console.log("Tree", JSON.stringify(tree, null, 2));
});
const inlineAttributeSample = `
Hello there [a link](http://zef.plus) and [age:: 100]
`;
Deno.test("Test inline attribute syntax", () => {
const lang = buildMarkdown([]);
const tree = parse(lang, inlineAttributeSample);
const nameNode = findNodeOfType(tree, "AttributeName");
assertEquals(nameNode?.children![0].text, "age");
const valueNode = findNodeOfType(tree, "AttributeValue");
assertEquals(valueNode?.children![0].text, "100");
});
+50
View File
@@ -142,6 +142,55 @@ export const Highlight: MarkdownConfig = {
],
};
export const attributeRegex = /^\[([^:]+)(::\s*)([^\]]+)\]/;
export const Attribute: MarkdownConfig = {
defineNodes: [
{ name: "Attribute", style: { "Attribute/...": ct.AttributeTag } },
{ name: "AttributeName", style: ct.AttributeNameTag },
{ name: "AttributeValue", style: ct.AttributeValueTag },
{ name: "AttributeMark", style: t.processingInstruction },
{ name: "AttributeColon", style: t.processingInstruction },
],
parseInline: [
{
name: "Attribute",
parse(cx, next, pos) {
let match: RegExpMatchArray | null;
if (
next != 91 /* '[' */ ||
// and match the whole thing
!(match = attributeRegex.exec(cx.slice(pos, cx.end)))
) {
return -1;
}
const [fullMatch, attributeName, attributeColon, attributeValue] =
match;
const endPos = pos + fullMatch.length;
return cx.addElement(
cx.elt("Attribute", pos, endPos, [
cx.elt("AttributeMark", pos, pos + 1), // [
cx.elt("AttributeName", pos + 1, pos + 1 + attributeName.length),
cx.elt(
"AttributeColon",
pos + 1 + attributeName.length,
pos + 1 + attributeName.length + attributeColon.length,
),
cx.elt(
"AttributeValue",
pos + 1 + attributeName.length + attributeColon.length,
endPos - 1,
),
cx.elt("AttributeMark", endPos - 1, endPos), // [
]),
);
},
after: "Emphasis",
},
],
};
class CommentParser implements LeafBlockParser {
nextLine() {
return false;
@@ -343,6 +392,7 @@ export default function buildMarkdown(mdExtensions: MDExt[]): Language {
extensions: [
WikiLink,
CommandLink,
Attribute,
FrontMatter,
Directive,
TaskList,