Initial implementation of command link arguments (#573)
Initial implementation of command link arguments --------- Co-authored-by: prcrst <p-github@prcr.st> Co-authored-by: Zef Hemel <zef@zef.me>
This commit is contained in:
co-authored by
prcrst
Zef Hemel
parent
a03b211dad
commit
e6f77b12af
@@ -2,6 +2,7 @@ import { Tag } from "../deps.ts";
|
||||
|
||||
export const CommandLinkTag = Tag.define();
|
||||
export const CommandLinkNameTag = Tag.define();
|
||||
export const CommandLinkArgsTag = Tag.define();
|
||||
export const WikiLinkTag = Tag.define();
|
||||
export const WikiLinkPageTag = Tag.define();
|
||||
export const CodeInfoTag = Tag.define();
|
||||
|
||||
@@ -121,3 +121,40 @@ Deno.test("Test multi-status tasks", () => {
|
||||
assertEquals(tasks[1].children![0].children![1].text, "x");
|
||||
assertEquals(tasks[2].children![0].children![1].text, "TODO");
|
||||
});
|
||||
|
||||
const commandLinkSample = `
|
||||
{[Some: Command]}
|
||||
{[Other: Command|Alias]}
|
||||
{[Command: Space | Spaces ]}
|
||||
`;
|
||||
|
||||
Deno.test("Test command links", () => {
|
||||
const lang = buildMarkdown([]);
|
||||
const tree = parse(lang, commandLinkSample);
|
||||
const commands = collectNodesOfType(tree, "CommandLink");
|
||||
console.log("Command links parsed", JSON.stringify(commands, null, 2));
|
||||
assertEquals(commands.length, 3);
|
||||
assertEquals(commands[0].children![1].children![0].text, "Some: Command");
|
||||
assertEquals(commands[1].children![1].children![0].text, "Other: Command");
|
||||
assertEquals(commands[1].children![3].children![0].text, "Alias");
|
||||
assertEquals(commands[2].children![1].children![0].text, "Command: Space ");
|
||||
assertEquals(commands[2].children![3].children![0].text, " Spaces ");
|
||||
});
|
||||
|
||||
const commandLinkArgsSample = `
|
||||
{[Args: Command]("with", "args")}
|
||||
{[Othargs: Command|Args alias]("other", "args", 123)}
|
||||
`;
|
||||
|
||||
Deno.test("Test command link arguments", () => {
|
||||
const lang = buildMarkdown([]);
|
||||
const tree = parse(lang, commandLinkArgsSample);
|
||||
const commands = collectNodesOfType(tree, "CommandLink");
|
||||
assertEquals(commands.length, 2);
|
||||
|
||||
const args1 = findNodeOfType(commands[0], "CommandLinkArgs")
|
||||
assertEquals(args1!.children![0].text, '"with", "args"');
|
||||
|
||||
const args2 = findNodeOfType(commands[1], "CommandLinkArgs")
|
||||
assertEquals(args2!.children![0].text, '"other", "args", 123');
|
||||
});
|
||||
@@ -68,13 +68,14 @@ const WikiLink: MarkdownConfig = {
|
||||
],
|
||||
};
|
||||
|
||||
export const commandLinkRegex = /^\{\[([^\]\|]+)(\|([^\]]+))?\]\}/;
|
||||
export const commandLinkRegex = /^\{\[([^\]\|]+)(\|([^\]]+))?\](\(([^\)]+)\))?\}/;
|
||||
|
||||
const CommandLink: MarkdownConfig = {
|
||||
defineNodes: [
|
||||
{ name: "CommandLink", style: { "CommandLink/...": ct.CommandLinkTag } },
|
||||
{ name: "CommandLinkName", style: ct.CommandLinkNameTag },
|
||||
{ name: "CommandLinkAlias", style: ct.CommandLinkNameTag },
|
||||
{ name: "CommandLinkArgs", style: ct.CommandLinkArgsTag },
|
||||
{ name: "CommandLinkMark", style: t.processingInstruction },
|
||||
],
|
||||
parseInline: [
|
||||
@@ -88,7 +89,7 @@ const CommandLink: MarkdownConfig = {
|
||||
) {
|
||||
return -1;
|
||||
}
|
||||
const [fullMatch, command, pipePart, label] = match;
|
||||
const [fullMatch, command, pipePart, label, argsPart, args] = match;
|
||||
const endPos = pos + fullMatch.length;
|
||||
|
||||
let aliasElts: any[] = [];
|
||||
@@ -103,11 +104,26 @@ const CommandLink: MarkdownConfig = {
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
let argsElts: any[] = [];
|
||||
if (argsPart) {
|
||||
const argsStartPos = pos + 2 + command.length + (pipePart?.length ?? 0);
|
||||
argsElts = [
|
||||
cx.elt("CommandLinkMark", argsStartPos, argsStartPos + 2),
|
||||
cx.elt(
|
||||
"CommandLinkArgs",
|
||||
argsStartPos + 2,
|
||||
argsStartPos + 2 + args.length,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return cx.addElement(
|
||||
cx.elt("CommandLink", pos, endPos, [
|
||||
cx.elt("CommandLinkMark", pos, pos + 2),
|
||||
cx.elt("CommandLinkName", pos + 2, pos + 2 + command.length),
|
||||
...aliasElts,
|
||||
...argsElts,
|
||||
cx.elt("CommandLinkMark", endPos - 2, endPos),
|
||||
]),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user