-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetalsmith.js
169 lines (146 loc) · 4.21 KB
/
metalsmith.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-disable import/no-extraneous-dependencies */
import { performance } from 'perf_hooks';
import browserSync from 'browser-sync';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import Metalsmith from 'metalsmith';
import markdown from '@metalsmith/markdown';
import layouts from '@metalsmith/layouts';
import collections from '@metalsmith/collections';
import drafts from '@metalsmith/drafts';
import permalinks from '@metalsmith/permalinks';
import sass from '@metalsmith/sass';
import postcss from '@metalsmith/postcss';
import htmlMinifier from 'metalsmith-html-minifier';
import assets from 'metalsmith-static-files';
import metadata from '@metalsmith/metadata';
import prism from 'metalsmith-prism';
import * as marked from 'marked';
import esbuild from '@metalsmith/js-bundle';
// ESM does not currently import JSON modules by default.
// Ergo we'll JSON.parse the file manually
import * as fs from 'fs';
const { dependencies } = JSON.parse( fs.readFileSync( './package.json' ) );
/* eslint-disable no-underscore-dangle */
const __dirname = dirname( fileURLToPath( import.meta.url ) );
const isProduction = process.env.NODE_ENV === 'production';
// functions to extend Nunjucks environment
const spaceToDash = ( string ) => string.replace( /\s+/g, '-' );
const condenseTitle = ( string ) => string.toLowerCase().replace( /\s+/g, '' );
const UTCdate = ( date ) => date.toUTCString( 'M d, yyyy' );
const blogDate = ( string ) =>
new Date( string ).toLocaleString( 'en-US', { year: 'numeric', month: 'long', day: 'numeric' } );
const trimSlashes = ( string ) => string.replace( /(^\/)|(\/$)/g, '' );
const mdToHTML = ( mdString ) => {
try {
return marked.parse( mdString );
} catch ( e ) {
return mdString;
}
};
const thisYear = () => new Date().getFullYear();
// Define engine options for the inplace and layouts plugins
const templateConfig = {
directory: 'layouts',
engineOptions: {
path: [ 'layouts' ],
filters: {
spaceToDash,
condenseTitle,
UTCdate,
blogDate,
trimSlashes,
mdToHTML,
thisYear
}
}
};
function noop() { };
// to use a plugin conditionally, use this pattern:
// .use( isProduction ? htmlMinifier() : noop ) )
let devServer = null;
let t1 = performance.now();
function msBuild() {
return (
Metalsmith( __dirname )
.clean( true )
.watch( isProduction ? false : [ 'src', 'layouts' ] )
.source( './src/content' )
.destination( './build' )
.metadata( {
msVersion: dependencies.metalsmith,
nodeVersion: process.version
} )
.use( isProduction ? noop : drafts() )
.use(
metadata( {
site: 'src/content/data/site.json',
nav: 'src/content/data/navigation.json'
} )
)
.use(
collections( {
blog: {
pattern: 'blog/*.md',
sortBy: 'date',
reverse: true,
limit: 10
}
} )
)
.use( markdown() )
.use( permalinks() )
.use( layouts( templateConfig ) )
.use(
prism( {
lineNumbers: true,
decode: true
} )
)
.use(
assets( {
source: 'src/assets/',
destination: 'assets/'
} )
)
.use(
sass( {
entries: {
'src/sass/styles.scss': 'assets/styles.css'
}
} )
)
.use( postcss( { plugins: [ 'postcss-preset-env', 'autoprefixer', 'cssnano' ], map: !isProduction } ) )
.use(
esbuild( {
entries: {
'assets/scripts': 'src/js/main.js'
}
} )
)
.use( isProduction ? htmlMinifier() : noop )
);
}
const ms = msBuild();
ms.build( ( err ) => {
if ( err ) {
throw err;
}
/* eslint-disable no-console */
console.log( `Build success in ${ ( ( performance.now() - t1 ) / 1000 ).toFixed( 1 ) }s` );
if ( ms.watch() ) {
if ( devServer ) {
t1 = performance.now();
devServer.reload();
} else {
devServer = browserSync.create();
devServer.init( {
host: 'localhost',
server: './build',
port: 3000,
injectChanges: false,
reloadThrottle: 0
} );
}
}
} );