@ethercorps/sveltekit-og 0.1.8 → 1.1.0

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/README.md CHANGED
@@ -3,6 +3,66 @@
3
3
  About
4
4
  Generate Open Graph Images dynamically from HTML/CSS without a browser in SvelteKit.
5
5
 
6
+ ## v1.0.0 Update (Breaking Changes)
7
+
8
+ Finally, We have added html to react like element like object converter out of the box and with svelte compiler.
9
+ Now you can use `{ toReactElement }` with `"@ethercorps/sveltekit-og"` like:
10
+
11
+ ```typescript
12
+ // +page.server.js
13
+
14
+ import { toReactElement, satori } from '@ethercorps/sveltekit-og';
15
+
16
+ const htmlString = `
17
+ <div tw="bg-gray-50 flex w-full">
18
+ <div tw="flex flex-col md:flex-row w-full py-12 px-4 md:items-center justify-between p-8">
19
+ <h2 tw="flex flex-col text-3xl sm:text-4xl font-bold tracking-tight text-gray-900 text-left">
20
+ <span>Ready to dive in?</span>
21
+ <span tw="text-indigo-600">Start your free trial today.</span>
22
+ </h2>
23
+ <div tw="mt-8 flex md:mt-0">
24
+ <div tw="flex rounded-md shadow">
25
+ <a tw="flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-5 py-3 text-base font-medium text-white">Get started</a>
26
+ </div>
27
+ <div tw="ml-3 flex rounded-md shadow">
28
+ <a tw="flex items-center justify-center rounded-md border border-transparent bg-white px-5 py-3 text-base font-medium text-indigo-600">Learn more</a>
29
+ </div>
30
+ </div>
31
+ </div>
32
+ </div>
33
+ `;
34
+ const newNode = toReactElement(htmlString);
35
+
36
+ /** @type {import('./$types').PageServerLoad} */
37
+ export async function load() {
38
+ const fontFile400 = await fetch(
39
+ 'https://og-playground.vercel.app/inter-latin-ext-400-normal.woff'
40
+ );
41
+ const fontData400 = await fontFile400.arrayBuffer();
42
+ const svg = await satori(newNode, {
43
+ height: 350,
44
+ width: 500,
45
+ fonts: [
46
+ {
47
+ name: 'sans serif',
48
+ data: fontData400,
49
+ style: 'normal',
50
+ weight: 700
51
+ }
52
+ ]
53
+ });
54
+
55
+ return { svg };
56
+ }
57
+ ```
58
+
59
+ - We have changed to function based instead of class based ImageResponse and componentToImageResponse.
60
+ - Removed `@resvg/resvg-wasm` with `@resvg/resvg-js` because of internal errors.
61
+ - Removed `satori-html` because now we have `toReactElement` out of the box with svelte compiler.
62
+ > If you find a problem related to undefined a please check [_vite.config.js_](/vite.config.ts) and add ` define: { _a: 'undefined' } in config.`
63
+
64
+ > If you find any issue and have suggestion for this project please open a ticket and if you want to contribute please create a new discussion.
65
+
6
66
  ## Quick Start
7
67
 
8
68
  Install `@ethercorps/sveltekit-og`, then use it inside a server endpoint route (+server.ts or +server.js):
@@ -34,7 +94,7 @@ const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-4
34
94
  const fontData: ArrayBuffer = await fontFile.arrayBuffer();
35
95
 
