Files
plainleaf/web/syscalls/markdown.ts
T
MrMugameandGitHub fa8bfdeef7 Improve transclusions (#1505)
* Remove `documentUrlPrefix`

This seems to be a relic from the time SB used `fs/` as a file prefix on the server

* Move and rename files

* Factor out more code

* Make `expandMarkdown` and `renderMarkdownToHTML` handle transclusions properly

* Typo
2025-08-26 13:21:29 +02:00

43 lines
1.4 KiB
TypeScript

import type { SysCallMapping } from "../../lib/plugos/system.ts";
import { parse } from "../markdown_parser/parse_tree.ts";
import { type ParseTree, renderToText } from "../../plug-api/lib/tree.ts";
import { extendedMarkdownLanguage } from "../markdown_parser/parser.ts";
import { expandMarkdown } from "../markdown/inline.ts";
import type { Client } from "../client.ts";
import { LuaEnv, LuaStackFrame } from "../../lib/space_lua/runtime.ts";
import {
type MarkdownRenderOptions,
renderMarkdownToHtml,
} from "../markdown/markdown_render.ts";
export function markdownSyscalls(client: Client): SysCallMapping {
return {
"markdown.parseMarkdown": (_ctx, text: string): ParseTree => {
return parse(extendedMarkdownLanguage, text);
},
"markdown.renderParseTree": (_ctx, tree: ParseTree): string => {
return renderToText(tree);
},
"markdown.expandMarkdown": (_ctx, tree: ParseTree): Promise<ParseTree> => {
const globalEnv = client.clientSystem.spaceLuaEnv.env;
const tl = new LuaEnv();
tl.setLocal("_GLOBAL", globalEnv);
const sf = new LuaStackFrame(tl, null);
return expandMarkdown(
client,
tree,
globalEnv,
sf,
);
},
"markdown.markdownToHtml": (
_ctx,
text: string,
options: MarkdownRenderOptions = {},
) => {
const mdTree = parse(extendedMarkdownLanguage, text);
return renderMarkdownToHtml(mdTree, options);
},
};
}