Refactor: Merging common/ into web/

This commit is contained in:
Zef Hemel
2025-06-04 14:01:59 +02:00
parent e5c137a505
commit bf2e0cdb87
122 changed files with 194 additions and 235 deletions
+5
View File
@@ -0,0 +1,5 @@
export const wikiLinkRegex = /(!?\[\[)([^\]\|]+)(?:\|([^\]]+))?(\]\])/g; // [fullMatch, firstMark, url, alias, lastMark]
export const mdLinkRegex = /!?\[(?<title>[^\]]*)\]\((?<url>.+)\)/g; // [fullMatch, alias, url]
export const tagRegex =
/#(?:(?:\d*[^\d\s!@#$%^&*(),.?":{}|<>\\][^\s!@#$%^&*(),.?":{}|<>\\]*)|(?:<[^>\n]+>))/;
export const pWikiLinkRegex = new RegExp("^" + wikiLinkRegex.source); // Modified regex used only in parser
+31
View File
@@ -0,0 +1,31 @@
import { Tag } from "@lezer/highlight";
export const WikiLinkTag = Tag.define();
export const WikiLinkPageTag = Tag.define();
export const CodeInfoTag = Tag.define();
export const CommentTag = Tag.define();
export const CommentMarkerTag = Tag.define();
export const BulletList = Tag.define();
export const OrderedList = Tag.define();
export const Highlight = Tag.define();
export const HorizontalRuleTag = Tag.define();
export const AttributeTag = Tag.define();
export const AttributeNameTag = Tag.define();
export const AttributeValueTag = Tag.define();
export const NamedAnchorTag = Tag.define();
export const TaskTag = Tag.define();
export const TaskMarkTag = Tag.define();
export const TaskStateTag = Tag.define();
export const TaskDeadlineTag = Tag.define();
export const HashtagTag = Tag.define();
export const NakedURLTag = Tag.define();
export const DirectiveMarkTag = Tag.define();
export const DirectiveTag = Tag.define();
export const SubscriptTag = Tag.define();
export const SuperscriptTag = Tag.define();
+60
View File
@@ -0,0 +1,60 @@
import type {
BlockContext,
LeafBlock,
LeafBlockParser,
MarkdownConfig,
} from "@lezer/markdown";
import { tags as t } from "@lezer/highlight";
import { TaskStateTag } from "./customtags.ts";
// Taken from https://github.com/lezer-parser/markdown/blob/main/src/extension.ts and adapted
class MultiStatusTaskParser implements LeafBlockParser {
constructor(private status: string) {
}
nextLine() {
return false;
}
finish(cx: BlockContext, leaf: LeafBlock) {
cx.addLeafElement(
leaf,
cx.elt("Task", leaf.start, leaf.start + leaf.content.length, [
cx.elt("TaskState", leaf.start, leaf.start + 2 + this.status.length, [
cx.elt("TaskMark", leaf.start, leaf.start + 1),
cx.elt(
"TaskMark",
leaf.start + 1 + this.status.length,
leaf.start + 2 + this.status.length,
),
]),
...cx.parser.parseInline(
leaf.content.slice(this.status.length + 2),
leaf.start + this.status.length + 2,
),
]),
);
return true;
}
}
export const TaskList: MarkdownConfig = {
defineNodes: [
{ name: "Task", block: true, style: t.list },
{ name: "TaskMark", style: t.atom },
{ name: "TaskState", style: TaskStateTag },
],
parseBlock: [{
name: "TaskList",
leaf(cx, leaf) {
const match = /^\[([^\]]+)\][ \t]/.exec(leaf.content);
return match &&
cx.parentType().name == "ListItem"
? new MultiStatusTaskParser(match[1])
: null;
},
after: "SetextHeading",
}],
};
+67
View File
@@ -0,0 +1,67 @@
import type { ParseTree } from "../../plug-api/lib/tree.ts";
import type { SyntaxNode } from "@lezer/common";
import type { Language } from "@codemirror/language";
export function lezerToParseTree(
text: string,
n: SyntaxNode,
offset = 0,
): ParseTree {
let children: ParseTree[] = [];
let nodeText: string | undefined;
let child = n.firstChild;
while (child) {
children.push(lezerToParseTree(text, child));
child = child.nextSibling;
}
if (children.length === 0) {
children = [
{
from: n.from + offset,
to: n.to + offset,
text: text.substring(n.from, n.to),
},
];
} else {
const newChildren: ParseTree[] = [];
let index = n.from;
for (const child of children) {
const s = text.substring(index, child.from);
if (s) {
newChildren.push({
from: index + offset,
to: child.from! + offset,
text: s,
});
}
newChildren.push(child);
index = child.to!;
}
const s = text.substring(index, n.to);
if (s) {
newChildren.push({ from: index + offset, to: n.to + offset, text: s });
}
children = newChildren;
}
const result: ParseTree = {
type: n.name,
from: n.from + offset,
to: n.to + offset,
};
if (children.length > 0) {
result.children = children;
}
if (nodeText) {
result.text = nodeText;
}
return result;
}
export function parse(language: Language, text: string): ParseTree {
// Remove \r for Windows before parsing
text = text.replaceAll("\r", "");
const tree = lezerToParseTree(text, language.parser.parse(text).topNode);
return tree;
}
+207
View File
@@ -0,0 +1,207 @@
import {
collectNodesOfType,
findNodeOfType,
renderToText,
} from "@silverbulletmd/silverbullet/lib/tree";
import { assertEquals, assertNotEquals } from "@std/assert";
import { parseMarkdown } from "./parser.ts";
import { extractHashtag, renderHashtag } from "../../plug-api/lib/tags.ts";
const sample1 = `---
type: page
tags:
- hello
- world
---
# This is a doc
Here is a [[wiki link]] and a [[wiki link|alias]].
Supper`;
const sampleInvalid1 = `---
name: Zef
# This is a doc
Supper`;
Deno.test("Test parser", () => {
let tree = parseMarkdown(sample1);
// console.log("tree", JSON.stringify(tree, null, 2));
// Check if rendering back to text works
assertEquals(renderToText(tree), sample1);
// Find wiki link and wiki link alias
const links = collectNodesOfType(tree, "WikiLink");
assertEquals(links.length, 2);
const nameNode = findNodeOfType(links[0], "WikiLinkPage");
assertEquals(nameNode!.children![0].text, "wiki link");
// Check if alias is parsed properly
const aliasNode = findNodeOfType(links[1], "WikiLinkAlias");
assertEquals(aliasNode!.children![0].text, "alias");
// Find frontmatter
let node = findNodeOfType(tree, "FrontMatter");
assertNotEquals(node, undefined);
tree = parseMarkdown(sampleInvalid1);
node = findNodeOfType(tree, "FrontMatter");
// console.log("Invalid node", node);
assertEquals(node, undefined);
});
const inlineAttributeSample = `
Hello there [a link](http://zef.plus)
[age: 100]
[age:: 200]
Here's a more [ambiguous: case](http://zef.plus)
And one with nested brackets: [array: [1, 2, 3]]
`;
Deno.test("Test inline attribute syntax", () => {
const tree = parseMarkdown(inlineAttributeSample);
// console.log("Attribute parsed", JSON.stringify(tree, null, 2));
const attributes = collectNodesOfType(tree, "Attribute");
let nameNode = findNodeOfType(attributes[0], "AttributeName");
assertEquals(nameNode!.children![0].text, "age");
let valueNode = findNodeOfType(attributes[0], "AttributeValue");
assertEquals(valueNode!.children![0].text, "100");
nameNode = findNodeOfType(attributes[1], "AttributeName");
assertEquals(nameNode!.children![0].text, "age");
valueNode = findNodeOfType(attributes[1], "AttributeValue");
assertEquals(valueNode!.children![0].text, "200");
nameNode = findNodeOfType(attributes[2], "AttributeName");
assertEquals(nameNode!.children![0].text, "array");
valueNode = findNodeOfType(attributes[2], "AttributeValue");
assertEquals(valueNode!.children![0].text, "[1, 2, 3]");
});
const multiStatusTaskExample = `
* [ ] Task 1
- [x] Task 2
* [TODO] Task 3
`;
Deno.test("Test multi-status tasks", () => {
const tree = parseMarkdown(multiStatusTaskExample);
// console.log("Tasks parsed", JSON.stringify(tree, null, 2));
const tasks = collectNodesOfType(tree, "Task");
assertEquals(tasks.length, 3);
// Check " " checkbox state parsing
assertEquals(tasks[0].children![0].children![1].text, " ");
assertEquals(tasks[1].children![0].children![1].text, "x");
assertEquals(tasks[2].children![0].children![1].text, "TODO");
});
Deno.test("Test lua directive parser", () => {
const simpleExample = `Simple \${query_coll("something")}`;
console.log(JSON.stringify(parseMarkdown(simpleExample), null, 2));
});
const hashtagSample = `
Hashtags, e.g. #mytag but ignore in code \`#mytag\`.
They can contain slashes like #level/beginner, single quotes, and dashes: #Mike's-idea.
Can be just #a single letter.
But no other #interpunction: #exclamation! #question?
There is a way to write #<tag with spaces>
These cannot span #<multiple
lines>
#no#spacing also works.
Hashtags can start with number if there's something after it: #3dprint #15-52_Trip-to-NYC.
But magazine issue #1 or #123 are not hashtags.
Should support other languages, like #żółć or #井号
`;
Deno.test("Test hashtag parser", () => {
const tree = parseMarkdown(hashtagSample);
const hashtags = collectNodesOfType(tree, "Hashtag");
assertEquals(hashtags.length, 14);
assertEquals(hashtags[0].children![0].text, "#mytag");
assertEquals(hashtags[1].children![0].text, "#level/beginner");
assertEquals(hashtags[2].children![0].text, "#Mike's-idea");
assertEquals(hashtags[3].children![0].text, "#a");
assertEquals(hashtags[4].children![0].text, "#interpunction");
assertEquals(hashtags[5].children![0].text, "#exclamation");
assertEquals(hashtags[6].children![0].text, "#question");
assertEquals(hashtags[7].children![0].text, "#<tag with spaces>");
// multiple lines not allowed
assertEquals(hashtags[8].children![0].text, "#no");
assertEquals(hashtags[9].children![0].text, "#spacing");
assertEquals(hashtags[10].children![0].text, "#3dprint");
assertEquals(hashtags[11].children![0].text, "#15-52_Trip-to-NYC");
assertEquals(hashtags[12].children![0].text, "#żółć");
assertEquals(hashtags[13].children![0].text, "#井号");
});
Deno.test("Test hashtag helper functions", () => {
assertEquals(extractHashtag("#name"), "name");
assertEquals(extractHashtag("#123-content"), "123-content");
assertEquals(extractHashtag("#<escaped tag>"), "escaped tag");
assertEquals(
extractHashtag("#<allow < and # inside>"),
"allow < and # inside",
);
assertEquals(renderHashtag("simple"), "#simple");
assertEquals(renderHashtag("123-content"), "#123-content");
assertEquals(renderHashtag("with spaces"), "#<with spaces>");
assertEquals(renderHashtag("single'quote"), "#single'quote");
// should behave like this for all characters in tagRegex
assertEquals(renderHashtag("exclamation!"), "#<exclamation!>");
});
const nakedURLSample = `
http://abc.com is a URL
Also http://no-trailing-period.com. That's a URL. It ends with m, not '.'.
http://no-trailing-comma.com, that same a URL, ends with m (and not ',').
http://trailing-slash.com/. That ends with '/' (still not '.').
http://abc.com?e=2.71,pi=3.14 is a URL too.
http://abc.com?e=2.71. That is a URL, which ends with 1 (and not '.').
`;
Deno.test("Test NakedURL parser", () => {
const tree = parseMarkdown(nakedURLSample);
const urls = collectNodesOfType(tree, "NakedURL");
assertEquals(urls.map((x) => x.children![0].text), [
"http://abc.com",
"http://no-trailing-period.com",
"http://no-trailing-comma.com",
"http://trailing-slash.com/",
"http://abc.com?e=2.71,pi=3.14",
"http://abc.com?e=2.71",
]);
});
const tableSample = `
| Header A | Header B |
|----------|----------|
| [[Wiki|Alias]] | 1B |
| 2A | 2B |
`;
Deno.test("Test table parser", () => {
const tree = parseMarkdown(tableSample);
const cells = collectNodesOfType(tree, "TableCell");
assertEquals(cells.map((x) => x.children![0].text), [
"Header A",
"Header B",
undefined,
"1B",
"2A",
"2B",
]);
// Check the Wiki Link - Make sure no backslash has been added (issue 943)
assertEquals(cells[2].children![0].type, "WikiLink");
const wikiName = findNodeOfType(cells[2], "WikiLinkPage");
const wikiAlias = findNodeOfType(cells[2], "WikiLinkAlias");
assertEquals(wikiName!.children![0].text, "Wiki");
assertEquals(wikiAlias!.children![0].text, "Alias");
});
+436
View File
@@ -0,0 +1,436 @@
import { yaml as yamlLanguage } from "@codemirror/legacy-modes/mode/yaml?external=@codemirror/language&target=es2022";
import { styleTags, type Tag, tags as t } from "@lezer/highlight";
import {
type Line,
type MarkdownConfig,
Strikethrough,
Subscript,
Superscript,
} from "@lezer/markdown";
import { markdown } from "@codemirror/lang-markdown";
import { StreamLanguage } from "@codemirror/language";
import * as ct from "./customtags.ts";
import { NakedURLTag } from "./customtags.ts";
import { TaskList } from "./extended_task.ts";
const WikiLink: MarkdownConfig = {
defineNodes: [
{ name: "WikiLink", style: ct.WikiLinkTag },
{ name: "WikiLinkPage", style: ct.WikiLinkPageTag },
{ name: "WikiLinkAlias", style: ct.WikiLinkPageTag },
{ name: "WikiLinkDimensions", style: ct.WikiLinkPageTag },
{ name: "WikiLinkMark", style: t.processingInstruction },
],
parseInline: [
{
name: "WikiLink",
parse(cx, next, pos) {
let match: RegExpMatchArray | null;
if (
next != 91 /* '[' */ &&
next != 33 /* '!' */ ||
!(match = pWikiLinkRegex.exec(cx.slice(pos, cx.end)))
) {
return -1;
}
const [fullMatch, firstMark, page, alias, _lastMark] = match;
const endPos = pos + fullMatch.length;
let aliasElts: any[] = [];
if (alias) {
const pipeStartPos = pos + firstMark.length + page.length;
aliasElts = [
cx.elt("WikiLinkMark", pipeStartPos, pipeStartPos + 1),
cx.elt(
"WikiLinkAlias",
pipeStartPos + 1,
pipeStartPos + 1 + alias.length,
),
];
}
let allElts = cx.elt("WikiLink", pos, endPos, [
cx.elt("WikiLinkMark", pos, pos + firstMark.length),
cx.elt(
"WikiLinkPage",
pos + firstMark.length,
pos + firstMark.length + page.length,
),
...aliasElts,
cx.elt("WikiLinkMark", endPos - 2, endPos),
]);
// If inline image
if (next == 33) {
allElts = cx.elt("Image", pos, endPos, [allElts]);
}
return cx.addElement(allElts);
},
after: "Emphasis",
},
],
};
const LuaDirectives: MarkdownConfig = {
defineNodes: [
{ name: "LuaDirective" },
{ name: "LuaExpressionDirective" },
{ name: "LuaDirectiveMark", style: ct.DirectiveMarkTag },
],
parseInline: [
{
name: "LuaDirective",
parse(cx, next, pos) {
const textFromPos = cx.slice(pos, cx.end);
if (
next !== 36 /* '$' */ ||
cx.slice(pos, pos + 2) !== "${"
) {
return -1;
}
let bracketNestingDepth = 0;
let valueLength = 0;
// We need to ensure balanced { and } pairs
loopLabel:
for (; valueLength < textFromPos.length; valueLength++) {
switch (textFromPos[valueLength]) {
case "{":
bracketNestingDepth++;
break;
case "}":
bracketNestingDepth--;
if (bracketNestingDepth === 0) {
// Done!
break loopLabel;
}
break;
}
}
if (bracketNestingDepth !== 0) {
return -1;
}
const bodyText = textFromPos.slice(2, valueLength);
const endPos = pos + valueLength + 1;
// Let's parse as an expression
const parsedExpression = luaLanguage.parser.parse(`_(${bodyText})`);
// If bodyText starts with whitespace, we need to offset this later
const whiteSpaceOffset = bodyText.match(/^\s*/)?.[0].length ?? 0;
const node = parsedExpression.resolveInner(2, 0).firstChild?.nextSibling
?.nextSibling;
if (!node) {
return -1;
}
const bodyEl = cx.elt(
"LuaExpressionDirective",
pos + 2,
endPos - 1,
[cx.elt(node.toTree()!, pos + 2 + whiteSpaceOffset)],
);
return cx.addElement(
cx.elt("LuaDirective", pos, endPos, [
cx.elt("LuaDirectiveMark", pos, pos + 2),
bodyEl,
cx.elt("LuaDirectiveMark", endPos - 1, endPos),
]),
);
},
after: "Emphasis",
},
],
};
const HighlightDelim = { resolve: "Highlight", mark: "HighlightMark" };
export const Highlight: MarkdownConfig = {
defineNodes: [
{
name: "Highlight",
style: { "Highlight/...": ct.Highlight },
},
{
name: "HighlightMark",
style: t.processingInstruction,
},
],
parseInline: [
{
name: "Highlight",
parse(cx, next, pos) {
if (next != 61 /* '=' */ || cx.char(pos + 1) != 61) return -1;
return cx.addDelimiter(HighlightDelim, pos, pos + 2, true, true);
},
after: "Emphasis",
},
],
};
export const attributeStartRegex = /^\[([\w\$]+)(::?\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;
const textFromPos = cx.slice(pos, cx.end);
if (
next != 91 /* '[' */ ||
// and match the whole thing
!(match = attributeStartRegex.exec(textFromPos))
) {
return -1;
}
const [fullMatch, attributeName, attributeColon] = match;
let bracketNestingDepth = 1;
let valueLength = fullMatch.length;
loopLabel:
for (; valueLength < textFromPos.length; valueLength++) {
switch (textFromPos[valueLength]) {
case "[":
bracketNestingDepth++;
break;
case "]":
bracketNestingDepth--;
if (bracketNestingDepth === 0) {
// Done!
break loopLabel;
}
break;
}
}
if (bracketNestingDepth !== 0) {
console.log("Failed to parse attribute", fullMatch, textFromPos);
return -1;
}
if (textFromPos[valueLength + 1] === "(") {
// This turns out to be a link, back out!
return -1;
}
return cx.addElement(
cx.elt("Attribute", pos, pos + valueLength + 1, [
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,
pos + valueLength,
),
cx.elt("AttributeMark", pos + valueLength, pos + valueLength + 1), // [
]),
);
},
after: "Emphasis",
},
],
};
type RegexParserExtension = {
// unicode char code for efficiency .charCodeAt(0)
firstCharCode: number;
regex: RegExp;
nodeType: string;
tag: Tag;
className?: string;
};
function regexParser({
regex,
firstCharCode,
nodeType,
}: RegexParserExtension): MarkdownConfig {
return {
defineNodes: [nodeType],
parseInline: [
{
name: nodeType,
parse(cx, next, pos) {
if (firstCharCode !== next) {
return -1;
}
const match = regex.exec(cx.slice(pos, cx.end));
if (!match) {
return -1;
}
return cx.addElement(cx.elt(nodeType, pos, pos + match[0].length));
},
},
],
};
}
const NakedURL = regexParser(
{
firstCharCode: 104, // h
regex:
/(^https?:\/\/([-a-zA-Z0-9@:%_\+~#=]|(?:[.](?!(\s|$)))){1,256})(([-a-zA-Z0-9(@:%_\+~#?&=\/]|(?:[.,:;)](?!(\s|$))))*)/,
nodeType: "NakedURL",
className: "sb-naked-url",
tag: NakedURLTag,
},
);
const Hashtag = regexParser({
firstCharCode: 35, // #
regex: new RegExp(`^${tagRegex.source}`),
nodeType: "Hashtag",
className: "sb-hashtag-text",
tag: ct.HashtagTag,
});
const TaskDeadline = regexParser({
firstCharCode: 55357, // 📅
regex: /^📅\s*\d{4}\-\d{2}\-\d{2}/,
className: "sb-task-deadline",
nodeType: "DeadlineDate",
tag: ct.TaskDeadlineTag,
});
import { Table } from "./table_parser.ts";
import { foldNodeProp } from "@codemirror/language";
import { pWikiLinkRegex, tagRegex } from "../markdown_parser/constants.ts";
import { parse } from "../markdown_parser/parse_tree.ts";
import type { ParseTree } from "@silverbulletmd/silverbullet/lib/tree";
import { luaLanguage } from "../space_lua/parse.ts";
// FrontMatter parser
const yamlLang = StreamLanguage.define(yamlLanguage);
export const FrontMatter: MarkdownConfig = {
defineNodes: [
{ name: "FrontMatter", block: true },
{ name: "FrontMatterMarker" },
{ name: "FrontMatterCode" },
],
parseBlock: [{
name: "FrontMatter",
parse: (cx, line: Line) => {
if (cx.parsedPos !== 0) {
return false;
}
if (line.text !== "---") {
return false;
}
const frontStart = cx.parsedPos;
const elts = [
cx.elt(
"FrontMatterMarker",
cx.parsedPos,
cx.parsedPos + line.text.length + 1,
),
];
cx.nextLine();
const startPos = cx.parsedPos;
let endPos = startPos;
let text = "";
let lastPos = cx.parsedPos;
do {
text += line.text + "\n";
endPos += line.text.length + 1;
cx.nextLine();
if (cx.parsedPos === lastPos) {
// End of file, no progress made, there may be a better way to do this but :shrug:
return false;
}
lastPos = cx.parsedPos;
} while (line.text !== "---");
const yamlTree = yamlLang.parser.parse(text);
elts.push(
cx.elt("FrontMatterCode", startPos, endPos, [
cx.elt(yamlTree, startPos),
]),
);
endPos = cx.parsedPos + line.text.length;
elts.push(cx.elt(
"FrontMatterMarker",
cx.parsedPos,
cx.parsedPos + line.text.length,
));
cx.nextLine();
cx.addElement(cx.elt("FrontMatter", frontStart, endPos, elts));
return true;
},
before: "HorizontalRule",
}],
};
export const extendedMarkdownLanguage = markdown({
extensions: [
WikiLink,
Attribute,
FrontMatter,
TaskList,
Highlight,
LuaDirectives,
Strikethrough,
Table,
NakedURL,
Hashtag,
TaskDeadline,
Superscript,
Subscript,
{
props: [
foldNodeProp.add({
// Don't fold at the list level
BulletList: () => null,
OrderedList: () => null,
// Fold list items
ListItem: (tree, state) => ({
from: state.doc.lineAt(tree.from).to,
to: tree.to,
}),
// Fold frontmatter
FrontMatter: (tree) => ({
from: tree.from,
to: tree.to,
}),
}),
styleTags({
Task: ct.TaskTag,
TaskMark: ct.TaskMarkTag,
Comment: ct.CommentTag,
"Subscript": ct.SubscriptTag,
"Superscript": ct.SuperscriptTag,
"TableDelimiter StrikethroughMark": t.processingInstruction,
"TableHeader/...": t.heading,
TableCell: t.content,
CodeInfo: ct.CodeInfoTag,
HorizontalRule: ct.HorizontalRuleTag,
Hashtag: ct.HashtagTag,
NakedURL: ct.NakedURLTag,
DeadlineDate: ct.TaskDeadlineTag,
NamedAnchor: ct.NamedAnchorTag,
}),
],
},
],
}).language;
export function parseMarkdown(text: string): ParseTree {
return parse(extendedMarkdownLanguage, text);
}
+191
View File
@@ -0,0 +1,191 @@
import type {
BlockContext,
Element,
LeafBlock,
LeafBlockParser,
Line,
MarkdownConfig,
} from "@lezer/markdown";
import { tags as t } from "@lezer/highlight";
// Forked from https://github.com/lezer-parser/markdown/blob/main/src/extension.ts
// MIT License
// Author: Marijn Haverbeke
// Change made: Avoid wiki links and commands with aliases [[link|alias]], {[My: Command|alias]}
// from being parsed as table row separators
function parseRow(
cx: BlockContext,
line: string,
startI = 0,
elts?: Element[],
offset = 0,
) {
let count = 0, first = true, cellStart = -1, cellEnd = -1, esc = false;
const parseCell = () => {
elts!.push(
cx.elt(
"TableCell",
offset + cellStart,
offset + cellEnd,
cx.parser.parseInline(
line.slice(cellStart, cellEnd),
offset + cellStart,
),
),
);
};
let inWikilink = false;
let inCommandButton = false;
for (let i = startI; i < line.length; i++) {
const next = line.charCodeAt(i);
if (line[i] === "[" && line.charAt(i + 1) === "[") {
inWikilink = true;
} else if (line[i] === "]" && line.charAt(i - 1) === "]" && inWikilink) {
inWikilink = false;
}
if (line[i] === "{" && line.charAt(i + 1) === "[") {
inCommandButton = true;
} else if (inCommandButton) {
if (line[i] === "}" && line.charAt(i - 1) === "]") {
// Command button without arguments: {[My Command: BeepBoop|Alias]}
inCommandButton = false;
} else if (line[i] === "}" && line.charAt(i - 1) === ")") {
// Command button with arguments: {[My Command: BeepBoop|Alias]("args")}
inCommandButton = false;
}
}
if (next == 124 /* '|' */ && !esc && !inWikilink && !inCommandButton) {
if (!first || cellStart > -1) count++;
first = false;
if (elts) {
if (cellStart > -1) parseCell();
elts.push(cx.elt("TableDelimiter", i + offset, i + offset + 1));
}
cellStart = cellEnd = -1;
} else if (esc || next != 32 && next != 9) {
if (cellStart < 0) cellStart = i;
cellEnd = i + 1;
}
esc = !esc && next == 92;
}
if (cellStart > -1) {
count++;
if (elts) parseCell();
}
return count;
}
function hasPipe(str: string, start: number) {
for (let i = start; i < str.length; i++) {
const next = str.charCodeAt(i);
if (next == 124 /* '|' */) return true;
if (next == 92 /* '\\' */) i++;
}
return false;
}
const delimiterLine = /^\|?(\s*:?-+:?\s*\|)+(\s*:?-+:?\s*)?$/;
class TableParser implements LeafBlockParser {
// Null means we haven't seen the second line yet, false means this
// isn't a table, and an array means this is a table and we've
// parsed the given rows so far.
rows: false | null | Element[] = null;
nextLine(cx: BlockContext, line: Line, leaf: LeafBlock) {
if (this.rows == null) { // Second line
this.rows = false;
let lineText;
if (
(line.next == 45 || line.next == 58 || line.next == 124 /* '-:|' */) &&
delimiterLine.test(lineText = line.text.slice(line.pos))
) {
const firstRow: Element[] = [],
firstCount = parseRow(cx, leaf.content, 0, firstRow, leaf.start);
if (firstCount == parseRow(cx, lineText, line.pos)) {
this.rows = [
cx.elt(
"TableHeader",
leaf.start,
leaf.start + leaf.content.length,
firstRow,
),
cx.elt(
"TableDelimiter",
cx.lineStart + line.pos,
cx.lineStart + line.text.length,
),
];
}
}
} else if (this.rows) { // Line after the second
const content: Element[] = [];
parseRow(cx, line.text, line.pos, content, cx.lineStart);
this.rows.push(
cx.elt(
"TableRow",
cx.lineStart + line.pos,
cx.lineStart + line.text.length,
content,
),
);
}
return false;
}
finish(cx: BlockContext, leaf: LeafBlock) {
if (!this.rows) return false;
cx.addLeafElement(
leaf,
cx.elt(
"Table",
leaf.start,
leaf.start + leaf.content.length,
this.rows as readonly Element[],
),
);
return true;
}
}
/// This extension provides
/// [GFM-style](https://github.github.com/gfm/#tables-extension-)
/// tables, using syntax like this:
///
/// ```
/// | head 1 | head 2 |
/// | --- | --- |
/// | cell 1 | cell 2 |
/// ```
export const Table: MarkdownConfig = {
defineNodes: [
{ name: "Table", block: true },
{ name: "TableHeader", style: { "TableHeader/...": t.heading } },
"TableRow",
{ name: "TableCell", style: t.content },
{ name: "TableDelimiter", style: t.processingInstruction },
],
parseBlock: [{
name: "Table",
leaf(_, leaf) {
return hasPipe(leaf.content, 0) ? new TableParser() : null;
},
endLeaf(cx, line, leaf) {
if (
leaf.parsers.some((p) => p instanceof TableParser) ||
!hasPipe(line.text, line.basePos)
) return false;
// @ts-ignore: internal
const next = cx.scanLine(cx.absoluteLineEnd + 1).text;
return delimiterLine.test(next) &&
parseRow(cx, line.text, line.basePos) ==
parseRow(cx, next, line.basePos);
},
before: "SetextHeading",
}],
};