2023-10-11 16:48:34 +00:00
|
|
|
import type { RemarkPlugin } from "@astrojs/markdown-remark";
|
2023-10-11 17:47:18 +00:00
|
|
|
import type { Node, Root, Parent, 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";
|
|
|
|
import { mkdtempSync, mkdirSync, readFileSync } from "node:fs";
|
|
|
|
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 = () => {
|
2023-10-12 22:22:58 +00:00
|
|
|
return function (tree, { history }) {
|
|
|
|
// console.log("args", arguments)
|
|
|
|
const path: string = history[history.length - 1];
|
|
|
|
console.log("path", history);
|
2023-10-11 16:48:34 +00:00
|
|
|
if (!(path.endsWith(".lagda.md") || path.endsWith(".agda"))) return;
|
|
|
|
|
|
|
|
const tempDir = mkdtempSync(join(tmpdir(), "agdaRender."));
|
|
|
|
const outDir = join(tempDir, "output");
|
|
|
|
mkdirSync(outDir, { recursive: true });
|
|
|
|
|
|
|
|
const childOutput = spawnSync(
|
|
|
|
"agda",
|
2023-10-11 17:47:18 +00:00
|
|
|
["--html", `--html-dir=${outDir}`, "--highlight-occurrences", "--html-highlight=code", path],
|
2023-10-11 16:48:34 +00:00
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log("output", childOutput.output.toString());
|
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
|
|
|
console.log();
|
|
|
|
console.log("filename", filename);
|
|
|
|
|
|
|
|
const fullOutputPath = join(outDir, filename);
|
|
|
|
console.log("outDir", fullOutputPath);
|
|
|
|
console.log();
|
|
|
|
|
|
|
|
const doc = readFileSync(fullOutputPath);
|
|
|
|
const tree2 = fromMarkdown(doc);
|
2023-10-11 17:47:18 +00:00
|
|
|
// console.log("tree", tree);
|
2023-10-11 16:48:34 +00:00
|
|
|
|
2023-10-11 17:47:18 +00:00
|
|
|
const collectedCodeBlocks: RootContent[] = [];
|
|
|
|
visit(tree2, "html", (node) => {
|
|
|
|
// console.log("node", node);
|
|
|
|
// collectedCodeBlocks.push
|
|
|
|
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]!;
|
|
|
|
console.log("child", firstChild);
|
|
|
|
|
|
|
|
visit(html, "element", (node) => {
|
|
|
|
if (node.tagName !== "a") return;
|
|
|
|
|
|
|
|
if (node.properties.href && node.properties.href.includes(htmlname)) {
|
|
|
|
console.log("a", node.properties);
|
|
|
|
node.properties.href = node.properties.href.replace(htmlname, "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!firstChild?.properties?.className?.includes("Agda")) return;
|
|
|
|
|
|
|
|
const stringContents = toHtml(firstChild);
|
|
|
|
// console.log("result", stringContents);
|
|
|
|
collectedCodeBlocks.push({
|
|
|
|
contents: stringContents,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("collected len", collectedCodeBlocks.length);
|
|
|
|
|
|
|
|
let idx = 0;
|
|
|
|
visit(tree, "code", (node) => {
|
|
|
|
if (node.lang !== "agda") return;
|
|
|
|
|
|
|
|
// console.log("node", node);
|
|
|
|
node.type = "html";
|
|
|
|
node.value = collectedCodeBlocks[idx].contents;
|
|
|
|
idx += 1;
|
|
|
|
});
|
|
|
|
console.log("len", idx);
|
2023-10-11 16:48:34 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default remarkAgda;
|