Skip to content

Commit 6fc551d

Browse files
get all figma files from mdx and store in json fil;e
1 parent 0163f33 commit 6fc551d

File tree

6 files changed

+531
-353
lines changed

6 files changed

+531
-353
lines changed

.github/workflows/getFigmaImages.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Get Figma Images
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
findNodeReferences:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
- name: Set up Node
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: 20
18+
cache: 'npm'
19+
- name: Install dependencies
20+
run: npm ci --no-audit --no-fund --include=dev --ignore-scripts
21+
- name: Get Figma Images
22+
run: node scripts/getFigmaImages.js '**/*.mdx'
23+
- name: Log file content
24+
run: cat figmaImageNodeUrls.json

content/components/avatar.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ Avatars start at 16px and increment by base-4 until 32px. At 32px, the scale swi
3737
src="https://user-images.githubusercontent.com/586552/207139770-429f7327-c3e5-4e38-aa58-185a03d82304.png"
3838
/>
3939

40+
<img
41+
width="960"
42+
alt="Demo"
43+
src="https://www.figma.com/design/GCvY3Qv8czRgZgvl1dG6lp/Primer-Web?node-id=26998-2424&t=eruKCwLaYv3FX2Vu-4"
44+
/>
45+
4046
### Shape
4147

4248
Avatars appear as two different shapes, each with their own functional purpose.

figmaImageNodeUrls.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"https://www.figma.com/design/GCvY3Qv8czRgZgvl1dG6lp/Primer-Web?node-id=26998-2424&t=eruKCwLaYv3FX2Vu-4"
3+
]

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"@primer/gatsby-theme-doctocat": "^4.7.1",
2121
"@primer/octicons-react": "^17.3.0",
2222
"@primer/react": "35.5.0",
23-
"@styled-system/props": "^5.1.5",
2423
"@styled-system/prop-types": "5.1.5",
24+
"@styled-system/props": "^5.1.5",
2525
"@styled-system/theme-get": "^5.1.2",
2626
"@svgr/webpack": "^6.5.1",
2727
"@tanstack/react-query": "^4.23.0",
@@ -51,6 +51,7 @@
5151
"devDependencies": {
5252
"@github/markdownlint-github": "^0.3.0",
5353
"esm": "^3.2.25",
54+
"fast-glob": "^3.3.2",
5455
"markdownlint-cli2": "^0.5.1",
5556
"markdownlint-cli2-formatter-pretty": "^0.0.3",
5657
"mustache": "^4.2.0",

scripts/getFigmaImages.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Import necessary modules
2+
const fs = require('fs').promises;
3+
const fastGlob = require('fast-glob');
4+
5+
const isFigmaLink = (match) => {
6+
return match.startsWith('https://www.figma.com/design/')
7+
}
8+
9+
const findMatches = async (regexPattern, files) => {
10+
const matches = await Promise.all(
11+
files.map(async (filePath) => {
12+
// read file content
13+
const content = await fs.readFile(filePath, { encoding: 'utf8' })
14+
// try to find all matches in the file content
15+
const matches = [...content.matchAll(regexPattern)]
16+
// for each match, return the first group
17+
return matches.map((match) => match[1]).filter(isFigmaLink);
18+
})
19+
)
20+
//
21+
return matches.flat()
22+
}
23+
24+
const run = async () => {
25+
// get arguments
26+
const [fileGlob] = process.argv.slice(2)
27+
if(!fileGlob) {
28+
console.error('❌ Please provide a file glob as the argument. It needs to be wrapped in quotes.')
29+
return
30+
}
31+
// get all files that match the file glob
32+
const files = await fastGlob([fileGlob])
33+
// define the regex pattern to search
34+
const pattern = /<img\s+[^>]*src="([^"]+)"[^>]*>/g
35+
// find matches in find
36+
const matches = await findMatches(pattern, files)
37+
// write the matches to a file
38+
await fs.writeFile('figmaImageNodeUrls.json', JSON.stringify(matches, null, 2))
39+
40+
}
41+
42+
run()

0 commit comments

Comments
 (0)