2023-10-11 16:48:34 +00:00
|
|
|
import type { RemarkPlugin } from "@astrojs/markdown-remark";
|
2024-06-26 23:18:47 +00:00
|
|
|
import type { RootContent } from "hast";
|
2023-10-11 16:48:34 +00:00
|
|
|
import { fromMarkdown } from "mdast-util-from-markdown";
|
2023-10-11 17:47:18 +00:00
|
|
|
import { fromHtml } from "hast-util-from-html";
|
|
|
|
import { toHtml } from "hast-util-to-html";
|
2023-10-11 16:48:34 +00:00
|
|
|
|
|
|
|
import { spawnSync } from "node:child_process";
|
2024-04-05 15:34:32 +00:00
|
|
|
import { mkdirSync, mkdtempSync, readFileSync } from "node:fs";
|
2023-10-11 16:48:34 +00:00
|
|
|
import { tmpdir } from "node:os";
|
|
|
|
import { join, parse } from "node:path";
|
2023-10-11 17:47:18 +00:00
|
|
|
import { visit } from "unist-util-visit";
|
2023-10-11 16:48:34 +00:00
|
|
|
|
|
|
|
const remarkAgda: RemarkPlugin = () => {
|
2024-04-05 15:34:32 +00:00
|
|
|
return (tree, { history }) => {
|
2024-06-26 23:18:47 +00:00
|
|
|
const path: string = history[history.length - 1]!;
|
2023-10-11 16:48:34 +00:00
|
|
|
if (!(path.endsWith(".lagda.md") || path.endsWith(".agda"))) return;
|
|
|
|
|
2024-06-26 23:18:47 +00:00
|
|
|
console.log("AGDA:processing path", path);
|
|
|
|
|
2023-10-11 16:48:34 +00:00
|
|
|
const tempDir = mkdtempSync(join(tmpdir(), "agdaRender."));
|
|
|
|
const outDir = join(tempDir, "output");
|
|
|
|
mkdirSync(outDir, { recursive: true });
|
|
|
|
|
|
|
|
const childOutput = spawnSync(
|
|
|
|
"agda",
|
2024-04-05 15:34:32 +00:00
|
|
|
[
|
|
|
|
"--html",
|
|
|
|
`--html-dir=${outDir}`,
|
|
|
|
"--highlight-occurrences",
|
|
|
|
"--html-highlight=code",
|
|
|
|
path,
|
|
|
|
],
|
2023-10-11 16:48:34 +00:00
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
2024-06-27 00:17:01 +00:00
|
|
|
// TODO: Handle child output
|
|
|
|
console.error("--AGDA OUTPUT--")
|
|
|
|
console.error(childOutput);
|
|
|
|
console.error(childOutput.stdout?.toString());
|
|
|
|
console.error(childOutput.stderr?.toString());
|
|
|
|
console.error("--AGDA OUTPUT--")
|
|
|
|
// if (childOutput.status !== 0)
|
|
|
|
// throw new Error("Agda failed", childOutput.stderr)
|
|
|
|
|
2023-10-11 17:47:18 +00:00
|
|
|
const filename = parse(path).base.replace(/\.lagda.md/, ".md");
|
|
|
|
const htmlname = parse(path).base.replace(/\.lagda.md/, ".html");
|
2023-10-11 16:48:34 +00:00
|
|
|
const fullOutputPath = join(outDir, filename);
|
|
|
|
|
|
|
|
const doc = readFileSync(fullOutputPath);
|
2024-06-26 23:18:47 +00:00
|
|
|
|
|
|
|
// This is the post-processed markdown with HTML code blocks replacing the Agda code blocks
|
2023-10-11 16:48:34 +00:00
|
|
|
const tree2 = fromMarkdown(doc);
|
|
|
|
|
2023-10-11 17:47:18 +00:00
|
|
|
const collectedCodeBlocks: RootContent[] = [];
|
|
|
|
visit(tree2, "html", (node) => {
|
|
|
|
const html = fromHtml(node.value, { fragment: true });
|
2023-10-11 16:48:34 +00:00
|
|
|
|
2023-10-11 17:47:18 +00:00
|
|
|
const firstChild: RootContent = html.children[0]!;
|
|
|
|
|
|
|
|
visit(html, "element", (node) => {
|
|
|
|
if (node.tagName !== "a") return;
|
|
|
|
|
|
|
|
if (node.properties.href && node.properties.href.includes(htmlname)) {
|
|
|
|
node.properties.href = node.properties.href.replace(htmlname, "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!firstChild?.properties?.className?.includes("Agda")) return;
|
|
|
|
|
|
|
|
const stringContents = toHtml(firstChild);
|
|
|
|
collectedCodeBlocks.push({
|
|
|
|
contents: stringContents,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let idx = 0;
|
|
|
|
visit(tree, "code", (node) => {
|
2024-06-26 23:18:47 +00:00
|
|
|
if (!(node.lang === null || node.lang === "agda")) return;
|
2023-10-11 17:47:18 +00:00
|
|
|
|
|
|
|
node.type = "html";
|
|
|
|
node.value = collectedCodeBlocks[idx].contents;
|
|
|
|
idx += 1;
|
|
|
|
});
|
2023-10-11 16:48:34 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default remarkAgda;
|