| 1 | import type { ResolvedSlidevOptions, SeoMeta } from '@slidev/types' |
| 2 | import type { ResolvableLink } from 'unhead/types' |
| 3 | import { existsSync } from 'node:fs' |
| 4 | import { readFile } from 'node:fs/promises' |
| 5 | import { slash } from '@antfu/utils' |
| 6 | import { white, yellow } from 'ansis' |
| 7 | import { join } from 'pathe' |
| 8 | import { parseHtmlForUnheadExtraction } from 'unhead/parser' |
| 9 | import { createHead, transformHtmlTemplate } from 'unhead/server' |
| 10 | import { version } from '../../package.json' |
| 11 | import { getSlideTitle } from '../commands/shared' |
| 12 | import { toAtFS } from '../resolver' |
| 13 | import { generateCoollabsFontsUrl, generateGoogleFontsUrl } from '../utils' |
| 14 | |
| 15 | const RE_TRAILING_SLASH = /\/$/ |
| 16 | const RE_BODY_CONTENT = /<body>([\s\S]*?)<\/body>/i |
| 17 | |
| 18 | function escapeHtml(str: string): string { |
| 19 | return str |
| 20 | .replace(/&/g, '&') |
| 21 | .replace(/</g, '<') |
| 22 | .replace(/>/g, '>') |
| 23 | .replace(/"/g, '"') |
| 24 | .replace(/'/g, ''') |
| 25 | } |
| 26 | |
| 27 | function toAttrValue(unsafe: unknown) { |
| 28 | return JSON.stringify(escapeHtml(String(unsafe))) |
| 29 | } |
| 30 | |
| 31 | function collectPreloadImages(data: Omit<ResolvedSlidevOptions, 'utils'>['data'], base?: string): ResolvableLink[] { |
| 32 | const config = data.config |
| 33 | if (config.preloadImages === false) |
| 34 | return [] |
| 35 | |
| 36 | const seen = new Set<string>() |
| 37 | const links: ResolvableLink[] = [] |
| 38 | const basePrefix = base |
| 39 | ? base.replace(RE_TRAILING_SLASH, '') |
| 40 | : '' |
| 41 | |
| 42 | for (const slide of data.slides) { |
| 43 | const images = slide.images || slide.source?.images |
| 44 | if (!images?.length) |
| 45 | continue |
| 46 | for (const url of images) { |
| 47 | if (seen.has(url)) |
| 48 | continue |
| 49 | seen.add(url) |
| 50 | const href = url.startsWith('http') || url.startsWith('//') |
| 51 | ? url |
| 52 | : `${basePrefix}${url.startsWith('/') |
| 53 | ? url |
| 54 | : `/${url}`}` |
| 55 | links.push({ rel: 'preload', as: 'image', href }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return links |
| 60 | } |
| 61 | |
| 62 | export default async function setupIndexHtml({ mode, entry, clientRoot, userRoot, roots, data, base }: Omit<ResolvedSlidevOptions, 'utils'>): Promise<string> { |
| 63 | let main = await readFile(join(clientRoot, 'index.html'), 'utf-8') |
| 64 | let body = '' |
| 65 | |
| 66 | const inputs: any[] = [] |
| 67 | |
| 68 | for (const root of roots) { |
| 69 | const path = join(root, 'index.html') |
| 70 | if (!existsSync(path)) |
| 71 | continue |
| 72 | |
| 73 | const html = await readFile(path, 'utf-8') |
| 74 | |
| 75 | if (root === userRoot && html.includes('<!DOCTYPE')) { |
| 76 | console.error(yellow(`[Slidev] Ignored provided index.html with doctype declaration. (${white(path)})`)) |
| 77 | console.error(yellow('This file may be generated by Slidev, please remove it from your project.')) |
| 78 | continue |
| 79 | } |
| 80 | |
| 81 | inputs.push(parseHtmlForUnheadExtraction(html).input) |
| 82 | body += `\n${(html.match(RE_BODY_CONTENT)?.[1] || '').trim()}` |
| 83 | } |
| 84 | |
| 85 | if (data.features.tweet) { |
| 86 | body += '\n<script async src="https://platform.twitter.com/widgets.js"></script>' |
| 87 | } |
| 88 | |
| 89 | if (data.features.bluesky) { |
| 90 | body += '\n<script async src="https://embed.bsky.app/static/embed.js" charset="utf-8"></script>' |
| 91 | } |
| 92 | |
| 93 | const webFontsLink: ResolvableLink[] = [] |
| 94 | if (data.config.fonts.webfonts.length) { |
| 95 | const { provider } = data.config.fonts |
| 96 | if (provider === 'google') { |
| 97 | webFontsLink.push({ |
| 98 | rel: 'stylesheet', |
| 99 | href: generateGoogleFontsUrl(data.config.fonts), |
| 100 | type: 'text/css', |
| 101 | }) |
| 102 | } |
| 103 | else if (provider === 'coollabs') { |
| 104 | webFontsLink.push({ |
| 105 | rel: 'stylesheet', |
| 106 | href: generateCoollabsFontsUrl(data.config.fonts), |
| 107 | type: 'text/css', |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | const { info, author, keywords } = data.headmatter |
| 113 | const seoMeta = (data.headmatter.seoMeta ?? {}) as SeoMeta |
| 114 | |
| 115 | const ogImage = seoMeta.ogImage === 'auto' |
| 116 | ? './og-image.png' |
| 117 | : seoMeta.ogImage |
| 118 | ? seoMeta.ogImage |
| 119 | : existsSync(join(userRoot, 'og-image.png')) |
| 120 | ? './og-image.png' |
| 121 | : undefined |
| 122 | |
| 123 | const title = getSlideTitle(data) |
| 124 | const description = info |
| 125 | ? toAttrValue(info) |
| 126 | : null |
| 127 | const unhead = createHead({ |
| 128 | init: [ |
| 129 | { |
| 130 | htmlAttrs: data.headmatter.lang |
| 131 | ? { lang: data.headmatter.lang as string } |
| 132 | : undefined, |
| 133 | title, |
| 134 | link: [ |
| 135 | data.config.favicon |
| 136 | ? { rel: 'icon', href: data.config.favicon } |
| 137 | : null, |
| 138 | ...webFontsLink, |
| 139 | ...collectPreloadImages(data, base), |
| 140 | ].filter(x => x), |
| 141 | meta: [ |
| 142 | { 'http-equiv': 'Content-Type', 'content': 'text/html; charset=UTF-8' }, |
| 143 | { property: 'slidev:version', content: version }, |
| 144 | { |
| 145 | property: 'slidev:entry', |
| 146 | content: mode === 'dev' |
| 147 | ? slash(entry) |
| 148 | : null, |
| 149 | }, |
| 150 | { name: 'description', content: description }, |
| 151 | { |
| 152 | name: 'author', |
| 153 | content: author |
| 154 | ? toAttrValue(author) |
| 155 | : null, |
| 156 | }, |
| 157 | { |
| 158 | name: 'keywords', |
| 159 | content: keywords |
| 160 | ? toAttrValue(Array.isArray(keywords) |
| 161 | ? keywords.join(', ') |
| 162 | : keywords) |
| 163 | : null, |
| 164 | }, |
| 165 | { property: 'og:title', content: seoMeta.ogTitle || title }, |
| 166 | { property: 'og:description', content: seoMeta.ogDescription || description }, |
| 167 | { property: 'og:image', content: ogImage }, |
| 168 | { property: 'og:url', content: seoMeta.ogUrl }, |
| 169 | { property: 'twitter:card', content: seoMeta.twitterCard }, |
| 170 | { property: 'twitter:site', content: seoMeta.twitterSite }, |
| 171 | { property: 'twitter:title', content: seoMeta.twitterTitle }, |
| 172 | { property: 'twitter:description', content: seoMeta.twitterDescription }, |
| 173 | { property: 'twitter:image', content: seoMeta.twitterImage }, |
| 174 | { property: 'twitter:url', content: seoMeta.twitterUrl }, |
| 175 | ].filter(x => x.content), |
| 176 | }, |
| 177 | ...inputs, |
| 178 | ], |
| 179 | }) |
| 180 | |
| 181 | const mainUrl = toAtFS(join(clientRoot, 'main.ts')) |
| 182 | if (mode === 'build') { |
| 183 | main = main.replace('__ENTRY__', mainUrl) |
| 184 | } |
| 185 | else { |
| 186 | const basePrefix = base |
| 187 | ? base.slice(0, -1) |
| 188 | : '' |
| 189 | main = main.replace('__ENTRY__', encodeURI(basePrefix + mainUrl)) |
| 190 | } |
| 191 | |
| 192 | main = main.replace('<!-- body -->', body) |
| 193 | |
| 194 | const html = transformHtmlTemplate(unhead, main) |
| 195 | return html |
| 196 | } |
| 197 |