nuxt-og-image 3.0.0-beta.5 → 3.0.0-beta.51

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.
Files changed (138) hide show
  1. package/README.md +4 -4
  2. package/dist/client/200.html +5 -5
  3. package/dist/client/404.html +5 -5
  4. package/dist/client/_nuxt/IconCSS.4bbe2613.css +1 -0
  5. package/dist/client/_nuxt/IconCSS.71f1059a.js +1 -0
  6. package/dist/client/_nuxt/builds/latest.json +1 -1
  7. package/dist/client/_nuxt/builds/meta/6b86c847-1bce-41b5-9ab8-acabeed13f1f.json +1 -0
  8. package/dist/client/_nuxt/entry.33a59bbf.css +1 -0
  9. package/dist/client/_nuxt/entry.99da9ce9.js +138 -0
  10. package/dist/client/_nuxt/{error-404.b07f263f.js → error-404.eed1a665.js} +1 -1
  11. package/dist/client/_nuxt/{error-500.ed41d8f2.js → error-500.05b9efb3.js} +1 -1
  12. package/dist/client/index.html +5 -5
  13. package/dist/module.d.mts +55 -46
  14. package/dist/module.d.ts +55 -46
  15. package/dist/module.json +2 -2
  16. package/dist/module.mjs +306 -162
  17. package/dist/runtime/cache.d.ts +7 -10
  18. package/dist/runtime/cache.mjs +40 -27
  19. package/dist/runtime/components/OgImage/OgImage.d.ts +5 -0
  20. package/dist/runtime/components/OgImage/{index.mjs → OgImage.mjs} +1 -1
  21. package/dist/runtime/components/OgImage/OgImageScreenshot.d.ts +5 -0
  22. package/dist/runtime/components/OgImage/{Screenshot.mjs → OgImageScreenshot.mjs} +1 -1
  23. package/dist/runtime/components/Templates/{Official → Community}/BrandedLogo.vue +3 -2
  24. package/dist/runtime/components/Templates/Community/Nuxt.vue +6 -5
  25. package/dist/runtime/components/Templates/Community/NuxtSeo.vue +137 -0
  26. package/dist/runtime/components/Templates/Community/Pergel.vue +104 -0
  27. package/dist/runtime/components/Templates/{Official → Community}/SimpleBlog.vue +7 -5
  28. package/dist/runtime/components/Templates/Community/UnJs.vue +108 -0
  29. package/dist/runtime/components/Templates/{Official → Community}/Wave.vue +3 -2
  30. package/dist/runtime/components/Templates/{Official → Community}/WithEmoji.vue +3 -2
  31. package/dist/runtime/composables/defineOgImage.d.ts +2 -23
  32. package/dist/runtime/composables/defineOgImage.mjs +33 -117
  33. package/dist/runtime/composables/defineOgImageComponent.d.ts +3 -0
  34. package/dist/runtime/composables/defineOgImageComponent.mjs +8 -0
  35. package/dist/runtime/composables/defineOgImageScreenshot.d.ts +2 -0
  36. package/dist/runtime/composables/defineOgImageScreenshot.mjs +13 -0
  37. package/dist/runtime/core/bindings/css-inline/node.d.ts +2 -5
  38. package/dist/runtime/core/bindings/css-inline/node.mjs +2 -10
  39. package/dist/runtime/core/bindings/resvg/wasm-fs.d.ts +40 -0
  40. package/dist/runtime/core/bindings/resvg/wasm-fs.mjs +6 -0
  41. package/dist/runtime/core/bindings/resvg/wasm.mjs +2 -2
  42. package/dist/runtime/core/bindings/satori/wasm-fs.mjs +13 -0
  43. package/dist/runtime/core/bindings/satori/wasm.d.ts +6 -0
  44. package/dist/runtime/core/bindings/satori/wasm.mjs +14 -0
  45. package/dist/runtime/core/cache/emojis.d.ts +1 -0
  46. package/dist/runtime/core/cache/emojis.mjs +5 -0
  47. package/dist/runtime/core/cache/htmlPayload.d.ts +5 -0
  48. package/dist/runtime/core/cache/htmlPayload.mjs +6 -0
  49. package/dist/runtime/core/cache/prerender.d.ts +1 -5
  50. package/dist/runtime/core/cache/prerender.mjs +1 -2
  51. package/dist/runtime/core/env/assets.d.ts +0 -1
  52. package/dist/runtime/core/env/assets.mjs +0 -4
  53. package/dist/runtime/core/font/fetch.d.ts +2 -3
  54. package/dist/runtime/core/font/fetch.mjs +32 -15
  55. package/dist/runtime/core/html/applyEmojis.d.ts +3 -0
  56. package/dist/runtime/core/html/applyEmojis.mjs +37 -0
  57. package/dist/runtime/core/html/applyInlineCss.d.ts +3 -0
  58. package/dist/runtime/core/html/applyInlineCss.mjs +32 -0
  59. package/dist/runtime/core/html/devIframeTemplate.d.ts +2 -0
  60. package/dist/runtime/core/html/{fetch.mjs → devIframeTemplate.mjs} +33 -42
  61. package/dist/runtime/core/html/fetchIsland.d.ts +3 -0
  62. package/dist/runtime/core/html/fetchIsland.mjs +17 -0
  63. package/dist/runtime/core/options/fetch.d.ts +1 -1
  64. package/dist/runtime/core/options/fetch.mjs +10 -5
  65. package/dist/runtime/core/renderers/chromium/index.mjs +9 -13
  66. package/dist/runtime/core/renderers/chromium/screenshot.d.ts +2 -3
  67. package/dist/runtime/core/renderers/chromium/screenshot.mjs +18 -13
  68. package/dist/runtime/core/renderers/satori/fonts.d.ts +2 -2
  69. package/dist/runtime/core/renderers/satori/fonts.mjs +2 -2
  70. package/dist/runtime/core/renderers/satori/index.d.ts +2 -3
  71. package/dist/runtime/core/renderers/satori/index.mjs +26 -22
  72. package/dist/runtime/core/renderers/satori/instances.d.ts +3 -0
  73. package/dist/runtime/core/renderers/satori/instances.mjs +15 -0
  74. package/dist/runtime/core/renderers/satori/plugins/emojis.mjs +15 -13
  75. package/dist/runtime/core/renderers/satori/plugins/imageSrc.mjs +23 -10
  76. package/dist/runtime/core/renderers/satori/plugins/unocss.d.ts +2 -0
  77. package/dist/runtime/core/renderers/satori/plugins/unocss.mjs +45 -0
  78. package/dist/runtime/core/renderers/satori/utils.d.ts +2 -3
  79. package/dist/runtime/core/renderers/satori/vnodes.d.ts +2 -3
  80. package/dist/runtime/core/renderers/satori/vnodes.mjs +16 -6
  81. package/dist/runtime/core/utils/resolveRendererContext.d.ts +2 -6
  82. package/dist/runtime/core/utils/resolveRendererContext.mjs +47 -29
  83. package/dist/runtime/core/utils/wasm.d.ts +3 -0
  84. package/dist/runtime/core/utils/wasm.mjs +16 -0
  85. package/dist/runtime/nitro/plugins/nuxt-content.mjs +3 -4
  86. package/dist/runtime/nitro/plugins/prerender.d.ts +1 -1
  87. package/dist/runtime/nitro/plugins/prerender.mjs +20 -18
  88. package/dist/runtime/nitro/utils.d.ts +2 -0
  89. package/dist/runtime/nitro/utils.mjs +17 -0
  90. package/dist/runtime/nuxt/plugins/og-image-canonical-urls.server.mjs +31 -0
  91. package/dist/runtime/nuxt/plugins/route-rule-og-image.server.mjs +16 -51
  92. package/dist/runtime/nuxt/utils.d.ts +3 -0
  93. package/dist/runtime/nuxt/utils.mjs +69 -0
  94. package/dist/runtime/server/routes/__og-image__/debug.json.d.ts +2 -3
  95. package/dist/runtime/server/routes/__og-image__/debug.json.mjs +5 -7
  96. package/dist/runtime/server/routes/__og-image__/image.mjs +88 -0
  97. package/dist/runtime/types.d.ts +89 -25
  98. package/dist/runtime/utils.d.ts +4 -0
  99. package/dist/runtime/utils.mjs +11 -0
  100. package/dist/runtime/utils.pure.d.ts +5 -0
  101. package/dist/runtime/utils.pure.mjs +43 -0
  102. package/package.json +30 -31
  103. package/virtual.d.ts +49 -0
  104. package/dist/client/_nuxt/IconCSS.8f429b14.css +0 -1
  105. package/dist/client/_nuxt/IconCSS.b16882c0.js +0 -1
  106. package/dist/client/_nuxt/builds/meta/df74ddc4-3ccb-4ada-8e61-014943e6befc.json +0 -1
  107. package/dist/client/_nuxt/entry.434c2c45.css +0 -1
  108. package/dist/client/_nuxt/entry.d4edd2a5.js +0 -137
  109. package/dist/client/grid.png +0 -0
  110. package/dist/runtime/components/OgImage/Cached.d.ts +0 -5
  111. package/dist/runtime/components/OgImage/Cached.mjs +0 -10
  112. package/dist/runtime/components/OgImage/Dynamic.d.ts +0 -8
  113. package/dist/runtime/components/OgImage/Dynamic.mjs +0 -10
  114. package/dist/runtime/components/OgImage/Screenshot.d.ts +0 -6
  115. package/dist/runtime/components/OgImage/Static.d.ts +0 -8
  116. package/dist/runtime/components/OgImage/Static.mjs +0 -10
  117. package/dist/runtime/components/OgImage/WithoutCache.d.ts +0 -5
  118. package/dist/runtime/components/OgImage/WithoutCache.mjs +0 -10
  119. package/dist/runtime/components/OgImage/index.d.ts +0 -5
  120. package/dist/runtime/components/Templates/Official/Fallback.vue +0 -147
  121. package/dist/runtime/core/bindings/css-inline/mock.d.ts +0 -5
  122. package/dist/runtime/core/bindings/css-inline/mock.mjs +0 -3
  123. package/dist/runtime/core/bindings/satori/yoga-wasm.mjs +0 -7
  124. package/dist/runtime/core/bindings/sharp/wasm.d.ts +0 -2
  125. package/dist/runtime/core/bindings/sharp/wasm.mjs +0 -2
  126. package/dist/runtime/core/html/fetch.d.ts +0 -3
  127. package/dist/runtime/core/options/normalise.d.ts +0 -2
  128. package/dist/runtime/core/options/normalise.mjs +0 -26
  129. package/dist/runtime/nuxt/plugins/nuxt-content-canonical-urls.mjs +0 -29
  130. package/dist/runtime/public-assets/__nuxt_og_image__/browser-provider-not-supported.png +0 -0
  131. package/dist/runtime/server/routes/__og-image__/image-[path]-og.[extension].mjs +0 -45
  132. package/dist/runtime/utilts.d.ts +0 -2
  133. package/dist/runtime/utilts.mjs +0 -8
  134. /package/dist/runtime/core/bindings/satori/{yoga-wasm.d.ts → wasm-fs.d.ts} +0 -0
  135. /package/dist/runtime/nuxt/plugins/{nuxt-content-canonical-urls.d.ts → og-image-canonical-urls.server.d.ts} +0 -0
  136. /package/dist/runtime/{public-assets-optional/inter-font → server/assets}/inter-latin-ext-400-normal.woff +0 -0
  137. /package/dist/runtime/{public-assets-optional/inter-font → server/assets}/inter-latin-ext-700-normal.woff +0 -0
  138. /package/dist/runtime/server/routes/__og-image__/{image-[path]-og.[extension].d.ts → image.d.ts} +0 -0
