Skip to content

Commit ddcda87

Browse files
committed
refactor: use unhead to apply head
1 parent 00a0cc7 commit ddcda87

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

packages/slidev/node/setups/indexHtml.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { ResolvedSlidevOptions, SeoMeta } from '@slidev/types'
2+
import type { ResolvableLink } from 'unhead/types'
23
import { existsSync, readFileSync } from 'node:fs'
34
import { join } from 'node:path'
45
import { slash } from '@antfu/utils'
56
import { white, yellow } from 'ansis'
67
import { escapeHtml } from 'markdown-it/lib/common/utils.mjs'
7-
88
import { createHead, transformHtmlTemplate } from 'unhead/server'
99
import { version } from '../../package.json'
1010
import { getSlideTitle } from '../commands/shared'
@@ -20,17 +20,6 @@ export default async function setupIndexHtml({ mode, entry, clientRoot, userRoot
2020
let head = ''
2121
let body = ''
2222

23-
const { info, author, keywords } = data.headmatter
24-
head += [
25-
`<meta name="slidev:version" content="${version}">`,
26-
mode === 'dev' && `<meta charset="slidev:entry" content="${slash(entry)}">`,
27-
`<link rel="icon" href="${data.config.favicon}">`,
28-
`<title>${getSlideTitle(data)}</title>`,
29-
info && `<meta name="description" content=${toAttrValue(info)}>`,
30-
author && `<meta name="author" content=${toAttrValue(author)}>`,
31-
keywords && `<meta name="keywords" content=${toAttrValue(Array.isArray(keywords) ? keywords.join(', ') : keywords)}>`,
32-
].filter(Boolean).join('\n')
33-
3423
for (const root of roots) {
3524
const path = join(root, 'index.html')
3625
if (!existsSync(path))
@@ -48,27 +37,39 @@ export default async function setupIndexHtml({ mode, entry, clientRoot, userRoot
4837
body += `\n${(index.match(/<body>([\s\S]*?)<\/body>/i)?.[1] || '').trim()}`
4938
}
5039

51-
if (data.features.tweet)
40+
if (data.features.tweet) {
5241
body += '\n<script async src="https://platform.twitter.com/widgets.js"></script>'
42+
}
5343

44+
const webFontsLink: ResolvableLink[] = []
5445
if (data.config.fonts.webfonts.length) {
5546
const { provider } = data.config.fonts
56-
if (provider === 'google')
57-
head += `\n<link rel="stylesheet" href="${generateGoogleFontsUrl(data.config.fonts)}" type="text/css">`
58-
else if (provider === 'coollabs')
59-
head += `\n<link rel="stylesheet" href="${generateCoollabsFontsUrl(data.config.fonts)}" type="text/css">`
60-
}
61-
62-
if (data.headmatter.lang) {
63-
main = main.replace('<html lang="en">', `<html lang="${data.headmatter.lang}">`)
47+
if (provider === 'google') {
48+
webFontsLink.push({ rel: 'stylesheet', href: generateGoogleFontsUrl(data.config.fonts), type: 'text/css' })
49+
}
50+
else if (provider === 'coollabs') {
51+
webFontsLink.push({ rel: 'stylesheet', href: generateCoollabsFontsUrl(data.config.fonts), type: 'text/css' })
52+
}
6453
}
6554

55+
const { info, author, keywords } = data.headmatter
6656
const seoMeta = (data.headmatter.seoMeta ?? {}) as SeoMeta
6757

6858
const unhead = createHead({
6959
init: [
7060
{
61+
htmlAttrs: { lang: (data.headmatter.lang as string | undefined) ?? 'en' },
62+
title: getSlideTitle(data),
63+
link: [
64+
{ rel: 'icon', href: data.config.favicon },
65+
...webFontsLink,
66+
],
7167
meta: [
68+
{ property: 'slidev:version', content: version },
69+
{ charset: 'slidev:entry', content: mode === 'dev' && slash(entry) },
70+
{ name: 'description', content: toAttrValue(info) },
71+
{ name: 'author', content: toAttrValue(author) },
72+
{ name: 'keywords', content: toAttrValue(Array.isArray(keywords) ? keywords.join(', ') : keywords) },
7273
{ property: 'og:title', content: seoMeta.ogTitle },
7374
{ property: 'og:description', content: seoMeta.ogDescription },
7475
{ property: 'og:image', content: seoMeta.ogImage },

0 commit comments

Comments
 (0)