Skip to content

Commit abfd545

Browse files
committed
feat(assets): Extract all assets locally.
1 parent 2e5d791 commit abfd545

File tree

3 files changed

+85
-46
lines changed

3 files changed

+85
-46
lines changed

assets/obsidian-styles.txt.css

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,235 +30,235 @@
3030
@font-face
3131
{
3232
font-family: MJXZERO;
33-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Zero.woff") format("woff");
33+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Zero.woff") format("woff");
3434
}
3535

3636
@font-face
3737
{
3838
font-family: MJXTEX;
39-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Main-Regular.woff") format("woff");
39+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Main-Regular.woff") format("woff");
4040
}
4141

4242
@font-face
4343
{
4444
font-family: MJXTEX-B;
45-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Main-Bold.woff") format("woff");
45+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Main-Bold.woff") format("woff");
4646
}
4747

4848
@font-face
4949
{
5050
font-family: MJXTEX-I;
51-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Math-Italic.woff") format("woff");
51+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Math-Italic.woff") format("woff");
5252
}
5353

5454
@font-face
5555
{
5656
font-family: MJXTEX-MI;
57-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Main-Italic.woff") format("woff");
57+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Main-Italic.woff") format("woff");
5858
}
5959

6060
@font-face
6161
{
6262
font-family: MJXTEX-BI;
63-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Math-BoldItalic.woff") format("woff");
63+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Math-BoldItalic.woff") format("woff");
6464
}
6565

6666
@font-face
6767
{
6868
font-family: MJXTEX-S1;
69-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size1-Regular.woff") format("woff");
69+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size1-Regular.woff") format("woff");
7070
}
7171

7272
@font-face
7373
{
7474
font-family: MJXTEX-S2;
75-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size2-Regular.woff") format("woff");
75+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size2-Regular.woff") format("woff");
7676
}
7777

7878
@font-face
7979
{
8080
font-family: MJXTEX-S3;
81-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size3-Regular.woff") format("woff");
81+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size3-Regular.woff") format("woff");
8282
}
8383

8484
@font-face
8585
{
8686
font-family: MJXTEX-S4;
87-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size4-Regular.woff") format("woff");
87+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Size4-Regular.woff") format("woff");
8888
}
8989

9090
@font-face
9191
{
9292
font-family: MJXTEX-A;
93-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_AMS-Regular.woff") format("woff");
93+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_AMS-Regular.woff") format("woff");
9494
}
9595

9696
@font-face
9797
{
9898
font-family: MJXTEX-C;
99-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Regular.woff") format("woff");
99+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Regular.woff") format("woff");
100100
}
101101

102102
@font-face
103103
{
104104
font-family: MJXTEX-CB;
105-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Bold.woff") format("woff");
105+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Bold.woff") format("woff");
106106
}
107107

108108
@font-face
109109
{
110110
font-family: MJXTEX-FR;
111-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Fraktur-Regular.woff") format("woff");
111+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Fraktur-Regular.woff") format("woff");
112112
}
113113

114114
@font-face
115115
{
116116
font-family: MJXTEX-FRB;
117-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Fraktur-Bold.woff") format("woff");
117+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Fraktur-Bold.woff") format("woff");
118118
}
119119

120120
@font-face
121121
{
122122
font-family: MJXTEX-SS;
123-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_SansSerif-Regular.woff") format("woff");
123+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_SansSerif-Regular.woff") format("woff");
124124
}
125125

126126
@font-face
127127
{
128128
font-family: MJXTEX-SSB;
129-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_SansSerif-Bold.woff") format("woff");
129+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_SansSerif-Bold.woff") format("woff");
130130
}
131131

132132
@font-face
133133
{
134134
font-family: MJXTEX-SSI;
135-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_SansSerif-Italic.woff") format("woff");
135+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_SansSerif-Italic.woff") format("woff");
136136
}
137137

138138
@font-face
139139
{
140140
font-family: MJXTEX-SC;
141-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Script-Regular.woff") format("woff");
141+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Script-Regular.woff") format("woff");
142142
}
143143

144144
@font-face
145145
{
146146
font-family: MJXTEX-T;
147-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Typewriter-Regular.woff") format("woff");
147+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Typewriter-Regular.woff") format("woff");
148148
}
149149

150150
@font-face
151151
{
152152
font-family: MJXTEX-V;
153-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Vector-Regular.woff") format("woff");
153+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Vector-Regular.woff") format("woff");
154154
}
155155

156156
@font-face
157157
{
158158
font-family: MJXTEX-VB;
159-
src: url("https://publish.obsidian.md/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Vector-Bold.woff") format("woff");
159+
src: url("/lib/mathjax/output/chtml/fonts/woff-v2/MathJax_Vector-Bold.woff") format("woff");
160160
}
161161

162162
@font-face {
163163
font-family: 'Avenir Next';
164164
font-weight: normal;
165165
font-style: normal;
166166
font-display: swap;
167-
src: url('https://publish.obsidian.md/public/fonts/94f2f163d4b698242fef.otf');
167+
src: url('/public/fonts/94f2f163d4b698242fef.otf');
168168
}
169169

