@ethercorps/sveltekit-og 0.1.8 → 1.0.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,65 @@
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
+ Finally, We have added html to react like element like object converter out of the box and with svelte compiler.
8
+ Now you can use `{ toReactElement }` with `"@ethercorps/sveltekit-og"` like:
9
+
10
+ ```typescript
11
+ // +page.server.js
12
+
13
+ import { toReactElement, satori } from "@ethercorps/sveltekit-og"
14
+
15
+ const htmlString = `
16
+ <div tw="bg-gray-50 flex w-full">
17
+ <div tw="flex flex-col md:flex-row w-full py-12 px-4 md:items-center justify-between p-8">
18
+ <h2 tw="flex flex-col text-3xl sm:text-4xl font-bold tracking-tight text-gray-900 text-left">
19
+ <span>Ready to dive in?</span>
20
+ <span tw="text-indigo-600">Start your free trial today.</span>
21
+ </h2>
22
+ <div tw="mt-8 flex md:mt-0">
23
+ <div tw="flex rounded-md shadow">
24
+ <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>
25
+ </div>
26
+ <div tw="ml-3 flex rounded-md shadow">
27
+ <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>
28
+ </div>
29
+ </div>
30
+ </div>
31
+ </div>
32
+ `;
33
+ const newNode = toReactElement(htmlString);
34
+
35
+ /** @type {import('./$types').PageServerLoad} */
36
+ export async function load() {
37
+ const fontFile400 = await fetch(
38
+ 'https://og-playground.vercel.app/inter-latin-ext-400-normal.woff'
39
+ );
40
+ const fontData400 = await fontFile400.arrayBuffer();
41
+ const svg = await satori(newNode, {
42
+ height: 350,
43
+ width: 500,
44
+ fonts: [
45
+ {
46
+ name: 'sans serif',
47
+ data: fontData400,
48
+ style: 'normal',
49
+ weight: 700
50
+ }
51
+ ]
52
+ });
53
+
54
+ return { svg };
55
+ }
56
+ ```
57
+
58
+ - We have changed to function based instead of class based ImageResponse and componentToImageResponse.
59
+ - Removed `@resvg/resvg-wasm` with `@resvg/resvg-js` because of internal errors.
60
+ - Removed `satori-html` because now we have `toReactElement` out of the box with svelte compiler.
61
+ - Access to `satori` directly from `"@ethercorps/sveltekit-og"`. [_source_](/src/routes/test/+page.server.js) · [_demo_](https://sveltekit-og-five.vercel.app/test)
62
+
63
+ > 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.
64
+
6
65
  ## Quick Start
7
66
 
8
67
  Install `@ethercorps/sveltekit-og`, then use it inside a server endpoint route (+server.ts or +server.js):
@@ -34,7 +93,7 @@ const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-4
34
93
  const fontData: ArrayBuffer = await fontFile.arrayBuffer();
35
94
 
36
95
  export const GET: RequestHandler = async () => {
37
- return new ImageResponse(template, {
96
+ return await ImageResponse(template, {
38
97
  height: 250,
39
98
  width: 500,
40
99
  fonts: [
@@ -56,7 +115,7 @@ Read more about the API, supported features and check out the examples on Satori
56
115
 
57
116
  ## Examples:
58
117
 
59
- - `ImageResponse` · [_source_](/src/routes/+server.ts) · [_demo_](https://sveltekit-og-five.vercel.app)
118
+ - `ImageResponse` · [_source_](/src/routes/new/+server.ts) · [_demo_](https://sveltekit-og-five.vercel.app/new)
60
119
  - `componentToImageResponse` · [_source_](/src/routes/component-og/) · [_demo_](https://sveltekit-og-five.vercel.app/component-og)
61
120
 
62
121
  ## API Reference
@@ -68,7 +127,7 @@ import {ImageResponse, componentToImageResponse} from '@ethercorps/sveltekit-og'
68
127
  import {SvelteComponent} from "svelte";
69
128
 
70
129
  // ...
71
- new ImageResponse(
130
+ ImageResponse(
72
131
  element : string,
73
132
  options : {
74
133
  width ? : number = 1200
@@ -88,7 +147,7 @@ new ImageResponse(
88
147
  headers ? : Record<string, string>
89
148
  })
90
149
 
91
- new componentToImageResponse(
150
+ componentToImageResponse(
92
151
  component : typeof SvelteComponent,
93
152
  props : {}, // All export let example inside prop dictionary
94
153
  options : {
@@ -130,7 +189,6 @@ By default, `@ethercorps/sveltekit-og` only has the 'Noto Sans' font included. I
130
189
  This project will not be possible without the following projects:
131
190
 
132
191
  - [Satori & @vercel/og](https://github.com/vercel/satori)
133
- - [Satori-Html](https://github.com/natemoo-re/satori-html)
134
192
  - [Noto by Google Fonts](https://fonts.google.com/noto)
135
193
  - [Resvg.js](https://github.com/yisibl/resvg-js)
136
194
 
package/index.d.ts CHANGED
@@ -1,13 +1,10 @@
1
- import type { SatoriOptions } from 'satori';
1
+ import satori, { 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, satori, 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, satori };
package/package.json CHANGED
@@ -1,31 +1,33 @@
1
1
  {
2
2
  "name": "@ethercorps/sveltekit-og",
3
- "version": "0.1.8",
3
+ "version": "1.0.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
8
  "@sveltejs/kit": "next",
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
+ "eslint": "^8.29.0",
13
13
  "eslint-config-prettier": "^8.5.0",
14
14
  "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",
15
+ "prettier": "^2.8.1",
16
+ "prettier-plugin-svelte": "^2.8.1",
17
+ "svelte": "^3.54.0",
18
+ "svelte-check": "^2.10.2",
19
19
  "svelte-preprocess": "^4.10.7",
20
20
  "tslib": "^2.4.1",
21
- "typescript": "^4.8.4",
22
- "vite": "^3.2.3"
21
+ "typescript": "^4.9.4",
22
+ "vite": "^3.2.5"
23
23
  },
24
24
  "type": "module",
25
25
  "dependencies": {
26
- "@resvg/resvg-wasm": "^2.1.0",
27
- "satori": "^0.0.44",
28
- "satori-html": "^0.3.0"
26
+ "@resvg/resvg-js": "^2.2.0",
27
+ "satori": "^0.0.44"
28
+ },
29
+ "peerDependencies": {
30
+ "svelte": "^3.54.0"
29
31
  },
30
32
  "keywords": [
31
33
  "open graph image",
@@ -43,6 +45,7 @@
43
45
  "homepage": "https://github.com/etherCorps/sveltekit-og#readme",
44
46
  "exports": {
45
47
  "./package.json": "./package.json",
46
- ".": "./index.js"
48
+ ".": "./index.js",
49
+ "./toReactElement": "./toReactElement.js"
47
50
  }
48
51
  }
@@ -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;