2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
BlockContext,
|
2022-10-10 12:50:21 +00:00
|
|
|
Language,
|
2022-04-25 08:33:38 +00:00
|
|
|
LeafBlock,
|
|
|
|
LeafBlockParser,
|
2022-10-17 13:48:21 +00:00
|
|
|
Line,
|
2022-10-10 12:50:21 +00:00
|
|
|
markdown,
|
2022-04-25 08:33:38 +00:00
|
|
|
MarkdownConfig,
|
2022-10-17 13:48:21 +00:00
|
|
|
StreamLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
styleTags,
|
2022-04-25 08:33:38 +00:00
|
|
|
Table,
|
2022-10-10 12:50:21 +00:00
|
|
|
tags as t,
|
2022-04-25 08:33:38 +00:00
|
|
|
TaskList,
|
2022-10-17 13:48:21 +00:00
|
|
|
yamlLanguage,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "./deps.ts";
|
|
|
|
import * as ct from "./customtags.ts";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
MDExt,
|
|
|
|
mdExtensionStyleTags,
|
|
|
|
mdExtensionSyntaxConfig,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "./markdown_ext.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-11-27 07:48:01 +00:00
|
|
|
export const pageLinkRegex = /^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/;
|
2022-04-08 15:46:09 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
const WikiLink: MarkdownConfig = {
|
2022-11-29 08:11:23 +00:00
|
|
|
defineNodes: [
|
|
|
|
{ name: "WikiLink", style: ct.WikiLinkTag },
|
|
|
|
{ name: "WikiLinkPage", style: ct.WikiLinkPageTag },
|
|
|
|
{ name: "WikiLinkAlias", style: ct.WikiLinkPageTag },
|
|
|
|
{ name: "WikiLinkMark", style: t.processingInstruction },
|
|
|
|
],
|
2022-03-20 08:56:28 +00:00
|
|
|
parseInline: [
|
|
|
|
{
|
|
|
|
name: "WikiLink",
|
|
|
|
parse(cx, next, pos) {
|
|
|
|
let match: RegExpMatchArray | null;
|
|
|
|
if (
|
|
|
|
next != 91 /* '[' */ ||
|
2022-04-10 09:04:07 +00:00
|
|
|
!(match = pageLinkRegex.exec(cx.slice(pos, cx.end)))
|
2022-03-20 08:56:28 +00:00
|
|
|
) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-29 08:11:23 +00:00
|
|
|
const [fullMatch, page, pipePart, label] = match;
|
|
|
|
const endPos = pos + fullMatch.length;
|
2022-11-27 07:48:01 +00:00
|
|
|
let aliasElts: any[] = [];
|
|
|
|
if (pipePart) {
|
|
|
|
const pipeStartPos = pos + 2 + page.length;
|
|
|
|
aliasElts = [
|
|
|
|
cx.elt("WikiLinkMark", pipeStartPos, pipeStartPos + 1),
|
|
|
|
cx.elt(
|
|
|
|
"WikiLinkAlias",
|
|
|
|
pipeStartPos + 1,
|
|
|
|
pipeStartPos + 1 + label.length,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
return cx.addElement(
|
|
|
|
cx.elt("WikiLink", pos, endPos, [
|
|
|
|
cx.elt("WikiLinkMark", pos, pos + 2),
|
2022-11-27 07:48:01 +00:00
|
|
|
cx.elt("WikiLinkPage", pos + 2, pos + 2 + page.length),
|
|
|
|
...aliasElts,
|
2022-11-18 15:04:37 +00:00
|
|
|
cx.elt("WikiLinkMark", endPos - 2, endPos),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
after: "Emphasis",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-11-29 08:11:23 +00:00
|
|
|
export const commandLinkRegex = /^\{\[([^\]\|]+)(\|([^\]]+))?\]\}/;
|
2022-11-18 15:04:37 +00:00
|
|
|
|
|
|
|
const CommandLink: MarkdownConfig = {
|
|
|
|
defineNodes: [
|
|
|
|
{ name: "CommandLink", style: { "CommandLink/...": ct.CommandLinkTag } },
|
|
|
|
{ name: "CommandLinkName", style: ct.CommandLinkNameTag },
|
2022-11-29 08:11:23 +00:00
|
|
|
{ name: "CommandLinkAlias", style: ct.CommandLinkNameTag },
|
|
|
|
{ name: "CommandLinkMark", style: t.processingInstruction },
|
2022-11-18 15:04:37 +00:00
|
|
|
],
|
|
|
|
parseInline: [
|
|
|
|
{
|
|
|
|
name: "CommandLink",
|
|
|
|
parse(cx, next, pos) {
|
|
|
|
let match: RegExpMatchArray | null;
|
|
|
|
if (
|
|
|
|
next != 123 /* '{' */ ||
|
|
|
|
!(match = commandLinkRegex.exec(cx.slice(pos, cx.end)))
|
|
|
|
) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-29 08:11:23 +00:00
|
|
|
const [fullMatch, command, pipePart, label] = match;
|
|
|
|
const endPos = pos + fullMatch.length;
|
|
|
|
|
|
|
|
let aliasElts: any[] = [];
|
|
|
|
if (pipePart) {
|
|
|
|
const pipeStartPos = pos + 2 + command.length;
|
|
|
|
aliasElts = [
|
|
|
|
cx.elt("CommandLinkMark", pipeStartPos, pipeStartPos + 1),
|
|
|
|
cx.elt(
|
|
|
|
"CommandLinkAlias",
|
|
|
|
pipeStartPos + 1,
|
|
|
|
pipeStartPos + 1 + label.length,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
return cx.addElement(
|
2022-11-18 15:04:37 +00:00
|
|
|
cx.elt("CommandLink", pos, endPos, [
|
|
|
|
cx.elt("CommandLinkMark", pos, pos + 2),
|
2022-11-29 08:11:23 +00:00
|
|
|
cx.elt("CommandLinkName", pos + 2, pos + 2 + command.length),
|
|
|
|
...aliasElts,
|
2022-11-18 15:04:37 +00:00
|
|
|
cx.elt("CommandLinkMark", endPos - 2, endPos),
|
2022-10-10 12:50:21 +00:00
|
|
|
]),
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|
2022-11-29 08:11:23 +00:00
|
|
|
|
|
|
|
// return cx.addElement(
|
|
|
|
// cx.elt("CommandLink", pos, endPos, [
|
|
|
|
// cx.elt("CommandLinkMark", pos, pos + 2),
|
|
|
|
// cx.elt("CommandLinkName", pos + 2, endPos - 2),
|
|
|
|
// cx.elt("CommandLinkMark", endPos - 2, endPos),
|
|
|
|
// ]),
|
|
|
|
// );
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
after: "Emphasis",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-07-04 13:07:27 +00:00
|
|
|
const HighlightDelim = { resolve: "Highlight", mark: "HighlightMark" };
|
|
|
|
|
|
|
|
export const Strikethrough: 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",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
class CommentParser implements LeafBlockParser {
|
|
|
|
nextLine() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish(cx: BlockContext, leaf: LeafBlock) {
|
|
|
|
cx.addLeafElement(
|
|
|
|
leaf,
|
|
|
|
cx.elt("Comment", leaf.start, leaf.start + leaf.content.length, [
|
|
|
|
// cx.elt("CommentMarker", leaf.start, leaf.start + 3),
|
|
|
|
...cx.parser.parseInline(leaf.content.slice(3), leaf.start + 3),
|
2022-10-10 12:50:21 +00:00
|
|
|
]),
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export const Comment: MarkdownConfig = {
|
|
|
|
defineNodes: [{ name: "Comment", block: true }],
|
|
|
|
parseBlock: [
|
|
|
|
{
|
|
|
|
name: "Comment",
|
2022-10-17 13:48:21 +00:00
|
|
|
leaf(_cx, leaf) {
|
2022-03-20 08:56:28 +00:00
|
|
|
return /^%%\s/.test(leaf.content) ? new CommentParser() : null;
|
|
|
|
},
|
|
|
|
after: "SetextHeading",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-10-17 13:48:21 +00:00
|
|
|
// FrontMatter parser
|
|
|
|
|
2022-11-18 15:04:37 +00:00
|
|
|
const yamlLang = StreamLanguage.define(yamlLanguage);
|
2022-10-17 13:48:21 +00:00
|
|
|
|
|
|
|
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 !== "---");
|
2022-11-18 15:04:37 +00:00
|
|
|
const yamlTree = yamlLang.parser.parse(text);
|
2022-10-17 13:48:21 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
export default function buildMarkdown(mdExtensions: MDExt[]): Language {
|
2022-09-13 06:41:01 +00:00
|
|
|
return markdown({
|
|
|
|
extensions: [
|
2022-04-11 18:34:09 +00:00
|
|
|
WikiLink,
|
2022-11-18 15:04:37 +00:00
|
|
|
CommandLink,
|
2022-10-17 13:48:21 +00:00
|
|
|
FrontMatter,
|
2022-04-11 18:34:09 +00:00
|
|
|
TaskList,
|
|
|
|
Comment,
|
2022-07-04 13:07:27 +00:00
|
|
|
Strikethrough,
|
2022-04-20 08:56:43 +00:00
|
|
|
Table,
|
2022-04-11 18:34:09 +00:00
|
|
|
...mdExtensions.map(mdExtensionSyntaxConfig),
|
2022-10-17 13:48:21 +00:00
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
{
|
|
|
|
props: [
|
|
|
|
styleTags({
|
2022-11-29 08:11:23 +00:00
|
|
|
// WikiLink: ct.WikiLinkTag,
|
|
|
|
// WikiLinkPage: ct.WikiLinkPageTag,
|
|
|
|
// WikiLinkAlias: ct.WikiLinkPageTag,
|
2022-11-18 15:04:37 +00:00
|
|
|
// CommandLink: ct.CommandLinkTag,
|
|
|
|
// CommandLinkName: ct.CommandLinkNameTag,
|
2022-04-11 18:34:09 +00:00
|
|
|
Task: ct.TaskTag,
|
|
|
|
TaskMarker: ct.TaskMarkerTag,
|
|
|
|
Comment: ct.CommentTag,
|
2022-04-20 08:56:43 +00:00
|
|
|
"TableDelimiter SubscriptMark SuperscriptMark StrikethroughMark":
|
|
|
|
t.processingInstruction,
|
|
|
|
"TableHeader/...": t.heading,
|
|
|
|
TableCell: t.content,
|
2022-04-24 16:06:34 +00:00
|
|
|
CodeInfo: ct.CodeInfoTag,
|
2022-08-29 14:48:52 +00:00
|
|
|
HorizontalRule: ct.HorizontalRuleTag,
|
2022-04-11 18:34:09 +00:00
|
|
|
}),
|
|
|
|
...mdExtensions.map((mdExt) =>
|
|
|
|
styleTags(mdExtensionStyleTags(mdExt))
|
|
|
|
),
|
|
|
|
],
|
|
|
|
},
|
2022-09-13 06:41:01 +00:00
|
|
|
],
|
|
|
|
}).language;
|
2022-04-11 18:34:09 +00:00
|
|
|
}
|