-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetalsmith.js
146 lines (127 loc) · 3.69 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
/* 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 when from "metalsmith-if";
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";
// 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 md = ( mdString ) => {
try {
return marked.parse( mdString, { mangle: false, headerIds: false } );
} 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,
md,
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( 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,
} );
}
}
} );