170170
@font-face {
171171
font-family: 'Inter';
172172
font-style: normal;
173173
font-weight: 200;
174174
font-display: swap;
175-
src: url('https://publish.obsidian.md/public/fonts/72505e6a122c6acd5471.woff2') format('woff2');
175+
src: url('/public/fonts/72505e6a122c6acd5471.woff2') format('woff2');
176176
}
177177

178178
@font-face {
179179
font-family: 'Inter';
180180
font-style: normal;
181181
font-weight: 300;
182182
font-display: swap;
183-
src: url('https://publish.obsidian.md/public/fonts/2d5198822ab091ce4305.woff2') format('woff2');
183+
src: url('/public/fonts/2d5198822ab091ce4305.woff2') format('woff2');
184184
}
185185

186186
@font-face {
187187
font-family: 'Inter';
188188
font-weight: 400;
189189
font-style: normal;
190190
font-display: swap;
191-
src: url('https://publish.obsidian.md/public/fonts/c8ba52b05a9ef10f4758.woff2');
191+
src: url('/public/fonts/c8ba52b05a9ef10f4758.woff2');
192192
}
193193

194194
@font-face {
195195
font-family: 'Inter';
196196
font-weight: 400;
197197
font-style: italic;
198198
font-display: swap;
199-
src: url('https://publish.obsidian.md/public/fonts/cb10ffd7684cd9836a05.woff2');
199+
src: url('/public/fonts/cb10ffd7684cd9836a05.woff2');
200200
}
201201

202202
@font-face {
203203
font-family: 'Inter';
204204
font-weight: 600;
205205
font-style: normal;
206206
font-display: swap;
207-
src: url('https://publish.obsidian.md/public/fonts/b5f0f109bc88052d4000.woff2');
207+
src: url('/public/fonts/b5f0f109bc88052d4000.woff2');
208208
}
209209

210210
@font-face {
211211
font-family: 'Inter';
212212
font-weight: 800;
213213
font-style: normal;
214214
font-display: swap;
215-
src: url('https://publish.obsidian.md/public/fonts/cbe0ae49c52c920fd563.woff2');
215+
src: url('/public/fonts/cbe0ae49c52c920fd563.woff2');
216216
}
217217

218218
@font-face {
219219
font-family: 'Inter';
220220
font-weight: 800;
221221
font-style: italic;
222222
font-display: swap;
223-
src: url('https://publish.obsidian.md/public/fonts/535a6cf662596b3bd6a6.woff2');
223+
src: url('/public/fonts/535a6cf662596b3bd6a6.woff2');
224224
}
225225

226226
@font-face {
227227
font-family: 'Source Code Pro';
228228
font-weight: normal;
229229
font-style: normal;
230230
font-display: swap;
231-
src: url('https://publish.obsidian.md/public/fonts/70cc7ff27245e82ad414.ttf');
231+
src: url('/public/fonts/70cc7ff27245e82ad414.ttf');
232232
}
233233

234234
@font-face {
235235
font-family: 'Source Code Pro';
236236
font-weight: normal;
237237
font-style: italic;
238238
font-display: swap;
239-
src: url('https://publish.obsidian.md/public/fonts/454577c22304619db035.ttf');
239+
src: url('/public/fonts/454577c22304619db035.ttf');
240240
}
241241

242242
@font-face {
243243
font-family: 'Source Code Pro';
244244
font-weight: bold;
245245
font-style: normal;
246246
font-display: swap;
247-
src: url('https://publish.obsidian.md/public/fonts/52ac8f3034507f1d9e53.ttf');
247+
src: url('/public/fonts/52ac8f3034507f1d9e53.ttf');
248248
}
249249

250250
@font-face {
251251
font-family: 'Source Code Pro';
252252
font-weight: bold;
253253
font-style: italic;
254254
font-display: swap;
255-
src: url('https://publish.obsidian.md/public/fonts/05b618077343fbbd92b7.ttf');
255+
src: url('/public/fonts/05b618077343fbbd92b7.ttf');
256256
}
257257

258258
@font-face {
259259
font-family: 'Flow Circular';
260260
font-display: swap;
261-
src: url('https://publish.obsidian.md/public/fonts/853ff76f08786ae44ca0.woff');
261+
src: url('/public/fonts/853ff76f08786ae44ca0.woff');
262262
}
263263

264264
.collapse-icon::before

src/html-generation/asset-handler.ts

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { InvioSettingTab } from "src/settings.js";
1717
import { RenderLog } from "./render-log.js";
1818
import { Utils } from "src/utils/utils.js";
1919
import { StatsView } from "src/statsView.js";
20+
import { log } from "src/moreOnLog.js";
2021

