Skip to content

Commit 78863c7

Browse files
meetparmar392005atul-upadhyay-7
authored andcommitted
feat(customize): improve HTML export alt text with dynamic username
1 parent b8ff6c3 commit 78863c7

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
@@ -11,9 +11,11 @@ describe('Export Snippet utilities', () => {
1111
const result = getExportSnippet('markdown', queryString);
1212

1313
expect(typeof result).toBe('string');
14-
expect(result.startsWith('![CommitPulse]')).toBe(true);
14+
expect(result.startsWith('![CommitPulse Contribution Graph for testuser]')).toBe(true);
1515
expect(result).toContain(EXPECTED_BASE_URL);
16-
expect(result).toBe(`![CommitPulse](${EXPECTED_BASE_URL}?${queryString})`);
16+
expect(result).toBe(
17+
`![CommitPulse Contribution Graph for testuser](${EXPECTED_BASE_URL}?${queryString})`
18+
);
1719
});
1820

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

2933
it('generates action snippet', () => {
@@ -41,7 +45,7 @@ describe('Export Snippet utilities', () => {
4145
const markdownResult = getExportSnippet('markdown', emptyQuery);
4246
const htmlResult = getExportSnippet('html', emptyQuery);
4347

44-
expect(markdownResult.startsWith('![CommitPulse]')).toBe(true);
48+
expect(markdownResult.startsWith('![CommitPulse Contribution Graph]')).toBe(true);
4549
expect(markdownResult).toContain(EXPECTED_BASE_URL);
4650

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

6266
expect(result).toContain(complexQuery);
63-
expect(result).toBe(`![CommitPulse](${EXPECTED_BASE_URL}?${complexQuery})`);
67+
expect(result).toBe(
68+
`![CommitPulse Contribution Graph for complex%20name](${EXPECTED_BASE_URL}?${complexQuery})`
69+
);
6470
});
6571

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

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

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 { CustomizeOptions, 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)