-
Notifications
You must be signed in to change notification settings - Fork 702
v8.5 refactor: consolidate TOC file handling in cloud document scripts (#21512) #21514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2eed69d
9b7543b
94ca58e
1efee8b
079f8e1
3ad721f
61f5d34
96e9619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as fs from "fs"; | ||
import path from "path"; | ||
import { getAllCloudMdList } from "./utils.js"; | ||
|
||
const allFilePaths = getAllCloudMdList(); | ||
|
||
// Set to store filtered file paths | ||
const filePaths = new Set(); | ||
|
||
// Filter the file paths | ||
for (const filePath of allFilePaths) { | ||
// Skip external links (starting with http/https) | ||
if (filePath.startsWith("http")) { | ||
continue; | ||
} | ||
|
||
// Skip anchor links (starting with #) | ||
if (filePath.startsWith("#")) { | ||
continue; | ||
} | ||
|
||
// Skip files in tidb-cloud folder | ||
if (cleanPath.startsWith("tidb-cloud/")) { | ||
continue; | ||
} | ||
|
||
filePaths.add(cleanPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
// Create tmp directory if it doesn't exist | ||
const tmpDir = "tmp"; | ||
if (!fs.existsSync(tmpDir)) { | ||
fs.mkdirSync(tmpDir, { recursive: true }); | ||
} | ||
|
||
// Copy files to tmp directory | ||
let copiedCount = 0; | ||
let skippedCount = 0; | ||
|
||
for (const filePath of filePaths) { | ||
const sourcePath = filePath; | ||
const targetPath = path.join(tmpDir, filePath); | ||
|
||
// Create target directory if it doesn't exist | ||
const targetDir = path.dirname(targetPath); | ||
if (!fs.existsSync(targetDir)) { | ||
fs.mkdirSync(targetDir, { recursive: true }); | ||
} | ||
|
||
// Check if source file exists | ||
if (fs.existsSync(sourcePath)) { | ||
try { | ||
fs.copyFileSync(sourcePath, targetPath); | ||
console.log(`✓ Copied: ${filePath}`); | ||
copiedCount++; | ||
} catch (error) { | ||
console.error(`✗ Error copying ${filePath}: ${error.message}`); | ||
} | ||
} else { | ||
console.log(`⚠ Skipped (not found): ${filePath}`); | ||
skippedCount++; | ||
} | ||
} | ||
|
||
console.log(`\nSummary:`); | ||
console.log(`- Total files referenced: ${filePaths.size}`); | ||
console.log(`- Files copied: ${copiedCount}`); | ||
console.log(`- Files skipped: ${skippedCount}`); | ||
console.log(`- Files copied to: ${tmpDir}/`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import * as fs from "fs"; | |
import path from "path"; | ||
import axios from "axios"; | ||
import { Octokit } from "octokit"; | ||
import { CLOUD_TOC_LIST, getAllCloudMdList } from "./utils.js"; | ||
|
||
const GH_TOKEN = process.env.GH_TOKEN || ""; | ||
|
||
|
@@ -92,68 +93,29 @@ const deleteFile = (targetFile) => { | |
} | ||
}; | ||
|
||
// read toc file and parse the file paths | ||
const parseTOCFile = (tocPath) => { | ||
try { | ||
if (!fs.existsSync(tocPath)) { | ||
console.log(`TOC file not found: ${tocPath}`); | ||
return new Set(); | ||
} | ||
|
||
const content = fs.readFileSync(tocPath, "utf8"); | ||
const filePaths = new Set(); | ||
|
||
// use regex to match the file paths in markdown links | ||
// match [text](path) format | ||
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g; | ||
let match; | ||
|
||
while ((match = linkRegex.exec(content)) !== null) { | ||
const link = match[2]; | ||
// only process links ending with .md | ||
if (link.endsWith(".md")) { | ||
// remove ./ or / at the beginning to ensure path consistency | ||
const normalizedPath = link.replace(/^\.?\//, ""); | ||
filePaths.add(normalizedPath); | ||
} | ||
} | ||
|
||
console.log(`Found ${filePaths.size} files in TOC: ${tocPath}`); | ||
if (filePaths.size > 0) { | ||
console.log( | ||
"Files in TOC:", | ||
Array.from(filePaths).slice(0, 5).join(", "), | ||
filePaths.size > 5 ? `... and ${filePaths.size - 5} more` : "" | ||
); | ||
} | ||
return filePaths; | ||
} catch (error) { | ||
console.error(`Error parsing TOC file ${tocPath}:`, error); | ||
return new Set(); | ||
} | ||
}; | ||
|
||
// get the file list from the toc file | ||
const getCloudTOCFiles = () => { | ||
// check ./tmp/TOC-tidb-cloud.md first | ||
const tmpTocPath = "./tmp/TOC-tidb-cloud.md"; | ||
const localTocPath = "TOC-tidb-cloud.md"; | ||
const tmpTocFiles = getAllCloudMdList([ | ||
"./tmp/TOC-tidb-cloud.md", | ||
"./tmp/TOC-tidb-cloud-starter.md", | ||
"./tmp/TOC-tidb-cloud-essential.md", | ||
]); | ||
const tocFiles = getAllCloudMdList(CLOUD_TOC_LIST); | ||
Comment on lines
97
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The This change simplifies the function and ensures consistency in how TOC files are handled. const getCloudTOCFiles = () => {
const tmpTocFiles = CLOUD_TOC_LIST.map((tocFile) => `./tmp/${tocFile}`);
const allTocFiles = [...tmpTocFiles, ...CLOUD_TOC_LIST];
const finalTocFiles = new Set(getAllCloudMdList(allTocFiles));
if (finalTocFiles.size === 0) {
console.log("Warning: No TOC file found or no files in TOC. All .md files will be processed.");
}
return finalTocFiles;
}; |
||
|
||
let tocFiles = parseTOCFile(tmpTocPath); | ||
// Convert to Set | ||
const tmpTocFilesSet = new Set(tmpTocFiles); | ||
const tocFilesSet = new Set(tocFiles); | ||
|
||
// if not found in /tmp, check the current directory | ||
if (tocFiles.size === 0) { | ||
console.log(`No files found in ${tmpTocPath}, trying ${localTocPath}`); | ||
tocFiles = parseTOCFile(localTocPath); | ||
} | ||
// Use tmpTocFiles if not empty, otherwise use tocFiles | ||
const finalTocFiles = tmpTocFilesSet.size > 0 ? tmpTocFilesSet : tocFilesSet; | ||
|
||
if (tocFiles.size === 0) { | ||
if (finalTocFiles.size === 0) { | ||
console.log( | ||
"Warning: No TOC file found or no files in TOC. All .md files will be processed." | ||
); | ||
} | ||
|
||
return tocFiles; | ||
return finalTocFiles; | ||
}; | ||
|
||
// filter the files in tmp directory by the toc file | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
cleanPath
is used here but it has not been defined in this scope, which will cause aReferenceError
at runtime. Based on the context of the loop, you likely intended to usefilePath
.