figma-metadata-extractor 1.0.3 → 1.0.5

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/dist/index.cjs CHANGED
@@ -1374,7 +1374,8 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
1374
1374
  downloadImages = false,
1375
1375
  localPath,
1376
1376
  imageFormat = "png",
1377
- pngScale = 2
1377
+ pngScale = 2,
1378
+ useRelativePaths = true
1378
1379
  } = options;
1379
1380
  if (!apiKey && !oauthToken) {
1380
1381
  throw new Error("Either apiKey or oauthToken is required");
@@ -1432,7 +1433,7 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
1432
1433
  imageNodes,
1433
1434
  { pngScale: imageFormat === "png" ? pngScale : void 0 }
1434
1435
  );
1435
- result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults);
1436
+ result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths, localPath);
1436
1437
  Logger.log(`Successfully downloaded and enriched ${downloadResults.length} images`);
1437
1438
  }
1438
1439
  }
@@ -1556,18 +1557,34 @@ function hasImageFill(node, globalVars) {
1556
1557
  }
1557
1558
  return fillData.some((fill) => fill?.type === "IMAGE");
1558
1559
  }
1559
- function enrichNodesWithImages(nodes, imageAssets, downloadResults) {
1560
+ function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths = true, localPath) {
1560
1561
  const imageMap = /* @__PURE__ */ new Map();
1561
1562
  imageAssets.forEach((asset, index) => {
1562
1563
  const result = downloadResults[index];
1563
1564
  if (result) {
1565
+ let pathForMarkup;
1566
+ if (useRelativePaths === false) {
1567
+ pathForMarkup = result.filePath;
1568
+ } else if (typeof useRelativePaths === "string") {
1569
+ const basePath = useRelativePaths.endsWith("/") ? useRelativePaths : useRelativePaths + "/";
1570
+ const normalizedFilePath = result.filePath.replace(/\\/g, "/");
1571
+ const normalizedBasePath = basePath.replace(/\\/g, "/");
1572
+ if (normalizedFilePath.startsWith(normalizedBasePath)) {
1573
+ pathForMarkup = "./" + normalizedFilePath.substring(normalizedBasePath.length);
1574
+ } else {
1575
+ pathForMarkup = "./" + result.filePath.split(/[/\\]/).pop();
1576
+ }
1577
+ } else {
1578
+ const fileName = result.filePath.split(/[/\\]/).pop();
1579
+ pathForMarkup = "./" + fileName;
1580
+ }
1564
1581
  imageMap.set(asset.id, {
1565
1582
  filePath: result.filePath,
1566
- relativePath: result.filePath.replace(process.cwd(), "."),
1583
+ relativePath: pathForMarkup,
1567
1584
  dimensions: result.finalDimensions,
1568
1585
  wasCropped: result.wasCropped,
1569
- markdown: `![${asset.name}](${result.filePath.replace(process.cwd(), ".")})`,
1570
- html: `<img src="${result.filePath.replace(process.cwd(), ".")}" alt="${asset.name}" width="${result.finalDimensions.width}" height="${result.finalDimensions.height}">`
1586
+ markdown: `![${asset.name}](${pathForMarkup})`,
1587
+ html: `<img src="${pathForMarkup}" alt="${asset.name}" width="${result.finalDimensions.width}" height="${result.finalDimensions.height}">`
1571
1588
  });
1572
1589
  }
1573
1590
  });
package/dist/index.js CHANGED
@@ -1372,7 +1372,8 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
1372
1372
  downloadImages = false,
1373
1373
  localPath,
1374
1374
  imageFormat = "png",
1375
- pngScale = 2
1375
+ pngScale = 2,
1376
+ useRelativePaths = true
1376
1377
  } = options;
1377
1378
  if (!apiKey && !oauthToken) {
1378
1379
  throw new Error("Either apiKey or oauthToken is required");
@@ -1430,7 +1431,7 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
1430
1431
  imageNodes,
1431
1432
  { pngScale: imageFormat === "png" ? pngScale : void 0 }
1432
1433
  );
1433
- result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults);
1434
+ result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths, localPath);
1434
1435
  Logger.log(`Successfully downloaded and enriched ${downloadResults.length} images`);
1435
1436
  }
1436
1437
  }
@@ -1554,18 +1555,34 @@ function hasImageFill(node, globalVars) {
1554
1555
  }
1555
1556
  return fillData.some((fill) => fill?.type === "IMAGE");
1556
1557
  }
1557
- function enrichNodesWithImages(nodes, imageAssets, downloadResults) {
1558
+ function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths = true, localPath) {
1558
1559
  const imageMap = /* @__PURE__ */ new Map();
1559
1560
  imageAssets.forEach((asset, index) => {
1560
1561
  const result = downloadResults[index];
1561
1562
  if (result) {
1563
+ let pathForMarkup;
1564
+ if (useRelativePaths === false) {
1565
+ pathForMarkup = result.filePath;
1566
+ } else if (typeof useRelativePaths === "string") {
1567
+ const basePath = useRelativePaths.endsWith("/") ? useRelativePaths : useRelativePaths + "/";
1568
+ const normalizedFilePath = result.filePath.replace(/\\/g, "/");
1569
+ const normalizedBasePath = basePath.replace(/\\/g, "/");
1570
+ if (normalizedFilePath.startsWith(normalizedBasePath)) {
1571
+ pathForMarkup = "./" + normalizedFilePath.substring(normalizedBasePath.length);
1572
+ } else {
1573
+ pathForMarkup = "./" + result.filePath.split(/[/\\]/).pop();
1574
+ }
1575
+ } else {
1576
+ const fileName = result.filePath.split(/[/\\]/).pop();
1577
+ pathForMarkup = "./" + fileName;
1578
+ }
1562
1579
  imageMap.set(asset.id, {
1563
1580
  filePath: result.filePath,
1564
- relativePath: result.filePath.replace(process.cwd(), "."),
1581
+ relativePath: pathForMarkup,
1565
1582
  dimensions: result.finalDimensions,
1566
1583
  wasCropped: result.wasCropped,
1567
- markdown: `![${asset.name}](${result.filePath.replace(process.cwd(), ".")})`,
1568
- html: `<img src="${result.filePath.replace(process.cwd(), ".")}" alt="${asset.name}" width="${result.finalDimensions.width}" height="${result.finalDimensions.height}">`
1584
+ markdown: `![${asset.name}](${pathForMarkup})`,
1585
+ html: `<img src="${pathForMarkup}" alt="${asset.name}" width="${result.finalDimensions.width}" height="${result.finalDimensions.height}">`
1569
1586
  });
1570
1587
  }
1571
1588
  });
package/dist/lib.d.ts CHANGED
@@ -17,6 +17,14 @@ export interface FigmaMetadataOptions {
17
17
  imageFormat?: 'png' | 'svg';
18
18
  /** Export scale for PNG images (defaults to 2) */
19
19
  pngScale?: number;
20
+ /**
21
+ * Control how image paths are generated in downloadedImage properties.
22
+ * - true (default): Just the filename (e.g., "./icon.png")
23
+ * - false: Absolute file path (e.g., "/absolute/path/to/images/icon.png")
24
+ * - string: Strip this base path from file path (e.g., "/var/www" → "./images/icon.png")
25
+ * Default: true
26
+ */
27
+ useRelativePaths?: boolean | string;
20
28
  }
21
29
  export interface FigmaImageOptions {
22
30
  /** Export scale for PNG images (defaults to 2) */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "figma-metadata-extractor",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
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",