Skip to content

Commit a9a1a67

Browse files
committed
webui: Bugfix (font embedding)
1 parent bbb04a2 commit a9a1a67

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

tools/server/webui/vite.config.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const GUIDE_FOR_FRONTEND = `
1818

1919
const MAX_BUNDLE_SIZE = 2 * 1024 * 1024;
2020

21+
const DO_EMBED_FONTS = false; // The embedded mode is not yet ready :-(.
22+
2123
function llamaCppBuildPlugin() {
2224
return {
2325
name: 'llamacpp:build',
@@ -37,48 +39,46 @@ function llamaCppBuildPlugin() {
3739

3840
// Inline KaTeX fonts directly from node_modules
3941
const katexFonts = [
40-
'KaTeX_AMS-Regular.woff2',
41-
'KaTeX_Caligraphic-Bold.woff2',
42-
'KaTeX_Caligraphic-Regular.woff2',
43-
'KaTeX_Fraktur-Bold.woff2',
44-
'KaTeX_Fraktur-Regular.woff2',
45-
'KaTeX_Main-BoldItalic.woff2',
46-
'KaTeX_Main-Bold.woff2',
47-
'KaTeX_Main-Italic.woff2',
48-
'KaTeX_Main-Regular.woff2',
49-
'KaTeX_Math-BoldItalic.woff2',
50-
'KaTeX_Math-Italic.woff2',
51-
'KaTeX_SansSerif-Bold.woff2',
52-
'KaTeX_SansSerif-Italic.woff2',
53-
'KaTeX_SansSerif-Regular.woff2',
54-
'KaTeX_Script-Regular.woff2',
55-
'KaTeX_Size1-Regular.woff2',
56-
'KaTeX_Size2-Regular.woff2',
57-
'KaTeX_Size3-Regular.woff2',
58-
'KaTeX_Size4-Regular.woff2',
59-
'KaTeX_Typewriter-Regular.woff2'
42+
'KaTeX_AMS-Regular',
43+
'KaTeX_Caligraphic-Bold',
44+
'KaTeX_Caligraphic-Regular',
45+
'KaTeX_Fraktur-Bold',
46+
'KaTeX_Fraktur-Regular',
47+
'KaTeX_Main-BoldItalic',
48+
'KaTeX_Main-Bold',
49+
'KaTeX_Main-Italic',
50+
'KaTeX_Main-Regular',
51+
'KaTeX_Math-BoldItalic',
52+
'KaTeX_Math-Italic',
53+
'KaTeX_SansSerif-Bold',
54+
'KaTeX_SansSerif-Italic',
55+
'KaTeX_SansSerif-Regular',
56+
'KaTeX_Script-Regular',
57+
'KaTeX_Size1-Regular',
58+
'KaTeX_Size2-Regular',
59+
'KaTeX_Size3-Regular',
60+
'KaTeX_Size4-Regular',
61+
'KaTeX_Typewriter-Regular'
6062
];
6163

6264
katexFonts.forEach((font) => {
63-
const fontPath = resolve('node_modules/katex/dist/fonts', font);
65+
const fontPath = resolve('node_modules/katex/dist/fonts', font + '.woff2');
66+
let srcUrl = '';
6467
if (existsSync(fontPath)) {
65-
const fontContent = readFileSync(fontPath, 'utf-8');
66-
// Replace both the original and hashed references
67-
// e.g. url(./KaTeX_Math-Italic.flOr_0UB.ttf) has to be replaces by a base64 data url.
68-
content = content.replace(
69-
new RegExp(`url\\(./${font.split('.')[0]}[.][^.]*[.]woff2\\)`, 'g'),
70-
`url('data:application/font-woff2;base64,${Buffer.from(fontContent).toString('base64')}')`
71-
);
72-
// Remove ttf- and woff-URLs. See node_modules/katex/src/styles/fonts.scss.
73-
content = content.replace(
74-
new RegExp(`,url\\(./${font.split('.')[0]}[.][^.]*[.]woff\\)`, 'g'),
75-
``
76-
);
77-
content = content.replace(
78-
new RegExp(`,url\\(./${font.split('.')[0]}[.][^.]*[.]ttf\\)`, 'g'),
79-
``
80-
);
68+
const fontContent = readFileSync(fontPath);
69+
if (DO_EMBED_FONTS) {
70+
// Replace both the original and hashed references
71+
// e.g. url(./KaTeX_Math-Italic.flOr_0UB.ttf) has to be replaces by a base64 data url.
72+
const bufData = Buffer.from(fontContent).toString('base64');
73+
srcUrl = `src:url('data:application/font-woff2;base64,${bufData}') format("woff2")`;
74+
}
75+
} else {
76+
console.log('Missing font file', fontPath);
8177
}
78+
content = content.replace(
79+
new RegExp(`src:(?:,?url\\(./${font}[.][^)]+\\) format\\("[a-z0-9]+"\\))+`),
80+
srcUrl
81+
);
8282
});
8383

8484
const faviconPath = resolve('static/favicon.svg');

0 commit comments

Comments
 (0)