-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.ts
94 lines (81 loc) · 2.25 KB
/
index.ts
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
import fs from 'fs-extra';
import path from 'node:path';
import { globSync } from 'glob';
import richtypo from '../../src/richtypo.js';
import enRules from '../../src/rules/en.js';
import frRules from '../../src/rules/fr.js';
import ruRules from '../../src/rules/ru.js';
function template({
lang,
title,
content,
contentTypo,
}: {
lang: string;
title: string;
content: string;
contentTypo: string;
}) {
return `<html lang="${lang}">
<head>
<meta charset="utf-8" />
<title>${title}</title>
<link
href="https://fonts.googleapis.com/css?family=Vollkorn:400,400i,700i&subset=cyrillic,latin-ext"
rel="stylesheet"
/>
<link href="example.css" rel="stylesheet" />
</head>
<body>
<main role="main" class="content">
<div class="content__column">${content}</div>
<div class="content__column">${contentTypo}</div>
</main>
</body>
</html>`;
}
console.log('Building the example site...');
const highlight = (text: string) =>
text
.replaceAll(
/( |\u00A0)/gm,
'<span class="rule rule-nbsp" title="Non-breaking space">$1</span>',
)
.replaceAll(
/(\u202F)/gm,
'<span class="rule rule-narrow" title="Narrow space">$1</span>',
)
.replaceAll(
/([«»“”])/gm,
'<span class="rule rule-quote" title="Quote">$1</span>',
)
.replaceAll(
/(—)/gm,
'<span class="rule rule-emdash" title="Em dash">$1</span>',
);
const rt: Record<string, (x: string) => string> = {
index: (x: string) => richtypo([...enRules, highlight], x),
french: (x: string) => richtypo([...frRules, highlight], x),
russian: (x: string) => richtypo([...ruRules, highlight], x),
};
fs.ensureDirSync('dist');
const files = globSync('example/src/content/*.html');
for (const file of files) {
console.log('👉', file);
const lang = path.basename(file, '.html');
const content = fs.readFileSync(file, 'utf8');
const contentTypo = rt[lang]?.(content) ?? '';
const html = template({
lang: { french: 'fr', russian: 'ru' }[lang] ?? 'en',
title: `Richtypo ${lang}`,
content,
contentTypo,
});
fs.writeFileSync(file.replace('src/content', 'dist'), html);
}
console.log('👉 example.css');
fs.writeFileSync(
'example/dist/example.css',
fs.readFileSync('example/src/example.css', 'utf8'),
);
console.log('👉 Done');