Fix formating authors

This commit is contained in:
chrox
2024-12-11 19:28:54 +01:00
parent 3f4a274412
commit 89c6c4d971
2 changed files with 22 additions and 4 deletions
+1 -1
View File
@@ -136,7 +136,7 @@ export abstract class BaseAppService implements AppService {
hash,
format,
title: formatTitle(loadedBook.metadata.title),
author: formatAuthors(loadedBook.metadata.author),
author: formatAuthors(loadedBook.metadata.language, loadedBook.metadata.author),
lastUpdated: Date.now(),
};
if (!(await this.fs.exists(getDir(book), 'Books'))) {
+21 -3
View File
@@ -46,11 +46,29 @@ const formatLanguageMap = (x: string | LanguageMap): string => {
return x[userLang] || x[keys[0]!]!;
};
const listFormat = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
const listFormat = (lang: string) => {
if (lang === 'zh') {
return new Intl.ListFormat('en', { style: 'narrow', type: 'unit' });
} else {
return new Intl.ListFormat(lang, { style: 'long', type: 'conjunction' });
}
};
export const formatAuthors = (contributors: string | Contributor | [string | Contributor]) =>
const getBookLangCode = (lang: string | string[] | undefined) => {
try {
const bookLang = typeof lang === 'string' ? lang : lang?.[0];
return bookLang ? bookLang.split('-')[0]! : 'en';
} catch {
return 'en';
}
};
export const formatAuthors = (
bookLang: string | string[] | undefined,
contributors: string | Contributor | [string | Contributor],
) =>
Array.isArray(contributors)
? listFormat.format(
? listFormat(getBookLangCode(bookLang)).format(
contributors.map((contributor) =>
typeof contributor === 'string' ? contributor : formatLanguageMap(contributor?.name),
),