-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
204 lines (189 loc) · 6.97 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const path = require('path');
const fs = require('fs');
const webfont = require('webfont').webfont;
const getDirectories = (path) => fs.readdirSync(path).filter(file => fs.statSync(`${path}/${file}`).isDirectory());
const { version, url, name } = require('./package.json');
/*
https://github.com/itgalaxy/webfont
https://github.com/nfroidure/svgicons2svgfont
https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AEmoji%3DYes%3A%5D&g=&i=
https://publish.obsidian.md/tasks/Reference/Task+Formats/Tasks+Emoji+Format
Obsidian Tasks Custom Icons
https://github.com/obsidian-tasks-group/obsidian-tasks-custom-icons
*/
const testingVaultPath = path.resolve('./_vault/.obsidian/snippets/');
const iconFontFolders = getDirectories(__dirname).filter(dir => !dir.match(/\.git|node_modules|_vault/));
iconFontFolders.forEach(folderName => {
const folderPath = `${__dirname}/${folderName}`;
const [ fontName ] = path.parse(folderName).name.split('-');
let glyphs = [];
webfont({
files: `${folderName}/*.svg`,
fontName: fontName,
formats: ['woff2'],
ligatures: false,
normalize: true,
verbose: false,
fontHeight:2000,
glyphTransformFn: (glyphData) => {
glyphs.push({
...glyphData,
filename: path.parse(glyphData.path).name + '.svg',
code: path.parse(glyphData.path).name.split('-')[0].substring(1)
});
return glyphData
}
})
.then(result => render(result, folderName, folderPath, fontName, glyphs))
.catch(error => { throw error });
});
function render(result, folderName, folderPath, fontName, glyphs) {
const woff2 = Buffer.from(result.woff2);
const sortedCodepoints = glyphs.map(g => g.code).sort((a, b) => parseInt(a, 16) - parseInt(b, 16));
const codepointComment = `/* ${sortedCodepoints.map(cp => `U+${cp}:` + String.fromCodePoint(parseInt(cp, 16))).join(', ')} */`;
// const paddedCodepoints = sortedCodepoints.map(c => c.padStart(5, '0'));
// const incrementHex = hex => (parseInt(hex, 16) + 1).toString(16).toUpperCase().padStart(hex.length, '0');
const headerCSS = fs.existsSync(`${folderPath}/LICENSE.TXT`) ?
`/*!
${fs.readFileSync(`${folderPath}/LICENSE.TXT`).toString()}
*/` : '';
const fontFaceCSS =
`@charset "UTF-8";
${headerCSS}
/*! Generator: ${name} v${version} ${url} */
@font-face {
font-family: '${fontName}';
src: url('data:@file/octet-stream;base64,${woff2.toString('base64')}') format('woff2');
unicode-range: ${sortedCodepoints.map(u => `U+${u}`).join(', ')};
${codepointComment}
}
@supports (-webkit-touch-callout: none) {
/* Target Safari iOS */
@font-face {
font-family: '${fontName}';
src: url('data:@file/octet-stream;base64,${woff2.toString('base64')}') format('woff2');
/*
The following is a specific fix for Obsidian iOS.
For reasons unknown, a single hardcoded wide range is required to make all of the icons replace correctly.
*/
unicode-range: U+02000-1F9FF;
}
}
`;
const implementationCSS =
`${fontFaceCSS}
span.tasks-list-text,
.cm-line:has(.task-list-label) [class^=cm-list-],
span.task-extras,
.tasks-postpone,
.tasks-backlink,
.tasks-edit::after,
.tasks-modal-priority-section,
.tasks-modal-parsed-date,
.suggestion-container .suggestion {
font-family: '${fontName}', var(--font-text);
}
span.task-extras {
display: inline-flex;
align-items: flex-start;
margin-left: 0.33em;
}
`;
const demoHTML =
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${folderName} Demo </title>
<style>
@font-face {
font-family: '${fontName}';
src: url('data:@file/octet-stream;base64,${woff2.toString('base64')}') format('woff2');
unicode-range: ${sortedCodepoints.map(u => `U+${u}`).join(', ')};
${codepointComment}
}
/* Just for the demo version we are using the u1234 format for Safari */
@supports (-webkit-touch-callout: none) {
/* Target Safari iOS */
@font-face {
font-family: '${fontName}';
src: url('data:@file/octet-stream;base64,${woff2.toString('base64')}') format('woff2');
unicode-range: ${sortedCodepoints.map(u => `u${u}`).join(', ')};
${codepointComment}
}
}
@supports not (-webkit-touch-callout: none) {
/* Target Safari Desktop */
@font-face {
font-family: '${fontName}';
src: url('data:@file/octet-stream;base64,${woff2.toString('base64')}') format('woff2');
unicode-range: ${sortedCodepoints.map(u => `u${u}`).join(', ')};
${codepointComment}
}
}
html {font-family: sans-serif}
table {width:100%; max-width: 600px}
tr td:last-child {font-family: '${fontName}', sans-serif; font-size:120%}
td, th {padding:6px 4px 4px 4px; border-top: 1px solid #ccc}
td img {height:28px; width:28px;}
th {text-align:left}
</style>
</head>
<body>
<h1>${folderName}</h1>
<small>Iconfont column should display the same icon as SVG column</small><br>
<table>
<thead>
<tr>
<th>SVG</th>
<th>Filename</th>
<th>Codepoint</th>
<th>Emoji</th>
<th>Iconfont</th>
</tr>
</thead>
<tbody>
${glyphs.map(g => `<tr>
<td><img src="${g.filename}"/></td>
<td>${g.filename}</td>
<td>U+${g.code}</td>
<td>${g.unicode[0]}</td>
<td>${g.unicode[0]}</td>
</tr>`).join('')}
</tbody>
</table>
</body>
</html>`;
console.log(`Created '${folderName}' webfont:`);
// Write CSS snippet file to disk:
fs.writeFileSync(`${folderPath}/${folderName}.css`, implementationCSS);
console.log(`...Saved ${folderName}.css to ./${folderName}/`)
// Write demo file to disk:
fs.writeFileSync(`${folderPath}/${folderName}.html`, demoHTML);
console.log(`...Saved ${folderName}.html to ./${folderName}/`)
/*
If copysnippetpath.txt exists inside a icon font folder, the directory path inside
will have a copy of the obsidian snippet .css file copied to, overwriting it.
Optional convenience feature to copy snippets while you are working on an icon set
to your home obsidian vault for testing.
*/
const cspPath = path.resolve(`${folderPath}/copysnippetpath.txt`)
if (fs.existsSync(cspPath)) {
const cspTargetPath = fs.readFileSync(cspPath).toString().trim();
const resolvedCspTargetPath = path.resolve(cspTargetPath)
if (fs.existsSync(resolvedCspTargetPath)) {
const cspTargetSnippetFilepath = `${resolvedCspTargetPath}/${folderName}.css`;
fs.writeFileSync(cspTargetSnippetFilepath, implementationCSS);
console.log(`...csp: Copied '${folderName}.css' snippet to ${cspTargetSnippetFilepath}`)
} else {
console.log(`...csp: Target path '${cspTargetPath}' does not exist, ignoring.`)
}
}
// Write binary font file to disk (unused by generated HTML CSS snipets, purely for theme developers):
fs.writeFileSync(`${folderPath}/${folderName}.woff2`, woff2, 'binary');
console.log(`...Saved ${folderName}.woff2 to ./${folderName}/`)
// Copy CSS snippet to repo testing vault in _vault
fs.writeFileSync(`${testingVaultPath}/${folderName}.css`, implementationCSS);
console.log(`...Duped ${folderName}.css at ./_vault/.obsidian/snippets/`)
}