@@ -21,11 +21,7 @@ const TEST_DATA = {
21
21
}
22
22
}
23
23
24
- const TEST_MD = `---
25
- layout: base.html
26
- author: Lea Rosema
27
- ---
28
- # {{ title }}
24
+ const TEST_MD = `# {{ title }}
29
25
30
26
An article by {{ author }}
31
27
`
@@ -112,13 +108,72 @@ describe('template function', () => {
112
108
} ) ;
113
109
} ) ;
114
110
115
- describe ( 'handleTemplateFile function' , ( ) => {
111
+ describe ( 'handleTemplateFile function' , { only : true } , ( ) => {
112
+
113
+ const withFrontmatter = ( str , data ) => `---json\n${ JSON . stringify ( data ) } \n---\n${ str } `
114
+
115
+ it ( 'should work with basic html files without specifying a layout' , async ( ) => {
116
+ const config = new SissiConfig ( ) ;
117
+ config . addExtension ( md ) ;
118
+
119
+ const vFS = new Map ( ) ;
120
+ vFS . set ( 'index.html' , '<h1>{{ title }}</h1>' ) ;
121
+
122
+ config . resolve = ( ...paths ) => {
123
+ const resource = path . normalize ( path . join ( ...paths ) ) ;
124
+ return vFS . get ( resource ) ;
125
+ }
126
+
127
+ const result = await handleTemplateFile ( config , { title : 'Lea was here' } , 'index.html' ) ;
128
+
129
+ assert . equal ( result . filename , 'public/index.html' ) ;
130
+ assert . equal ( result . content , '<h1>Lea was here</h1>' )
131
+ } ) ;
132
+
133
+ it ( 'should work with basic html files with specifying a layout' , async ( ) => {
134
+ const config = new SissiConfig ( ) ;
135
+ config . addExtension ( md ) ;
136
+
137
+ const vFS = new Map ( ) ;
138
+ vFS . set ( 'index.html' , withFrontmatter ( '<h1>{{ title }}</h1>' , { layout : 'base.html' } ) ) ;
139
+ vFS . set ( '_layouts/base.html' , '<body>{{ content }}</body>' )
140
+
141
+ config . resolve = ( ...paths ) => {
142
+ const resource = path . normalize ( path . join ( ...paths ) ) ;
143
+ return vFS . get ( resource ) ;
144
+ }
145
+
146
+ const result = await handleTemplateFile ( config , { title : 'Lea was here' } , 'index.html' ) ;
147
+
148
+ assert . equal ( result . filename , 'public/index.html' ) ;
149
+ assert . equal ( result . content , '<body><h1>Lea was here</h1></body>' ) ;
150
+ } ) ;
151
+
116
152
it ( 'should work with the default markdown plugin' , async ( ) => {
117
153
const config = new SissiConfig ( ) ;
118
154
config . addExtension ( md ) ;
119
155
120
156
const vFS = new Map ( ) ;
121
- vFS . set ( 'index.md' , TEST_MD ) ;
157
+ vFS . set ( 'index.md' , withFrontmatter ( TEST_MD , { 'layout' : 'base.html' , author : 'Lea Rosema' } ) ) ;
158
+ vFS . set ( '_layouts/base.html' , '<body>{{ content }}</body>' ) ;
159
+
160
+ config . resolve = ( ...paths ) => {
161
+ const resource = path . normalize ( path . join ( ...paths ) ) ;
162
+ return vFS . get ( resource ) ;
163
+ }
164
+
165
+ const result = await handleTemplateFile ( config , { title : 'Lea was here' } , 'index.md' ) ;
166
+
167
+ assert . equal ( result . filename , 'public/index.html' ) ;
168
+ assert . equal ( result . content , '<body><h1>Lea was here</h1>\n\n<p>An article by Lea Rosema</p>\n</body>' )
169
+ } ) ;
170
+
171
+ it ( 'should work with the default markdown plugin.' , async ( ) => {
172
+ const config = new SissiConfig ( ) ;
173
+ config . addExtension ( md ) ;
174
+
175
+ const vFS = new Map ( ) ;
176
+ vFS . set ( 'index.md' , withFrontmatter ( TEST_MD , { 'layout' : 'base.html' , author : 'Lea Rosema' } ) ) ;
122
177
vFS . set ( '_layouts/base.html' , '<body>{{ content }}</body>' ) ;
123
178
124
179
config . resolve = ( ...paths ) => {
@@ -131,4 +186,43 @@ describe('handleTemplateFile function', () => {
131
186
assert . equal ( result . filename , 'public/index.html' ) ;
132
187
assert . equal ( result . content , '<body><h1>Lea was here</h1>\n\n<p>An article by Lea Rosema</p>\n</body>' )
133
188
} ) ;
189
+
190
+ it ( 'should throw an error when a non-existant file is specified' , async ( ) => {
191
+ const config = new SissiConfig ( ) ;
192
+ config . addExtension ( md ) ;
193
+
194
+ const vFS = new Map ( ) ;
195
+ vFS . set ( 'index.md' , withFrontmatter ( TEST_MD , { 'layout' : 'notfound.html' , author : 'Lea Rosema' } ) ) ;
196
+
197
+ config . resolve = ( ...paths ) => {
198
+ const resource = path . normalize ( path . join ( ...paths ) ) ;
199
+ if ( vFS . has ( resource ) ) {
200
+ return vFS . get ( resource ) ;
201
+ }
202
+ }
203
+
204
+ assert . rejects ( async ( ) => {
205
+ await handleTemplateFile ( config , { title : 'Lea was here' } , 'something-completely-different.md' ) ;
206
+ } ) ;
207
+ } ) ;
208
+
209
+ it ( 'should throw an error when a non-existant file is specified as layout' , { only : true } , async ( ) => {
210
+ const config = new SissiConfig ( ) ;
211
+ config . addExtension ( md ) ;
212
+
213
+ const vFS = new Map ( ) ;
214
+ vFS . set ( 'index.md' , withFrontmatter ( TEST_MD , { 'layout' : 'notfound.html' , author : 'Lea Rosema' } ) ) ;
215
+
216
+ config . resolve = ( ...paths ) => {
217
+ const resource = path . normalize ( path . join ( ...paths ) ) ;
218
+ if ( vFS . has ( resource ) ) {
219
+ return vFS . get ( resource ) ;
220
+ }
221
+ }
222
+
223
+ assert . rejects ( async ( ) => {
224
+ await handleTemplateFile ( config , { title : 'Lea was here' } , 'index.md' ) ;
225
+ } ) ;
226
+ } ) ;
227
+
134
228
} ) ;
0 commit comments