-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (94 loc) · 2.79 KB
/
index.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
import path from 'node:path';
import {unified} from 'unified';
import rehypeParse from 'rehype-parse';
import rehypeFormat from 'rehype-format';
import rehypeStringify from 'rehype-stringify';
import remarkParse from 'remark-parse';
import remarkFrontmatter from 'remark-frontmatter';
import remarkGfm from 'remark-gfm';
import remarkRehype from 'remark-rehype';
import hashAssets from './src/unified-plugins/hashAssets.js';
import applyPageTemplate from './src/unified-plugins/applyPageTemplate.js';
import parseYamlFrontmatter from './src/unified-plugins/parseYamlFrontmatter.js';
import applyPageScript from './src/unified-plugins/applyPageScript.js';
import fixInternalLinks from './src/unified-plugins/fixInternalLinks.js';
import applyGitHubSha from './src/unified-plugins/applyGitHubSha.js';
import optimizeImages from './src/unified-plugins/optimizeImages.js';
import {imageTitlesToCaptions} from 'rehype-image-titles-to-captions';
import applyCopyrightDate from './src/unified-plugins/applyCopyrightDate.js';
/**
* Applies HTML formatting and cache-busts the CSS of a given HTML file.
* @param {Object} params
* @param {Object} params.inputFile
* @param {string} params.inputFile.name
* @param {string} params.inputFile.text
* @param {string} params.outputDir
* @param {import('./src/util.js').FileCache} params.fileCache
*/
export async function processDocument({
inputFile,
outputDir,
fileCache,
assets = new Map(),
skipImageOptimization,
writtenAssets
}) {
const ext = path.extname(inputFile.name);
const processor = unified();
if (ext === '.html') {
processor.use(rehypeParse, {
fragment: false
});
} else if (ext === '.md') {
processor
.use(remarkParse)
.use(remarkFrontmatter, ['yaml'])
.use(parseYamlFrontmatter)
.use(remarkGfm)
.use(remarkRehype)
.use(applyPageTemplate, {
pathToFile: inputFile.name,
outputDir,
assets
});
}
return await processor
.use(applyPageScript, {
pathToFile: inputFile.name,
fileCache
})
.use(fixInternalLinks, {
pathToFile: inputFile.name,
fileCache
})
.use(optimizeImages, {
skip: skipImageOptimization,
pathToFile: inputFile.name,
outputDir,
assets,
writtenAssets
})
.use(imageTitlesToCaptions, {
pictureSelector: 'article picture:has(img[title])'
})
.use(hashAssets, {
pathToFile: inputFile.name,
outputDir,
assets
})
.use(applyCopyrightDate, {
startYear: '2024'
})
.use(applyGitHubSha)
.use(rehypeFormat, {
indentInitial: false,
})
.use(rehypeStringify, {
allowDangerousCharacters: true,
allowDangerousHtml: true,
characterReferences: {
useNamedReferences: true
}
})
.process(inputFile.text);
}