Skip to content

Commit

Permalink
apply nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Dec 14, 2024
1 parent 5bed511 commit 89f7fbc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
14 changes: 9 additions & 5 deletions scripts/build-post-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ async function buildPostList(postDirectories, basePath, writeFilePath) {
try {

if (!basePath || !writeFilePath) {
throw new Error('Error while building post list: basePath and writeFilePath are required');
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`);
}

if (postDirectories.length === 0) {
Expand Down Expand Up @@ -57,7 +60,7 @@ async function walkDirectories(directories, resultObj, basePath, sectionTitle, s
const fileName = posix.join(directory, file);
const fileNameWithSection = posix.join(fileName, '_section.mdx')
const normalizedSlug = posix.join('/', relative(basePath, fileName))
const slug = normalizedSlug.replace(/\\/g,'/')
const slug = normalizedSlug.replace(/\\/g, '/')
const slugElements = slug.split('/')

if (await isDirectory(fileName)) {
Expand Down Expand Up @@ -143,15 +146,16 @@ async function walkDirectories(directories, resultObj, basePath, sectionTitle, s
}

function slugifyToC(str) {
if (typeof str !== 'string') return '';
let slug = '';

// Match heading IDs like {# myHeadingId}
const headingIdMatch = str.match(/[\s]?\{\#([\w\d\-_]+)\}/);
const headingIdMatch = str.match(/[\s]*\{#([a-zA-Z0-9\-_]+)\}/);
if (headingIdMatch && headingIdMatch[1].trim()) {
slug = headingIdMatch[1];
} else {
// Match heading IDs like {<a name="myHeadingId"/>}
const anchorTagMatch = str.match(/[\s]*<a[\s]+name="([\w\d\-_]+)"/);
const anchorTagMatch = str.match(/[\s]*<a[\s]+name="([a-zA-Z0-9\-_]+)"/);
if (anchorTagMatch && anchorTagMatch[1].trim()) {
slug = anchorTagMatch[1];
}
Expand All @@ -166,7 +170,7 @@ async function isDirectory(dir) {
}

function capitalize(text) {
return text.split(/[\s\-]/g).map(word => `${word[0].toUpperCase()}${word.substr(1)}`).join(' ')
return text.replace(/(?:^|\s|-)([a-z])/g, (_, char) => char.toUpperCase())
}

module.exports = { slugifyToC, buildPostList }
22 changes: 18 additions & 4 deletions tests/build-post-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ describe('buildPostList', () => {
await fs.remove(tempDir);
});

it('writes the result to a file', async () => {
it('writes a valid JSON file with the expected structure', async () => {
await buildPostList(postDirectories, tempDir, writeFilePath);

const outputExists = await fs.pathExists(writeFilePath);
expect(outputExists).toBe(true);

const content = await fs.readFile(writeFilePath, 'utf-8');
expect(() => JSON.parse(content)).not.toThrow();
});

it('correctly structures docs entries', async () => {
Expand Down Expand Up @@ -134,7 +137,7 @@ describe('buildPostList', () => {
const invalidDir = [join(tempDir, 'non-existent-dir'), '/invalid'];
await expect(
buildPostList([invalidDir], tempDir, writeFilePath),
).rejects.toThrow();
).rejects.toThrow(/Error while building post list: ENOENT/);
});

it('does not process specification files without a title', async () => {
Expand Down Expand Up @@ -209,15 +212,15 @@ describe('buildPostList', () => {
await expect(
buildPostList(postDirectories, undefined, writeFilePath),
).rejects.toThrow(
/Error while building post list: basePath and writeFilePath are required/,
"Error while building post list: Error while building post list: basePath is required",
);
});

it('throws specific error message when writeFilePath parameter is undefined', async () => {
await expect(
buildPostList(postDirectories, tempDir, undefined),
).rejects.toThrow(
/Error while building post list: basePath and writeFilePath are required/,
"Error while building post list: Error while building post list: writeFilePath is required",
);
});

Expand Down Expand Up @@ -245,5 +248,16 @@ describe('buildPostList', () => {
it('handles mixed format heading IDs', () => {
expect(slugifyToC('## Heading {#id} {<a name="name"/>}')).toBe('id');
});

it('handles invalid input types gracefully', () => {
expect(slugifyToC(null)).toBe('');
expect(slugifyToC(undefined)).toBe('');
expect(slugifyToC(123)).toBe('');
});

it('ignores invalid characters in heading IDs', () => {
expect(slugifyToC('## Heading {#invalid@id}')).toBe('');
expect(slugifyToC('## Heading {#invalid spaces}')).toBe('');
});
});
});

0 comments on commit 89f7fbc

Please sign in to comment.