2122
export class AssetHandler
2223
{
@@ -41,6 +42,8 @@ export class AssetHandler
4142
private static lastMathjaxChanged: number = -1;
4243
private static mathjaxStylesheet: CSSStyleSheet | undefined = undefined;
4344

45+
private static appInternalAssets: Downloadable[] = [];
46+
4447
public static webpageJS: string = "";
4548
public static graphViewJS: string = "";
4649
public static graphWASMJS: string = "";
@@ -68,7 +71,7 @@ export class AssetHandler
6871

6972
public static async getDownloads() : Promise<Downloadable[]>
7073
{
71-
let toDownload: Downloadable[] = [];
74+
let toDownload: Downloadable[] = [...this.appInternalAssets];
7275
if (!InvioSettingTab.settings.inlineCSS)
7376
{
7477
let pluginCSS = this.webpageStyles;
@@ -129,7 +132,7 @@ export class AssetHandler
129132
this.lastMathjaxChanged = -1;
130133
}
131134

132-
public static loadMathjaxStyles()
135+
public static async loadMathjaxStyles()
133136
{
134137
// @ts-ignore
135138
if (this.mathjaxStylesheet == undefined) this.mathjaxStylesheet = Array.from(document.styleSheets).find((sheet) => sheet.ownerNode.id == ("MJX-CHTML-styles"));
@@ -144,12 +147,20 @@ export class AssetHandler
144147
{
145148
AssetHandler.mathStyles += this.mathjaxStylesheet.cssRules[i].cssText + "\n";
146149
}
147-
148-
AssetHandler.mathStyles.replaceAll("app://obsidian.md/", "https://publish.obsidian.md/").trim();
150+
const regexAsset = /"(app\:\/\/obsidian\.md\/[^"]+)"/g;
151+
let matchedAsset;
152+
while ((matchedAsset = regexAsset.exec(AssetHandler.mathStyles))) {
153+
log.info('AssetHandler.mathStyles asset matched: ', matchedAsset[1]);
154+
const asset = matchedAsset[1];
155+
const assetContent = await this.getAppAssetsContent(asset);
156+
const assetPathInfo = asset.split('/');
157+
const fileName = assetPathInfo.splice(-1)[0];
158+
this.appInternalAssets.push(new Downloadable(fileName, assetContent, new Path(assetPathInfo.join('/'))))
159+
}
149160
}
150161
else
151162
{
152-
console.log(Utils.getActiveTextView()?.file.name + " does not have latex");
163+
log.info(Utils.getActiveTextView()?.file.name + " does not have latex");
153164
AssetHandler.mathStyles = "";
154165
}
155166

@@ -162,7 +173,6 @@ export class AssetHandler
162173
// @ts-ignore
163174
const pathString = Path.vaultConfigDir.joinString('obsidian-plugin-cache-html').asString || '';
164175

165-
console.log('path str: ', pathString);
166176
return new Path(pathString).directory.absolute();
167177
}
168178

@@ -186,14 +196,22 @@ export class AssetHandler
186196
let rule = appSheet.cssRules[i];
187197
if (rule)
188198
{
189-
if (rule.cssText.startsWith("@font-face")) continue;
199+
// if (rule.cssText.startsWith("@font-face")) continue;
190200
if (rule.cssText.startsWith(".CodeMirror")) continue;
191201
if (rule.cssText.startsWith(".cm-")) continue;
192202

193203
let cssText = rule.cssText + "\n";
194-
cssText = cssText.replaceAll("public/", "https://publish.obsidian.md/public/");
195-
cssText = cssText.replaceAll("lib/", "https://publish.obsidian.md/lib/")
196-
204+
const regexAsset = /"((public|lib)\/[^"]+)"/g;
205+
206+
let matchedAsset;
207+
while ((matchedAsset = regexAsset.exec(cssText))) {
208+
log.info('cssText asset matched: ', matchedAsset[1]);
209+
const asset = matchedAsset[1];
210+
const assetContent = await this.getAppAssetsContent(asset);
211+
const assetPathInfo = asset.split('/');
212+
const fileName = assetPathInfo.splice(-1)[0];
213+
this.appInternalAssets.push(new Downloadable(fileName, assetContent, new Path(assetPathInfo.join('/'))))
214+
}
197215
this.appStyles += cssText;
198216
}
199217
}
@@ -218,6 +236,27 @@ export class AssetHandler
218236
}
219237
}
220238

239+
240+
private static async getAppAssetsContent(asset: string, view?: StatsView): Promise<Buffer>
241+
{
242+
return new Promise((resolve) => {
243+
const xhr = new XMLHttpRequest();
244+
xhr.responseType = 'arraybuffer';
245+
246+
xhr.open('GET', asset, true);
247+
xhr.onload = function() {
248+
if (xhr.status === 200) {
249+
// Get the binary data from the response
250+
const buffer = new Uint8Array(xhr.response);
251+
resolve(Buffer.from(buffer))
252+
} else {
253+
view.error(`load asset ${asset} failed`);
254+
}
255+
};
256+
xhr.send();
257+
})
258+
}
259+
221260
private static async getPluginStyles() : Promise<string>
222261
{
223262
// load 3rd party plugin css

0 commit comments

Comments
 (0)