Skip to content

Commit 0123276

Browse files
committed
refactor: extract title generation logic into a separate function
1 parent 968a4e4 commit 0123276

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/build/mdn-comments.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ async function walkDirectory(dir: URL): Promise<URL[]> {
6868
return results;
6969
}
7070

71+
function generateTitle(content: string, file: URL) {
72+
const match = content.match(/title:\s*["']?([^"'\n]+)["']?/);
73+
if (match) {
74+
return match[1].replace(/ extension$/, "").split(":")[0];
75+
}
76+
return basename(file.pathname) || "";
77+
}
78+
7179
export async function generateDescriptions(): Promise<Record<string, string>> {
7280
const stats = await fs.stat(basePath);
7381
if (!stats.isDirectory()) {
@@ -84,10 +92,7 @@ export async function generateDescriptions(): Promise<Record<string, string>> {
8492
try {
8593
const content = await fs.readFile(fileURL, "utf-8");
8694

87-
const titleMatch = content.match(/title:\s*["']?([^"'\n]+)["']?/);
88-
const title = titleMatch
89-
? titleMatch[1].replace(/ extension$/, "").split(":")[0]
90-
: basename(fileURL.pathname) || "";
95+
const title = generateTitle(content, fileURL);
9196

9297
const relPath = fileURL.pathname
9398
.replace(basePath.pathname, "")

0 commit comments

Comments
 (0)