@luzzle/web.tools 0.0.69 → 0.0.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -7
- package/dist/src/browser.d.ts +0 -1
- package/dist/src/browser.js +0 -2
- package/dist/src/browser.js.map +0 -1
- package/dist/src/commands/opengraph/html.d.ts +0 -4
- package/dist/src/commands/opengraph/html.js +0 -47
- package/dist/src/commands/opengraph/html.js.map +0 -1
- package/dist/src/commands/opengraph/html.test.d.ts +0 -1
- package/dist/src/commands/opengraph/html.test.js +0 -79
- package/dist/src/commands/opengraph/html.test.js.map +0 -1
- package/dist/src/commands/opengraph/svelte.d.ts +0 -7
- package/dist/src/commands/opengraph/svelte.js +0 -77
- package/dist/src/commands/opengraph/svelte.js.map +0 -1
- package/dist/src/commands/opengraph/svelte.test.d.ts +0 -1
- package/dist/src/commands/opengraph/svelte.test.js +0 -189
- package/dist/src/commands/opengraph/svelte.test.js.map +0 -1
- package/dist/src/commands/opengraph/utils.d.ts +0 -8
- package/dist/src/commands/opengraph/utils.js +0 -136
- package/dist/src/commands/opengraph/utils.js.map +0 -1
- package/dist/src/commands/opengraph/utils.test.d.ts +0 -1
- package/dist/src/commands/opengraph/utils.test.js +0 -215
- package/dist/src/commands/opengraph/utils.test.js.map +0 -1
- package/dist/src/lib/browser.d.ts +0 -55
- package/dist/src/lib/browser.js +0 -52
- package/dist/src/lib/browser.js.map +0 -1
- package/dist/src/lib/browser.test.d.ts +0 -1
- package/dist/src/lib/browser.test.js +0 -67
- package/dist/src/lib/browser.test.js.map +0 -1
- package/dist/src/lib/config/config.d.ts +0 -9
- package/dist/src/lib/config/config.js +0 -56
- package/dist/src/lib/config/config.js.map +0 -1
- package/dist/src/lib/config/config.test.d.ts +0 -1
- package/dist/src/lib/config/config.test.js +0 -71
- package/dist/src/lib/config/config.test.js.map +0 -1
- package/dist/src/lib/config/config.test.ts +0 -88
- package/dist/src/lib/config/config.ts +0 -74
- package/dist/src/lib/config/defaults.json +0 -145
- package/dist/src/lib/config/defaults.yaml +0 -151
- package/dist/src/lib/config/schema.d.ts +0 -172
- package/dist/src/lib/config/schema.js +0 -8
- package/dist/src/lib/config/schema.js.map +0 -1
- package/dist/src/lib/config/schema.json +0 -663
- package/dist/src/lib/config/schema.ts +0 -294
- package/dist/src/lib/config/user-error.config.yaml +0 -2
- package/dist/src/lib/config/user.config.yaml +0 -2
- package/dist/src/lib/deep-merge.d.ts +0 -1
- package/dist/src/lib/deep-merge.js +0 -27
- package/dist/src/lib/deep-merge.js.map +0 -1
- package/dist/src/lib/deep-merge.test.d.ts +0 -1
- package/dist/src/lib/deep-merge.test.js +0 -29
- package/dist/src/lib/deep-merge.test.js.map +0 -1
- package/dist/src/lib/vibrant.d.ts +0 -21
- package/dist/src/lib/vibrant.js +0 -78
- package/dist/src/lib/vibrant.js.map +0 -1
- package/dist/src/lib/vibrant.test.d.ts +0 -1
- package/dist/src/lib/vibrant.test.js +0 -164
- package/dist/src/lib/vibrant.test.js.map +0 -1
- package/dist/src/sdk.d.ts +0 -4
- package/dist/src/sdk.js +0 -4
- package/dist/src/sdk.js.map +0 -1
- package/dist/src/types.d.ts +0 -2
- package/dist/src/types.js +0 -2
- package/dist/src/types.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luzzle/web.tools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.71",
|
|
4
4
|
"author": "eleith",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint src",
|
|
7
|
-
"compile": "tsc --noEmit",
|
|
8
7
|
"build:executable": "chmod +x ./dist/src/cli.js",
|
|
9
8
|
"postbuild": "npm run build:executable",
|
|
10
9
|
"build": "tsc",
|
|
@@ -22,10 +21,10 @@
|
|
|
22
21
|
],
|
|
23
22
|
"repository": {
|
|
24
23
|
"type": "git",
|
|
25
|
-
"url": "git+https://
|
|
24
|
+
"url": "git+https://github.com/eleith/luzzle.git"
|
|
26
25
|
},
|
|
27
|
-
"homepage": "https://
|
|
28
|
-
"description": "
|
|
26
|
+
"homepage": "https://github.com/eleith/luzzle",
|
|
27
|
+
"description": "cli to prepare luzzle archive for the web",
|
|
29
28
|
"devDependencies": {
|
|
30
29
|
"@types/eslint": "9.6.1",
|
|
31
30
|
"@types/node": "22.12.0",
|
|
@@ -42,8 +41,8 @@
|
|
|
42
41
|
"vitest": "3.2.4"
|
|
43
42
|
},
|
|
44
43
|
"dependencies": {
|
|
45
|
-
"@luzzle/core": "0.0.
|
|
46
|
-
"@luzzle/web.utils": "0.0.
|
|
44
|
+
"@luzzle/core": "0.0.34",
|
|
45
|
+
"@luzzle/web.utils": "0.0.13",
|
|
47
46
|
"lightningcss": "^1.30.2",
|
|
48
47
|
"puppeteer": "24.19.0",
|
|
49
48
|
"sharp": "0.33.5",
|
package/dist/src/browser.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { getOpenGraphPath, getImageAssetPath, getAssetDir, getAssetPath, ASSET_SIZES, ASSET_PATH_MATCHER, ASSET_IMAGE_MATCHER, OpengraphImageWidth, OpengraphImageHeight, } from './lib/browser.js';
|
package/dist/src/browser.js
DELETED
package/dist/src/browser.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,WAAW,EACX,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,GACpB,MAAM,kBAAkB,CAAA"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { OpengraphImageWidth, OpengraphImageHeight } from '@luzzle/web.utils';
|
|
2
|
-
import { getIconComponentForType, getOpengraphComponentForType } from './svelte.js';
|
|
3
|
-
import { render } from 'svelte/server';
|
|
4
|
-
import { findAndReplaceLuzzleUrls, getProps } from './utils.js';
|
|
5
|
-
async function buildHtmlDoc(head, body, pieces) {
|
|
6
|
-
const html = await findAndReplaceLuzzleUrls(body, pieces);
|
|
7
|
-
return `
|
|
8
|
-
<html>
|
|
9
|
-
<head>
|
|
10
|
-
<meta charset="UTF-8" />
|
|
11
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
12
|
-
<style>
|
|
13
|
-
* {
|
|
14
|
-
box-sizing: border-box;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
html, body {
|
|
18
|
-
margin: 0;
|
|
19
|
-
padding: 0;
|
|
20
|
-
width: ${OpengraphImageWidth}px;
|
|
21
|
-
height: ${OpengraphImageHeight}px;
|
|
22
|
-
}
|
|
23
|
-
</style>
|
|
24
|
-
${head}
|
|
25
|
-
</head>
|
|
26
|
-
<body>${html}</body>
|
|
27
|
-
</html>`;
|
|
28
|
-
}
|
|
29
|
-
async function generateHtml(item, pieces, config) {
|
|
30
|
-
console.log(`generating opengraph for ${item.file_path} (${item.id})`);
|
|
31
|
-
try {
|
|
32
|
-
const OpenGraph = await getOpengraphComponentForType(item, config);
|
|
33
|
-
if (OpenGraph) {
|
|
34
|
-
const Icon = await getIconComponentForType(item, config);
|
|
35
|
-
const props = await getProps(item, Icon, pieces, config);
|
|
36
|
-
const { head, body } = await render(OpenGraph, { props: props });
|
|
37
|
-
return buildHtmlDoc(head, body, pieces);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
console.log(pieces, config);
|
|
42
|
-
throw new Error(`Error generating Open Graph for ${item.file_path} (${item.id}): ${e}`);
|
|
43
|
-
}
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
export { generateHtml };
|
|
47
|
-
//# sourceMappingURL=html.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"html.js","sourceRoot":"","sources":["../../../../src/commands/opengraph/html.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M,EAAE,MAAM,CAAC,CAAA;QAC3B,MAAM,IAAI,KAAK,CAAC,mCAAmC,IAAI,CAAC,SAAS,KAAK,IAAI,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,CAAA;IACxF,CAAC;IAED,OAAO,IAAI,CAAA;AACZ,CAAC;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, vi, afterEach } from 'vitest';
|
|
2
|
-
import { generateHtml } from './html.js';
|
|
3
|
-
import { Pieces } from '@luzzle/cli';
|
|
4
|
-
import { getIconComponentForType, getOpengraphComponentForType } from './svelte.js';
|
|
5
|
-
import { findAndReplaceLuzzleUrls, getProps } from './utils.js';
|
|
6
|
-
import { render } from 'svelte/server';
|
|
7
|
-
vi.mock('@luzzle/cli');
|
|
8
|
-
vi.mock('./svelte.js');
|
|
9
|
-
vi.mock('./utils.js');
|
|
10
|
-
vi.mock('svelte/server');
|
|
11
|
-
const mocks = {
|
|
12
|
-
findAndReplaceLuzzleUrls: vi.mocked(findAndReplaceLuzzleUrls),
|
|
13
|
-
getOpengraphComponentForType: vi.mocked(getOpengraphComponentForType),
|
|
14
|
-
getIconComponentForType: vi.mocked(getIconComponentForType),
|
|
15
|
-
getProps: vi.mocked(getProps),
|
|
16
|
-
render: vi.mocked(render),
|
|
17
|
-
};
|
|
18
|
-
const spies = {};
|
|
19
|
-
const mockPieces = new Pieces({});
|
|
20
|
-
const mockConfig = {
|
|
21
|
-
paths: { database: 'test.db', config: 'test' },
|
|
22
|
-
url: { app: 'test', luzzle_assets: 'test', app_assets: 'test' },
|
|
23
|
-
text: { title: 'test', description: 'test' },
|
|
24
|
-
};
|
|
25
|
-
describe('src/commands/opengraph/html', () => {
|
|
26
|
-
afterEach(() => {
|
|
27
|
-
Object.values(mocks).forEach((mock) => {
|
|
28
|
-
mock.mockReset();
|
|
29
|
-
});
|
|
30
|
-
Object.values(spies).forEach((spy) => {
|
|
31
|
-
spy.mockRestore();
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
test('generateHtml should correctly generate HTML for an item', async () => {
|
|
35
|
-
const item = {
|
|
36
|
-
type: 'book',
|
|
37
|
-
id: '1',
|
|
38
|
-
file_path: 'test',
|
|
39
|
-
date_added: 'test',
|
|
40
|
-
};
|
|
41
|
-
const mockOpenGraphComponent = { render: vi.fn() };
|
|
42
|
-
const mockIconComponent = { render: vi.fn() };
|
|
43
|
-
const mockProps = { some: 'props' };
|
|
44
|
-
const mockRenderResult = { head: '<meta name="test" />', body: '<h1>Hello</h1>', html: '--' };
|
|
45
|
-
mocks.getOpengraphComponentForType.mockResolvedValue(mockOpenGraphComponent);
|
|
46
|
-
mocks.getIconComponentForType.mockResolvedValue(mockIconComponent);
|
|
47
|
-
mocks.getProps.mockResolvedValue(mockProps);
|
|
48
|
-
mocks.render.mockResolvedValue(mockRenderResult);
|
|
49
|
-
mocks.findAndReplaceLuzzleUrls.mockResolvedValue(mockRenderResult.body);
|
|
50
|
-
const result = await generateHtml(item, mockPieces, mockConfig);
|
|
51
|
-
expect(mocks.render).toHaveBeenCalledOnce();
|
|
52
|
-
expect(result).toContain(mockRenderResult.head);
|
|
53
|
-
expect(result).toContain(mockRenderResult.body);
|
|
54
|
-
});
|
|
55
|
-
test('generateHtml should skip html generation', async () => {
|
|
56
|
-
const item = {
|
|
57
|
-
type: 'book',
|
|
58
|
-
id: '1',
|
|
59
|
-
file_path: 'test',
|
|
60
|
-
date_added: 'test',
|
|
61
|
-
};
|
|
62
|
-
mocks.getOpengraphComponentForType.mockResolvedValue(null);
|
|
63
|
-
const html = await generateHtml(item, mockPieces, mockConfig);
|
|
64
|
-
expect(html).toBeNull();
|
|
65
|
-
});
|
|
66
|
-
test('generateHtml should handle errors during HTML generation', async () => {
|
|
67
|
-
const item = {
|
|
68
|
-
type: 'book',
|
|
69
|
-
id: '1',
|
|
70
|
-
file_path: 'test',
|
|
71
|
-
date_added: 'test',
|
|
72
|
-
};
|
|
73
|
-
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
74
|
-
mocks.getOpengraphComponentForType.mockRejectedValue(new Error('Test Error'));
|
|
75
|
-
await expect(generateHtml(item, mockPieces, mockConfig)).rejects.toThrowError();
|
|
76
|
-
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
//# sourceMappingURL=html.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"html.test.js","sourceRoot":"","sources":["../../../../src/commands/opengraph/html.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAgB,MAAM,QAAQ,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,MAAM,EAAW,MAAM,aAAa,CAAA;AAE7C,OAAO,EAAE,uBAAuB,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAA;AACnF,OAAO,EAAE,wBAAwB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAGtC,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;AACtB,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;AACtB,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;AACrB,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;AAExB,MAAM,KAAK,GAAG;IACb,wBAAwB,EAAE,EAAE,CAAC,MAAM,CAAC,wBAAwB,CAAC;IAC7D,4BAA4B,EAAE,EAAE,CAAC,MAAM,CAAC,4BAA4B,CAAC;IACrE,uBAAuB,EAAE,EAAE,CAAC,MAAM,CAAC,uBAAuB,CAAC;IAC3D,QAAQ,EAAE,EAAE,CAAC,MAAM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AAC,CAAC,CAAA;IAEF,IAAI,CAAC,0DAA0D,EAAE,KAAK,IAAI,EAAE;QAC3E,MAAM,IAAI,GAAG;YACZ,IAAI,EAAE,MAAM;YACZ,EAAE,EAAE,GAAG;YACP,SAAS,EAAE,MAAM;YACjB,UAAU,EAAE,MAAM;SACM,CAAA;QACzB,MAAM,eAAe,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;QAEhF,KAAK,CAAC,4BAA4B,CAAC,iBAAiB,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAA;QAE7E,MAAM,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,EAAE,CAAA;QAC/E,MAAM,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;IAC/C,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Component } from 'svelte';
|
|
2
|
-
import { type WebPieces, type Config, type PieceIconProps, type PieceOpengraphProps } from '@luzzle/web.utils';
|
|
3
|
-
declare const CompiledStore: Record<string, Component>;
|
|
4
|
-
declare function getSvelteComponent(sveltePath: string): Promise<(typeof CompiledStore)[string]>;
|
|
5
|
-
declare function getOpengraphComponentForType(item: WebPieces, config: Config): Promise<Component<PieceOpengraphProps, {}, string> | null>;
|
|
6
|
-
declare function getIconComponentForType(item: WebPieces, config: Config): Promise<Component<PieceIconProps, {}, string> | null>;
|
|
7
|
-
export { getSvelteComponent, getOpengraphComponentForType, getIconComponentForType };
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'fs/promises';
|
|
2
|
-
import { compile } from 'svelte/compiler';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { createHash } from 'crypto';
|
|
5
|
-
import { bufferToBase64, replaceAsync } from './utils.js';
|
|
6
|
-
const CompiledStore = {};
|
|
7
|
-
async function embedFontsInCompiledCode(code, sveltePath) {
|
|
8
|
-
const fontRegExp = /src:\s*url\((.*\.woff2?)\)/g;
|
|
9
|
-
const codeWithFonts = await replaceAsync(code, fontRegExp, async (_, fontPath) => {
|
|
10
|
-
const fullFontPath = path.join(path.dirname(sveltePath), fontPath);
|
|
11
|
-
const fontBuffer = await readFile(fullFontPath);
|
|
12
|
-
const fontExt = path.extname(fullFontPath).slice(1);
|
|
13
|
-
const base64Font = bufferToBase64(fontBuffer, 'font', fontExt);
|
|
14
|
-
return `src: url(data:font/${fontExt};base64,${base64Font})`;
|
|
15
|
-
});
|
|
16
|
-
return codeWithFonts;
|
|
17
|
-
}
|
|
18
|
-
async function replaceImportsInCompiledCode(code) {
|
|
19
|
-
const importRegex = /(from|import)\s+(['"])([^'"]+)\2/g;
|
|
20
|
-
const finalCompiledCode = await replaceAsync(code, importRegex, async (match, keyword, quote, specifier) => {
|
|
21
|
-
if (specifier.startsWith('.') || specifier.startsWith('/')) {
|
|
22
|
-
return match;
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
const resolvedPath = import.meta.resolve(specifier).toString();
|
|
26
|
-
return `${keyword} ${quote}${resolvedPath}${quote}`;
|
|
27
|
-
}
|
|
28
|
-
catch (err) {
|
|
29
|
-
console.error(`Could not resolve module: ${specifier}`, err);
|
|
30
|
-
return match;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
return finalCompiledCode;
|
|
34
|
-
}
|
|
35
|
-
async function getSvelteComponent(sveltePath) {
|
|
36
|
-
const svelteHash = createHash('md5').update(sveltePath).digest('hex').slice(0, 8);
|
|
37
|
-
const svelteId = `${path.basename(sveltePath)}-${svelteHash}`;
|
|
38
|
-
if (!CompiledStore[svelteId]) {
|
|
39
|
-
const svelteCode = await readFile(sveltePath, 'utf-8');
|
|
40
|
-
const svelteCompiled = compile(svelteCode, {
|
|
41
|
-
generate: 'server',
|
|
42
|
-
filename: svelteId,
|
|
43
|
-
css: 'injected',
|
|
44
|
-
experimental: {
|
|
45
|
-
async: true,
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
const compiledCodeWithFonts = await embedFontsInCompiledCode(svelteCompiled.js.code, sveltePath);
|
|
49
|
-
const finalCompiledCode = await replaceImportsInCompiledCode(compiledCodeWithFonts);
|
|
50
|
-
const encodedCode = encodeURIComponent(finalCompiledCode);
|
|
51
|
-
const opengraphModule = await import(`data:text/javascript,${encodedCode}`);
|
|
52
|
-
CompiledStore[svelteId] = opengraphModule.default;
|
|
53
|
-
}
|
|
54
|
-
return CompiledStore[svelteId];
|
|
55
|
-
}
|
|
56
|
-
async function getOpengraphComponentForType(item, config) {
|
|
57
|
-
const ogComponentPath = config.pieces.find((p) => p.type === item.type)?.components?.opengraph;
|
|
58
|
-
if (ogComponentPath && config.paths.config) {
|
|
59
|
-
const ogPath = path.join(path.dirname(config.paths.config), ogComponentPath);
|
|
60
|
-
return getSvelteComponent(ogPath);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async function getIconComponentForType(item, config) {
|
|
67
|
-
const iconComponentPath = config.pieces.find((p) => p.type === item.type)?.components?.icon;
|
|
68
|
-
if (iconComponentPath && config.paths.config) {
|
|
69
|
-
const fullPath = path.join(path.dirname(config.paths.config), iconComponentPath);
|
|
70
|
-
return getSvelteComponent(fullPath);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
export { getSvelteComponent, getOpengraphComponentForType, getIconComponentForType };
|
|
77
|
-
//# sourceMappingURL=svelte.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"svelte.js","sourceRoot":"","sources":["../../../../src/commands/opengraph/svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EA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AC,QAAQ,CAAuC,CAAA;IAC1E,CAAC;SAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACZ,CAAC;AACF,CAAC;AAED,OAAO,EAAE,kBAAkB,EAAE,4BAA4B,EAAE,uBAAuB,EAAE,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, vi, afterEach, beforeEach } from 'vitest';
|
|
2
|
-
import { getSvelteComponent, getOpengraphComponentForType, getIconComponentForType, } from './svelte.js';
|
|
3
|
-
import { readFile, writeFile } from 'fs/promises';
|
|
4
|
-
import { compile } from 'svelte/compiler';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
import * as utils from './utils.js';
|
|
7
|
-
vi.mock('fs/promises');
|
|
8
|
-
vi.mock('svelte/compiler');
|
|
9
|
-
const mockedImportMetaResolve = vi.fn();
|
|
10
|
-
vi.mock('import.meta', () => {
|
|
11
|
-
return {
|
|
12
|
-
resolve: mockedImportMetaResolve,
|
|
13
|
-
};
|
|
14
|
-
});
|
|
15
|
-
const mocks = {
|
|
16
|
-
readFile: vi.mocked(readFile),
|
|
17
|
-
writeFile: vi.mocked(writeFile),
|
|
18
|
-
compile: vi.mocked(compile),
|
|
19
|
-
};
|
|
20
|
-
const spies = {};
|
|
21
|
-
const mockConfig = {
|
|
22
|
-
paths: {
|
|
23
|
-
database: 'test.db',
|
|
24
|
-
config: '/path/to/config.yaml',
|
|
25
|
-
},
|
|
26
|
-
url: {
|
|
27
|
-
app: 'https://example.com',
|
|
28
|
-
luzzle_assets: 'https://assets.example.com',
|
|
29
|
-
app_assets: 'https://app-assets.example.com',
|
|
30
|
-
editor: 'https://editor.example.com',
|
|
31
|
-
},
|
|
32
|
-
pieces: [
|
|
33
|
-
{
|
|
34
|
-
type: 'book',
|
|
35
|
-
components: {
|
|
36
|
-
opengraph: './components/BookOg.svelte',
|
|
37
|
-
icon: './components/BookIcon.svelte',
|
|
38
|
-
},
|
|
39
|
-
fields: {
|
|
40
|
-
title: 'book_title',
|
|
41
|
-
date_consumed: 'date_read',
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
type: 'article',
|
|
46
|
-
components: {
|
|
47
|
-
opengraph: './components/ArticleOg.svelte',
|
|
48
|
-
},
|
|
49
|
-
fields: {
|
|
50
|
-
title: 'article_title',
|
|
51
|
-
date_consumed: 'date_read',
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
],
|
|
55
|
-
};
|
|
56
|
-
describe('src/commands/opengraph/svelte', () => {
|
|
57
|
-
beforeEach(() => {
|
|
58
|
-
// Reset mocks before each test
|
|
59
|
-
Object.values(mocks).forEach((mock) => mock.mockReset());
|
|
60
|
-
// Restore spies before each test
|
|
61
|
-
Object.values(spies).forEach((spy) => spy.mockRestore());
|
|
62
|
-
spies.pathBasename = vi.spyOn(path, 'basename');
|
|
63
|
-
spies.pathDirname = vi.spyOn(path, 'dirname');
|
|
64
|
-
spies.pathJoin = vi.spyOn(path, 'join');
|
|
65
|
-
spies.pathExtname = vi.spyOn(path, 'extname');
|
|
66
|
-
spies.bufferToBase64 = vi.spyOn(utils, 'bufferToBase64');
|
|
67
|
-
spies.consoleError = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
68
|
-
});
|
|
69
|
-
afterEach(() => {
|
|
70
|
-
// Clear all mocks after each test
|
|
71
|
-
vi.clearAllMocks();
|
|
72
|
-
});
|
|
73
|
-
test('getSvelteComponent should compile and store a svelte component', async () => {
|
|
74
|
-
const sveltePath = './components/test1.svelte';
|
|
75
|
-
const code = '<p>Hello</p>';
|
|
76
|
-
const compiledCode = 'export default { test: "component" }';
|
|
77
|
-
spies.pathBasename.mockReturnValue('test1.svelte');
|
|
78
|
-
mocks.readFile.mockResolvedValue(code);
|
|
79
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
80
|
-
const component = await getSvelteComponent(sveltePath);
|
|
81
|
-
expect(mocks.readFile).toHaveBeenCalledOnce();
|
|
82
|
-
expect(mocks.compile).toHaveBeenCalledOnce();
|
|
83
|
-
expect(mocks.writeFile).not.toHaveBeenCalled();
|
|
84
|
-
expect(component).toEqual({ test: 'component' });
|
|
85
|
-
});
|
|
86
|
-
test('getSvelteComponent should return cached component if already compiled', async () => {
|
|
87
|
-
const sveltePath = './components/test2.svelte';
|
|
88
|
-
const code = '<p>Hello</p>';
|
|
89
|
-
const compiledCode = 'export default { test: "component2" }';
|
|
90
|
-
spies.pathBasename.mockReturnValue('test2.svelte');
|
|
91
|
-
mocks.readFile.mockResolvedValue(code);
|
|
92
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
93
|
-
await getSvelteComponent(sveltePath);
|
|
94
|
-
await getSvelteComponent(sveltePath);
|
|
95
|
-
expect(mocks.readFile).toHaveBeenCalledOnce();
|
|
96
|
-
expect(mocks.compile).toHaveBeenCalledOnce();
|
|
97
|
-
});
|
|
98
|
-
test('getSvelteComponent should handle font-face urls', async () => {
|
|
99
|
-
const sveltePath = './components/test3.svelte';
|
|
100
|
-
const fontPath = './components/fonts/test-font.woff2';
|
|
101
|
-
const fontBuffer = Buffer.from('fontdata');
|
|
102
|
-
const svelteCodeWithFont = `<style>@font-face { src: url(fonts/test-font.woff2); }</style>`;
|
|
103
|
-
const compiledCode = `const css = "src: url(fonts/test-font.woff2)";\nexport default {};`;
|
|
104
|
-
spies.pathBasename.mockReturnValue('test3.svelte');
|
|
105
|
-
spies.pathDirname.mockReturnValue('./components');
|
|
106
|
-
spies.pathJoin.mockReturnValue(fontPath);
|
|
107
|
-
spies.pathExtname.mockReturnValue('.woff2');
|
|
108
|
-
spies.bufferToBase64.mockReturnValue('Zm9udGRhdGE=');
|
|
109
|
-
mocks.readFile.mockResolvedValueOnce(svelteCodeWithFont);
|
|
110
|
-
mocks.readFile.mockResolvedValueOnce(fontBuffer);
|
|
111
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
112
|
-
spies.encodeURIComponent = vi.spyOn(global, 'encodeURIComponent');
|
|
113
|
-
await getSvelteComponent(sveltePath);
|
|
114
|
-
expect(mocks.readFile).toHaveBeenCalledTimes(2);
|
|
115
|
-
const finalCode = decodeURIComponent(spies.encodeURIComponent.mock.calls[0][0]);
|
|
116
|
-
expect(finalCode).toContain('src: url(data:font/woff2;base64,Zm9udGRhdGE=)');
|
|
117
|
-
});
|
|
118
|
-
test('replaceImportsInCompiledCode should ignore relative paths', async () => {
|
|
119
|
-
const sveltePath = './components/test5.svelte';
|
|
120
|
-
const code = `<script>import './local.js';</script>`;
|
|
121
|
-
const compiledCode = `import './local.js';\nexport default {};`;
|
|
122
|
-
spies.pathBasename.mockReturnValue('test5.svelte');
|
|
123
|
-
mocks.readFile.mockResolvedValue(code);
|
|
124
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
125
|
-
spies.encodeURIComponent = vi
|
|
126
|
-
.spyOn(global, 'encodeURIComponent')
|
|
127
|
-
.mockImplementation((str) => {
|
|
128
|
-
expect(str).toContain(`import './local.js'`);
|
|
129
|
-
throw new Error('Stop execution');
|
|
130
|
-
});
|
|
131
|
-
await expect(getSvelteComponent(sveltePath)).rejects.toThrow('Stop execution');
|
|
132
|
-
expect(mockedImportMetaResolve).not.toHaveBeenCalled();
|
|
133
|
-
});
|
|
134
|
-
test('replaceImportsInCompiledCode should handle resolution errors', async () => {
|
|
135
|
-
const sveltePath = './components/test-bad-module.svelte';
|
|
136
|
-
const code = `<script>import 'bad-module';</script>`;
|
|
137
|
-
const compiledCode = `import 'bad-module';\nexport default {};`;
|
|
138
|
-
spies.pathBasename.mockReturnValue('test-bad-module.svelte');
|
|
139
|
-
mocks.readFile.mockResolvedValue(code);
|
|
140
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
141
|
-
spies.encodeURIComponent = vi
|
|
142
|
-
.spyOn(global, 'encodeURIComponent')
|
|
143
|
-
.mockImplementation((str) => {
|
|
144
|
-
expect(str).toContain(`import 'bad-module'`);
|
|
145
|
-
throw new Error('Stop execution');
|
|
146
|
-
});
|
|
147
|
-
await expect(getSvelteComponent(sveltePath)).rejects.toThrow('Stop execution');
|
|
148
|
-
expect(spies.consoleError).toHaveBeenCalledWith('Could not resolve module: bad-module', expect.any(Error));
|
|
149
|
-
});
|
|
150
|
-
test('getOpengraphComponentForType should return the correct opengraph component', async () => {
|
|
151
|
-
const item = { type: 'book' };
|
|
152
|
-
const sveltePath = '/path/to/components/BookOg.svelte';
|
|
153
|
-
const compiledCode = 'export default { isOg: true }';
|
|
154
|
-
spies.pathDirname.mockReturnValue('/path/to');
|
|
155
|
-
spies.pathJoin.mockReturnValue(sveltePath);
|
|
156
|
-
spies.pathBasename.mockReturnValue('BookOg.svelte');
|
|
157
|
-
mocks.readFile.mockResolvedValue('');
|
|
158
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
159
|
-
const component = await getOpengraphComponentForType(item, mockConfig);
|
|
160
|
-
expect(spies.pathJoin).toHaveBeenCalledWith('/path/to', './components/BookOg.svelte');
|
|
161
|
-
expect(mocks.readFile).toHaveBeenCalledWith(sveltePath, 'utf-8');
|
|
162
|
-
expect(component).toEqual({ isOg: true });
|
|
163
|
-
});
|
|
164
|
-
test('getOpengraphComponentForType should return null if not found in config', async () => {
|
|
165
|
-
const item = { type: 'movie' };
|
|
166
|
-
const component = await getOpengraphComponentForType(item, mockConfig);
|
|
167
|
-
expect(component).toBeNull();
|
|
168
|
-
});
|
|
169
|
-
test('getIconComponentForType should return the correct icon component', async () => {
|
|
170
|
-
const item = { type: 'book' };
|
|
171
|
-
const sveltePath = '/path/to/components/BookIcon.svelte';
|
|
172
|
-
const compiledCode = 'export default { isIcon: true }';
|
|
173
|
-
spies.pathDirname.mockReturnValue('/path/to');
|
|
174
|
-
spies.pathJoin.mockReturnValue(sveltePath);
|
|
175
|
-
spies.pathBasename.mockReturnValue('BookIcon.svelte');
|
|
176
|
-
mocks.readFile.mockResolvedValue('');
|
|
177
|
-
mocks.compile.mockReturnValue({ js: { code: compiledCode } });
|
|
178
|
-
const component = await getIconComponentForType(item, mockConfig);
|
|
179
|
-
expect(spies.pathJoin).toHaveBeenCalledWith('/path/to', './components/BookIcon.svelte');
|
|
180
|
-
expect(mocks.readFile).toHaveBeenCalledWith(sveltePath, 'utf-8');
|
|
181
|
-
expect(component).toEqual({ isIcon: true });
|
|
182
|
-
});
|
|
183
|
-
test('getIconComponentForType should return null if icon component not found in config', async () => {
|
|
184
|
-
const item = { type: 'article' };
|
|
185
|
-
const component = await getIconComponentForType(item, mockConfig);
|
|
186
|
-
expect(component).toBeNull();
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
//# sourceMappingURL=svelte.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"svelte.test.js","sourceRoot":"","sources":["../../../../src/commands/opengraph/svelte.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAgB,UAAU,EAAE,MAAM,QAAQ,CAAA;AACxF,OAAO,EACN,kBAAkB,EAClB,4BAA4B,EAC5B,uBAAuB,GACvB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AACzC,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAEnC,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;AACtB,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAA;AAE1B,MAAM,uBAAuB,GAAG,EAAE,CAAC,EAAE,EAAE,CAAA;AACvC,EAAE,CAAC,IAAI,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,OAAO;QACN,OAAO,EAAE,uBAAuB;KAChC,CAAA;AACF,CAAC,CAAC,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AE;QACnG,MAAM,IAAI,GAAG,EAAE,IAAI,EAAE,SAAS,EAA0B,CAAA;QACxD,MAAM,SAAS,GAAG,MAAM,uBAAuB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;QACjE,MAAM,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,CAAA;IAC7B,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Pieces } from '@luzzle/cli';
|
|
2
|
-
import { Component } from 'svelte';
|
|
3
|
-
import { type PieceOpengraphProps, type PieceIconProps, type WebPieces, type Config } from '@luzzle/web.utils';
|
|
4
|
-
declare function getProps(piece: WebPieces, Icon: Component<PieceIconProps> | null, pieces: Pieces, config: Config): Promise<PieceOpengraphProps>;
|
|
5
|
-
declare function replaceAsync(str: string, regex: RegExp, asyncFn: (match: string, ...args: string[]) => Promise<string>): Promise<string>;
|
|
6
|
-
declare function findAndReplaceLuzzleUrls(html: string, pieces: Pieces): Promise<string>;
|
|
7
|
-
declare function bufferToBase64(buffer: Buffer, type: string, format: string): string;
|
|
8
|
-
export { getProps, findAndReplaceLuzzleUrls, bufferToBase64, replaceAsync };
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import * as cheerio from 'cheerio';
|
|
3
|
-
import { OpengraphImageHeight, OpengraphImageWidth, } from '@luzzle/web.utils';
|
|
4
|
-
import { getPalette } from '@luzzle/web.utils/server';
|
|
5
|
-
async function getProps(piece, Icon, pieces, config) {
|
|
6
|
-
const props = {
|
|
7
|
-
Icon: Icon || undefined,
|
|
8
|
-
piece: piece,
|
|
9
|
-
metadata: JSON.parse(piece.json_metadata || '{}'),
|
|
10
|
-
tags: JSON.parse(piece.keywords || '[]'),
|
|
11
|
-
size: {
|
|
12
|
-
width: OpengraphImageWidth,
|
|
13
|
-
height: OpengraphImageHeight,
|
|
14
|
-
},
|
|
15
|
-
helpers: getHelpers(piece, config),
|
|
16
|
-
};
|
|
17
|
-
if (piece.media) {
|
|
18
|
-
const imageBuffer = await pieces.getPieceAsset(piece.media);
|
|
19
|
-
const palette = (await getPalette(imageBuffer));
|
|
20
|
-
return {
|
|
21
|
-
...props,
|
|
22
|
-
palette,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
return props;
|
|
26
|
-
}
|
|
27
|
-
async function luzzleImageUrlToBase64(url, pieces) {
|
|
28
|
-
const srcPath = url.replace('luzzle://', '').trim();
|
|
29
|
-
const ext = path.extname(srcPath).slice(1);
|
|
30
|
-
const newSrc = await pieces.getPieceAsset(srcPath);
|
|
31
|
-
return bufferToBase64(newSrc, 'image', ext);
|
|
32
|
-
}
|
|
33
|
-
async function replaceAsync(str, regex, asyncFn) {
|
|
34
|
-
const matches = Array.from(str.matchAll(regex));
|
|
35
|
-
const replacements = await Promise.all(matches.map((match) => asyncFn(match[0], ...match.slice(1))));
|
|
36
|
-
let result = '';
|
|
37
|
-
let lastIndex = 0;
|
|
38
|
-
matches.forEach((match, index) => {
|
|
39
|
-
result += str.substring(lastIndex, match.index);
|
|
40
|
-
result += replacements[index];
|
|
41
|
-
lastIndex = (match.index || 0) + match[0].length;
|
|
42
|
-
});
|
|
43
|
-
result += str.substring(lastIndex);
|
|
44
|
-
return result;
|
|
45
|
-
}
|
|
46
|
-
async function findAndReplaceLuzzleUrls(html, pieces) {
|
|
47
|
-
const $ = cheerio.load(html);
|
|
48
|
-
const tasks = [];
|
|
49
|
-
$('img').each((_, el) => {
|
|
50
|
-
const $el = $(el);
|
|
51
|
-
const src = $el.attr('src');
|
|
52
|
-
if (src && src.startsWith('luzzle://')) {
|
|
53
|
-
const task = (async () => {
|
|
54
|
-
const base64 = await luzzleImageUrlToBase64(src, pieces);
|
|
55
|
-
$el.attr('src', base64);
|
|
56
|
-
})();
|
|
57
|
-
tasks.push(task);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
$('img[srcset], picture source[srcset]').each((_, el) => {
|
|
61
|
-
const $el = $(el);
|
|
62
|
-
const srcset = $el.attr('srcset');
|
|
63
|
-
if (srcset) {
|
|
64
|
-
const task = (async () => {
|
|
65
|
-
const sourcePromises = srcset.split(',').map(async (source) => {
|
|
66
|
-
const [url, ...descriptors] = source.trim().split(/\s+/);
|
|
67
|
-
if (url && url.startsWith('luzzle://')) {
|
|
68
|
-
const base64 = await luzzleImageUrlToBase64(url, pieces);
|
|
69
|
-
return [base64, ...descriptors].join(' ');
|
|
70
|
-
}
|
|
71
|
-
return source;
|
|
72
|
-
});
|
|
73
|
-
const newSources = await Promise.all(sourcePromises);
|
|
74
|
-
$el.attr('srcset', newSources.join(', '));
|
|
75
|
-
})();
|
|
76
|
-
tasks.push(task);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
const urlRegex = /url\((['"]?\s*)(.*?)\1\)/g;
|
|
80
|
-
$('style').each((_, el) => {
|
|
81
|
-
const $el = $(el);
|
|
82
|
-
const styleContent = $el.html(); // Get the raw text content of the style tag
|
|
83
|
-
if (styleContent) {
|
|
84
|
-
const task = (async () => {
|
|
85
|
-
const newStyleContent = await replaceAsync(styleContent, urlRegex, async (_, quote, url) => {
|
|
86
|
-
if (url.startsWith('luzzle://')) {
|
|
87
|
-
const base64 = await luzzleImageUrlToBase64(url, pieces);
|
|
88
|
-
return `url(${quote}${base64}${quote})`;
|
|
89
|
-
}
|
|
90
|
-
return `url(${quote}${url}${quote})`;
|
|
91
|
-
});
|
|
92
|
-
$el.html(newStyleContent); // Set the modified CSS back
|
|
93
|
-
})();
|
|
94
|
-
tasks.push(task);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
$('[style*="url("]').each((_, el) => {
|
|
98
|
-
const $el = $(el);
|
|
99
|
-
const style = $el.attr('style');
|
|
100
|
-
if (style) {
|
|
101
|
-
const task = (async () => {
|
|
102
|
-
const newStyle = await replaceAsync(style, urlRegex, async (_, quote, url) => {
|
|
103
|
-
if (url.startsWith('luzzle://')) {
|
|
104
|
-
const base64 = await luzzleImageUrlToBase64(url, pieces);
|
|
105
|
-
return `url(${quote}${base64}${quote})`;
|
|
106
|
-
}
|
|
107
|
-
return `url(${quote}${url}${quote})`;
|
|
108
|
-
});
|
|
109
|
-
$el.attr('style', newStyle);
|
|
110
|
-
})();
|
|
111
|
-
tasks.push(task);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
await Promise.all(tasks);
|
|
115
|
-
return $.html();
|
|
116
|
-
}
|
|
117
|
-
function bufferToBase64(buffer, type, format) {
|
|
118
|
-
const base64 = buffer.toString('base64');
|
|
119
|
-
return `data:${type}/${format};base64,${base64}`;
|
|
120
|
-
}
|
|
121
|
-
function getHelpers(item, config) {
|
|
122
|
-
return {
|
|
123
|
-
getPieceUrl: function () {
|
|
124
|
-
return `${config.url.app}/pieces/${item.type}/${item.slug}`;
|
|
125
|
-
},
|
|
126
|
-
getPieceImageUrl: function (image,
|
|
127
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
128
|
-
_minWidth,
|
|
129
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
130
|
-
_format) {
|
|
131
|
-
return `luzzle://${image}`;
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
export { getProps, findAndReplaceLuzzleUrls, bufferToBase64, replaceAsync };
|
|
136
|
-
//# sourceMappingURL=utils.js.map
|