Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions scripts/build-post-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for null or undefined before calling match().

The pipeline failure indicates that str can be null or undefined. Add a null check to avoid a possible TypeError when str.match(...) is invoked.

+ if (!str) return '';
  const match = str.match(anchorTagRegex);
  if (match) {
    // ...

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Actions: PR testing - if Node project

[error] 185-185: TypeError: Cannot read properties of null (reading 'match') in slugifyToC function

if (match) {
// Extract text after the closing anchor tag
str = str.split('</a>')[1]?.trim() || '';
Copy link

Choose a reason for hiding this comment

The 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, create a new variable for clarity and maintainability.

- str = str.split('</a>')[1]?.trim() || '';
+ let headingContent = str.split('</a>')[1]?.trim() || '';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
str = str.split('</a>')[1]?.trim() || '';
let headingContent = str.split('</a>')[1]?.trim() || '';
🧰 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) {
Expand Down
Loading