@@ -1,37 +1,21 @@
1
- import { createError } from "h3";
2
- import { hash } from "ohash";
3
1
  import { createHeadCore } from "@unhead/vue";
4
- import twemoji from "twemoji";
5
2
  import { renderSSRHead } from "@unhead/ssr";
6
- import { useRuntimeConfig } from "#imports";
7
- export async function fetchHTML(e, options) {
8
- const { fonts, satoriOptions } = useRuntimeConfig()["nuxt-og-image"];
9
- if (!options.component) {
10
- throw createError({
11
- statusCode: 500,
12
- statusMessage: `Nuxt OG Image trying to render an invalid component. Received options ${JSON.stringify(options)}`
13
- });
14
- }
15
- const hashId = hash([options.component, options, Math.random() * 100]);
16
- const island = await e.$fetch(`/__nuxt_island/${options.component}_${hashId}.json`, {
17
- params: {
18
- props: JSON.stringify(options)
19
- }
20
- });
3
+ import { useOgImageRuntimeConfig } from "../../utils.mjs";
4
+ import { applyEmojis } from "./applyEmojis.mjs";
5
+ import { fetchIsland } from "./fetchIsland.mjs";
6
+ import { theme } from "#nuxt-og-image/unocss-config.mjs";
7
+ export async function devIframeTemplate(ctx) {
8
+ const { options } = ctx;
9
+ const { fonts } = useOgImageRuntimeConfig();
10
+ const island = await fetchIsland(ctx);
21
11
  const head = createHeadCore();
22
12
  head.push(island.head);
23
13
  let defaultFontFamily = "sans-serif";
24
14
  const firstFont = fonts[0];
25
15
  if (firstFont)
26
16
  defaultFontFamily = firstFont.name;
17
+ await applyEmojis(ctx, island);
27
18
  let html = island.html;
28
- try {
29
- html = twemoji.parse(html, {
30
- folder: "svg",
31
- ext: ".svg"
32
- });
33
- } catch (e2) {
34
- }
35
19
  const googleFonts = {};
36
20
  fonts.filter((font) => !font.path).forEach((font) => {
37
21
  if (!googleFonts[font.name])
@@ -46,21 +30,23 @@ export async function fetchHTML(e, options) {
46
30
  },
47
31
  {
48
32
  innerHTML: `body {
49
- transform: scale(${options.scale || 1});
33
+ transform: scale(${options.props.scale || 1});
50
34
  transform-origin: top left;
51
35
  max-height: 100vh;
52
36
  position: relative;
53
37
  width: ${options.width}px;
54
38
  height: ${options.height}px;
55
39
  overflow: hidden;
56
- background-color: ${options.mode === "dark" ? "#1b1b1b" : "#fff"};
40
+ background-color: ${options.props.colorMode === "dark" ? "#1b1b1b" : "#fff"};
41
+ }
42
+ div {
43
+ display: flex;
44
+ flex-direction: column;
45
+ }
46
+ svg[data-emoji] {
47
+ display: inline-block;
57
48
  }
58
- img.emoji {
59
- height: 1em;
60
- width: 1em;
61
- margin: 0 .05em 0 .1em;
62
- vertical-align: -0.1em;
63
- }`
49
+ `
64
50
  },
65
51
  ...fonts.filter((font) => font.path).map((font) => {
66
52
  return `
@@ -80,15 +66,20 @@ img.emoji {
80
66
  ],
81
67
  script: [
82
68
  {
83
- src: "https://cdn.tailwindcss.com"
69
+ src: "https://cdn.jsdelivr.net/npm/@unocss/runtime/preset-wind.global.js"
70
+ },
71
+ {
72
+ innerHTML: `
73
+ window.__unocss = {
74
+ theme: ${JSON.stringify(theme)},
75
+ presets: [
76
+ () => window.__unocss_runtime.presets.presetWind(),
77
+ ],
78
+ }
79
+ `
84
80
  },
85
81
  {
86
- innerHTML: `tailwind.config = {
87
- corePlugins: {
88
- preflight: false,
89
- },
90
- theme: ${JSON.stringify(satoriOptions?.tailwindConfig?.theme || {})}
91
- }`
82
+ src: "https://cdn.jsdelivr.net/npm/@unocss/runtime/core.global.js"
92
83
  }
93
84
  ],
94
85
  link: [
@@ -98,6 +89,7 @@ img.emoji {
98
89
  rel: "stylesheet"
99
90
  },
100
91
  // have to add each weight as their own stylesheet
92
+ // we should use the local font file no?
101
93
  ...Object.entries(googleFonts).map(([name, fonts2]) => {
102
94
  return {
103
95
  href: `https://fonts.googleapis.com/css2?family=${name}:wght@${fonts2.map((f) => f.weight).join(";")}&display=swap`,
@@ -108,10 +100,9 @@ img.emoji {
108
100
  });
109
101
  html = html.replaceAll(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
110
102
  const headChunk = await renderSSRHead(head);
111
- const htmlTemplate = `<!DOCTYPE html>
103
+ return `<!DOCTYPE html>
112
104
  <html ${headChunk.htmlAttrs}>
113
105
  <head>${headChunk.headTags}</head>
114
106
  <body ${headChunk.bodyAttrs}>${headChunk.bodyTagsOpen}<div data-v-inspector-ignore="true" style="position: relative; display: flex; margin: 0 auto; width: ${options.width}px; height: ${options.height}px; overflow: hidden;">${html}</div>${headChunk.bodyTags}</body>
115
107
  </html>`;
116
- return htmlTemplate;
117
108
  }
@@ -0,0 +1,3 @@
1
+ import type { NuxtIslandResponse } from 'nuxt/dist/core/runtime/nitro/renderer';
2
+ import type { OgImageRenderEventContext } from '../../types';
3
+ export declare function fetchIsland({ options, e }: OgImageRenderEventContext): Promise<NuxtIslandResponse>;
@@ -0,0 +1,17 @@
1
+ import { createError } from "h3";
2
+ import { hash } from "ohash";
3
+ export function fetchIsland({ options, e }) {
4
+ if (!options.component) {
5
+ throw createError({
6
+ statusCode: 500,
7
+ statusMessage: `Nuxt OG Image trying to render an invalid component. Received options ${JSON.stringify(options)}`
8
+ });
9
+ }
10
+ const hashId = hash([options.component, options]);
11
+ const props = typeof options.props !== "undefined" ? options.props : options;
12
+ return e.$fetch(`/__nuxt_island/${options.component}_${hashId}.json`, {
13
+ params: {
14
+ props: JSON.stringify(props)
15
+ }
16
+ });
17
+ }
@@ -1,3 +1,3 @@
1
1
  import { type H3Error, type H3Event } from 'h3';
2
2
  import type { OgImageOptions } from '../../types';
3
- export declare function fetchPathHtmlAndExtractOptions(e: H3Event, path: string): Promise<H3Error | OgImageOptions>;
3
+ export declare function fetchPathHtmlAndExtractOptions(e: H3Event, path: string, key: string): Promise<H3Error | OgImageOptions>;
@@ -1,6 +1,10 @@
1
1
  import { createError } from "h3";
2
+ import { htmlPayloadCache } from "../cache/htmlPayload.mjs";
2
3
  import { extractAndNormaliseOgImageOptions } from "./extract.mjs";
3
- export async function fetchPathHtmlAndExtractOptions(e, path) {
4
+ export async function fetchPathHtmlAndExtractOptions(e, path, key) {
5
+ const cachedHtmlPayload = await htmlPayloadCache.getItem(key);
6
+ if (cachedHtmlPayload && cachedHtmlPayload.expiresAt < Date.now())
7
+ return cachedHtmlPayload.value;
4
8
  let html;
5
9
  try {
6
10
  html = await e.$fetch(path);
@@ -11,10 +15,11 @@ export async function fetchPathHtmlAndExtractOptions(e, path) {
11
15
  });
12
16
  }
13
17
  const payload = extractAndNormaliseOgImageOptions(html);
14
- if (!payload) {
15
- return createError({
16
- statusCode: 400,
17
- statusMessage: `The path is missing the Nuxt OG Image payload. Did you forget to use defineOgImage()?`
18
+ if (payload) {
19
+ await htmlPayloadCache.setItem(key, {
20
+ // 60 minutes for prerender, 10 seconds for runtime
21
+ expiresAt: Date.now() + 1e3 * (import.meta.prerender ? 60 * 60 : 10),
22
+ value: payload
18
23
  });
19
24
  }
20
25
  return payload;
@@ -1,24 +1,20 @@
1
- import { getOgImagePath } from "../../../utilts.mjs";
2
- import { prerenderChromiumContext } from "../../cache/prerender.mjs";
3
1
  import { createScreenshot } from "./screenshot.mjs";
4
2
  import { createBrowser } from "#nuxt-og-image/bindings/chromium";
5
3
  const ChromiumRenderer = {
6
4
  name: "chromium",
7
- supportedFormats: ["png", "jpeg"],
8
- async createImage(e, options) {
9
- const browser = (import.meta.prerender ? prerenderChromiumContext.browser : null) || await createBrowser();
10
- if (!browser) {
5
+ supportedFormats: ["png", "jpeg", "jpg"],
6
+ async debug() {
7
+ return {};
8
+ },
9
+ async createImage(ctx) {
10
+ const browser = await createBrowser();
11
+ if (!browser.isConnected()) {
11
12
  return createError({
12
13
  statusCode: 400,
13
- statusMessage: "Failed to create Local Chromium Browser."
14
+ statusMessage: "Failed to create connect to Chromium Browser."
14
15
  });
15
16
  }
16
- if (import.meta.prerender)
17
- prerenderChromiumContext.browser = browser;
18
- return createScreenshot(e, browser, {
19
- ...options,
20
- path: options.component === "PageScreenshot" ? options.path : getOgImagePath(options.path, "html")
21
- }).finally(async () => {
17
+ return await createScreenshot(ctx, browser).finally(async () => {
22
18
  await browser.close();
23
19
  });
24
20
  }
@@ -1,6 +1,5 @@
1
1
  /// <reference types="node" />
2
2
  import type { Buffer } from 'node:buffer';
3
3
  import type { Browser } from 'playwright-core';
4
- import type { H3Event } from 'h3';
5
- import type { RendererOptions } from '../../../types';
6
- export declare function createScreenshot(e: H3Event, browser: Browser, options: RendererOptions): Promise<Buffer>;
4
+ import type { OgImageRenderEventContext } from '../../../types';
5
+ export declare function createScreenshot({ basePath, e, options, extension }: OgImageRenderEventContext, browser: Browser): Promise<Buffer>;
@@ -1,21 +1,23 @@
1
- import { withQuery } from "ufo";
1
+ import { joinURL, withQuery } from "ufo";
2
+ import { useOgImageRuntimeConfig } from "../../../utils.mjs";
2
3
  import { useNitroOrigin } from "#imports";
3
- export async function createScreenshot(e, browser, options) {
4
+ export async function createScreenshot({ basePath, e, options, extension }, browser) {
5
+ const { colorPreference } = useOgImageRuntimeConfig();
6
+ const path = options.component === "PageScreenshot" ? basePath : joinURL("/__og-image__/image", basePath, `og.html`);
4
7
  const page = await browser.newPage({
5
- colorScheme: options.colorScheme,
8
+ colorScheme: colorPreference || "no-preference",
6
9
  baseURL: useNitroOrigin(e)
7
10
  });
8
11
  if (import.meta.prerender && !options.html) {
9
- options.html = await e.$fetch(options.path);
12
+ options.html = await e.$fetch(path);
10
13
  }
11
14
  try {
12
15
  await page.setViewportSize({
13
16
  width: options.width || 1200,
14
17
  height: options.height || 630
15
18
  });
16
- const isHtml = options.html || options.path?.startsWith("html:");
17
- if (isHtml) {
18
- const html = options.html || options.path?.substring(5);
19
+ if (options.html) {
20
+ const html = options.html;
19
21
  await page.evaluate((html2) => {
20
22
  document.open("text/html");
21
23
  document.write(html2);
@@ -23,7 +25,7 @@ export async function createScreenshot(e, browser, options) {
23
25
  }, html);
24
26
  await page.waitForLoadState("networkidle");
25
27
  } else {
26
- await page.goto(withQuery(options.path, options), {
28
+ await page.goto(withQuery(path, options.props), {
27
29
  timeout: 1e4,
28
30
  waitUntil: "networkidle"
29
31
  });
@@ -31,16 +33,19 @@ export async function createScreenshot(e, browser, options) {
31
33
  const screenshotOptions = {
32
34
  timeout: 1e4,
33
35
  animations: "disabled",
34
- type: options.extension
36
+ type: extension === "png" ? "png" : "jpeg"
35
37
  };
36
- if (options.mask) {
38
+ const _options = options.screenshot || {};
39
+ if (_options.delay)
40
+ await page.waitForTimeout(_options.delay);
41
+ if (_options.mask) {
37
42
  await page.evaluate((mask) => {
38
43
  for (const el of document.querySelectorAll(mask))
39
44
  el.style.display = "none";
40
- }, options.mask);
45
+ }, _options.mask);
41
46
  }
42
- if (options.selector)
43
- return await page.locator(options.selector).screenshot(screenshotOptions);
47
+ if (_options.selector)
48
+ return await page.locator(_options.selector).screenshot(screenshotOptions);
44
49
  return await page.screenshot(screenshotOptions);
45
50
  } finally {
46
51
  await page.close();
@@ -1,3 +1,3 @@
1
- import type { FontConfig } from '../../../types';
1
+ import type { FontConfig, OgImageRenderEventContext } from '../../../types';
2
2
  export declare const satoriFonts: any[];
3
- export declare function loadFonts(baseURL: string, fonts: FontConfig[]): Promise<any>;
3
+ export declare function loadFonts(e: OgImageRenderEventContext, fonts: FontConfig[]): Promise<any>;
@@ -1,8 +1,8 @@
1
1
  import { loadFont } from "../../font/fetch.mjs";
2
2
  export const satoriFonts = [];
3
3
  let fontLoadPromise = null;
4
- export function loadFonts(baseURL, fonts) {
4
+ export function loadFonts(e, fonts) {
5
5
  if (fontLoadPromise)
6
6
  return fontLoadPromise;
7
- return fontLoadPromise = Promise.all(fonts.map((font) => loadFont(baseURL, font)));
7
+ return fontLoadPromise = Promise.all(fonts.map((font) => loadFont(e, font)));
8
8
  }
@@ -1,5 +1,4 @@
1
- import type { H3Event } from 'h3';
2
- import type { Renderer, RendererOptions } from '../../../types';
3
- export declare function createSvg(e: H3Event, options: RendererOptions): Promise<string>;
1
+ import type { OgImageRenderEventContext, Renderer } from '../../../types';
2
+ export declare function createSvg(event: OgImageRenderEventContext): Promise<string>;
4
3
  declare const SatoriRenderer: Renderer;
5
4
  export default SatoriRenderer;
@@ -1,13 +1,15 @@
1
1
  import { defu } from "defu";
2
+ import { useOgImageRuntimeConfig } from "../../../utils.mjs";
2
3
  import { createVNodes } from "./vnodes.mjs";
3
4
  import { loadFonts, satoriFonts } from "./fonts.mjs";
4
5
  import { useResvg, useSatori, useSharp } from "./instances.mjs";
5
- import { useRuntimeConfig } from "#imports";
6
- export async function createSvg(e, options) {
7
- const { fonts, satoriOptions } = useRuntimeConfig()["nuxt-og-image"];
8
- const vnodes = await createVNodes(e, options);
6
+ export async function createSvg(event) {
7
+ const { options } = event;
8
+ const { fonts, satoriOptions } = useOgImageRuntimeConfig();
9
+ const vnodes = await createVNodes(event);
10
+ await event._nitro.hooks.callHook("nuxt-og-image:satori:vnodes", vnodes);
9
11
  if (!satoriFonts.length)
10
- satoriFonts.push(...await loadFonts(e, fonts));
12
+ satoriFonts.push(...await loadFonts(event, fonts));
11
13
  const satori = await useSatori();
12
14
  return satori(vnodes, defu(options.satori, satoriOptions, {
13
15
  fonts: satoriFonts,
@@ -16,38 +18,40 @@ export async function createSvg(e, options) {
16
18
  height: options.height
17
19
  }));
18
20
  }
19
- async function createPng(e, options) {
20
- const { resvgOptions } = useRuntimeConfig()["nuxt-og-image"];
21
- const svg = await createSvg(e, options);
21
+ async function createPng(event) {
22
+ const { resvgOptions } = useOgImageRuntimeConfig();
23
+ const svg = await createSvg(event);
22
24
  const Resvg = await useResvg();
23
25
  const resvg = new Resvg(svg, defu(
24
- options.resvg,
26
+ event.options.resvg,
25
27
  resvgOptions
26
28
  ));
27
29
  const pngData = resvg.render();
28
30
  return pngData.asPng();
29
31
  }
30
- async function createJpeg(e, options) {
31
- const { sharpOptions } = useRuntimeConfig()["nuxt-og-image"];
32
- const png = await createPng(e, options);
32
+ async function createJpeg(event) {
33
+ const { sharpOptions } = useOgImageRuntimeConfig();
34
+ const png = await createPng(event);
33
35
  const sharp = await useSharp();
34
- return sharp(png, defu(options.sharp, sharpOptions)).jpeg(defu(options.sharp, sharpOptions)).toBuffer();
36
+ return sharp(png, defu(event.options.sharp, sharpOptions)).jpeg().toBuffer();
35
37
  }
36
38
  const SatoriRenderer = {
37
39
  name: "satori",
38
- supportedFormats: ["svg", "png", "jpeg", "jpg", "json"],
39
- async createImage(e, options) {
40
- switch (options.extension) {
41
- case "json":
42
- return createVNodes(e, options);
43
- case "svg":
44
- return createSvg(e, options);
40
+ supportedFormats: ["png", "jpeg", "jpg", "json"],
41
+ async createImage(e) {
42
+ switch (e.extension) {
45
43
  case "png":
46
- return createPng(e, options);
44
+ return createPng(e);
47
45
  case "jpeg":
48
46
  case "jpg":
49
- return createJpeg(e, options);
47
+ return createJpeg(e);
50
48
  }
49
+ },
50
+ async debug(e) {
51
+ return {
52
+ vnodes: await createVNodes(e),
53
+ svg: await createSvg(e)
54
+ };
51
55
  }
52
56
  };
53
57
  export default SatoriRenderer;
@@ -1,4 +1,6 @@
1
1
  import type _satori from 'satori';
2
+ export declare function useSatoriRenderer(): Promise<import("../../../types").Renderer>;
3
+ export declare function useChromiumRenderer(): Promise<import("../../../types").Renderer>;
2
4
  export declare function useResvg(): Promise<new (svg: string | Uint8Array, options?: import("@resvg/resvg-wasm").ResvgRenderOptions | undefined) => {
3
5
  free(): void;
4
6
  render(): {
@@ -37,3 +39,4 @@ export declare function useResvg(): Promise<new (svg: string | Uint8Array, optio
37
39
  }>;
38
40
  export declare function useSatori(): Promise<typeof _satori>;
39
41
  export declare function useSharp(): Promise<any>;
42
+ export declare function useCssInline(): Promise<any>;
@@ -1,6 +1,17 @@
1
+ const cssInlineInstance = { instance: void 0 };
1
2
  const sharpInstance = { instance: void 0 };
2
3
  const resvgInstance = { instance: void 0 };
3
4
  const satoriInstance = { instance: void 0 };
5
+ const satoriRendererInstance = { instance: void 0 };
6
+ const chromiumRendererInstance = { instance: void 0 };
7
+ export async function useSatoriRenderer() {
8
+ satoriRendererInstance.instance = satoriRendererInstance.instance || await import("#nuxt-og-image/renderers/satori").then((m) => m.default);
9
+ return satoriRendererInstance.instance;
10
+ }
11
+ export async function useChromiumRenderer() {
12
+ chromiumRendererInstance.instance = chromiumRendererInstance.instance || await import("#nuxt-og-image/renderers/chromium").then((m) => m.default);
13
+ return chromiumRendererInstance.instance;
14
+ }
4
15
  export async function useResvg() {
5
16
  resvgInstance.instance = resvgInstance.instance || await import("#nuxt-og-image/bindings/resvg").then((m) => m.default);
6
17
  await resvgInstance.instance.initWasmPromise;
@@ -15,3 +26,7 @@ export async function useSharp() {
15
26
  sharpInstance.instance = sharpInstance.instance || await import("#nuxt-og-image/bindings/sharp").then((m) => m.default);
16
27
  return sharpInstance.instance;
17
28
  }
29
+ export async function useCssInline() {
30
+ cssInlineInstance.instance = cssInlineInstance.instance || await import("#nuxt-og-image/bindings/css-inline").then((m) => m.default);
31
+ return cssInlineInstance.instance;
32
+ }
@@ -1,26 +1,28 @@
1
1
  import { defineSatoriTransformer } from "../utils.mjs";
2
2
  function isEmojiFilter(node) {
3
- return node.type === "img" && node.props?.class?.includes("emoji");
3
+ return node.type === "svg" && typeof node.props?.["data-emoji"] !== "undefined";
4
4
  }
5
5
  export default defineSatoriTransformer([
6
6
  // need to make sure parent div has flex for the emoji to render inline
7
7
  {
8
- filter: (node) => node.type === "div" && Array.isArray(node.props?.children) && node.props.children.some(isEmojiFilter),
8
+ filter: (node) => ["div", "p"].includes(node.type) && Array.isArray(node.props?.children) && node.props.children.some(isEmojiFilter),
9
9
  transform: async (node) => {
10
10
  node.props.style = node.props.style || {};
11
11
  node.props.style.display = "flex";
12
12
  node.props.style.alignItems = "center";
13
- }
14
- },
15
- {
16
- filter: isEmojiFilter,
17
- transform: async (node) => {
18
- node.props.style = node.props.style || {};
19
- node.props.style.height = "1em";
20
- node.props.style.width = "1em";
21
- node.props.style.margin = "0 .3em 0 .3em";
22
- node.props.style.verticalAlign = "0.1em";
23
- node.props.class = "";
13
+ node.props.children = node.props.children.map((child) => {
14
+ if (typeof child === "string") {
15
+ return {
16
+ type: "div",
17
+ props: {
18
+ children: child
19
+ }
20
+ };
21
+ }
22
+ if (child.props.class?.includes("emoji"))
23
+ delete child.props.class;
24
+ return child;
25
+ });
24
26
  }
25
27
  }
26
28
  ]);
@@ -3,28 +3,41 @@ import { withBase } from "ufo";
3
3
  import sizeOf from "image-size";
4
4
  import { defineSatoriTransformer } from "../utils.mjs";
5
5
  import { toBase64Image } from "../../../env/assets.mjs";
6
- import { useNitroOrigin } from "#imports";
6
+ import { useNitroOrigin, useStorage } from "#imports";
7
7
  export default defineSatoriTransformer({
8
8
  filter: (node) => node.type === "img",
9
- transform: async (node, e) => {
9
+ transform: async (node, { e }) => {
10
10
  const src = node.props?.src;
11
+ const isRelative = src?.startsWith("/");
11
12
  if (src) {
12
13
  let updated = false;
13
14
  let dimensions;
14
- if (!updated) {
15
- let valid = true;
16
- const response = await e.$fetch(src, {
15
+ let imageBuffer;
16
+ let valid = true;
17
+ if (import.meta.prerender || import.meta.dev) {
18
+ const key = `root:public${src.replace("./", ":").replace("/", ":")}`;
19
+ if (await useStorage().hasItem(key)) {
20
+ imageBuffer = await useStorage().getItemRaw(key);
21
+ updated = !!imageBuffer;
22
+ }
23
+ }
24
+ if (!import.meta.prerender && !updated) {
25
+ imageBuffer = await e.$fetch(src, {
17
26
  baseURL: useNitroOrigin(e),
18
27
  responseType: "arrayBuffer"
19
28
  }).catch(() => {
20
29
  valid = false;
21
30
  });
22
- if (valid) {
23
- node.props.src = toBase64Image(src, response);
24
- const imageSize = sizeOf(Buffer.from(response));
31
+ valid = !!imageBuffer;
32
+ }
33
+ if (valid) {
34
+ node.props.src = toBase64Image(src, imageBuffer);
35
+ try {
36
+ const imageSize = sizeOf(Buffer.from(imageBuffer));
25
37
  dimensions = { width: imageSize.width, height: imageSize.height };
26
- updated = true;
38
+ } catch (e2) {
27
39
  }
40
+ updated = true;
28
41
  }
29
42
  if (dimensions?.width && dimensions?.height) {
30
43
  const naturalAspectRatio = dimensions.width / dimensions.height;
@@ -37,7 +50,7 @@ export default defineSatoriTransformer({
37
50
  node.props.height = dimensions.height;
38
51
  }
39
52
  }
40
- if (!updated) {
53
+ if (!updated && isRelative) {
41
54
  node.props.src = `${withBase(src, `${useNitroOrigin(e)}`)}?${Date.now()}`;
42
55
  }
43
56
  }
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../../../types").SatoriTransformer | import("../../../../types").SatoriTransformer[];
2
+ export default _default;
@@ -0,0 +1,45 @@
1
+ import { createGenerator } from "@unocss/core";
2
+ import presetWind from "@unocss/preset-wind";
3
+ import { defineSatoriTransformer } from "../utils.mjs";
4
+ import { theme } from "#nuxt-og-image/unocss-config.mjs";
5
+ const uno = createGenerator({ theme }, {
6
+ presets: [
7
+ presetWind()
8
+ ]
9
+ });
10
+ export default defineSatoriTransformer({
11
+ filter: (node) => !!node.props?.class,
12
+ transform: async (node) => {
13
+ const classes = node.props.class || "";
14
+ const styles = node.props.style || {};
15
+ const replacedClasses = /* @__PURE__ */ new Set();
16
+ for (const token of classes.split(" ").filter((c) => c.trim())) {
17
+ const parsedToken = await uno.parseToken(token);
18
+ if (parsedToken) {
19
+ const inlineStyles = parsedToken[0][2].split(";").filter((s) => !!s?.trim());
20
+ const vars = {};
21
+ inlineStyles.filter((s) => s.startsWith("--")).forEach((s) => {
22
+ const [key, value] = s.split(":");
23
+ vars[key] = value;
24
+ });
25
+ inlineStyles.filter((s) => !s.startsWith("--")).forEach((s) => {
26
+ const [key, value] = s.split(":");
27
+ const camelCasedKey = key.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
28
+ if (!styles[camelCasedKey])
29
+ styles[camelCasedKey] = value.replace(/var\((.*?)\)/g, (_, k) => vars[k.trim()]);
30
+ if (styles[camelCasedKey] && styles[camelCasedKey].includes("/")) {
31
+ const [rgb, opacity] = styles[camelCasedKey].split("/");
32
+ if (opacity.trim() === "1)")
33
+ styles[camelCasedKey] = rgb.replace(/(\d+) (\d+) (\d+).*/, (_, r, g, b) => `${r}, ${g}, ${b})`);
34
+ else
35
+ styles[camelCasedKey] = `${rgb.replace("rgb", "rgba").replaceAll(" ", ", ")}${opacity.trim()}`;
36
+ }
37
+ });
38
+ replacedClasses.add(token);
39
+ }
40
+ }
41
+ node.props.class = classes.split(" ").filter((c) => !replacedClasses.has(c)).join(" ");
42
+ node.props.tw = classes.split(" ").filter((c) => !replacedClasses.has(c)).join(" ");
43
+ node.props.style = styles;
44
+ }
45
+ });
@@ -1,4 +1,3 @@
1
- import type { H3Event } from 'h3';
2
- import type { SatoriTransformer, VNode } from '../../../types';
3
- export declare function walkSatoriTree(e: H3Event, node: VNode, plugins: (SatoriTransformer | SatoriTransformer[])[]): Promise<void>;
1
+ import type { OgImageRenderEventContext, SatoriTransformer, VNode } from '../../../types';
2
+ export declare function walkSatoriTree(e: OgImageRenderEventContext, node: VNode, plugins: (SatoriTransformer | SatoriTransformer[])[]): Promise<void>;
4
3
  export declare function defineSatoriTransformer(transformer: SatoriTransformer | SatoriTransformer[]): SatoriTransformer | SatoriTransformer[];
@@ -1,3 +1,2 @@
1
- import type { H3Event } from 'h3';
2
- import type { RendererOptions, VNode } from '../../../types';
3
- export declare function createVNodes(e: H3Event, options: RendererOptions): Promise<VNode>;
1
+ import type { OgImageRenderEventContext, VNode } from '../../../types';
2
+ export declare function createVNodes(ctx: OgImageRenderEventContext): Promise<VNode>;
@@ -1,16 +1,26 @@
1
1
  import { html as convertHtmlToSatori } from "satori-html";
2
- import { fetchHTML } from "../../html/fetch.mjs";
2
+ import { fetchIsland } from "../../html/fetchIsland.mjs";
3
+ import { applyInlineCss } from "../../html/applyInlineCss.mjs";
4
+ import { applyEmojis } from "../../html/applyEmojis.mjs";
3
5
  import { walkSatoriTree } from "./utils.mjs";
6
+ import unocss from "./plugins/unocss.mjs";
4
7
  import emojis from "./plugins/emojis.mjs";
5
8
  import twClasses from "./plugins/twClasses.mjs";
6
9
  import imageSrc from "./plugins/imageSrc.mjs";
7
10
  import flex from "./plugins/flex.mjs";
8
11
  import encoding from "./plugins/encoding.mjs";
9
- export async function createVNodes(e, options) {
10
- const html = options.html || await fetchHTML(e, options);
11
- const body = html.match(/<body[^>]*>([\s\S]*)<\/body>/)?.[1] || html;
12
- const satoriTree = convertHtmlToSatori(body);
13
- await walkSatoriTree(e, satoriTree, [
12
+ export async function createVNodes(ctx) {
13
+ let html = ctx.options.html;
14
+ if (!html) {
15
+ const island = await fetchIsland(ctx);
16
+ await applyInlineCss(ctx, island);
17
+ await applyEmojis(ctx, island);
18
+ html = island.html;
19
+ }
20
+ const template = `<div data-v-inspector-ignore="true" style="position: relative; display: flex; margin: 0 auto; width: ${ctx.options.width}px; height: ${ctx.options.height}px; overflow: hidden;">${html}</div>`;
21
+ const satoriTree = convertHtmlToSatori(template);
22
+ await walkSatoriTree(ctx, satoriTree, [
23
+ unocss,
14
24
  emojis,
15
25
  twClasses,
16
26
  imageSrc,