Format title and authors when importing books

This commit is contained in:
chrox
2024-12-10 14:14:06 +01:00
parent d940fb8d06
commit 6efdbb78e5
3 changed files with 12 additions and 5 deletions
@@ -1,7 +1,6 @@
import React from 'react';
import Image from 'next/image';
import { MdInfoOutline } from 'react-icons/md';
import { formatAuthors } from '@/utils/book';
interface BookCardProps {
cover: string;
@@ -24,7 +23,7 @@ const BookCard: React.FC<BookCardProps> = ({ cover, title, author }) => {
/>
<div className='min-w-0 flex-1'>
<h4 className='line-clamp-2 w-[90%] text-sm font-semibold'>{title}</h4>
<p className='text-neutral-content truncate text-xs'>{formatAuthors(author)}</p>
<p className='text-neutral-content truncate text-xs'>{author}</p>
</div>
<button
className='btn btn-ghost hover:bg-base-300 h-6 min-h-6 w-6 rounded-full p-0 transition-colors'
+4 -2
View File
@@ -11,6 +11,8 @@ import {
getConfigFilename,
getLibraryFilename,
INIT_BOOK_CONFIG,
formatTitle,
formatAuthors,
} from '@/utils/book';
import { RemoteFile } from '@/utils/file';
import { partialMD5 } from '@/utils/md5';
@@ -130,8 +132,8 @@ export abstract class BaseAppService implements AppService {
const book: Book = {
hash,
format,
title: loadedBook.metadata.title,
author: loadedBook.metadata.author,
title: formatTitle(loadedBook.metadata.title),
author: formatAuthors(loadedBook.metadata.author),
lastUpdated: Date.now(),
};
if (!(await this.fs.exists(getDir(book), 'Books'))) {
+7 -1
View File
@@ -37,11 +37,13 @@ interface Contributor {
name: LanguageMap;
}
const userLang = navigator?.language || 'en';
const formatLanguageMap = (x: string | LanguageMap): string => {
if (!x) return '';
if (typeof x === 'string') return x;
const keys = Object.keys(x);
return x[keys[0]!]!;
return x[userLang] || x[keys[0]!]!;
};
const listFormat = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
@@ -56,3 +58,7 @@ export const formatAuthors = (contributors: string | Contributor | [string | Con
: typeof contributors === 'string'
? contributors
: formatLanguageMap(contributors?.name);
export const formatTitle = (title: string | LanguageMap) => {
return typeof title === 'string' ? title : formatLanguageMap(title);
};