Skip to content

Commit

Permalink
feat: formatting in the makeFileName function
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Jun 18, 2022
1 parent 83626e7 commit 537f33f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class BookSearchPlugin extends Plugin {

async createNewBookNote(): Promise<void> {
await this.searchBookMetadata('', async (book, metadata) => {
const fileName = makeFileName(book);
const fileName = makeFileName(book, this.settings.fileNameFormat);
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
const targetFile = await this.app.vault.create(filePath, metadata);

Expand Down
2 changes: 1 addition & 1 deletion src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class BookSearchSettingTab extends PluginSettingTab {
cb.setPlaceholder('Example: {{title}} - {{author}}')
.setValue(this.plugin.settings.fileNameFormat)
.onChange(newValue => {
this.plugin.settings.fileNameFormat = newValue;
this.plugin.settings.fileNameFormat = newValue?.trim();
this.plugin.saveSettings();

newFileNameHintDescCode.innerHTML = replaceDateInString(newValue) || '{{title}} - {{author}}';
Expand Down
6 changes: 5 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { App } from 'obsidian';
import { Book, FrontMatter } from 'src/models/book.model';
import { DefaultFrontmatterKeyType } from 'src/settings/settings';

Expand All @@ -14,12 +15,15 @@ export function isISBN(str: string) {
return /^(97(8|9))?\d{9}(\d|X)$/.test(str);
}

export function makeFileName(book: Book) {
export function makeFileName(book: Book, fileNameFormat: string) {
const titleForFileName = replaceIllegalFileNameCharactersInString(book.title);
if (!book.author) {
return titleForFileName;
}
const authorForFileName = replaceIllegalFileNameCharactersInString(book.author);
if(fileNameFormat) {
return replaceVariableSyntax(book, replaceDateInString(fileNameFormat))
}
return `${titleForFileName} - ${authorForFileName}`;
}

Expand Down

0 comments on commit 537f33f

Please sign in to comment.