diff --git a/docs/_scripts/compact.mjs b/docs/_scripts/compact.mjs new file mode 100644 index 000000000..8c4697fb3 --- /dev/null +++ b/docs/_scripts/compact.mjs @@ -0,0 +1,51 @@ +import * as path from "node:path"; +import * as fs from "node:fs/promises"; +import * as url from "node:url"; + +const mdPath = url.fileURLToPath( + new URL("./add_translation_js_ref_updated.md", import.meta.url) +); + +const extractedDir = url.fileURLToPath( + new URL( + "../../../oap-langgraphjs-tools-agent/src/add_transaction_js", + import.meta.url + ) +); + +const files = (await fs.readdir(extractedDir, { withFileTypes: true })).sort( + (a, b) => { + const aInt = Number.parseInt(a.name.split(".")[0], 10); + const bInt = Number.parseInt(b.name.split(".")[0], 10); + return aInt - bInt; + } +); + +let count = 0; + +let lines = []; + +for (let file of files) { + if (file.isDirectory() || !file.name.endsWith(".mts")) continue; + count += 1; + + const content = await fs.readFile(path.resolve(extractedDir, file.name), { + encoding: "utf-8", + }); + + lines = lines.concat( + content + .split("\n") + .reduce((acc, line) => { + if (line.trimStart().startsWith("// ```")) acc.push([]); + acc.at(-1)?.push(line); + return acc; + }, []) + .map((i) => { + const tag = i[0].trimStart().slice("// ```".length); + return ["```" + tag, ...i.slice(1), "```"].join("\n"); + }) + ); +} + +await fs.writeFile(mdPath, lines.join("\n\n")); diff --git a/docs/_scripts/extract.mjs b/docs/_scripts/extract.mjs new file mode 100644 index 000000000..480a0a677 --- /dev/null +++ b/docs/_scripts/extract.mjs @@ -0,0 +1,46 @@ +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import * as url from "node:url"; + +const mdPath = url.fileURLToPath( + new URL("./add_translation_js_ref.md", import.meta.url) +); + +const extractedDir = url.fileURLToPath( + new URL( + "../../../oap-langgraphjs-tools-agent/src/add_transaction_js", + import.meta.url + ) +); + +await fs.mkdir(extractedDir, { recursive: true }); + +const md = (await fs.readFile(mdPath, { encoding: "utf-8" })).split("\n"); + +const chunks = []; +let current = []; + +for (let line of md) { + if (line.trimStart().startsWith("```")) { + if (current.length > 0) { + chunks.push(current.join("\n")); + current = []; + } else { + current.push("// " + line.trimStart()); + } + } else if (current.length > 0) { + current.push(line); + } +} + +if (current.length > 0) { + chunks.push(current.join("\n")); +} + +for (let i = 0; i < chunks.length; i += 1) { + await fs.writeFile(path.resolve(extractedDir, `${i}.mts`), chunks[i], { + encoding: "utf-8", + }); +} + +console.log("finished");