Skip to content

Commit fd0be54

Browse files
authored
feat: add fetch-footer utility to automate OpenJS footer links
1 parent c95a6ef commit fd0be54

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

src/utilities/fetch-footer.mjs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { writeFile, mkdir } from 'fs/promises';
2+
import path from 'path';
3+
4+
const ARTWORK_README_URL =
5+
'https://raw.githubusercontent.com/openjs-foundation/artwork/main/README.md';
6+
const OUTPUT_PATH = path.resolve(
7+
'src/components/Footer/_footer-legal.json'
8+
);
9+
10+
async function fetchFooter() {
11+
console.log('Fetching OpenJS Foundation footer template...');
12+
13+
const response = await fetch(ARTWORK_README_URL);
14+
if (!response.ok) {
15+
throw new Error(`Failed to fetch artwork README: ${response.status}`);
16+
}
17+
const readme = await response.text();
18+
19+
// Extract the HTML block under "### HTML"
20+
const htmlSectionMatch = readme.match(
21+
/### HTML\s*\n+```html\s*\n([\s\S]*?)\n```/
22+
);
23+
24+
if (!htmlSectionMatch) {
25+
throw new Error(
26+
'Could not find HTML footer template in artwork README'
27+
);
28+
}
29+
30+
const html = htmlSectionMatch[1].trim();
31+
32+
// Parse links from the footer HTML
33+
const linkRegex = /<a\s+href="([^"]+)">([^<]+)<\/a>/g;
34+
const paragraphs = html.split('</p>');
35+
36+
// Second paragraph contains the links bar
37+
const linksSection = paragraphs.length > 1 ? paragraphs[1] : '';
38+
const links = [];
39+
let match;
40+
while ((match = linkRegex.exec(linksSection)) !== null) {
41+
links.push({ url: match[1], label: match[2] });
42+
}
43+
44+
const data = { links };
45+
46+
await mkdir(path.dirname(OUTPUT_PATH), { recursive: true });
47+
await writeFile(OUTPUT_PATH, JSON.stringify(data, null, 2));
48+
49+
console.log(`Footer data written to ${OUTPUT_PATH}`);
50+
console.log(`Found ${links.length} footer links`);
51+
}
52+
53+
fetchFooter().catch((err) => {
54+
console.error('Error fetching footer:', err);
55+
process.exit(1);
56+
});

0 commit comments

Comments
 (0)