Skip to content

Commit cc66e40

Browse files
adding FigmaImage component and script
1 parent 2fd468c commit cc66e40

File tree

6 files changed

+882
-1187
lines changed

6 files changed

+882
-1187
lines changed

.github/workflows/getFigmaImages.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,40 @@ name: Get Figma Images
22

33
on:
44
push:
5-
branches:
6-
- main
5+
# branches:
6+
# - main
77

88
jobs:
99
findNodeReferences:
1010
runs-on: ubuntu-latest
11+
env:
12+
FilesToScan: '**/*.mdx'
13+
ImageUrlFile: figmaImageNodeUrls.json
14+
ImageOutputDir: content/images/figma
15+
FigmaToken: ${{ secrets.FIGMA_TOKEN }}
1116
steps:
1217
- name: Checkout repository
1318
uses: actions/checkout@v4
1419
- name: Set up Node
1520
uses: actions/setup-node@v4
1621
with:
1722
node-version: 20
18-
cache: 'npm'
23+
cache: 'yarn'
1924
- name: Install dependencies
20-
run: npm ci --no-audit --no-fund --include=dev --ignore-scripts
25+
run: yarn
2126
- name: Get Figma Images
22-
run: node scripts/getFigmaImages.js '**/*.mdx' > figmaImageNodeUrls.json
27+
run: node scripts/getFigmaImages.js "${{env.FilesToScan}}" > ${{env.ImageUrlFile}}
2328
- name: Log file content
24-
run: cat figmaImageNodeUrls.json
29+
run: cat ${{env.ImageUrlFile}}
30+
- name: Download images from figma
31+
run: npx @primer/figma-images --figmaToken ${{env.FigmaToken}} --nodeURLsFile ${{env.ImageUrlFile}} --outputDir ${{env.ImageOutputDir}}
32+
- name: Log output dir content
33+
run: ls ${{env.ImageOutputDir}}
34+
- name: Add images to repo
35+
run: |
36+
git config --global user.name 'Figma Images Action'
37+
git config --global user.email '[email protected]'
38+
git add -A ${{env.ImageOutputDir}}
39+
git commit -m "Update Figma images"
40+
git push
41+

content/components/avatar.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {Box, Text} from '@primer/react'
1313
import ComponentLayout from '~/src/layouts/component-layout'
1414
export default ComponentLayout
1515
import {AccessibilityLink} from '~/src/components/accessibility-link'
16+
import {FigmaImage} from '~/src/components/figma-image'
1617

1718
<img
1819
width="960"
@@ -37,7 +38,7 @@ Avatars start at 16px and increment by base-4 until 32px. At 32px, the scale swi
3738
src="https://user-images.githubusercontent.com/586552/207139770-429f7327-c3e5-4e38-aa58-185a03d82304.png"
3839
/>
3940

40-
<img
41+
<FigmaImage
4142
width="960"
4243
alt="Demo"
4344
src="https://www.figma.com/design/GCvY3Qv8czRgZgvl1dG6lp/Primer-Web?node-id=26998-2424&t=eruKCwLaYv3FX2Vu-4"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
},
5151
"devDependencies": {
5252
"@github/markdownlint-github": "^0.3.0",
53+
"@primer/figma-images": "^0.1.1",
5354
"esm": "^3.2.25",
5455
"fast-glob": "^3.3.2",
5556
"markdownlint-cli2": "^0.5.1",

scripts/getFigmaImages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const run = async () => {
3131
// get all files that match the file glob
3232
const files = await fastGlob([fileGlob])
3333
// define the regex pattern to search
34-
const pattern = /<img\s+[^>]*src="([^"]+)"[^>]*>/g
34+
const pattern = /<FigmaImage\s+[^>]*src="([^"]+)"[^>]*>/g
3535
// find matches in find
3636
const matches = await findMatches(pattern, files)
3737
// output result

src/components/figma-image.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
import {parseFigmaNodeUrl} from '@primer/figma-images/src/utils';
3+
import { Link } from '@primer/react';
4+
5+
type FigmaImageProps = React.ImgHTMLAttributes<HTMLImageElement> & {
6+
src: string
7+
caption?: string
8+
}
9+
10+
const FigmaImageDir = 'content/images/figma';
11+
12+
export const FigmaImage: React.FC<FigmaImageProps> = ({src, caption, ...props}) => {
13+
// check for missing prop
14+
if(src === undefined) throw new Error("src is required on FigmaImage component");
15+
// get real image url
16+
const {nodeId, fileId} = parseFigmaNodeUrl(src);
17+
const imagePath = `${FigmaImageDir}/${fileId}-${nodeId}.png`
18+
// return image component
19+
return (<figure>
20+
<img src={imagePath} {...props}/>
21+
<figcaption>{caption}<Link href={src}>Edit Image</Link></figcaption>
22+
</figure>)
23+
}

0 commit comments

Comments
 (0)