forked from StartBootstrap/startbootstrap-clean-blog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetalsmith.js
121 lines (112 loc) · 3.04 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
import Metalsmith from 'metalsmith';
import drafts from '@metalsmith/drafts';
import layouts from '@metalsmith/layouts';
import markdown from '@metalsmith/markdown';
import sass from '@metalsmith/sass';
import collections from '@metalsmith/collections';
import postcss from '@metalsmith/postcss';
import { performance } from 'perf_hooks';
import browserSync from 'browser-sync';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
/* eslint-disable no-underscore-dangle */
const __dirname = dirname( fileURLToPath( import.meta.url ) );
const prod = process.env.NODE_ENV === 'production';
const sitedata = {
generator: {
name: 'Metalsmith'
},
site: {
base: !prod ? 'http://localhost:5000' : 'https://startbootstrap.github.io/startbootstrap-clean-blog/',
subject: 'Start Bootstrap Theme',
author: 'Start Bootstrap'
},
nav: [
{ path: 'index.html', label: 'Home' },
{ path: 'about.html', label: 'About' },
{ path: 'contact.html', label: 'Contact' }
],
socials: {
twitter: 'https://twitter.com/johndoe',
facebook: 'https://facebook.com/johndoe',
github: 'https://github.com/johndoe'
}
};
function formatDate( value, format ) {
const dt = new Date( value );
if ( format === 'iso' ) {
return dt.toISOString();
}
const utc = dt.toUTCString().match( /(\d{1,2}) (.*) (\d{4})/ );
return `${ utc[ 2 ] } ${ utc[ 1 ] }, ${ utc[ 3 ] }`;
}
function formattedDatesForPosts( files, metalsmith ) {
metalsmith.metadata().collections.posts.forEach( post => {
if ( post.date ) {
post.displayDate = formatDate( post.date );
post.isoDate = formatDate( post.date, 'iso' );
}
} );
}
function noop() { }
let devServer = null;
let t1 = performance.now();
function msBuild() {
return (
Metalsmith( __dirname )
.clean( true )
.watch( prod ? false : [ 'src' ] )
.source( 'src/content' )
.destination( 'dist' )
.metadata( sitedata )
.use( prod ? noop : drafts() )
.use( markdown() )
.use( collections( {
posts: {
pattern: 'posts/**/*.html',
reverse: true,
sortBy: 'date'
}
} ) )
.use( formattedDatesForPosts )
.use( layouts( {
directory: 'src/pug',
default: 'default.pug'
} ) )
.use( sass( {
entries: {
'src/scss/styles.scss': 'css/styles.css'
}
} ) )
.use( !prod ? postcss( {
plugins: [
'autoprefixer',
'cssnano'
],
map: true
} ) : 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: './dist',
port: 5000,
injectChanges: false,
reloadThrottle: 0
} );
}
}
} );