Skip to content

Commit

Permalink
fix: ensure bookTitle does not contain forbidden symbols in frontmatt…
Browse files Browse the repository at this point in the history
…er. (#6)

Update default template to pass unescaped content.
  • Loading branch information
bandantonio authored Feb 29, 2024
1 parent 878de2c commit a0cd9e6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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)));
});
Expand All @@ -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
);
});
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 6 additions & 6 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const defaultTemplate = `Title:: 📕 {{bookTitle}}
Author:: {{bookAuthor}}
const defaultTemplate = `Title:: 📕 {{{bookTitle}}}
Author:: {{{bookAuthor}}}
Link:: [Apple Books Link](ibooks://assetid/{{bookId}})
## Annotations
Expand All @@ -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}}
`;
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit a0cd9e6

Please sign in to comment.