36
96
  export const GET: RequestHandler = async () => {
37
- return new ImageResponse(template, {
97
+ return await ImageResponse(template, {
38
98
  height: 250,
39
99
  width: 500,
40
100
  fonts: [
@@ -56,7 +116,7 @@ Read more about the API, supported features and check out the examples on Satori
56
116
 
57
117
  ## Examples:
58
118
 
59
- - `ImageResponse` · [_source_](/src/routes/+server.ts) · [_demo_](https://sveltekit-og-five.vercel.app)
119
+ - `ImageResponse` · [_source_](/src/routes/new/+server.ts) · [_demo_](https://sveltekit-og-five.vercel.app/new)
60
120
  - `componentToImageResponse` · [_source_](/src/routes/component-og/) · [_demo_](https://sveltekit-og-five.vercel.app/component-og)
61
121
 
62
122
  ## API Reference
@@ -68,7 +128,7 @@ import {ImageResponse, componentToImageResponse} from '@ethercorps/sveltekit-og'
68
128
  import {SvelteComponent} from "svelte";
69
129
 
70
130
  // ...
71
- new ImageResponse(
131
+ ImageResponse(
72
132
  element : string,
73
133
  options : {
74
134
  width ? : number = 1200
@@ -88,7 +148,7 @@ new ImageResponse(
88
148
  headers ? : Record<string, string>
89
149
  })
90
150
 
91
- new componentToImageResponse(
151
+ componentToImageResponse(
92
152
  component : typeof SvelteComponent,
93
153
  props : {}, // All export let example inside prop dictionary
94
154
  options : {
@@ -130,7 +190,6 @@ By default, `@ethercorps/sveltekit-og` only has the 'Noto Sans' font included. I
130
190
  This project will not be possible without the following projects:
131
191
 
132
192
  - [Satori & @vercel/og](https://github.com/vercel/satori)
133
- - [Satori-Html](https://github.com/natemoo-re/satori-html)
134
193
  - [Noto by Google Fonts](https://fonts.google.com/noto)
135
194
  - [Resvg.js](https://github.com/yisibl/resvg-js)
136
195
 
package/index.d.ts CHANGED
@@ -1,13 +1,10 @@
1
- import type { SatoriOptions } from 'satori';
1
+ import { type SatoriOptions } from 'satori';
2
2
  import type { SvelteComponent } from 'svelte';
3
- declare class ImageResponse {
4
- constructor(htmlTemplate: string, optionsByUser: ImageResponseOptions);
5
- }
6
- declare class componentToImageResponse {
7
- constructor(component: typeof SvelteComponent, props: {} | undefined, optionsByUser: ImageResponseOptions);
8
- }
9
- declare type ImageResponseOptions = ConstructorParameters<typeof Response>[1] & ImageOptions;
10
- declare type ImageOptions = {
3
+ import toReactElement from './toReactElement';
4
+ declare const ImageResponse: (htmlTemplate: string, optionsByUser: ImageResponseOptions) => Promise<Response>;
5
+ declare const componentToImageResponse: (component: typeof SvelteComponent, props: {} | undefined, optionsByUser: ImageResponseOptions) => Promise<Response>;
6
+ type ImageResponseOptions = ConstructorParameters<typeof Response>[1] & ImageOptions;
7
+ type ImageOptions = {
11
8
  width?: number;
12
9
  height?: number;
13
10
  debug?: boolean;
@@ -15,4 +12,5 @@ declare type ImageOptions = {
15
12
  graphemeImages?: Record<string, string>;
16
13
  loadAdditionalAsset?: (languageCode: string, segment: string) => Promise<SatoriOptions['fonts'] | string | undefined>;
17
14
  };
18
- export { componentToImageResponse, ImageResponse };
15
+ type ImageResponseType = typeof ImageResponse;
16
+ export { componentToImageResponse, ImageResponse, toReactElement, type ImageResponseType };
package/index.js CHANGED
@@ -1,50 +1,45 @@
1
- import { html as toReactNode } from 'satori-html';
2
- import satori from 'satori';
3
- import { Resvg, initWasm } from '@resvg/resvg-wasm';
4
- const resSvgWasm = initWasm(fetch('https://sveltekit-og.ethercorps.io/resvg.wasm'));
1
+ import satori, {} from 'satori';
2
+ import { Resvg } from '@resvg/resvg-js';
3
+ import toReactElement from './toReactElement';
5
4
  const fontFile = await fetch('https://sveltekit-og.ethercorps.io/noto-sans.ttf');
6
5
  const fontData = await fontFile.arrayBuffer();
7
- class ImageResponse {
8
- constructor(htmlTemplate, optionsByUser) {
9
- const options = Object.assign({ width: 1200, height: 630, debug: !1 }, optionsByUser);
10
- const png = new ReadableStream({
11
- async start(a) {
12
- await resSvgWasm;
13
- const svg = await satori(toReactNode(htmlTemplate), {
14
- width: options.width,
15
- height: options.height,
16
- debug: options.debug,
17
- fonts: options.fonts || [
18
- {
19
- name: 'sans serif',
20
- data: fontData,
21
- style: 'normal',
22
- weight: 700
23
- }
24
- ]
25
- });
26
- const pngData = new Resvg(svg, { fitTo: { mode: 'width', value: options.width } });
27
- a.enqueue(pngData.render().asPng());
28
- a.close();
6
+ const ImageResponse = async (htmlTemplate, optionsByUser) => {
7
+ const options = Object.assign({ width: 1200, height: 630, debug: !1 }, optionsByUser);
8
+ const svg = await satori(toReactElement(htmlTemplate), {
9
+ width: options.width,
10
+ height: options.height,
11
+ debug: options.debug,
12
+ fonts: options.fonts || [
13
+ {
14
+ name: 'sans serif',
15
+ data: fontData,
16
+ style: 'normal',
17
+ weight: 700
29
18
  }
30
- });
31
- return new Response(png, {
32
- headers: {
33
- 'Content-Type': 'image/png',
34
- 'cache-control': 'public, immutable, no-transform, max-age=31536000',
35
- ...options.headers
36
- },
37
- status: options.status,
38
- statusText: options.statusText
39
- });
40
- }
41
- }
42
- class componentToImageResponse {
43
- constructor(component, props = {}, optionsByUser) {
44
- const htmlTemplate = componentToMarkup(component, props);
45
- return new ImageResponse(htmlTemplate, optionsByUser);
46
- }
47
- }
19
+ ]
20
+ });
21
+ const reSvgOptions = {
22
+ fitTo: {
23
+ mode: 'width',
24
+ value: options.width
25
+ }
26
+ };
27
+ const reSvgObject = new Resvg(svg, reSvgOptions);
28
+ const pngData = await reSvgObject.render().asPng();
29
+ return new Response(pngData, {
30
+ headers: {
31
+ 'Content-Type': 'image/png',
32
+ 'cache-control': 'public, immutable, no-transform, max-age=31536000',
33
+ ...options.headers
34
+ },
35
+ status: options.status,
36
+ statusText: options.statusText
37
+ });
38
+ };
39
+ const componentToImageResponse = (component, props = {}, optionsByUser) => {
40
+ const htmlTemplate = componentToMarkup(component, props);
41
+ return ImageResponse(htmlTemplate, optionsByUser);
42
+ };
48
43
  const componentToMarkup = (component, props = {}) => {
49
44
  const SvelteRenderedMarkup = component.render(props);
50
45
  let htmlTemplate = `${SvelteRenderedMarkup.html}`;
@@ -53,4 +48,4 @@ const componentToMarkup = (component, props = {}) => {
53
48
  }
54
49
  return htmlTemplate;
55
50
  };
56
- export { componentToImageResponse, ImageResponse };
51
+ export { componentToImageResponse, ImageResponse, toReactElement };
package/package.json CHANGED
@@ -1,31 +1,39 @@
1
1
  {
2
2
  "name": "@ethercorps/sveltekit-og",
3
- "version": "0.1.8",
3
+ "version": "1.1.0",
4
4
  "private": false,
5
5
  "devDependencies": {
6
- "@playwright/test": "^1.27.1",
6
+ "@playwright/test": "^1.28.1",
7
7
  "@sveltejs/adapter-auto": "next",
8
- "@sveltejs/kit": "next",
8
+ "@sveltejs/kit": "1.0.0-next.581",
9
9
  "@sveltejs/package": "next",
10
- "@typescript-eslint/eslint-plugin": "^5.42.1",
11
- "@typescript-eslint/parser": "^5.42.1",
12
- "eslint": "^8.27.0",
10
+ "@typescript-eslint/eslint-plugin": "^5.46.0",
11
+ "@typescript-eslint/parser": "^5.46.0",
12
+ "autoprefixer": "^10.4.13",
13
+ "brace": "^0.11.1",
14
+ "eslint": "^8.29.0",
13
15
  "eslint-config-prettier": "^8.5.0",
14
16
  "eslint-plugin-svelte3": "^4.0.0",
15
- "prettier": "^2.7.1",
16
- "prettier-plugin-svelte": "^2.8.0",
17
- "svelte": "^3.53.1",
18
- "svelte-check": "^2.9.2",
17
+ "monaco-editor": "^0.34.1",
18
+ "postcss": "^8.4.19",
19
+ "prettier": "^2.8.1",
20
+ "prettier-plugin-svelte": "^2.9.0",
21
+ "prismjs": "^1.29.0",
22
+ "svelte": "^3.54.0",
23
+ "svelte-check": "^2.10.2",
19
24
  "svelte-preprocess": "^4.10.7",
25
+ "tailwindcss": "^3.2.4",
20
26
  "tslib": "^2.4.1",
21
- "typescript": "^4.8.4",
22
- "vite": "^3.2.3"
27
+ "typescript": "^4.9.4",
28
+ "vite": "^4.0.0"
23
29
  },
24
30
  "type": "module",
25
31
  "dependencies": {
26
- "@resvg/resvg-wasm": "^2.1.0",
27
- "satori": "^0.0.44",
28
- "satori-html": "^0.3.0"
32
+ "@resvg/resvg-js": "^2.2.0",
33
+ "satori": "^0.0.44"
34
+ },
35
+ "peerDependencies": {
36
+ "svelte": "^3.54.0"
29
37
  },
30
38
  "keywords": [
31
39
  "open graph image",
@@ -43,6 +51,7 @@
43
51
  "homepage": "https://github.com/etherCorps/sveltekit-og#readme",
44
52
  "exports": {
45
53
  "./package.json": "./package.json",
46
- ".": "./index.js"
54
+ ".": "./index.js",
55
+ "./toReactElement": "./toReactElement.js"
47
56
  }
48
57
  }
@@ -0,0 +1,10 @@
1
+ interface VNode {
2
+ type: string;
3
+ props: {
4
+ style?: Record<string, any>;
5
+ children?: string | VNode | VNode[];
6
+ [prop: string]: any;
7
+ };
8
+ }
9
+ declare const toReactElement: (htmlString: string) => VNode;
10
+ export default toReactElement;
@@ -0,0 +1,100 @@
1
+ import { parse, walk } from 'svelte/compiler';
2
+ /* Start of code from satori-html for cssToObject converter*/
3
+ const camelize = (ident) => ident.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
4
+ const cssToObject = (str) => {
5
+ const obj = {};
6
+ let t = 0;
7
+ let pair = ['', ''];
8
+ const flags = {};
9
+ for (const c of str) {
10
+ if (!flags['('] && c === ':') {
11
+ t = 1;
12
+ }
13
+ else if (c === ';') {
14
+ const [decl = '', value = ''] = pair;
15
+ obj[camelize(decl.trim())] = value.trim();
16
+ t = 0;
17
+ pair = ['', ''];
18
+ }
19
+ else {
20
+ pair[t] += c;
21
+ switch (c) {
22
+ case '(': {
23
+ flags[c]++;
24
+ break;
25
+ }
26
+ case ')': {
27
+ flags['(']--;
28
+ break;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ const [decl = '', value = ''] = pair;
34
+ if (decl.trim() && value.trim()) {
35
+ obj[camelize(decl.trim())] = value.trim();
36
+ }
37
+ return obj;
38
+ };
39
+ const nodeMap = new WeakMap();
40
+ const root = {
41
+ type: 'div',
42
+ props: {
43
+ style: {
44
+ display: 'flex',
45
+ flexDirection: 'column',
46
+ width: '100%',
47
+ height: '100%'
48
+ },
49
+ children: []
50
+ }
51
+ };
52
+ /* End of satori-html */
53
+ const toReactElement = (htmlString) => {
54
+ const svelteAST = parse(htmlString);
55
+ walk(svelteAST, {
56
+ enter(node, parent, prop, index) {
57
+ let newNode = {};
58
+ if (node.type === 'Fragment') {
59
+ nodeMap.set(node, root);
60
+ }
61
+ else if (node.type === 'Element') {
62
+ newNode.type = node.name;
63
+ const { ...props } = node.attributes;
64
+ if (node.attributes.length > 0) {
65
+ node.attributes.forEach((attribute) => {
66
+ if (attribute.name === 'style') {
67
+ props['style'] = cssToObject(attribute.value[0].data);
68
+ }
69
+ else
70
+ props[attribute.name] = attribute.value[0].data;
71
+ });
72
+ delete props[0];
73
+ }
74
+ props.children = [];
75
+ Object.assign(newNode, { props });
76
+ nodeMap.set(node, newNode);
77
+ if (parent) {
78
+ const newParent = nodeMap.get(parent);
79
+ newParent.props.children[index] = newNode;
80
+ }
81
+ }
82
+ else if (node.type === 'Text') {
83
+ newNode = node.data.trim();
84
+ if (newNode) {
85
+ if (parent && parent.type !== 'Attribute') {
86
+ const newParent = nodeMap.get(parent);
87
+ if (parent.children.length === 1) {
88
+ newParent.props.children = newNode;
89
+ }
90
+ else {
91
+ newParent.props.children[index] = newNode;
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
97
+ });
98
+ return root;
99
+ };
100
+ export default toReactElement;