@ethercorps/sveltekit-og 0.1.0 → 0.1.2

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
@@ -30,10 +30,8 @@ const template = `
30
30
  </div>
31
31
  </div>
32
32
  `;
33
- const fontFile400 = await fetch('https://og-playground.vercel.app/inter-latin-ext-400-normal.woff');
34
- const fontFile700 = await fetch('https://og-playground.vercel.app/inter-latin-ext-700-normal.woff');
35
- const fontData400: ArrayBuffer = await fontFile400.arrayBuffer();
36
- const fontData700: ArrayBuffer = await fontFile700.arrayBuffer();
33
+ const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-400-normal.woff');
34
+ const fontData: ArrayBuffer = await fontFile.arrayBuffer();
37
35
 
38
36
  export const GET: RequestHandler = async () => {
39
37
  return new ImageResponse(template, {
@@ -42,13 +40,8 @@ export const GET: RequestHandler = async () => {
42
40
  fonts: [
43
41
  {
44
42
  name: 'Inter Latin',
45
- data: fontData400,
43
+ data: fontData,
46
44
  weight: 400
47
- },
48
- {
49
- name: 'Inter Latin',
50
- data: fontData700,
51
- weight: 700
52
45
  }
53
46
  ]
54
47
  });
@@ -56,40 +49,66 @@ export const GET: RequestHandler = async () => {
56
49
 
57
50
  ```
58
51
 
59
- Then run `pnpm dev` and access localhost:5173/og, the React element will be rendered and responded as a PNG from that endpoint:
52
+ Then run `pnpm dev` and access localhost:5173/og, the api/route endpoint be rendered and responded as a PNG from that api/endpoint:
60
53
 
61
54
  ![Rendered OG image](static/demo.png)
62
55
 
63
- Read more about the API, supported features and check out the examples in the following sections.
56
+ Read more about the API, supported features and check out the examples on Satori Playground.
57
+
58
+ ## Examples:
59
+ - `ImageResponse` · [_source_](/src/routes/+server.ts) · [_demo_](https://sveltekit-og-five.vercel.app)
60
+ - `componentToImageResponse` · [_source_](/src/routes/component-og/) · [_demo_](https://sveltekit-og-five.vercel.app/component-og)
61
+
64
62
 
65
63
  ## API Reference
66
64
 
67
- The package exposes an `ImageResponse` constructor, with the following options available:
65
+ The package exposes an `ImageResponse` and `componentToImageResponse` constructors, with the following options available:
68
66
 
69
67
  ```typescript
70
- import { ImageResponse } from '@ethercorps/sveltekit-og'
68
+ import {ImageResponse, componentToImageResponse} from '@ethercorps/sveltekit-og'
69
+ import {SvelteComponent} from "svelte";
71
70
 
72
71
  // ...
73
72
  new ImageResponse(
74
- element: string,
75
- options: {
76
- width?: number = 1200
77
- height?: number = 630
78
- fonts?: {
79
- name: string,
80
- data: ArrayBuffer,
81
- weight: number,
82
- style: 'normal' | 'italic'
73
+ element : string,
74
+ options : {
75
+ width ? : number = 1200
76
+ height ? : number = 630
77
+ fonts ? : {
78
+ name: string,
79
+ data: ArrayBuffer,
80
+ weight: number,
81
+ style: 'normal' | 'italic'
83
82
  }[]
84
- debug?: boolean = false
85
- graphemeImages?: Record<string, string>;
86
- loadAdditionalAsset?: (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
83
+ debug ? : boolean = false
84
+ graphemeImages ? : Record<string, string>;
85
+ loadAdditionalAsset ? : (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
87
86
  // Options that will be passed to the HTTP response
88
- status?: number = 200
89
- statusText?: string
90
- headers?: Record<string, string>
91
- },
92
- )
87
+ status ? : number = 200
88
+ statusText ? : string
89
+ headers ? : Record<string, string>
90
+ })
91
+
92
+ new componentToImageResponse(
93
+ component : typeof SvelteComponent,
94
+ props : {}, // All export let example inside prop dictionary
95
+ options : {
96
+ width ? : number = 1200
97
+ height ? : number = 630
98
+ fonts ? : {
99
+ name: string,
100
+ data: ArrayBuffer,
101
+ weight: number,
102
+ style: 'normal' | 'italic'
103
+ }[]
104
+ debug ? : boolean = false
105
+ graphemeImages ? : Record<string, string>;
106
+ loadAdditionalAsset ? : (languageCode: string, segment: string) => Promise<SatoriOptions["fonts"] | string | undefined>;
107
+ // Options that will be passed to the HTTP response
108
+ status ? : number = 200
109
+ statusText ? : string
110
+ headers ? : Record<string, string>
111
+ })
93
112
  ```
94
113
 
95
114
  When running in production, these headers will be included by `@ethercorps/sveltekit-og`:
@@ -105,13 +124,20 @@ During development, the `cache-control: no-cache, no-store` header is used inste
105
124
 
106
125
  Please refer to [Satori’s documentation](https://github.com/vercel/satori#documentation) for a list of supported HTML and CSS features.
107
126
 
108
- By default, `@ethercorps/sveltekit-og` only has the 'Inter Latin' font included. If you need to use other fonts, you can pass them in the `fonts` option.
127
+ By default, `@ethercorps/sveltekit-og` only has the 'Noto Sans' font included. If you need to use other fonts, you can pass them in the `fonts` option.
109
128
 
110
129
 
111
130
  ## Acknowledgements
112
131
 
113
132
  This project will not be possible without the following projects:
114
133
 
115
- - [Satori](https://github.com/vercel/satori)
116
- - [Google Fonts](https://fonts.google.com) and [Noto Sans](https://www.google.com/get/noto/)
117
- - [Resvg](https://github.com/RazrFalcon/resvg) and [Resvg.js](https://github.com/yisibl/resvg-js)
134
+ - [Satori & @vercel/og](https://github.com/vercel/satori)
135
+ - [Satori-Html](https://github.com/natemoo-re/satori-html)
136
+ - [Noto by Google Fonts](https://fonts.google.com/noto)
137
+ - [Resvg.js](https://github.com/yisibl/resvg-js)
138
+
139
+
140
+ ## Authors
141
+
142
+ - [@theetherGit](https://www.github.com/theetherGit)
143
+ - [@etherCorps](https://www.github.com/etherCorps)
package/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import type { SvelteComponent } from 'svelte';
2
1
  import type { SatoriOptions } from 'satori';
2
+ import type { SvelteComponent } from "svelte";
3
3
  export declare const ImageResponse: {
4
4
  new (htmlTemplate: string, optionsByUser: ImageResponseOptions): {};
5
5
  };
6
6
  export declare const componentToImageResponse: {
7
- new (component: SvelteComponent, props: {} | undefined, optionsByUser: ImageResponseOptions): {};
7
+ new (component: typeof SvelteComponent, props: {} | undefined, optionsByUser: ImageResponseOptions): {};
8
8
  };
9
- export declare type ImageResponseOptions = ConstructorParameters<typeof Response>[1] & ImageOptions;
9
+ declare type ImageResponseOptions = ConstructorParameters<typeof Response>[1] & ImageOptions;
10
10
  declare type ImageOptions = {
11
11
  width?: number;
12
12
  height?: number;
package/index.js CHANGED
@@ -1,12 +1,8 @@
1
1
  import { html as toReactNode } from 'satori-html';
2
2
  import satori from 'satori';
3
3
  import { Resvg, initWasm } from '@resvg/resvg-wasm';
4
- import { join } from 'path';
5
- import { readFileSync } from 'fs';
6
- import { fileURLToPath } from 'url';
7
- const __filename = fileURLToPath(import.meta.url);
8
- const resSvgWasm = initWasm(readFileSync(join(__filename, '../vendors/resvg.wasm')));
9
- const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-700-normal.woff');
4
+ const resSvgWasm = initWasm(fetch('https://sveltekit-og.ethercorps.io/resvg.wasm'));
5
+ const fontFile = await fetch('https://sveltekit-og.ethercorps.io/noto-sans.ttf');
10
6
  const fontData = await fontFile.arrayBuffer();
11
7
  export const ImageResponse = class {
12
8
  constructor(htmlTemplate, optionsByUser) {
@@ -17,16 +13,18 @@ export const ImageResponse = class {
17
13
  const svg = await satori(toReactNode(htmlTemplate), {
18
14
  width: options.width,
19
15
  height: options.height,
16
+ debug: options.debug,
20
17
  fonts: options.fonts || [
21
18
  {
22
- name: 'Noto Sans',
19
+ name: 'sans serif',
23
20
  data: fontData,
24
- style: 'normal'
21
+ style: 'normal',
22
+ weight: 700
25
23
  }
26
24
  ]
27
25
  });
28
26
  const pngData = new Resvg(svg, { fitTo: { mode: 'width', value: options.width } });
29
- a.enqueue(await pngData.render().asPng());
27
+ a.enqueue(pngData.render().asPng());
30
28
  a.close();
31
29
  }
32
30
  });
@@ -45,11 +43,15 @@ export const ImageResponse = class {
45
43
  };
46
44
  export const componentToImageResponse = class {
47
45
  constructor(component, props = {}, optionsByUser) {
48
- const SvelteRenderedMarkup = component.render(props);
49
- let htmlTemplate = `${SvelteRenderedMarkup.html}`;
50
- if (SvelteRenderedMarkup && SvelteRenderedMarkup.css && SvelteRenderedMarkup.css.code) {
51
- htmlTemplate = `${SvelteRenderedMarkup.html}<style>${SvelteRenderedMarkup.css.code}</style>`;
52
- }
46
+ const htmlTemplate = componentToMarkup(component, props);
53
47
  return new ImageResponse(htmlTemplate, optionsByUser);
54
48
  }
55
49
  };
50
+ const componentToMarkup = (component, props = {}) => {
51
+ const SvelteRenderedMarkup = component.render(props);
52
+ let htmlTemplate = `${SvelteRenderedMarkup.html}`;
53
+ if (SvelteRenderedMarkup && SvelteRenderedMarkup.css && SvelteRenderedMarkup.css.code) {
54
+ htmlTemplate = `${SvelteRenderedMarkup.html}<style>${SvelteRenderedMarkup.css.code}</style>`;
55
+ }
56
+ return htmlTemplate;
57
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethercorps/sveltekit-og",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "private": false,
5
5
  "devDependencies": {
6
6
  "@playwright/test": "^1.27.1",
@@ -24,12 +24,26 @@
24
24
  "type": "module",
25
25
  "dependencies": {
26
26
  "@resvg/resvg-wasm": "^2.1.0",
27
- "satori": "^0.0.42",
28
- "satori-html": "^0.2.0"
27
+ "satori": "^0.0.43",
28
+ "satori-html": "^0.2.0",
29
+ "yoga-wasm-web": "0.1.2"
29
30
  },
31
+ "keywords": [
32
+ "open graph image",
33
+ "open graph",
34
+ "og image",
35
+ "og:image",
36
+ "social",
37
+ "card",
38
+ "sveltekit og",
39
+ "sveltekit-og",
40
+ "@ethercorps/sveltekit-og"
41
+ ],
42
+ "license": "MIT",
43
+ "repository": "https://github.com/etherCorps/sveltekit-og",
44
+ "homepage": "https://github.com/etherCorps/sveltekit-og#readme",
30
45
  "exports": {
31
46
  "./package.json": "./package.json",
32
- ".": "./index.js",
33
- "./vendors/resvg.wasm": "./vendors/resvg.wasm"
47
+ ".": "./index.js"
34
48
  }
35
49
  }
Binary file