-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.html-typograf.js
57 lines (47 loc) · 1.91 KB
/
script.html-typograf.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
const fs = require('node:fs');
const path = require('node:path');
const weblog = require('webpack-log');
const frontMatter = require('front-matter');
const Typograf = require('typograf');
const ENV = require('./app.env');
const UTILS = require('./webpack.utils');
const logger = weblog({ name: 'html-typograf' });
const typografIgnore = UTILS.readIgnoreFile('./.typografignore');
const statMessages = { fixed: 0, skipped: 0, ignored: 0 };
const patterns = [...UTILS.processArgs._];
const options = UTILS.readJsonFile('./.typografrc.json');
const instance = new Typograf(options);
instance.addSafeTag('{{', '}}');
instance.addSafeTag('{%', '%}');
instance.addSafeTag('{#', '#}');
instance.addSafeTag('<%', '%>');
instance.addSafeTag('<!--', '-->');
instance.addSafeTag('<!-- typograf ignore:start -->', '<!-- typograf ignore:end -->');
const files = UTILS.globArraySync(patterns.length > 0 ? patterns : [`${ENV.SOURCE_PATH}/**/*.html`], {
ignore: [`${ENV.OUTPUT_PATH}/**/*.html`, `${ENV.SOURCE_PATH}/partials/macros/**/*.html`],
nodir: true,
});
logger.info(`${files.length} files\n`);
files.forEach((resourcePath) => {
const relativePath = UTILS.slash(path.relative(__dirname, resourcePath));
if (typografIgnore.ignores(relativePath)) {
statMessages.ignored += 1;
logger.info(`ignored ${relativePath}`);
return;
}
const html = fs.readFileSync(resourcePath).toString('utf-8');
const templateData = frontMatter(html);
const output =
(templateData.frontmatter ? ['---', templateData.frontmatter, '---', ''].join('\n') : '') +
instance.execute(templateData.body);
if (html !== output) {
fs.writeFileSync(resourcePath, output);
statMessages.fixed += 1;
logger.info(`fixed ${relativePath}`);
} else {
statMessages.skipped += 1;
logger.info(`skipped ${relativePath}`);
}
});
console.log('');
logger.info('stats:', statMessages);