Skip to content

Commit

Permalink
apply nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Dec 17, 2024
1 parent a4403bd commit fcff3ed
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions scripts/build-post-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ async function buildPostList(postDirectories, basePath, writeFilePath) {
try {

if (!basePath || !writeFilePath) {
const missing = [];
if (!basePath) missing.push('basePath');
if (!writeFilePath) missing.push('writeFilePath');
throw new Error(`Error while building post list: ${missing.join(' and ')} ${missing.length > 1 ? 'are' : 'is'} required`);
const missing = [
!basePath && 'basePath',
!writeFilePath && 'writeFilePath'
].filter(Boolean);
throw new Error(`Error while building post list: ${missing.join(' and ')} ${missing.length > 1 ? 'are' : 'is'} required`);
}

if (postDirectories.length === 0) {
Expand Down Expand Up @@ -69,7 +70,10 @@ async function walkDirectories(
for (let dir of directories) {
const directory = posix.normalize(dir[0]);
const sectionSlug = dir[1] || '';
const files = await readdir(directory)
const files = await Promise.all([
readdir(directory),
pathExists(directory)
]).then(([files]) => files)

for (let file of files) {
let details;
Expand Down Expand Up @@ -170,18 +174,9 @@ function slugifyToC(str) {
let slug = '';

// Match heading IDs like {# myHeadingId}
const headingIdMatch = str.match(/[\s]*\{#([a-zA-Z0-9\-_]+)\}/);
const [, headingId] = headingIdMatch || [];
if (headingId?.trim()) {
slug = headingId;
} else {
// Match heading IDs like {<a name="myHeadingId"/>}
const anchorTagMatch = str.match(/[\s]*<a[\s]+name="([a-zA-Z0-9\-_]+)"/);
const [, anchorId] = anchorTagMatch || [];
if (anchorId?.trim()) {
slug = anchorId;
}
}
const idMatch = str.match(/[\s]*(?:\{#([a-zA-Z0-9\-_]+)\}|<a[\s]+name="([a-zA-Z0-9\-_]+)")/);
const [, headingId, anchorId] = idMatch || [];
slug = (headingId || anchorId || '').trim();

// If no valid ID is found, return an empty string
return slug;
Expand Down

0 comments on commit fcff3ed

Please sign in to comment.