@vercel/og 0.8.0 → 0.8.1

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.
@@ -1,7 +1,6 @@
1
1
  import type { ReactElement } from 'react';
2
- import type { ImageResponseOptions, FigmaImageResponseProps } from './types';
2
+ import type { ImageResponseOptions } from './types';
3
3
  export declare class ImageResponse extends Response {
4
4
  constructor(element: ReactElement, options?: ImageResponseOptions);
5
5
  }
6
- export declare const experimental_FigmaImageResponse: (props: FigmaImageResponseProps) => Promise<ImageResponse>;
7
6
  export type EdgeImageResponse = typeof ImageResponse;
@@ -20526,204 +20526,6 @@ async function render(satori, resvg, opts, defaultFonts, element) {
20526
20526
  return pngBuffer;
20527
20527
  }
20528
20528
 
20529
- // src/figma/index.tsx
20530
- var FigmaImageResponse = async ({
20531
- url,
20532
- template,
20533
- fonts,
20534
- imageResponseOptions,
20535
- Response: Response2
20536
- }) => {
20537
- const { fileId, nodeId } = parseFigmaUrl(url);
20538
- const figmaAPIToken = assertValue(
20539
- process.env.FIGMA_PERSONAL_ACCESS_TOKEN,
20540
- "Missing environment variable: `FIGMA_PERSONAL_ACCESS_TOKEN`. You can get one at https://www.figma.com/developers/api#authentication"
20541
- );
20542
- const figmaResponse = await fetch(
20543
- `https://api.figma.com/v1/images/${fileId}?ids=${nodeId}&svg_outline_text=false&format=svg&svg_include_id=true`,
20544
- {
20545
- method: "GET",
20546
- headers: {
20547
- "X-FIGMA-TOKEN": figmaAPIToken
20548
- },
20549
- cache: "no-store"
20550
- }
20551
- );
20552
- const figmaResponseJson = await figmaResponse.json();
20553
- const svgDownloadPath = figmaResponseJson.images[nodeId.replace("-", ":")];
20554
- const svgResponse = await fetch(svgDownloadPath, { cache: "no-store" });
20555
- const svg = await svgResponse.text();
20556
- const { width, height } = getSvgDimensions(svg);
20557
- const textNodes = getTextNodes(svg);
20558
- const textNodeAttributes = textNodes.map(parseSvgText);
20559
- return new Response2(
20560
- {
20561
- key: "0",
20562
- type: "div",
20563
- props: {
20564
- style: { display: "flex" },
20565
- children: [
20566
- {
20567
- type: "img",
20568
- props: {
20569
- style: { position: "absolute" },
20570
- alt: "",
20571
- width,
20572
- height,
20573
- src: svgToBase64(svg)
20574
- }
20575
- },
20576
- {
20577
- type: "div",
20578
- props: {
20579
- style: { display: "flex", position: "relative", width: "100%" },
20580
- children: textNodeAttributes.map((textNode) => {
20581
- const t = template[textNode.id];
20582
- let value = "";
20583
- if (t === void 0) {
20584
- value = textNode.content;
20585
- } else if (isComplexTemplate(t)) {
20586
- value = t.value;
20587
- } else {
20588
- value = t;
20589
- }
20590
- let cssProps = {};
20591
- let centerHorizontally = false;
20592
- if (isComplexTemplate(t) && t.props) {
20593
- const {
20594
- centerHorizontally: centerHorizontallyProp,
20595
- ...otherCSSProps
20596
- } = t.props;
20597
- cssProps = otherCSSProps;
20598
- centerHorizontally = centerHorizontallyProp || false;
20599
- }
20600
- if (centerHorizontally) {
20601
- const templateStyles = {
20602
- color: textNode.fill,
20603
- marginTop: `${parseInt(textNode.y) - parseInt(textNode.fontSize)}px`,
20604
- fontWeight: textNode.fontWeight || "400",
20605
- fontSize: textNode.fontSize,
20606
- fontFamily: textNode.fontFamily,
20607
- letterSpacing: textNode.letterSpacing,
20608
- textAlign: "center",
20609
- ...cssProps
20610
- };
20611
- return {
20612
- type: "div",
20613
- props: {
20614
- style: {
20615
- display: "flex",
20616
- justifyContent: "center",
20617
- position: "absolute",
20618
- width: "100%"
20619
- },
20620
- children: {
20621
- type: "div",
20622
- props: {
20623
- style: templateStyles,
20624
- children: value
20625
- }
20626
- }
20627
- }
20628
- };
20629
- }
20630
- return {
20631
- type: "div",
20632
- props: {
20633
- style: {
20634
- position: "absolute",
20635
- color: textNode.fill,
20636
- left: `${textNode.x}px`,
20637
- top: `${parseInt(textNode.y) - parseInt(textNode.fontSize)}px`,
20638
- fontWeight: textNode.fontWeight || "400",
20639
- fontSize: textNode.fontSize,
20640
- fontFamily: textNode.fontFamily,
20641
- letterSpacing: textNode.letterSpacing,
20642
- ...cssProps
20643
- },
20644
- children: value
20645
- }
20646
- };
20647
- })
20648
- }
20649
- }
20650
- ]
20651
- }
20652
- },
20653
- {
20654
- width,
20655
- height,
20656
- fonts,
20657
- ...imageResponseOptions
20658
- }
20659
- );
20660
- };
20661
- var isComplexTemplate = (template) => {
20662
- return typeof template !== "string" && template !== void 0 && "value" in template;
20663
- };
20664
- function svgToBase64(svg) {
20665
- const base64 = Buffer.from(svg).toString("base64");
20666
- return "data:image/svg+xml;base64," + base64;
20667
- }
20668
- function getSvgDimensions(svg) {
20669
- const widthMatch = svg.match(/width="(\d+)/);
20670
- const heightMatch = svg.match(/height="(\d+)/);
20671
- if (widthMatch && heightMatch) {
20672
- const width = parseInt(widthMatch[1], 10);
20673
- const height = parseInt(heightMatch[1], 10);
20674
- return { width, height };
20675
- }
20676
- return { width: 0, height: 0 };
20677
- }
20678
- function getTextNodes(svg) {
20679
- const regex = /<text[^>]*>(.*?)<\/text>/g;
20680
- let match;
20681
- const matches = [];
20682
- while ((match = regex.exec(svg)) !== null) {
20683
- matches.push(match[0]);
20684
- }
20685
- return matches;
20686
- }
20687
- function parseSvgText(svgText) {
20688
- const id = svgText.match(/id="([^"]*)"/)?.[1] || "";
20689
- const fill = svgText.match(/fill="([^"]*)"/)?.[1] || "";
20690
- const fontFamily = svgText.match(/font-family="([^"]*)"/)?.[1] || "";
20691
- const fontSize = svgText.match(/font-size="([^"]*)"/)?.[1] || "";
20692
- const fontWeight = svgText.match(/font-weight="([^"]*)"/)?.[1] || "";
20693
- const letterSpacing = svgText.match(/letter-spacing="([^"]*)"/)?.[1] || "";
20694
- const x2 = svgText.match(/<tspan[^>]*x="([^"]*)"/)?.[1] || "";
20695
- const y = svgText.match(/<tspan[^>]*y="([^"]*)"/)?.[1] || "";
20696
- const content = svgText.match(/<tspan[^>]*>([^<]*)<\/tspan>/)?.[1] || "";
20697
- return {
20698
- id,
20699
- fill,
20700
- fontFamily,
20701
- fontSize,
20702
- fontWeight,
20703
- letterSpacing,
20704
- x: x2,
20705
- y,
20706
- content
20707
- };
20708
- }
20709
- function parseFigmaUrl(figmaUrl) {
20710
- const regex = /\/file\/([^/]+)\/[^?]+\?[^#]*node-id=([^&#]+)/;
20711
- const match = figmaUrl.match(regex);
20712
- let fileId = "";
20713
- let nodeId = "";
20714
- if (match) {
20715
- fileId = match[1] || "";
20716
- nodeId = match[2] || "";
20717
- }
20718
- return { fileId, nodeId };
20719
- }
20720
- function assertValue(v2, errorMessage) {
20721
- if (v2 === void 0) {
20722
- throw new Error(errorMessage);
20723
- }
20724
- return v2;
20725
- }
20726
-
20727
20529
  // src/index.edge.ts
20728
20530
  var initializedResvg = initWasm(resvg_wasm);
20729
20531
  var fallbackFont = fetch(
@@ -20758,12 +20560,8 @@ var ImageResponse = class extends Response {
20758
20560
  });
20759
20561
  }
20760
20562
  };
20761
- var experimental_FigmaImageResponse = async (props) => {
20762
- return FigmaImageResponse({ ...props, Response: ImageResponse });
20763
- };
20764
20563
  export {
20765
- ImageResponse,
20766
- experimental_FigmaImageResponse
20564
+ ImageResponse
20767
20565
  };
20768
20566
  /*! Copyright Twitter Inc. and other contributors. Licensed under MIT */
20769
20567
  /*! Bundled license information: