/
renderPageContent.spec.ts
155 lines (142 loc) · 3.82 KB
/
renderPageContent.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { createMarkdown } from '@vuepress/markdown'
import { path } from '@vuepress/utils'
import { describe, expect, it } from 'vitest'
import { createBaseApp, renderPageContent } from '../../src/index.js'
const app = createBaseApp({
source: path.resolve(__dirname, 'fake-source'),
theme: { name: 'test' },
bundler: {} as any,
})
app.markdown = createMarkdown()
describe('core > page > renderPageContent', () => {
it('should render page content correctly', () => {
const resolved = renderPageContent({
app,
content: `\
foobar
<script setup lang="ts">
const msg = 'msg'
</script>
`,
filePath: app.dir.source('foo.md'),
filePathRelative: 'foo.md',
options: {},
})
expect(resolved).toEqual({
contentRendered: '<p>foobar</p>\n',
deps: [],
frontmatter: {},
headers: [],
links: [],
markdownEnv: { excerpt: '' },
sfcBlocks: {
template: {
type: 'template',
content: '<template><p>foobar</p>\n</template>',
contentStripped: '<p>foobar</p>\n',
tagClose: '</template>',
tagOpen: '<template>',
},
script: null,
scriptSetup: {
type: 'script',
content: `<script setup lang="ts">\nconst msg = 'msg'\n</script>`,
contentStripped: `\nconst msg = 'msg'\n`,
tagClose: '</script>',
tagOpen: '<script setup lang="ts">',
},
scripts: [
{
type: 'script',
content: `<script setup lang="ts">\nconst msg = 'msg'\n</script>`,
contentStripped: `\nconst msg = 'msg'\n`,
tagClose: '</script>',
tagOpen: '<script setup lang="ts">',
},
],
styles: [],
customBlocks: [],
},
title: '',
})
})
describe('page title', () => {
it('should use title in frontmatter', () => {
const resolved = renderPageContent({
app,
content: '# title in header',
filePath: null,
filePathRelative: null,
options: {
frontmatter: {
title: 'title in frontmatter',
},
},
})
expect(resolved.title).toEqual('title in frontmatter')
})
it('should use title in the first h1 header', () => {
const resolved = renderPageContent({
app,
content: '# title in header',
filePath: null,
filePathRelative: null,
options: {},
})
expect(resolved.title).toEqual('title in header')
})
it('should use empty title', () => {
const resolved = renderPageContent({
app,
content: '',
filePath: null,
filePathRelative: null,
options: {},
})
expect(resolved.title).toEqual('')
})
})
describe('page frontmatter', () => {
it('should merge markdown frontmatter and options frontmatter', () => {
const resolved = renderPageContent({
app,
content: `\
---
title: title in markdown frontmatter
---
`,
filePath: null,
filePathRelative: null,
options: {
frontmatter: {
description: 'description in options frontmatter',
},
},
})
expect(resolved.frontmatter).toEqual({
title: 'title in markdown frontmatter',
description: 'description in options frontmatter',
})
})
it('should use fields from markdown frontmatter first', () => {
const resolved = renderPageContent({
app,
content: `\
---
title: title in markdown frontmatter
---
`,
filePath: null,
filePathRelative: null,
options: {
frontmatter: {
title: 'title in options frontmatter',
},
},
})
expect(resolved.frontmatter).toEqual({
title: 'title in markdown frontmatter',
})
})
})
})