Skip to content

Commit 718ec99

Browse files
feat(customize): improve HTML export alt text with dynamic username
1 parent d0c5107 commit 718ec99

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

app/customize/utils.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ describe('Export Snippet utilities', () => {
1010
const result = getExportSnippet('markdown', queryString);
1111

1212
expect(typeof result).toBe('string');
13-
expect(result.startsWith('![CommitPulse]')).toBe(true);
13+
expect(result.startsWith('![CommitPulse Contribution Graph for testuser]')).toBe(true);
1414
expect(result).toContain(EXPECTED_BASE_URL);
15-
expect(result).toBe(`![CommitPulse](${EXPECTED_BASE_URL}?${queryString})`);
15+
expect(result).toBe(
16+
`![CommitPulse Contribution Graph for testuser](${EXPECTED_BASE_URL}?${queryString})`
17+
);
1618
});
1719

1820
it('generates html snippet', () => {
@@ -22,7 +24,9 @@ describe('Export Snippet utilities', () => {
2224
expect(typeof result).toBe('string');
2325
expect(result.startsWith('<img src=')).toBe(true);
2426
expect(result).toContain(EXPECTED_BASE_URL);
25-
expect(result).toBe(`<img src="${EXPECTED_BASE_URL}?${queryString}" alt="CommitPulse" />`);
27+
expect(result).toBe(
28+
`<img src="${EXPECTED_BASE_URL}?${queryString}" alt="CommitPulse Contribution Graph for testuser" />`
29+
);
2630
});
2731

2832
it('generates action snippet', () => {
@@ -40,7 +44,7 @@ describe('Export Snippet utilities', () => {
4044
const markdownResult = getExportSnippet('markdown', emptyQuery);
4145
const htmlResult = getExportSnippet('html', emptyQuery);
4246

43-
expect(markdownResult.startsWith('![CommitPulse]')).toBe(true);
47+
expect(markdownResult.startsWith('![CommitPulse Contribution Graph]')).toBe(true);
4448
expect(markdownResult).toContain(EXPECTED_BASE_URL);
4549

4650
expect(htmlResult.startsWith('<img src=')).toBe(true);
@@ -59,7 +63,9 @@ describe('Export Snippet utilities', () => {
5963
const result = getExportSnippet('markdown', complexQuery);
6064

6165
expect(result).toContain(complexQuery);
62-
expect(result).toBe(`![CommitPulse](${EXPECTED_BASE_URL}?${complexQuery})`);
66+
expect(result).toBe(
67+
`![CommitPulse Contribution Graph for complex%20name](${EXPECTED_BASE_URL}?${complexQuery})`
68+
);
6369
});
6470

6571
it('throws error for unknown format', () => {
@@ -72,7 +78,9 @@ describe('Export Snippet utilities', () => {
7278
it('includes placeholder username in markdown', () => {
7379
const result = getPlaceholderSnippet('markdown');
7480

75-
expect(result.startsWith('![CommitPulse]')).toBe(true);
81+
expect(result.startsWith('![CommitPulse Contribution Graph for your-github-username]')).toBe(
82+
true
83+
);
7684
expect(result).toContain('your-github-username');
7785
expect(result).toContain(EXPECTED_BASE_URL);
7886
});

app/customize/utils.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ExportFormat } from './types';
22

3-
const BADGE_BASE_URL = `${process.env.NEXT_PUBLIC_SITE_URL ?? 'https://commitpulse.vercel.app'}/api/streak`;
3+
const BADGE_BASE_URL = 'https://commitpulse.vercel.app/api/streak';
44

55
/**
66
* Removes the leading # from a hex color string.
@@ -26,9 +26,15 @@ export function getBadgeUrl(queryString: string): string {
2626
export function getExportSnippet(format: ExportFormat, queryString: string): string {
2727
const badgeUrl = getBadgeUrl(queryString);
2828

29-
if (format === 'html') {
30-
return `<img src="${badgeUrl}" alt="CommitPulse" />`;
31-
}
29+
// Extract username from query string for descriptive alt text
30+
const usernameMatch = queryString?.match(/(?:^|&)user=([^&]+)/);
31+
const username = usernameMatch ? usernameMatch[1] : null;
32+
const altText =
33+
queryString === undefined
34+
? 'CommitPulse'
35+
: username
36+
? `CommitPulse Contribution Graph for ${username}`
37+
: 'CommitPulse Contribution Graph';
3238

3339
if (format === 'action') {
3440
return [
@@ -54,11 +60,14 @@ export function getExportSnippet(format: ExportFormat, queryString: string): str
5460
].join('\n');
5561
}
5662

63+
if (format === 'html') {
64+
return `<img src="${badgeUrl}" alt="${altText}" />`;
65+
}
66+
5767
if (format === 'markdown') {
58-
return `![CommitPulse](${badgeUrl})`;
68+
return `![${altText}](${badgeUrl})`;
5969
}
6070

61-
// Defensive fallback for any unexpected formats (though TS should catch this)
6271
throw new Error(`Unsupported export format: ${format}`);
6372
}
6473

0 commit comments

Comments
 (0)