Skip to content

Commit

Permalink
apply coderabbit suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Dec 10, 2024
1 parent 4893712 commit 10d1b72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 14 additions & 9 deletions scripts/build-post-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,22 @@ async function walkDirectories(directories, resultObj, basePath, sectionTitle, s
}

function slugifyToC(str) {
let slug
// Try to match heading ids like {# myHeadingId}
const headingIdMatch = str.match(/[\s]?\{\#([\w\d\-_]+)\}/)
if (headingIdMatch && headingIdMatch.length >= 2) {
slug = headingIdMatch[1]
let slug = '';

// Match heading IDs like {# myHeadingId}
const headingIdMatch = str.match(/[\s]?\{\#([\w\d\-_]+)\}/);
if (headingIdMatch && headingIdMatch[1].trim()) {
slug = headingIdMatch[1];
} else {
// Try to match heading ids like {<a name="myHeadingId"/>}
const anchorTagMatch = str.match(/[\s]*<a[\s]+name="([\w\d\s\-_]+)"/)
if (anchorTagMatch && anchorTagMatch.length >= 2) slug = anchorTagMatch[1]
// Match heading IDs like {<a name="myHeadingId"/>}
const anchorTagMatch = str.match(/[\s]*<a[\s]+name="([\w\d\-_]+)"/);
if (anchorTagMatch && anchorTagMatch[1].trim()) {
slug = anchorTagMatch[1];
}
}
return slug || slugify(str, { firsth1: true, maxdepth: 6 })

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

async function isDirectory(dir) {
Expand Down
10 changes: 10 additions & 0 deletions tests/build-post-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,15 @@ describe('buildPostList', () => {
it('handles empty strings', () => {
expect(slugifyToC('')).toBe('');
});

it('returns empty string for malformed heading IDs', () => {
expect(slugifyToC('## Heading {#}')).toBe('');
expect(slugifyToC('## Heading {# }')).toBe('');
expect(slugifyToC('## Heading {<a name=""/>}')).toBe('');
});

it('handles mixed format heading IDs', () => {
expect(slugifyToC('## Heading {#id} {<a name="name"/>}')).toBe('id');
});
});
});

0 comments on commit 10d1b72

Please sign in to comment.