2022-04-20 08:56:43 +00:00
|
|
|
import { styleTags, tags as t } from "@codemirror/highlight";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
BlockContext,
|
|
|
|
LeafBlock,
|
|
|
|
LeafBlockParser,
|
|
|
|
MarkdownConfig,
|
|
|
|
parseCode,
|
|
|
|
Table,
|
|
|
|
TaskList,
|
|
|
|
} from "@lezer/markdown";
|
2022-04-09 12:28:41 +00:00
|
|
|
import { commonmark, getCodeParser, mkLang } from "./markdown/markdown";
|
2022-03-20 08:56:28 +00:00
|
|
|
import * as ct from "./customtags";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
Language,
|
|
|
|
LanguageDescription,
|
|
|
|
LanguageSupport,
|
|
|
|
} from "@codemirror/language";
|
2022-04-09 12:28:41 +00:00
|
|
|
import { StreamLanguage } from "@codemirror/stream-parser";
|
|
|
|
import { yaml } from "@codemirror/legacy-modes/mode/yaml";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
javascriptLanguage,
|
|
|
|
typescriptLanguage,
|
|
|
|
} from "@codemirror/lang-javascript";
|
|
|
|
import {
|
|
|
|
MDExt,
|
|
|
|
mdExtensionStyleTags,
|
|
|
|
mdExtensionSyntaxConfig,
|
|
|
|
} from "./markdown_ext";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-10 09:04:07 +00:00
|
|
|
export const pageLinkRegex = /^\[\[([^\]]+)\]\]/;
|
2022-04-08 15:46:09 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
const WikiLink: MarkdownConfig = {
|
|
|
|
defineNodes: ["WikiLink", "WikiLinkPage"],
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return cx.addElement(
|
2022-04-04 09:51:41 +00:00
|
|
|
cx.elt("WikiLink", pos, pos + match[0].length, [
|
2022-03-20 08:56:28 +00:00
|
|
|
cx.elt("WikiLinkPage", pos + 2, pos + match[0].length - 2),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
},
|
|
|
|
after: "Emphasis",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
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),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export const Comment: MarkdownConfig = {
|
|
|
|
defineNodes: [{ name: "Comment", block: true }],
|
|
|
|
parseBlock: [
|
|
|
|
{
|
|
|
|
name: "Comment",
|
|
|
|
leaf(cx, leaf) {
|
|
|
|
return /^%%\s/.test(leaf.content) ? new CommentParser() : null;
|
|
|
|
},
|
|
|
|
after: "SetextHeading",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-04-11 18:34:09 +00:00
|
|
|
export default function buildMarkdown(mdExtensions: MDExt[]): Language {
|
|
|
|
return mkLang(
|
|
|
|
commonmark.configure([
|
|
|
|
WikiLink,
|
|
|
|
TaskList,
|
|
|
|
Comment,
|
2022-04-20 08:56:43 +00:00
|
|
|
Table,
|
2022-04-11 18:34:09 +00:00
|
|
|
...mdExtensions.map(mdExtensionSyntaxConfig),
|
|
|
|
parseCode({
|
|
|
|
codeParser: getCodeParser([
|
|
|
|
LanguageDescription.of({
|
|
|
|
name: "yaml",
|
2022-04-12 11:33:07 +00:00
|
|
|
alias: ["meta", "data"],
|
2022-04-11 18:34:09 +00:00
|
|
|
support: new LanguageSupport(StreamLanguage.define(yaml)),
|
|
|
|
}),
|
|
|
|
LanguageDescription.of({
|
|
|
|
name: "javascript",
|
|
|
|
alias: ["js"],
|
|
|
|
support: new LanguageSupport(javascriptLanguage),
|
|
|
|
}),
|
2022-04-24 16:06:34 +00:00
|
|
|
LanguageDescription.of({
|
|
|
|
name: "typescript",
|
|
|
|
alias: ["ts"],
|
|
|
|
support: new LanguageSupport(typescriptLanguage),
|
|
|
|
}),
|
2022-04-11 18:34:09 +00:00
|
|
|
]),
|
2022-03-20 08:56:28 +00:00
|
|
|
}),
|
2022-04-11 18:34:09 +00:00
|
|
|
{
|
|
|
|
props: [
|
|
|
|
styleTags({
|
|
|
|
WikiLink: ct.WikiLinkTag,
|
|
|
|
WikiLinkPage: ct.WikiLinkPageTag,
|
|
|
|
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-04-11 18:34:09 +00:00
|
|
|
}),
|
|
|
|
...mdExtensions.map((mdExt) =>
|
|
|
|
styleTags(mdExtensionStyleTags(mdExt))
|
|
|
|
),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|