Skip to content

Commit fcade96

Browse files
authored
feat(docs): honor description and keywords frontmatter in docs <head> (#834)
1 parent b515fac commit fcade96

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/routes/$libraryId/$version.docs.$.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export const Route = createFileRoute('/$libraryId/$version/docs/$')({
8181
meta: seo({
8282
title: `${loaderData?.title} | ${library.name} Docs`,
8383
description: loaderData?.description,
84+
keywords: loaderData?.keywords,
8485
image: ogImageUrl(library.id, {
8586
title: loaderData?.title,
8687
description: loaderData?.description,

src/routes/$libraryId/$version.docs.framework.$framework.$.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const Route = createFileRoute(
7676
? `${ctx.loaderData.title} | ${tail}`
7777
: tail,
7878
description: ctx.loaderData?.description,
79+
keywords: ctx.loaderData?.keywords,
7980
image: ogImageUrl(library.id, {
8081
title: ctx.loaderData?.title,
8182
description: ctx.loaderData?.description,

src/utils/docs.functions.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ export const fetchDocs = createServerFn({ method: 'GET' })
243243
}
244244

245245
const frontMatter = extractFrontMatter(file)
246-
const description = removeMarkdown(frontMatter.excerpt ?? '')
246+
const description =
247+
frontMatter.userDescription ?? removeMarkdown(frontMatter.excerpt ?? '')
248+
const keywords = extractFrontMatterKeywords(frontMatter.data.keywords)
247249
const { contentRsc, headings } = await renderMarkdownToRsc(
248250
frontMatter.content,
249251
)
@@ -255,6 +257,7 @@ export const fetchDocs = createServerFn({ method: 'GET' })
255257
contentRsc,
256258
title: frontMatter.data?.title ?? 'Content temporarily unavailable',
257259
description,
260+
keywords,
258261
frameworks: extractFrameworksFromMarkdown(frontMatter.content),
259262
filePath,
260263
headings,
@@ -270,6 +273,7 @@ export const fetchDocsPage = createServerFn({ method: 'GET' })
270273
return {
271274
contentRsc: doc.contentRsc,
272275
description: doc.description,
276+
keywords: doc.keywords,
273277
filePath: doc.filePath,
274278
frontmatter: doc.frontmatter,
275279
frameworks: doc.frameworks,
@@ -278,6 +282,24 @@ export const fetchDocsPage = createServerFn({ method: 'GET' })
278282
}
279283
})
280284

285+
function extractFrontMatterKeywords(value: unknown): string | undefined {
286+
if (Array.isArray(value)) {
287+
const normalized = value
288+
.filter((item): item is string => typeof item === 'string')
289+
.map((item) => item.trim())
290+
.filter((item) => item.length > 0)
291+
292+
return normalized.length > 0 ? normalized.join(', ') : undefined
293+
}
294+
295+
if (typeof value === 'string') {
296+
const trimmed = value.trim()
297+
return trimmed.length > 0 ? trimmed : undefined
298+
}
299+
300+
return undefined
301+
}
302+
281303
export const fetchFile = createServerFn({ method: 'GET' })
282304
.inputValidator(repoFileInput)
283305
.handler(async ({ data }: { data: RepoFileRequest }) => {

src/utils/documents.server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,25 @@ export function extractFrontMatter(content: string) {
421421
excerpt: (file: any) => (file.excerpt = createRichExcerpt(file.content)),
422422
})
423423
const redirectFrom = normalizeRedirectFrom(result.data.redirect_from)
424+
const userDescription =
425+
typeof result.data.description === 'string' &&
426+
result.data.description.trim().length > 0
427+
? result.data.description
428+
: undefined
424429

425430
return {
426431
...result,
427432
data: {
428433
...result.data,
429-
description: createExcerpt(result.content),
434+
description: userDescription ?? createExcerpt(result.content),
430435
redirect_from: redirectFrom,
431436
redirectFrom,
432437
} as { [key: string]: any } & {
433438
description: string
434439
redirect_from?: Array<string>
435440
redirectFrom?: Array<string>
436441
},
442+
userDescription,
437443
}
438444
}
439445

0 commit comments

Comments
 (0)