Skip to content

Commit 5c5085f

Browse files
committed
style(Authors): format as "First Last"
Address #179 Updated the Authors.astro component with: Added capitalizeNamePart() helper - Ensures proper capitalization (first letter uppercase, rest lowercase) Added formatAuthorName() helper - Intelligently converts "Last, First" format to "First Last": - Detects the comma separator - Splits on comma - Reverses the order - Applies proper capitalization to each part - Falls back to simple capitalization for non-comma names - Updated getAuthorName() function - Now uses the new formatting logic for all name types: - Structured name objects (given/family) - String names - Literal name objects
1 parent 529b483 commit 5c5085f

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

src/components/Authors.astro

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,61 @@ interface Props {
1818
1919
const { authors, affiliations } = Astro.props;
2020
21-
// Helper function to get author name as string
21+
// Helper function to capitalize the first letter of a name part
22+
function capitalizeNamePart(namePart: string): string {
23+
if (!namePart || namePart.length === 0) return namePart;
24+
return namePart.charAt(0).toUpperCase() + namePart.slice(1).toLowerCase();
25+
}
26+
27+
// Helper function to format and capitalize an author name
28+
// Handles "Last, First" format by converting to "First Last"
29+
function formatAuthorName(name: string): string {
30+
if (!name || name.length === 0) return name;
31+
32+
// Check if name is in "Last, First" format (contains comma)
33+
if (name.includes(',')) {
34+
const parts = name.split(',').map(p => p.trim());
35+
if (parts.length === 2) {
36+
// Reverse to "First Last" format
37+
const firstName = parts[1];
38+
const lastName = parts[0];
39+
const formattedFirst = firstName
40+
.split(/\s+/)
41+
.map(p => capitalizeNamePart(p))
42+
.join(' ');
43+
const formattedLast = lastName
44+
.split(/\s+/)
45+
.map(p => capitalizeNamePart(p))
46+
.join(' ');
47+
return `${formattedFirst} ${formattedLast}`;
48+
}
49+
}
50+
51+
// Otherwise, just capitalize each word part
52+
return name
53+
.split(/\s+/)
54+
.map(part => capitalizeNamePart(part))
55+
.join(' ');
56+
}
57+
58+
// Helper function to get author name as string with proper formatting and capitalization
2259
function getAuthorName(author: Contributor): string {
2360
if (!author.name) return 'Unknown Author';
2461
2562
if (typeof author.name === 'object' && typeof author.name.given === 'string' && typeof author.name.family === 'string') {
2663
const parts: string[] = [];
27-
if (author.name.given) parts.push(author.name.given);
28-
if (author.name.family) parts.push(author.name.family);
64+
// Capitalize each name part properly
65+
if (author.name.given) parts.push(capitalizeNamePart(author.name.given));
66+
if (author.name.family) parts.push(capitalizeNamePart(author.name.family));
2967
if (parts.length > 0) return parts.join(' ');
3068
}
31-
// If name is a string, return it directly
32-
if (typeof author.name === 'string') return author.name;
33-
// If name is an object, use the literal property
69+
// If name is a string, format and capitalize it
70+
if (typeof author.name === 'string') {
71+
return formatAuthorName(author.name);
72+
}
73+
// If name is an object, use the literal property and format
3474
if (typeof author.name === 'object' && author.name.literal) {
35-
return author.name.literal;
75+
return formatAuthorName(author.name.literal);
3676
}
3777
return 'Unknown Author';
3878
}

0 commit comments

Comments
 (0)