feat(naming): add {AuthorSort} template variable (#1484)

Closes #1178.

Adds `{AuthorSort}`, the first author surname-first ("Arthur Conan
Doyle" → "Doyle, Arthur Conan"), for rename and organize templates,
alongside `{FirstAuthor}`.

- A value already in "Last, First" form (one word before the comma) is
kept as is; Jr/Sr/II/III/IV stay at the end.
- Known limits, same as calibre's default: the last word is treated as
the surname, so particles ("Le Guin") and surname-first names aren't
detected.
- Added to the settings help, env docs, the frontend variable
picker/preview, and tests.
This commit is contained in:
Amalkrishna A S authored and GitHub committed 2026-10-20 01:22:05 -04:00
1 parent b83e35ee0a
commit b38e4e4a00
7 files changed
+185 -13

No files matched your search

@@ -8,6 +8,13 @@ import {
SAMPLE_NAMING_METADATA,
} from '../utils/namingTemplatePreview';
const renderAuthorSort = (author: string): string =>
renderNamingTemplate(
'{AuthorSort}/{Year}',
{ ...SAMPLE_NAMING_METADATA, Author: author, AuthorSort: '' },
{ allowPathSeparators: true },
).value;
describe('namingTemplatePreview', () => {
it('renders Audiobookshelf-style {{Narrator}} and drops it cleanly when empty', () => {
const template = '{Author}/{Title} {{Narrator}}/{Title}';
@@ -69,6 +76,21 @@ describe('namingTemplatePreview', () => {
expect(preview.value).toBe('Terry Pratchett/1902');
});
it('renders AuthorSort surname first from the first author', () => {
expect(renderAuthorSort('Arthur Conan Doyle')).toBe('Doyle, Arthur Conan/1902');
expect(renderAuthorSort('Terry Pratchett, Neil Gaiman')).toBe('Pratchett, Terry/1902');
expect(renderAuthorSort('Doyle, Arthur Conan')).toBe('Doyle, Arthur Conan/1902');
// The trailing dot of "Jr." is stripped like any other trailing dot in a path component.
expect(renderAuthorSort('Martin Luther King Jr.')).toBe('King, Martin Luther, Jr/1902');
});
it('offers AuthorSort as a core variable', () => {
const token = NAMING_TEMPLATE_TOKENS.find((t) => t.token === 'AuthorSort');
expect(token?.group).toBe('Core');
expect(token?.audiobookOnly).toBeFalsy();
});
it('offers FirstAuthor as a core variable', () => {
const token = NAMING_TEMPLATE_TOKENS.find((t) => t.token === 'FirstAuthor');
@@ -36,6 +36,13 @@ export const NAMING_TEMPLATE_TOKENS: NamingTemplateToken[] = [
value: 'Arthur Conan Doyle',
group: 'Core',
},
{
token: 'AuthorSort',
label: 'Author (surname first)',
description: 'First author as "Last, First"',
value: 'Doyle, Arthur Conan',
group: 'Core',
},
{
token: 'Title',
label: 'Full title',
@@ -123,6 +130,7 @@ const KNOWN_TOKENS = [
'primarytitle',
'originalname',
'firstauthor',
'authorsort',
'partnumber',
'language',
'narrator',
@@ -138,6 +146,35 @@ const KNOWN_TOKENS = [
// pre-joined with ',' or ';' and {FirstAuthor} keeps only the first entry.
const firstAuthor = (value: string): string => value.split(/\s*[,;]\s*/)[0]?.trim() ?? '';
// Mirrors author_sort() in shelfmark/core/naming.py.
const NAME_SUFFIXES = new Set(['jr', 'jr.', 'sr', 'sr.', 'ii', 'iii', 'iv']);
const authorSort = (value: string): string => {
const text = value.split(/\s+/).filter(Boolean).join(' ');
const entries = text.split(/\s*[,;]\s*/).filter(Boolean);
const [first, second] = entries;
if (!first) {
return '';
}
if (entries.length === 2 && second && !first.includes(' ') && !text.includes(';')) {
return `${first}, ${second}`;
}
const words = first.split(' ');
let suffix = '';
if (second && NAME_SUFFIXES.has(second.toLowerCase())) {
suffix = second;
} else if (words.length > 2 && NAME_SUFFIXES.has(words[words.length - 1].toLowerCase())) {
suffix = words.pop() ?? '';
}
if (words.length < 2) {
return first;
}
const sorted = `${words[words.length - 1]}, ${words.slice(0, -1).join(' ')}`;
return suffix ? `${sorted}, ${suffix}` : sorted;
};
const BRACE_PATTERN = /\{([^}]+)\}/g;
const INVALID_CHARS_PATTERN = /[\\/:*?"<>|]/g;
const WHITESPACE_RUN_PATTERN = /\s+/g;
@@ -191,6 +228,9 @@ export const renderNamingTemplate = (
if (placeholderName === 'firstauthor' && !normalized['firstauthor']) {
return firstAuthor(normalized['author'] ?? '');
}
if (placeholderName === 'authorsort' && !normalized['authorsort']) {
return authorSort(normalized['author'] ?? '');
}
return (normalized[placeholderName] ?? '').trim();
};