From bbc79e670e12c959482ea67d2525fd717d4d4a3f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 2 Jun 2025 23:00:59 +0800 Subject: [PATCH] Add firendly heading id to changelog entry (#13549) --- scripts/draft-blog-post.js | 2 +- scripts/utils/changelog.js | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/scripts/draft-blog-post.js b/scripts/draft-blog-post.js index 1791b9061..e20cc778e 100644 --- a/scripts/draft-blog-post.js +++ b/scripts/draft-blog-post.js @@ -49,7 +49,7 @@ for (const dir of changelogUnreleasedDirs) { throw new Error("Unknown category: " + dir.name); } - category.entries = getEntries(dirPath); + category.entries = getEntries(dirPath, { useFriendlyHeadingId: true }); } for (const filePath of fg.sync(postGlob)) { diff --git a/scripts/utils/changelog.js b/scripts/utils/changelog.js index 70ff104b5..2932f7b4d 100644 --- a/scripts/utils/changelog.js +++ b/scripts/utils/changelog.js @@ -39,15 +39,14 @@ export const changelogUnreleasedDirs = fs }) .filter((entry) => entry.isDirectory()); -export function getEntries(dirPath) { +export function getEntries(dirPath, options) { const fileNames = fs .readdirSync(dirPath) .filter((fileName) => path.extname(fileName) === ".md"); + const useFriendlyHeadingId = options?.useFriendlyHeadingId; const entries = fileNames.map((fileName) => { - const [title, ...rest] = fs - .readFileSync(path.join(dirPath, fileName), "utf8") - .trim() - .split("\n"); + const filePath = path.join(dirPath, fileName); + let [title, ...rest] = fs.readFileSync(filePath, "utf8").trim().split("\n"); const improvement = title.match(/\[IMPROVEMENT(:(\d+))?\]/u); @@ -64,8 +63,15 @@ export function getEntries(dirPath) { ? Number(improvement[2]) : undefined; - const content = [processTitle(title), ...rest].join("\n"); + title = processTitle(title); + if (useFriendlyHeadingId) { + // `1000` or `1000-2` + const basename = path.basename(filePath, ".md"); + title += ` {#change-${basename}}`; + } + + const content = [title, ...rest].join("\n"); return { fileName, section, order, content }; }); return entries;