Skip to content

Commit 97dc554

Browse files
committed
fix bug in script which shows duplicates
1 parent f071dbb commit 97dc554

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

plugins/frontmatter-validation/customParseFrontMatter.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const path = require('path');
33

44
// List to track files with issues
5-
const filesWithIssues = [];
5+
let filesWithIssues = [];
66

77
/**
88
* Custom frontmatter parser that enforces specific formatting rules
@@ -254,28 +254,46 @@ async function customParseFrontMatter(params) {
254254

255255
// If there are issues, add to the tracking list
256256
if (issues.length > 0) {
257-
filesWithIssues.push({
258-
filePath: relativePath,
259-
issues
260-
});
257+
// Check if we already have an entry for this file path
258+
const existingIndex = filesWithIssues.findIndex(item => item.filePath === relativePath);
259+
if (existingIndex !== -1) {
260+
// Update existing entry instead of adding a duplicate
261+
filesWithIssues[existingIndex].issues = issues;
262+
} else {
263+
// Add new entry
264+
filesWithIssues.push({
265+
filePath: relativePath,
266+
issues
267+
});
268+
}
261269
}
262270

263271
return parsedData;
264272
} catch (error) {
265273
console.error(`Error parsing frontmatter in ${relativePath}:`, error);
266-
filesWithIssues.push({
267-
filePath: relativePath,
268-
issues: [`parsing error: ${error.message}`]
269-
});
274+
275+
// Check if we already have an entry for this file path
276+
const existingIndex = filesWithIssues.findIndex(item => item.filePath === relativePath);
277+
if (existingIndex !== -1) {
278+
// Update existing entry instead of adding a duplicate
279+
filesWithIssues[existingIndex].issues = [`parsing error: ${error.message}`];
280+
} else {
281+
// Add new entry
282+
filesWithIssues.push({
283+
filePath: relativePath,
284+
issues: [`parsing error: ${error.message}`]
285+
});
286+
}
287+
270288
return await defaultParseFrontMatter(params);
271289
}
272290
}
273291

274292
// Export the function and the issue tracker for use in the build plugin
275293
module.exports = {
276294
customParseFrontMatter,
277-
getFilesWithIssues: () => filesWithIssues,
295+
getFilesWithIssues: () => [...filesWithIssues], // Return a copy to prevent external modifications
278296
resetIssues: () => {
279-
filesWithIssues.length = 0;
297+
filesWithIssues = []; // Replace the array instead of modifying it
280298
}
281299
};

0 commit comments

Comments
 (0)