Skip to content

Commit

Permalink
added generator for file iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
sahitya-chandra committed Jan 21, 2025
1 parent e9dd904 commit 44884e4
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions scripts/markdown/check-edit-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,42 @@ function determineEditLink(urlPath, filePath, editOptions) {
* @param {object[]} [result=[]] - Accumulator for results
* @returns {Promise<object[]>} Array of objects containing file paths and edit links
*/
async function generatePaths(folderPath, editOptions, relativePath = '', result = []) {
try {
const files = await fs.readdir(folderPath);

await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const relativeFilePath = path.join(relativePath, file);

// Skip _section.md files
if (file === '_section.md') {
return;
}
async function* walkDirectory(dir) {

const stats = await fs.stat(filePath);

if (stats.isDirectory()) {
await generatePaths(filePath, editOptions, relativeFilePath, result);
} else if (stats.isFile() && file.endsWith('.md')) {
const urlPath = relativeFilePath.split(path.sep).join('/').replace('.md', '');
result.push({
filePath,
urlPath,
editLink: determineEditLink(urlPath, filePath, editOptions)
});
}
})
);
try {
const files = await fs.readdir(dir);
for (const file of files) {
if (file === '_section.md') continue;

const filePath = path.join(dir, file);
const stats = await fs.stat(filePath);

if (stats.isDirectory()) {
yield* walkDirectory(filePath);
} else if (stats.isFile() && file.endsWith('.md')) {
yield filePath;
}
}
} catch (err) {
throw new Error(`Error processing directory ${dir}: ${err.message}`);
}
}

async function generatePaths(folderPath, editOptions) {

const result = [];
try {
for await (const filePath of walkDirectory(folderPath)) {
const relativePath = path.relative(folderPath, filePath);
const urlPath = relativePath.split(path.sep).join('/').replace('.md', '');

result.push({
filePath,
urlPath,
editLink: determineEditLink(urlPath, filePath, editOptions)
});
}
return result;
} catch (err) {
throw new Error(`Error processing directory ${folderPath}: ${err.message}`);
Expand Down

0 comments on commit 44884e4

Please sign in to comment.