-
-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: [BUG] Blog Table of contents links not working #3540 #3543
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -172,28 +172,28 @@ async function walkDirectories( | |||||
} | ||||||
} | ||||||
|
||||||
// Matches heading IDs in two formats: | ||||||
// 1. {#my-heading-id} | ||||||
// 2. <a name="my-heading-id"> | ||||||
const HEADING_ID_REGEX = /[\s]*(?:\{#([a-zA-Z0-9\-_]+)\}|<a[\s]+name="([a-zA-Z0-9\-_]+)")/; | ||||||
// Matches heading IDs in one formats: | ||||||
// 1. <a name=""></a>my heading id | ||||||
const anchorTagRegex =/<a\s+name="([a-zA-Z0-9\-_]+)"/ | ||||||
|
||||||
/** | ||||||
* Extracts heading IDs from markdown headings | ||||||
* @param {string} str - The heading text containing potential ID | ||||||
* @returns {string} The extracted ID or empty string if no valid ID found | ||||||
*/ | ||||||
function slugifyToC(str) { | ||||||
const match = str.match(anchorTagRegex); | ||||||
if (match) { | ||||||
// Extract text after the closing anchor tag | ||||||
str = str.split('</a>')[1]?.trim() || ''; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid reassigning function parameter. ESLint flags this as a "no-param-reassign" issue, which can lead to unexpected side effects. Instead of mutating the incoming parameter - str = str.split('</a>')[1]?.trim() || '';
+ let headingContent = str.split('</a>')[1]?.trim() || ''; 📝 Committable suggestion
Suggested change
🧰 Tools🪛 eslint[error] 188-188: Assignment to function parameter 'str'. (no-param-reassign) |
||||||
} | ||||||
if (typeof str !== 'string') return ''; | ||||||
if (!str.trim()) return ''; | ||||||
let slug = ''; | ||||||
|
||||||
// Match heading IDs like {# myHeadingId} | ||||||
const idMatch = str.match(HEADING_ID_REGEX); | ||||||
const [, headingId, anchorId] = idMatch || []; | ||||||
slug = (headingId || anchorId || '').trim(); | ||||||
|
||||||
// If no valid ID is found, return an empty string | ||||||
return slug; | ||||||
return str | ||||||
.toLowerCase() | ||||||
.trim() | ||||||
.replace(/\s+/g, '-') // Replace spaces with hyphens | ||||||
.replace(/[^\w\-]+/g, ''); // Remove non-alphanumeric characters except hyphens | ||||||
} | ||||||
|
||||||
async function isDirectory(dir) { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for null or undefined before calling
match()
.The pipeline failure indicates that
str
can benull
orundefined
. Add a null check to avoid a possible TypeError whenstr.match(...)
is invoked.+ if (!str) return ''; const match = str.match(anchorTagRegex); if (match) { // ...
🧰 Tools
🪛 GitHub Actions: PR testing - if Node project
[error] 185-185: TypeError: Cannot read properties of null (reading 'match') in slugifyToC function