From a0cd9e6c814b8adf02d7d2f68a5d78bb4635cd55 Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 1 Mar 2024 00:46:43 +0200 Subject: [PATCH] fix: ensure bookTitle does not contain forbidden symbols in frontmatter. (#6) Update default template to pass unescaped content. --- README.md | 17 +++++++++++------ main.ts | 14 ++++++++------ manifest.json | 2 +- package.json | 2 +- src/template.ts | 12 ++++++------ versions.json | 3 ++- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 7bcb95a..cbad3dc 100644 --- a/README.md +++ b/README.md @@ -39,16 +39,21 @@ Check Obsidian Help for more information about [Community plugins](https://help. ## Template variables -- `{{bookTitle}}` - The title of the book. +- `{{{bookTitle}}}` - The title of the book. - `{{bookId}}` - A unique identifier of the book. It is used to create a link to the book in Apple Books: `[Apple Books Link](ibooks://assetid/{{bookId}})`. -- `{{bookAuthor}}` - The author of the book. +- `{{{bookAuthor}}}` - The author of the book. - `{{annotations}}` - An array of all the annotations in the book. You can use `{{annotations.length}}` to get the total number of annotations you made in the book. Each annotation has the following properties: - - `{{chapter}}` - The chapter of the highlight in the book. It may not be available for all highlights due to the initial formatting of the book. - - `{{contextualText}}` - The text surrounding the highlight to give you more context. For example: + - `{{{chapter}}}` - The chapter of the highlight in the book. It may not be available for all highlights due to the initial formatting of the book. + - `{{{contextualText}}}` - The text surrounding the highlight to give you more context. For example: - If you highlight a part of a sentence, the - `contextualText` will contain the whole sentence. - If you highlight parts of two adjacent sentences, the `contextualText` will contain both sentences. - - `{{highlight}}` - The highlighted text. - - `{{note}}` - A note you added for the highlight. + - `{{{highlight}}}` - The highlighted text. + - `{{{note}}}` - A note you added for the highlight. + +> [!NOTE] +> When customizing the template, make sure to wrap variables with triple curly braces (`{{{variable}}}`) to avoid escaping the HTML characters in Markdown files (default behavior). +> +> If you want escaped output, use double curly braces: `{{variable}}`. ## Contributing diff --git a/main.ts b/main.ts index f701db2..8703342 100644 --- a/main.ts +++ b/main.ts @@ -108,8 +108,12 @@ export default class IBookHighlightsPlugin extends Plugin { const bookRelatedAnnotations: IBookAnnotation[] = annotations.filter(annotation => annotation.ZANNOTATIONASSETID === book.ZASSETID); if (bookRelatedAnnotations.length > 0) { + // Obsidian forbids adding certain characters to the title of a note, so they must be replaced with a dash (-) + // | # ^ [] \ / : + const normalizedBookTitle = book.ZTITLE.replace(/[|#\^\[\]\\\/:]+/g, ' -'); + highlights.push({ - bookTitle: book.ZTITLE, + bookTitle: normalizedBookTitle, bookId: book.ZASSETID, bookAuthor: book.ZAUTHOR, annotations: bookRelatedAnnotations.map(annotation => { @@ -147,7 +151,7 @@ export default class IBookHighlightsPlugin extends Plugin { await this.app.vault.createFolder(highlightsBackupFolder); - highlightsFilesToBackup.forEach(async (file: any) => { + highlightsFilesToBackup.forEach(async (file: string) => { const fileName = path.basename(file); await this.app.vault.adapter.copy(normalizePath(file), normalizePath(path.join(highlightsBackupFolder, fileName))); }); @@ -157,14 +161,12 @@ export default class IBookHighlightsPlugin extends Plugin { await this.app.vault.createFolder(this.settings.highlightsFolder); - highlights.forEach(async (highlight: any) => { - const bookFileName = highlight.bookTitle.replace(/:/g, ' -'); - + highlights.forEach(async (highlight: CombinedHighlight) => { const template = Handlebars.compile(this.settings.template); const renderedTemplate = template(highlight); await this.app.vault.create( - normalizePath(path.join(this.settings.highlightsFolder, `${bookFileName}.md`)), + normalizePath(path.join(this.settings.highlightsFolder, `${highlight.bookTitle}.md`)), renderedTemplate ); }); diff --git a/manifest.json b/manifest.json index 9a27b6c..818fd5d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "apple-books-import-highlights", "name": "Apple Books - Import Highlights", - "version": "1.1.0", + "version": "1.1.1", "minAppVersion": "0.15.0", "description": "Import your Apple Books highlights and notes to Obsidian.", "author": "bandantonio", diff --git a/package.json b/package.json index 444eb7c..8bb60fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-apple-books-highlights-plugin", - "version": "1.1.0", + "version": "1.1.1", "description": "Import highlights and notes from your Apple Books to Obsidian", "main": "main.js", "scripts": { diff --git a/src/template.ts b/src/template.ts index 8470f09..91df0b6 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,5 +1,5 @@ -const defaultTemplate = `Title:: 📕 {{bookTitle}} -Author:: {{bookAuthor}} +const defaultTemplate = `Title:: 📕 {{{bookTitle}}} +Author:: {{{bookAuthor}}} Link:: [Apple Books Link](ibooks://assetid/{{bookId}}) ## Annotations @@ -9,10 +9,10 @@ Number of annotations:: {{annotations.length}} {{#each annotations}} ---- -- 📖 Chapter:: {{#if chapter}}{{chapter}}{{else}}N/A{{/if}} -- 🔖 Context:: {{#if contextualText}}{{contextualText}}{{else}}N/A{{/if}} -- 🎯 Highlight:: {{highlight}} -- 📝 Note:: {{#if note}}{{note}}{{else}}N/A{{/if}} +- 📖 Chapter:: {{#if chapter}}{{{chapter}}}{{else}}N/A{{/if}} +- 🔖 Context:: {{#if contextualText}}{{{contextualText}}}{{else}}N/A{{/if}} +- 🎯 Highlight:: {{{highlight}}} +- 📝 Note:: {{#if note}}{{{note}}}{{else}}N/A{{/if}} {{/each}} `; diff --git a/versions.json b/versions.json index db543dd..4c14266 100644 --- a/versions.json +++ b/versions.json @@ -2,5 +2,6 @@ "1.0.0": "0.15.0", "1.0.1": "0.15.0", "1.0.2": "0.15.0", - "1.1.0": "0.15.0" + "1.1.0": "0.15.0", + "1.1.1": "0.15.0" }