|
| 1 | +let readMarkdownFile = (filePath: string): string => { |
| 2 | + let fileContent = Node.Fs.readFileSync2(filePath, "utf8") |
| 3 | + fileContent |
| 4 | +} |
| 5 | + |
| 6 | +let rec collectFiles = (dirPath: string): array<string> => { |
| 7 | + let entries = Node.Fs.readdirSync(dirPath) |
| 8 | + entries->Array.reduce([], (acc, entry) => { |
| 9 | + let fullPath = Node.Path.join([dirPath, entry]) |
| 10 | + let stats = Node.Fs.statSync(fullPath) |
| 11 | + switch stats["isDirectory"]() { |
| 12 | + | true => acc->Array.concat(collectFiles(fullPath)) |
| 13 | + | false => { |
| 14 | + acc->Array.push(fullPath) |
| 15 | + acc |
| 16 | + } |
| 17 | + } |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +let clearFile = (filePath: string): unit => { |
| 22 | + Node.Fs.writeFileSync(filePath, "") |
| 23 | +} |
| 24 | + |
| 25 | +let createDirectoryIfNotExists = (dirPath: string): unit => { |
| 26 | + if !Node.Fs.existsSync(dirPath) { |
| 27 | + Node.Fs.mkdirSync(dirPath) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +let removeCodeTabTags = (content: string): string => { |
| 32 | + let regex = RegExp.fromStringWithFlags("<CodeTab.*?>[\\s\\S]*?</CodeTab>", ~flags="g") |
| 33 | + String.replaceRegExp(content, regex, "") |
| 34 | +} |
| 35 | + |
| 36 | +let removeCodeBlocks = (content: string): string => { |
| 37 | + let regex = RegExp.fromStringWithFlags("```[a-zA-Z]+\\s*[\\s\\S]*?```", ~flags="g") |
| 38 | + String.replaceRegExp(content, regex, "") |
| 39 | +} |
| 40 | + |
| 41 | +let removeFileTitle = (content: string): string => { |
| 42 | + let regex = RegExp.fromStringWithFlags("---\ntitle[\\s\\S]*?---", ~flags="g") |
| 43 | + String.replaceRegExp(content, regex, "") |
| 44 | +} |
| 45 | + |
| 46 | +let removeUnnecessaryBreaks = (content: string): string => { |
| 47 | + let regex = RegExp.fromStringWithFlags("^\n{2,}", ~flags="g") |
| 48 | + String.replaceRegExp(content, regex, "") |
| 49 | +} |
| 50 | + |
| 51 | +let removeToDos = (content: string): string => { |
| 52 | + let regex = RegExp.fromStringWithFlags("<!-- TODO[\\s\\S]*?-->", ~flags="g") |
| 53 | + String.replaceRegExp(content, regex, "") |
| 54 | +} |
| 55 | + |
| 56 | +let fillContentWithVersion = (content: string, version: string): string => { |
| 57 | + let regex = RegExp.fromStringWithFlags("<VERSION>", ~flags="g") |
| 58 | + String.replaceRegExp(content, regex, version) |
| 59 | +} |
| 60 | + |
| 61 | +let createFullFile = (content: string, filePath: string): unit => { |
| 62 | + Node.Fs.appendFileSync(filePath, content ++ "\n", "utf8") |
| 63 | +} |
| 64 | + |
| 65 | +let createSmallFile = (content: string, filePath: string): unit => { |
| 66 | + let smallContent = |
| 67 | + content |
| 68 | + ->removeCodeTabTags |
| 69 | + ->removeFileTitle |
| 70 | + ->removeToDos |
| 71 | + ->removeCodeBlocks |
| 72 | + ->removeUnnecessaryBreaks |
| 73 | + Node.Fs.appendFileSync(filePath, smallContent, "utf8") |
| 74 | +} |
| 75 | + |
| 76 | +let createLlmsFiles = (version: string, docsDirectory: string, llmsDirectory: string): unit => { |
| 77 | + let mdxFileTemplatePath = llmsDirectory->Node.Path.join2("template.mdx") |
| 78 | + let mdxFilePath = docsDirectory->Node.Path.join2(version)->Node.Path.join2("llms.mdx") |
| 79 | + let txtFileTemplatePath = llmsDirectory->Node.Path.join2("template.txt") |
| 80 | + let txtFilePath = llmsDirectory->Node.Path.join2(version)->Node.Path.join2("llms.txt") |
| 81 | + |
| 82 | + Node.Fs.writeFileSync( |
| 83 | + mdxFilePath, |
| 84 | + readMarkdownFile(mdxFileTemplatePath)->fillContentWithVersion(version), |
| 85 | + ) |
| 86 | + |
| 87 | + Node.Fs.writeFileSync( |
| 88 | + txtFilePath, |
| 89 | + readMarkdownFile(txtFileTemplatePath)->fillContentWithVersion(version), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +let processVersions = ( |
| 94 | + versions: array<string>, |
| 95 | + docsDirectory: string, |
| 96 | + llmsDirectory: string, |
| 97 | +): unit => { |
| 98 | + let fullFileName = "llm-full.txt" |
| 99 | + let smallFileName = "llm-small.txt" |
| 100 | + |
| 101 | + versions->Array.forEach(version => { |
| 102 | + let versionDir = docsDirectory->Node.Path.join2(version) |
| 103 | + let llmsDir = llmsDirectory->Node.Path.join2(version) |
| 104 | + let fullFilePath = llmsDir->Node.Path.join2(fullFileName) |
| 105 | + let smallFilePath = llmsDir->Node.Path.join2(smallFileName) |
| 106 | + |
| 107 | + createDirectoryIfNotExists(llmsDir) |
| 108 | + clearFile(fullFilePath) |
| 109 | + clearFile(smallFilePath) |
| 110 | + |
| 111 | + createLlmsFiles(version, docsDirectory, llmsDirectory) |
| 112 | + |
| 113 | + versionDir |
| 114 | + ->collectFiles |
| 115 | + ->Array.forEach(filePath => { |
| 116 | + if String.endsWith(filePath, ".mdx") { |
| 117 | + let content = readMarkdownFile(filePath) |
| 118 | + |
| 119 | + content->createFullFile(fullFilePath) |
| 120 | + |
| 121 | + content->createSmallFile(smallFilePath) |
| 122 | + } |
| 123 | + }) |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +let manualVersions = ["v12.0.0", "v11.0.0"] |
| 128 | +let reactManualVersions = ["latest", "v0.10.0", "v0.11.0"] |
| 129 | + |
| 130 | +let manualDocsDirectory = "pages/docs/manual" |
| 131 | +let reactDocsDirectory = "pages/docs/react" |
| 132 | + |
| 133 | +let manualLlmsDirectory = "public/llms/manual" |
| 134 | +let reactLlmsDirectory = "public/llms/react" |
| 135 | + |
| 136 | +processVersions(manualVersions, manualDocsDirectory, manualLlmsDirectory) |
| 137 | +processVersions(reactManualVersions, reactDocsDirectory, reactLlmsDirectory) |
0 commit comments