2022-12-14 19:04:20 +00:00
|
|
|
import { Tag } from "../deps.ts";
|
|
|
|
import type { MarkdownConfig } from "../deps.ts";
|
|
|
|
import { System } from "../../plugos/system.ts";
|
|
|
|
import { Manifest } from "../manifest.ts";
|
2022-04-11 18:34:09 +00:00
|
|
|
|
|
|
|
export type MDExt = {
|
|
|
|
// unicode char code for efficiency .charCodeAt(0)
|
|
|
|
firstCharCodes: number[];
|
|
|
|
regex: RegExp;
|
|
|
|
nodeType: string;
|
|
|
|
tag: Tag;
|
|
|
|
styles: { [key: string]: string };
|
2022-08-29 14:16:55 +00:00
|
|
|
className?: string;
|
2022-04-11 18:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function mdExtensionSyntaxConfig({
|
|
|
|
regex,
|
|
|
|
firstCharCodes,
|
|
|
|
nodeType,
|
|
|
|
}: MDExt): MarkdownConfig {
|
|
|
|
return {
|
|
|
|
defineNodes: [nodeType],
|
|
|
|
parseInline: [
|
|
|
|
{
|
|
|
|
name: nodeType,
|
|
|
|
parse(cx, next, pos) {
|
|
|
|
if (!firstCharCodes.includes(next)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
let match = regex.exec(cx.slice(pos, cx.end));
|
|
|
|
if (!match) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return cx.addElement(cx.elt(nodeType, pos, pos + match[0].length));
|
|
|
|
},
|
2022-04-12 11:33:07 +00:00
|
|
|
// after: "Emphasis",
|
2022-04-11 18:34:09 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mdExtensionStyleTags({ nodeType, tag }: MDExt): {
|
|
|
|
[selector: string]: Tag | readonly Tag[];
|
|
|
|
} {
|
|
|
|
return {
|
|
|
|
[nodeType]: tag,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadMarkdownExtensions(system: System<any>): MDExt[] {
|
2022-11-01 14:01:28 +00:00
|
|
|
const mdExtensions: MDExt[] = [];
|
|
|
|
for (const plug of system.loadedPlugs.values()) {
|
|
|
|
const manifest = plug.manifest as Manifest;
|
2022-04-11 18:34:09 +00:00
|
|
|
if (manifest.syntax) {
|
2022-11-01 14:01:28 +00:00
|
|
|
for (const [nodeType, def] of Object.entries(manifest.syntax)) {
|
2022-04-11 18:34:09 +00:00
|
|
|
mdExtensions.push({
|
|
|
|
nodeType,
|
|
|
|
tag: Tag.define(),
|
|
|
|
firstCharCodes: def.firstCharacters.map((ch) => ch.charCodeAt(0)),
|
|
|
|
regex: new RegExp("^" + def.regex),
|
|
|
|
styles: def.styles,
|
2022-08-29 14:16:55 +00:00
|
|
|
className: def.className,
|
2022-04-11 18:34:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mdExtensions;
|
|
|
|
}
|