|
| 1 | +import { readFile, writeFile, mkdtemp, copyFile } from "fs/promises"; |
| 2 | +import { join } from "path"; |
| 3 | +import { tmpdir } from "os"; |
| 4 | +import * as pagesConfig from "./pagesconfig.json" with { type: "json" }; |
| 5 | +const config = pagesConfig.default; |
| 6 | +const tutorials = []; |
| 7 | +const tempDir = await mkdtemp(join(tmpdir(), "ts-library-template-")); |
| 8 | +const shouldPathUseRoot = (path) => !path.startsWith("./") && !path.startsWith("../") && !path.startsWith("/"); |
| 9 | +const generateTitle = (title, group) => { |
| 10 | + const tit = title.replace(/"/gu, `\\"`); |
| 11 | + const sub = (group ?? "").replace(/"/gu, `\\"`); |
| 12 | + const finalTitle = [sub, tit].filter(Boolean).join("/"); |
| 13 | + return `--- |
| 14 | +title: ${finalTitle} |
| 15 | +--- |
| 16 | +`; // leave the ending new line |
| 17 | +}; |
| 18 | +let i = 0; |
| 19 | +// eslint-disable-next-line @typescript-eslint/no-shadow |
| 20 | +const convertPages = async ({ pages, parent, root }) => { |
| 21 | + for (const page of pages) { |
| 22 | + if (page.source) { |
| 23 | + const fullPath = shouldPathUseRoot(page.source) ? join(root ?? "", page.source) : page.source; |
| 24 | + const tempPath = join(tempDir, `${i++}.md`); |
| 25 | + tutorials.push(tempPath); |
| 26 | + await copyFile(fullPath, tempPath); |
| 27 | + await writeFile(tempPath, generateTitle(page.title, parent) + await readFile(fullPath, "utf-8")); |
| 28 | + } |
| 29 | + if (page.children) { |
| 30 | + await convertPages({ |
| 31 | + pages: page.children, |
| 32 | + parent: parent ? parent + "/" + page.title : page.title, |
| 33 | + root: page.moduleRoot ? join(root ?? "", page.childrenSourceDir ?? "") : root, |
| 34 | + }); |
| 35 | + } |
| 36 | + } |
| 37 | +}; |
| 38 | +if (config.pages?.length) { |
| 39 | + const root = config.source; |
| 40 | + await convertPages({ pages: config.pages, parent: "", root: root }); |
| 41 | +} |
| 42 | +console.info({ |
| 43 | + tutorials, |
| 44 | + tempDir, |
| 45 | +}); |
| 46 | +export default { |
| 47 | + projectDocuments: tutorials, |
| 48 | + sortEntryPoints: false, |
| 49 | +}; |
0 commit comments