1
- const debug = require ( 'debug' ) ( '@metalsmith/layouts' ) ;
2
- const match = require ( 'multimatch' ) ;
3
- const path = require ( 'path' ) ;
4
- const isUtf8 = require ( 'is-utf8' ) ;
5
- const getTransformer = require ( './get-transformer' ) ;
1
+ const debug = require ( 'debug' ) ( '@metalsmith/layouts' )
2
+ const match = require ( 'multimatch' )
3
+ const path = require ( 'path' )
4
+ const isUtf8 = require ( 'is-utf8' )
5
+ const getTransformer = require ( './get-transformer' )
6
6
7
7
/**
8
8
* Resolves layouts, in the following order:
@@ -13,132 +13,132 @@ const getTransformer = require('./get-transformer');
13
13
14
14
function getLayout ( { file, settings } ) {
15
15
if ( file . layout || file . layout === false ) {
16
- return file . layout ;
16
+ return file . layout
17
17
}
18
18
19
- return settings . default ;
19
+ return settings . default
20
20
}
21
21
22
22
/**
23
23
* Engine, renders file with the appropriate layout
24
24
*/
25
25
26
26
function render ( { filename, files, metadata, settings, metalsmith } ) {
27
- const file = files [ filename ] ;
28
- const layout = getLayout ( { file, settings } ) ;
29
- const extension = layout . split ( '.' ) . pop ( ) ;
27
+ const file = files [ filename ]
28
+ const layout = getLayout ( { file, settings } )
29
+ const extension = layout . split ( '.' ) . pop ( )
30
30
31
- debug ( `rendering ${ filename } with layout ${ layout } ` ) ;
31
+ debug ( `rendering ${ filename } with layout ${ layout } ` )
32
32
33
33
// Stringify file contents
34
- const contents = file . contents . toString ( ) ;
34
+ const contents = file . contents . toString ( )
35
35
36
- const transform = getTransformer ( extension ) ;
37
- const locals = { ...metadata , ...file , contents } ;
38
- const layoutPath = path . join ( metalsmith . path ( settings . directory ) , layout ) ;
36
+ const transform = getTransformer ( extension )
37
+ const locals = { ...metadata , ...file , contents }
38
+ const layoutPath = path . join ( metalsmith . path ( settings . directory ) , layout )
39
39
40
40
// Transform the contents
41
41
return transform
42
42
. renderFileAsync ( layoutPath , settings . engineOptions , locals )
43
43
. then ( ( rendered ) => {
44
44
// Update file with results
45
45
// eslint-disable-next-line no-param-reassign
46
- file . contents = Buffer . from ( rendered . body ) ;
47
- debug ( `done rendering ${ filename } ` ) ;
46
+ file . contents = Buffer . from ( rendered . body )
47
+ debug ( `done rendering ${ filename } ` )
48
48
} )
49
49
. catch ( ( err ) => {
50
50
// Prepend error message with file path
51
51
// eslint-disable-next-line no-param-reassign
52
- err . message = `${ filename } : ${ err . message } ` ;
53
- throw err ;
54
- } ) ;
52
+ err . message = `${ filename } : ${ err . message } `
53
+ throw err
54
+ } )
55
55
}
56
56
57
57
/**
58
58
* Validate, checks whether a file should be processed
59
59
*/
60
60
61
61
function validate ( { filename, files, settings } ) {
62
- const file = files [ filename ] ;
63
- const layout = getLayout ( { file, settings } ) ;
62
+ const file = files [ filename ]
63
+ const layout = getLayout ( { file, settings } )
64
64
65
- debug ( `validating ${ filename } ` ) ;
65
+ debug ( `validating ${ filename } ` )
66
66
67
67
// Files without a layout cannot be processed
68
68
if ( ! layout ) {
69
- debug ( `validation failed, ${ filename } does not have a layout set` ) ;
70
- return false ;
69
+ debug ( `validation failed, ${ filename } does not have a layout set` )
70
+ return false
71
71
}
72
72
73
73
// Layouts without an extension cannot be processed
74
74
if ( ! layout . includes ( '.' ) ) {
75
- debug ( `validation failed, layout for ${ filename } does not have an extension` ) ;
76
- return false ;
75
+ debug ( `validation failed, layout for ${ filename } does not have an extension` )
76
+ return false
77
77
}
78
78
79
79
// Files that are not utf8 are ignored
80
80
if ( ! isUtf8 ( file . contents ) ) {
81
- debug ( `validation failed, ${ filename } is not utf-8` ) ;
82
- return false ;
81
+ debug ( `validation failed, ${ filename } is not utf-8` )
82
+ return false
83
83
}
84
84
85
85
// Files without an applicable jstransformer are ignored
86
- const extension = layout . split ( '.' ) . pop ( ) ;
87
- const transformer = getTransformer ( extension ) ;
86
+ const extension = layout . split ( '.' ) . pop ( )
87
+ const transformer = getTransformer ( extension )
88
88
89
89
if ( ! transformer ) {
90
- debug ( `validation failed, no jstransformer found for layout for ${ filename } ` ) ;
90
+ debug ( `validation failed, no jstransformer found for layout for ${ filename } ` )
91
91
}
92
92
93
- return transformer ;
93
+ return transformer
94
94
}
95
95
96
96
/**
97
97
* Plugin, the main plugin used by metalsmith
98
98
*/
99
99
100
100
module . exports = ( options ) => ( files , metalsmith , done ) => {
101
- const metadata = metalsmith . metadata ( ) ;
101
+ const metadata = metalsmith . metadata ( )
102
102
const defaults = {
103
103
pattern : '**' ,
104
104
directory : 'layouts' ,
105
105
engineOptions : { } ,
106
106
suppressNoFilesError : false
107
- } ;
108
- const settings = { ...defaults , ...options } ;
107
+ }
108
+ const settings = { ...defaults , ...options }
109
109
110
110
// Check whether the pattern option is valid
111
111
if ( ! ( typeof settings . pattern === 'string' || Array . isArray ( settings . pattern ) ) ) {
112
112
return done (
113
113
new Error (
114
114
'invalid pattern, the pattern option should be a string or array of strings. See https://www.npmjs.com/package/@metalsmith/layouts#pattern'
115
115
)
116
- ) ;
116
+ )
117
117
}
118
118
119
119
// Filter files by the pattern
120
- const matchedFiles = match ( Object . keys ( files ) , settings . pattern ) ;
120
+ const matchedFiles = match ( Object . keys ( files ) , settings . pattern )
121
121
122
122
// Filter files by validity
123
- const validFiles = matchedFiles . filter ( ( filename ) => validate ( { filename, files, settings } ) ) ;
123
+ const validFiles = matchedFiles . filter ( ( filename ) => validate ( { filename, files, settings } ) )
124
124
125
125
// Let the user know when there are no files to process, unless the check is suppressed
126
126
if ( validFiles . length === 0 ) {
127
127
const message =
128
- 'no files to process. See https://www.npmjs.com/package/@metalsmith/layouts#suppressnofileserror' ;
128
+ 'no files to process. See https://www.npmjs.com/package/@metalsmith/layouts#suppressnofileserror'
129
129
130
130
if ( settings . suppressNoFilesError ) {
131
- debug ( message ) ;
132
- return done ( ) ;
131
+ debug ( message )
132
+ return done ( )
133
133
}
134
134
135
- return done ( new Error ( message ) ) ;
135
+ return done ( new Error ( message ) )
136
136
}
137
137
138
138
// Map all files that should be processed to an array of promises and call done when finished
139
139
return Promise . all (
140
140
validFiles . map ( ( filename ) => render ( { filename, files, metadata, settings, metalsmith } ) )
141
141
)
142
142
. then ( ( ) => done ( ) )
143
- . catch ( /* istanbul ignore next */ ( error ) => done ( error ) ) ;
144
- } ;
143
+ . catch ( /* istanbul ignore next */ ( error ) => done ( error ) )
144
+ }
0 commit comments