docs: update recommendation for canonical urls to use transformPageData instead

pull/3488/head
Divyansh Singh 10 months ago
parent c882fa1469
commit ba3c6441be

@ -649,15 +649,16 @@ export default {
```ts
export default {
transformHead({ page }) {
// Skip the 404 page
if (page !== '404.md') {
const canonicalUrl = `https://example.com/${page}`
transformPageData(pageData) {
const canonicalUrl = `https://example.com/${pageData.relativePath}`
.replace(/index\.md$/, '')
.replace(/\.md$/, '.html')
return [['link', { rel: 'canonical', href: canonicalUrl }]]
}
pageData.frontmatter.head ??= []
pageData.frontmatter.head.push([
'link',
{ rel: 'canonical', href: canonicalUrl }
])
}
}
```

Loading…
Cancel
Save