Skip to content

Commit 212fbcc

Browse files
committed
refactor: ♻️ refactor title sanitazion into a separate function
we sanitize the title in two locations, hence a separate central functions makes sense.
1 parent 9dade12 commit 212fbcc

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/main.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export default class ReadwiseMirror extends Plugin {
202202
const { title, highlights } = book;
203203
const num_highlights = highlights.length;
204204
console.warn(`Readwise: Replacing colon with ${this.settings.colonSubstitute}`);
205-
const sanitizedTitle = title.replace(/:/g, this.settings.colonSubstitute ?? '-').replace(/[<>"'/\\|?*]+/g, '');
205+
const sanitizedTitle = this.sanitizeTitle(book.title);
206206
const contents = `\n- [[${sanitizedTitle}]] *(${num_highlights} highlights)*`;
207207
logString += contents;
208208
}
@@ -389,19 +389,7 @@ export default class ReadwiseMirror extends Plugin {
389389

390390

391391
// Sanitize title, replace colon with substitute from settings
392-
const sanitizedTitle = this.settings.useSlugify
393-
? slugify(title.replace(/:/g, this.settings.colonSubstitute ?? '-'), {
394-
separator: this.settings.slugifySeparator,
395-
lowercase: this.settings.slugifyLowercase,
396-
})
397-
: // ... else filenamify the title
398-
filenamify(title.replace(/:/g, this.settings.colonSubstitute ?? '-'), {
399-
replacement: ' ',
400-
maxLength: 255,
401-
}) // Ensure we remove additional critical characters, replace multiple spaces with one, and trim
402-
.replace(/[<>"'/\\|?*#]+/g, '')
403-
.replace(/ +/g, ' ')
404-
.trim();
392+
const sanitizedTitle = this.sanitizeTitle(title);
405393

406394
// Filter highlights
407395
const filteredHighlights = this.filterHighlights(highlights);
@@ -628,6 +616,25 @@ export default class ReadwiseMirror extends Plugin {
628616
}
629617
}
630618

619+
// Sanitize title for use as filename
620+
private sanitizeTitle(title: string) {
621+
return this.settings.useSlugify
622+
? slugify(title.replace(/:/g, this.settings.colonSubstitute ?? '-'), {
623+
separator: this.settings.slugifySeparator,
624+
lowercase: this.settings.slugifyLowercase,
625+
})
626+
: // ... else filenamify the title and limit to 255 characters
627+
filenamify(title.replace(/:/g, this.settings.colonSubstitute ?? '-') , {
628+
replacement: ' ',
629+
maxLength: 255,
630+
})
631+
// Ensure we remove additional critical characters, replace multiple spaces with one, and trim
632+
// Replace # as this inrerferes with WikiLinks (other characters are taken care of in "filenamify")
633+
.replace(/[#]+/g, ' ')
634+
.replace(/ +/g, ' ')
635+
.trim();
636+
}
637+
631638
async deleteLibraryFolder() {
632639
const vault = this.app.vault;
633640
const path = `${this.settings.baseFolderName}`;

0 commit comments

Comments
 (0)