|
| 1 | +// @ts-check |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +/** |
| 10 | + * Extracts the "## Returns" section from a markdown file and writes it to a separate file. |
| 11 | + * @param {string} filePath - The path to the markdown file |
| 12 | + * @returns {boolean} True if a file was created |
| 13 | + */ |
| 14 | +function extractReturnsSection(filePath) { |
| 15 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 16 | + |
| 17 | + // Find the "## Returns" section |
| 18 | + const returnsStart = content.indexOf('## Returns'); |
| 19 | + |
| 20 | + if (returnsStart === -1) { |
| 21 | + return false; // No Returns section found |
| 22 | + } |
| 23 | + |
| 24 | + // Find the next heading after "## Returns" (or end of file) |
| 25 | + const afterReturns = content.slice(returnsStart + 10); // Skip past "## Returns" |
| 26 | + const nextHeadingMatch = afterReturns.match(/\n## /); |
| 27 | + const returnsEnd = |
| 28 | + nextHeadingMatch && typeof nextHeadingMatch.index === 'number' |
| 29 | + ? returnsStart + 10 + nextHeadingMatch.index |
| 30 | + : content.length; |
| 31 | + |
| 32 | + // Extract the Returns section and trim trailing whitespace |
| 33 | + const returnsContent = content.slice(returnsStart, returnsEnd).trimEnd(); |
| 34 | + |
| 35 | + // Generate the new filename: use-auth.mdx -> use-auth-return.mdx |
| 36 | + const fileName = path.basename(filePath, '.mdx'); |
| 37 | + const dirName = path.dirname(filePath); |
| 38 | + const newFilePath = path.join(dirName, `${fileName}-return.mdx`); |
| 39 | + |
| 40 | + // Write the extracted Returns section to the new file |
| 41 | + fs.writeFileSync(newFilePath, returnsContent, 'utf-8'); |
| 42 | + |
| 43 | + console.log(`[extract-returns] Created ${path.relative(process.cwd(), newFilePath)}`); |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Replaces generic type names in the parameters table with expanded types. |
| 49 | + * @param {string} content |
| 50 | + * @returns {string} |
| 51 | + */ |
| 52 | +function replaceGenericTypesInParamsTable(content) { |
| 53 | + // Replace Fetcher in the parameters table |
| 54 | + content = content.replace( |
| 55 | + /(\|\s*`fetcher`\s*\|\s*)`Fetcher`(\s*\|)/g, |
| 56 | + '$1`Fetcher extends (...args: any[]) => Promise<any>`$2', |
| 57 | + ); |
| 58 | + return content; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Extracts the "## Parameters" section from a markdown file and writes it to a separate file. |
| 63 | + * @param {string} filePath - The path to the markdown file |
| 64 | + * @returns {boolean} True if a file was created |
| 65 | + */ |
| 66 | +function extractParametersSection(filePath) { |
| 67 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 68 | + const fileName = path.basename(filePath, '.mdx'); |
| 69 | + const dirName = path.dirname(filePath); |
| 70 | + |
| 71 | + // Always use -params suffix |
| 72 | + const suffix = '-params'; |
| 73 | + const targetFileName = `${fileName}${suffix}.mdx`; |
| 74 | + const propsFileName = `${fileName}-props.mdx`; |
| 75 | + |
| 76 | + // Delete any existing -props file (TypeDoc-generated) |
| 77 | + const propsFilePath = path.join(dirName, propsFileName); |
| 78 | + if (fs.existsSync(propsFilePath)) { |
| 79 | + fs.unlinkSync(propsFilePath); |
| 80 | + console.log(`[extract-returns] Deleted ${path.relative(process.cwd(), propsFilePath)}`); |
| 81 | + } |
| 82 | + |
| 83 | + // Find the "## Parameters" section |
| 84 | + const paramsStart = content.indexOf('## Parameters'); |
| 85 | + |
| 86 | + if (paramsStart === -1) { |
| 87 | + return false; // No Parameters section found |
| 88 | + } |
| 89 | + |
| 90 | + // Find the next heading after "## Parameters" (or end of file) |
| 91 | + const afterParams = content.slice(paramsStart + 13); // Skip past "## Parameters" |
| 92 | + const nextHeadingMatch = afterParams.match(/\n## /); |
| 93 | + const paramsEnd = |
| 94 | + nextHeadingMatch && typeof nextHeadingMatch.index === 'number' |
| 95 | + ? paramsStart + 13 + nextHeadingMatch.index |
| 96 | + : content.length; |
| 97 | + |
| 98 | + // Extract the Parameters section and trim trailing whitespace |
| 99 | + const paramsContent = content.slice(paramsStart, paramsEnd).trimEnd(); |
| 100 | + const processedParams = replaceGenericTypesInParamsTable(paramsContent); |
| 101 | + |
| 102 | + // Write to new file |
| 103 | + const newFilePath = path.join(dirName, targetFileName); |
| 104 | + fs.writeFileSync(newFilePath, processedParams, 'utf-8'); |
| 105 | + |
| 106 | + console.log(`[extract-returns] Created ${path.relative(process.cwd(), newFilePath)}`); |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Recursively reads all .mdx files in a directory, excluding generated files |
| 112 | + * @param {string} dir - The directory to read |
| 113 | + * @returns {string[]} Array of file paths |
| 114 | + */ |
| 115 | +function getAllMdxFiles(dir) { |
| 116 | + /** @type {string[]} */ |
| 117 | + const files = []; |
| 118 | + |
| 119 | + if (!fs.existsSync(dir)) { |
| 120 | + return files; |
| 121 | + } |
| 122 | + |
| 123 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 124 | + |
| 125 | + for (const entry of entries) { |
| 126 | + const fullPath = path.join(dir, entry.name); |
| 127 | + |
| 128 | + if (entry.isDirectory()) { |
| 129 | + files.push(...getAllMdxFiles(fullPath)); |
| 130 | + } else if (entry.isFile() && entry.name.endsWith('.mdx')) { |
| 131 | + // Exclude generated files |
| 132 | + const isGenerated = |
| 133 | + entry.name.endsWith('-return.mdx') || entry.name.endsWith('-params.mdx') || entry.name.endsWith('-props.mdx'); |
| 134 | + if (!isGenerated) { |
| 135 | + files.push(fullPath); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return files; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Main function to process all clerk-react files |
| 145 | + */ |
| 146 | +function main() { |
| 147 | + const packages = ['clerk-react']; |
| 148 | + const dirs = packages.map(folder => path.join(__dirname, 'temp-docs', folder)); |
| 149 | + |
| 150 | + for (const dir of dirs) { |
| 151 | + if (!fs.existsSync(dir)) { |
| 152 | + console.log(`[extract-returns] ${dir} directory not found, skipping extraction`); |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + const mdxFiles = getAllMdxFiles(dir); |
| 157 | + console.log(`[extract-returns] Processing ${mdxFiles.length} files in ${dir}/`); |
| 158 | + |
| 159 | + let returnsCount = 0; |
| 160 | + let paramsCount = 0; |
| 161 | + |
| 162 | + for (const filePath of mdxFiles) { |
| 163 | + // Extract Returns sections |
| 164 | + if (extractReturnsSection(filePath)) { |
| 165 | + returnsCount++; |
| 166 | + } |
| 167 | + |
| 168 | + // Extract Parameters sections |
| 169 | + if (extractParametersSection(filePath)) { |
| 170 | + paramsCount++; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + console.log(`[extract-returns] Extracted ${returnsCount} Returns sections`); |
| 175 | + console.log(`[extract-returns] Extracted ${paramsCount} Parameters sections`); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +main(); |
0 commit comments