@@ -5,7 +5,7 @@ import { join } from 'node:path'
5
5
import { slash } from '@antfu/utils'
6
6
import { white , yellow } from 'ansis'
7
7
import { escapeHtml } from 'markdown-it/lib/common/utils.mjs'
8
- import { createHead , transformHtmlTemplate } from 'unhead/server'
8
+ import { createHead , extractUnheadInputFromHtml , transformHtmlTemplate } from 'unhead/server'
9
9
import { version } from '../../package.json'
10
10
import { getSlideTitle } from '../commands/shared'
11
11
import { toAtFS } from '../resolver'
@@ -17,24 +17,25 @@ function toAttrValue(unsafe: unknown) {
17
17
18
18
export default async function setupIndexHtml ( { mode, entry, clientRoot, userRoot, roots, data, base } : Omit < ResolvedSlidevOptions , 'utils' > ) : Promise < string > {
19
19
let main = readFileSync ( join ( clientRoot , 'index.html' ) , 'utf-8' )
20
- let head = ''
21
20
let body = ''
22
21
22
+ const inputs : any [ ] = [ ]
23
+
23
24
for ( const root of roots ) {
24
25
const path = join ( root , 'index.html' )
25
26
if ( ! existsSync ( path ) )
26
27
continue
27
28
28
- const index = readFileSync ( path , 'utf-8' )
29
+ const html = readFileSync ( path , 'utf-8' )
29
30
30
- if ( root === userRoot && index . includes ( '<!DOCTYPE' ) ) {
31
+ if ( root === userRoot && html . includes ( '<!DOCTYPE' ) ) {
31
32
console . error ( yellow ( `[Slidev] Ignored provided index.html with doctype declaration. (${ white ( path ) } )` ) )
32
33
console . error ( yellow ( 'This file may be generated by Slidev, please remove it from your project.' ) )
33
34
continue
34
35
}
35
36
36
- head += `\n ${ ( index . match ( / < h e a d > ( [ \s \S ] * ? ) < \/ h e a d > / i ) ?. [ 1 ] || '' ) . trim ( ) } `
37
- body += `\n${ ( index . match ( / < b o d y > ( [ \s \S ] * ?) < \/ b o d y > / i) ?. [ 1 ] || '' ) . trim ( ) } `
37
+ inputs . push ( extractUnheadInputFromHtml ( html ) . input )
38
+ body += `\n${ ( html . match ( / < b o d y > ( [ \s \S ] * ?) < \/ b o d y > / i) ?. [ 1 ] || '' ) . trim ( ) } `
38
39
}
39
40
40
41
if ( data . features . tweet ) {
@@ -60,12 +61,12 @@ export default async function setupIndexHtml({ mode, entry, clientRoot, userRoot
60
61
const unhead = createHead ( {
61
62
init : [
62
63
{
63
- htmlAttrs : { lang : ( data . headmatter . lang as string | undefined ) ?? 'en' } ,
64
+ htmlAttrs : data . headmatter . lang ? { lang : data . headmatter . lang as string } : undefined ,
64
65
title,
65
66
link : [
66
- { rel : 'icon' , href : data . config . favicon } ,
67
+ data . config . favicon ? { rel : 'icon' , href : data . config . favicon } : null ,
67
68
...webFontsLink ,
68
- ] ,
69
+ ] . filter ( x => x ) ,
69
70
meta : [
70
71
{ property : 'slidev:version' , content : version } ,
71
72
{ charset : 'slidev:entry' , content : mode === 'dev' && slash ( entry ) } ,
@@ -82,18 +83,18 @@ export default async function setupIndexHtml({ mode, entry, clientRoot, userRoot
82
83
{ property : 'twitter:description' , content : seoMeta . twitterDescription } ,
83
84
{ property : 'twitter:image' , content : seoMeta . twitterImage } ,
84
85
{ property : 'twitter:url' , content : seoMeta . twitterUrl } ,
85
- ] ,
86
+ ] . filter ( x => x . content ) ,
86
87
} ,
88
+ ...inputs ,
87
89
] ,
88
90
} )
89
91
90
92
const baseInDev = mode === 'dev' && base ? base . slice ( 0 , - 1 ) : ''
93
+
91
94
main = main
92
95
. replace ( '__ENTRY__' , baseInDev + toAtFS ( join ( clientRoot , 'main.ts' ) ) )
93
- . replace ( '<!-- head -->' , head )
94
96
. replace ( '<!-- body -->' , body )
95
97
96
98
const html = await transformHtmlTemplate ( unhead , main )
97
-
98
99
return html
99
100
}
0 commit comments