figma-metadata-extractor 1.0.1 → 1.0.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 +78 -1
- package/dist/index.cjs +52 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +52 -0
- package/dist/lib.d.ts +24 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ npm install figma-metadata-extractor
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import { getFigmaMetadata, downloadFigmaImages } from 'figma-metadata-extractor';
|
|
14
|
+
import { getFigmaMetadata, downloadFigmaImages, downloadFigmaFrameImage } from 'figma-metadata-extractor';
|
|
15
15
|
|
|
16
16
|
// Extract metadata from a Figma file
|
|
17
17
|
const metadata = await getFigmaMetadata(
|
|
@@ -45,6 +45,20 @@ const images = await downloadFigmaImages(
|
|
|
45
45
|
);
|
|
46
46
|
|
|
47
47
|
console.log(images); // Array of download results
|
|
48
|
+
|
|
49
|
+
// Download a single frame image from a Figma URL
|
|
50
|
+
const frameImage = await downloadFigmaFrameImage(
|
|
51
|
+
'https://figma.com/file/ABC123/My-Design?node-id=1234-5678',
|
|
52
|
+
{
|
|
53
|
+
apiKey: 'your-figma-api-key',
|
|
54
|
+
localPath: './assets/frames',
|
|
55
|
+
fileName: 'my-frame.png',
|
|
56
|
+
format: 'png', // or 'svg'
|
|
57
|
+
pngScale: 2
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
console.log(frameImage.filePath); // Path to downloaded image
|
|
48
62
|
```
|
|
49
63
|
|
|
50
64
|
## API Reference
|
|
@@ -90,6 +104,25 @@ Downloads SVG and PNG images from a Figma file.
|
|
|
90
104
|
|
|
91
105
|
**Returns:** Promise<FigmaImageResult[]>
|
|
92
106
|
|
|
107
|
+
### `downloadFigmaFrameImage(figmaUrl, options)`
|
|
108
|
+
|
|
109
|
+
Downloads a single frame image from a Figma URL that contains a node-id parameter.
|
|
110
|
+
|
|
111
|
+
**Parameters:**
|
|
112
|
+
- `figmaUrl` (string): The Figma URL with node-id parameter (e.g., `https://figma.com/file/ABC123/My-Design?node-id=1234-5678`)
|
|
113
|
+
- `options` (FigmaFrameImageOptions): Configuration options
|
|
114
|
+
|
|
115
|
+
**Options:**
|
|
116
|
+
- `apiKey?: string` - Figma API key (Personal Access Token)
|
|
117
|
+
- `oauthToken?: string` - Figma OAuth Bearer token
|
|
118
|
+
- `useOAuth?: boolean` - Whether to use OAuth instead of API key
|
|
119
|
+
- `localPath: string` - Absolute path to save the image
|
|
120
|
+
- `fileName: string` - Local filename (must end with .png or .svg)
|
|
121
|
+
- `format?: 'png' | 'svg'` - Image format to download (default: 'png')
|
|
122
|
+
- `pngScale?: number` - Export scale for PNG images (default: 2)
|
|
123
|
+
|
|
124
|
+
**Returns:** Promise<FigmaImageResult>
|
|
125
|
+
|
|
93
126
|
## Authentication
|
|
94
127
|
|
|
95
128
|
You need either a Figma API key or OAuth token:
|
|
@@ -104,6 +137,50 @@ You need either a Figma API key or OAuth token:
|
|
|
104
137
|
2. Use the bearer token in the `oauthToken` option
|
|
105
138
|
3. Set `useOAuth: true`
|
|
106
139
|
|
|
140
|
+
## Usage Examples
|
|
141
|
+
|
|
142
|
+
### Download Frame Image from Figma URL
|
|
143
|
+
|
|
144
|
+
The easiest way to download a frame image is to copy the Figma URL directly from your browser when viewing a specific frame:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { downloadFigmaFrameImage } from 'figma-metadata-extractor';
|
|
148
|
+
|
|
149
|
+
// Copy this URL from Figma when viewing a frame
|
|
150
|
+
const figmaUrl = 'https://www.figma.com/design/ABC123/My-Design?node-id=1234-5678&t=xyz123';
|
|
151
|
+
|
|
152
|
+
const result = await downloadFigmaFrameImage(figmaUrl, {
|
|
153
|
+
apiKey: 'your-figma-api-key',
|
|
154
|
+
localPath: './downloads',
|
|
155
|
+
fileName: 'my-frame.png',
|
|
156
|
+
format: 'png',
|
|
157
|
+
pngScale: 2 // High resolution
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
console.log(`Downloaded to: ${result.filePath}`);
|
|
161
|
+
console.log(`Dimensions: ${result.finalDimensions.width}x${result.finalDimensions.height}`);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Download Multiple Frame Images
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { downloadFigmaImages } from 'figma-metadata-extractor';
|
|
168
|
+
|
|
169
|
+
// For multiple frames, use the batch download function
|
|
170
|
+
const results = await downloadFigmaImages(
|
|
171
|
+
'https://figma.com/file/ABC123/My-Design',
|
|
172
|
+
[
|
|
173
|
+
{ nodeId: '1234:5678', fileName: 'frame1.png' },
|
|
174
|
+
{ nodeId: '9876:5432', fileName: 'frame2.svg' },
|
|
175
|
+
{ nodeId: '1111:2222', fileName: 'frame3.png' }
|
|
176
|
+
],
|
|
177
|
+
{
|
|
178
|
+
apiKey: 'your-figma-api-key',
|
|
179
|
+
localPath: './frames'
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
```
|
|
183
|
+
|
|
107
184
|
## Advanced Usage
|
|
108
185
|
|
|
109
186
|
The library also exports the underlying extractor system for custom processing:
|
package/dist/index.cjs
CHANGED
|
@@ -1446,10 +1446,62 @@ async function downloadFigmaImages(figmaUrl, nodes, options) {
|
|
|
1446
1446
|
throw new Error(`Failed to download images: ${error instanceof Error ? error.message : String(error)}`);
|
|
1447
1447
|
}
|
|
1448
1448
|
}
|
|
1449
|
+
async function downloadFigmaFrameImage(figmaUrl, options) {
|
|
1450
|
+
const {
|
|
1451
|
+
apiKey,
|
|
1452
|
+
oauthToken,
|
|
1453
|
+
useOAuth = false,
|
|
1454
|
+
pngScale = 2,
|
|
1455
|
+
localPath,
|
|
1456
|
+
fileName,
|
|
1457
|
+
format = "png"
|
|
1458
|
+
} = options;
|
|
1459
|
+
if (!apiKey && !oauthToken) {
|
|
1460
|
+
throw new Error("Either apiKey or oauthToken is required");
|
|
1461
|
+
}
|
|
1462
|
+
const urlMatch = figmaUrl.match(/figma\.com\/(file|design)\/([a-zA-Z0-9]+)/);
|
|
1463
|
+
if (!urlMatch) {
|
|
1464
|
+
throw new Error("Invalid Figma URL format");
|
|
1465
|
+
}
|
|
1466
|
+
const fileKey = urlMatch[2];
|
|
1467
|
+
const nodeIdMatch = figmaUrl.match(/node-id=([^&]+)/);
|
|
1468
|
+
if (!nodeIdMatch) {
|
|
1469
|
+
throw new Error("No frame node-id found in URL. Please provide a Figma URL with a node-id parameter (e.g., ?node-id=123-456)");
|
|
1470
|
+
}
|
|
1471
|
+
const nodeId = nodeIdMatch[1].replace(/-/g, ":");
|
|
1472
|
+
const expectedExtension = `.${format}`;
|
|
1473
|
+
if (!fileName.toLowerCase().endsWith(expectedExtension)) {
|
|
1474
|
+
throw new Error(`Filename must end with ${expectedExtension} for ${format} format`);
|
|
1475
|
+
}
|
|
1476
|
+
const figmaService = new FigmaService({
|
|
1477
|
+
figmaApiKey: apiKey || "",
|
|
1478
|
+
figmaOAuthToken: oauthToken || "",
|
|
1479
|
+
useOAuth: useOAuth && !!oauthToken
|
|
1480
|
+
});
|
|
1481
|
+
try {
|
|
1482
|
+
Logger.log(`Downloading ${format.toUpperCase()} image for frame ${nodeId} from file ${fileKey}`);
|
|
1483
|
+
const imageNode = {
|
|
1484
|
+
nodeId,
|
|
1485
|
+
fileName
|
|
1486
|
+
};
|
|
1487
|
+
const results = await figmaService.downloadImages(fileKey, localPath, [imageNode], {
|
|
1488
|
+
pngScale: format === "png" ? pngScale : void 0
|
|
1489
|
+
});
|
|
1490
|
+
if (results.length === 0) {
|
|
1491
|
+
throw new Error(`Failed to download image for frame ${nodeId}`);
|
|
1492
|
+
}
|
|
1493
|
+
Logger.log(`Successfully downloaded frame image to: ${results[0].filePath}`);
|
|
1494
|
+
return results[0];
|
|
1495
|
+
} catch (error) {
|
|
1496
|
+
Logger.error(`Error downloading frame image from ${fileKey}:`, error);
|
|
1497
|
+
throw new Error(`Failed to download frame image: ${error instanceof Error ? error.message : String(error)}`);
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1449
1500
|
exports.allExtractors = allExtractors;
|
|
1450
1501
|
exports.collapseSvgContainers = collapseSvgContainers;
|
|
1451
1502
|
exports.componentExtractor = componentExtractor;
|
|
1452
1503
|
exports.contentOnly = contentOnly;
|
|
1504
|
+
exports.downloadFigmaFrameImage = downloadFigmaFrameImage;
|
|
1453
1505
|
exports.downloadFigmaImages = downloadFigmaImages;
|
|
1454
1506
|
exports.extractFromDesign = extractFromDesign;
|
|
1455
1507
|
exports.getFigmaMetadata = getFigmaMetadata;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { getFigmaMetadata, downloadFigmaImages, type FigmaMetadataOptions, type FigmaImageOptions, type FigmaImageNode, type FigmaMetadataResult, type FigmaImageResult, } from "./lib.js";
|
|
1
|
+
export { getFigmaMetadata, downloadFigmaImages, downloadFigmaFrameImage, type FigmaMetadataOptions, type FigmaImageOptions, type FigmaFrameImageOptions, type FigmaImageNode, type FigmaMetadataResult, type FigmaImageResult, } from "./lib.js";
|
|
2
2
|
export type { SimplifiedDesign } from "./extractors/types.js";
|
|
3
3
|
export type { ExtractorFn, TraversalContext, TraversalOptions, GlobalVars, StyleTypes, } from "./extractors/index.js";
|
|
4
4
|
export { extractFromDesign, simplifyRawFigmaObject, layoutExtractor, textExtractor, visualsExtractor, componentExtractor, allExtractors, layoutAndText, contentOnly, visualsOnly, layoutOnly, collapseSvgContainers, } from "./extractors/index.js";
|
package/dist/index.js
CHANGED
|
@@ -1444,11 +1444,63 @@ async function downloadFigmaImages(figmaUrl, nodes, options) {
|
|
|
1444
1444
|
throw new Error(`Failed to download images: ${error instanceof Error ? error.message : String(error)}`);
|
|
1445
1445
|
}
|
|
1446
1446
|
}
|
|
1447
|
+
async function downloadFigmaFrameImage(figmaUrl, options) {
|
|
1448
|
+
const {
|
|
1449
|
+
apiKey,
|
|
1450
|
+
oauthToken,
|
|
1451
|
+
useOAuth = false,
|
|
1452
|
+
pngScale = 2,
|
|
1453
|
+
localPath,
|
|
1454
|
+
fileName,
|
|
1455
|
+
format = "png"
|
|
1456
|
+
} = options;
|
|
1457
|
+
if (!apiKey && !oauthToken) {
|
|
1458
|
+
throw new Error("Either apiKey or oauthToken is required");
|
|
1459
|
+
}
|
|
1460
|
+
const urlMatch = figmaUrl.match(/figma\.com\/(file|design)\/([a-zA-Z0-9]+)/);
|
|
1461
|
+
if (!urlMatch) {
|
|
1462
|
+
throw new Error("Invalid Figma URL format");
|
|
1463
|
+
}
|
|
1464
|
+
const fileKey = urlMatch[2];
|
|
1465
|
+
const nodeIdMatch = figmaUrl.match(/node-id=([^&]+)/);
|
|
1466
|
+
if (!nodeIdMatch) {
|
|
1467
|
+
throw new Error("No frame node-id found in URL. Please provide a Figma URL with a node-id parameter (e.g., ?node-id=123-456)");
|
|
1468
|
+
}
|
|
1469
|
+
const nodeId = nodeIdMatch[1].replace(/-/g, ":");
|
|
1470
|
+
const expectedExtension = `.${format}`;
|
|
1471
|
+
if (!fileName.toLowerCase().endsWith(expectedExtension)) {
|
|
1472
|
+
throw new Error(`Filename must end with ${expectedExtension} for ${format} format`);
|
|
1473
|
+
}
|
|
1474
|
+
const figmaService = new FigmaService({
|
|
1475
|
+
figmaApiKey: apiKey || "",
|
|
1476
|
+
figmaOAuthToken: oauthToken || "",
|
|
1477
|
+
useOAuth: useOAuth && !!oauthToken
|
|
1478
|
+
});
|
|
1479
|
+
try {
|
|
1480
|
+
Logger.log(`Downloading ${format.toUpperCase()} image for frame ${nodeId} from file ${fileKey}`);
|
|
1481
|
+
const imageNode = {
|
|
1482
|
+
nodeId,
|
|
1483
|
+
fileName
|
|
1484
|
+
};
|
|
1485
|
+
const results = await figmaService.downloadImages(fileKey, localPath, [imageNode], {
|
|
1486
|
+
pngScale: format === "png" ? pngScale : void 0
|
|
1487
|
+
});
|
|
1488
|
+
if (results.length === 0) {
|
|
1489
|
+
throw new Error(`Failed to download image for frame ${nodeId}`);
|
|
1490
|
+
}
|
|
1491
|
+
Logger.log(`Successfully downloaded frame image to: ${results[0].filePath}`);
|
|
1492
|
+
return results[0];
|
|
1493
|
+
} catch (error) {
|
|
1494
|
+
Logger.error(`Error downloading frame image from ${fileKey}:`, error);
|
|
1495
|
+
throw new Error(`Failed to download frame image: ${error instanceof Error ? error.message : String(error)}`);
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1447
1498
|
export {
|
|
1448
1499
|
allExtractors,
|
|
1449
1500
|
collapseSvgContainers,
|
|
1450
1501
|
componentExtractor,
|
|
1451
1502
|
contentOnly,
|
|
1503
|
+
downloadFigmaFrameImage,
|
|
1452
1504
|
downloadFigmaImages,
|
|
1453
1505
|
extractFromDesign,
|
|
1454
1506
|
getFigmaMetadata,
|
package/dist/lib.d.ts
CHANGED
|
@@ -46,6 +46,22 @@ export interface FigmaImageResult {
|
|
|
46
46
|
wasCropped: boolean;
|
|
47
47
|
cssVariables?: string;
|
|
48
48
|
}
|
|
49
|
+
export interface FigmaFrameImageOptions {
|
|
50
|
+
/** The Figma API key (Personal Access Token) */
|
|
51
|
+
apiKey?: string;
|
|
52
|
+
/** The Figma OAuth Bearer token */
|
|
53
|
+
oauthToken?: string;
|
|
54
|
+
/** Whether to use OAuth instead of API key */
|
|
55
|
+
useOAuth?: boolean;
|
|
56
|
+
/** Export scale for PNG images (defaults to 2) */
|
|
57
|
+
pngScale?: number;
|
|
58
|
+
/** The absolute path to the directory where the image should be stored */
|
|
59
|
+
localPath: string;
|
|
60
|
+
/** The filename for the downloaded image (must end with .png or .svg) */
|
|
61
|
+
fileName: string;
|
|
62
|
+
/** Image format to download (defaults to 'png') */
|
|
63
|
+
format?: 'png' | 'svg';
|
|
64
|
+
}
|
|
49
65
|
/**
|
|
50
66
|
* Extract metadata from a Figma file or specific nodes
|
|
51
67
|
*
|
|
@@ -63,6 +79,14 @@ export declare function getFigmaMetadata(figmaUrl: string, options?: FigmaMetada
|
|
|
63
79
|
* @returns Promise resolving to array of download results
|
|
64
80
|
*/
|
|
65
81
|
export declare function downloadFigmaImages(figmaUrl: string, nodes: FigmaImageNode[], options: FigmaMetadataOptions & FigmaImageOptions): Promise<FigmaImageResult[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Download a frame image from a Figma URL
|
|
84
|
+
*
|
|
85
|
+
* @param figmaUrl - The Figma URL containing the frame (with node-id parameter)
|
|
86
|
+
* @param options - Configuration options including API credentials, local path, and filename
|
|
87
|
+
* @returns Promise resolving to the download result
|
|
88
|
+
*/
|
|
89
|
+
export declare function downloadFigmaFrameImage(figmaUrl: string, options: FigmaFrameImageOptions): Promise<FigmaImageResult>;
|
|
66
90
|
export type { SimplifiedDesign } from "./extractors/types.js";
|
|
67
91
|
export type { ExtractorFn, TraversalContext, TraversalOptions, GlobalVars, StyleTypes, } from "./extractors/index.js";
|
|
68
92
|
export { extractFromDesign, simplifyRawFigmaObject, layoutExtractor, textExtractor, visualsExtractor, componentExtractor, allExtractors, layoutAndText, contentOnly, visualsOnly, layoutOnly, collapseSvgContainers, } from "./extractors/index.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "figma-metadata-extractor",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Extract metadata and download images from Figma files. A standalone library for accessing Figma design data programmatically.",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Extract metadata and download images from Figma files. A standalone library for accessing Figma design data and downloading frame images programmatically.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|