moved to using a yaml lib instead of manually mangling the yaml

This commit is contained in:
Lena Brueder 2023-01-29 09:36:00 +01:00
parent 739d7bd448
commit a4597f9a63
5 changed files with 59 additions and 35 deletions

4
package-lock.json generated
View file

@ -9,7 +9,8 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"epub-cfi-resolver": "^1.0.1"
"epub-cfi-resolver": "^1.0.1",
"yaml": "^2.2.1"
},
"devDependencies": {
"@types/node": "^16.11.6",
@ -5030,7 +5031,6 @@
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.2.1.tgz",
"integrity": "sha512-e0WHiYql7+9wr4cWMx3TVQrNwejKaEe7/rHNmQmqRjazfOP5W8PB6Jpebb5o6fIapbz9o9+2ipcaTM2ZwDI6lw==",
"dev": true,
"engines": {
"node": ">= 14"
}

View file

@ -25,6 +25,7 @@
"typescript": "4.7.4"
},
"dependencies": {
"epub-cfi-resolver": "^1.0.1"
"epub-cfi-resolver": "^1.0.1",
"yaml": "^2.2.1"
}
}

View file

@ -19,6 +19,9 @@ export interface PocketbookCloudBook {
fast_hash: string;
path: string;
link: string;
created_at: Date;
read_status: string;
collections: string;
metadata: PocketbookCloudBookMetadata;
}
export interface PocketbookCloudNoteInfo {

View file

@ -1,6 +1,7 @@
import { App, Notice, TFile } from 'obsidian';
import { PocketbookCloudApiClient, PocketbookCloudLoginClient } from './apiclient';
import { PocketbookCloudHighlightsImporterPluginSettings } from './settings';
import { parse, stringify } from 'yaml';
var CFI = require('epub-cfi-resolver');
@ -34,17 +35,23 @@ export class PocketbookCloudHighlightsImporter {
//await this.writeFileBinary(cover_filename, await this.api_client.getBookCover(book));
// write metadata file, which should be used to get all highlights together
const book_yaml_frontmatter = {
title: book.title,
authors: book.metadata.authors,
isbn: book.metadata.isbn,
year: book.metadata.year,
id: book.id,
fast_hash: book.fast_hash,
collections: book.collections,
uploaded_at: book.created_at,
read_status: book.read_status,
type: 'book',
plugin: 'pocketbook-cloud-highlights-importer',
};
await this.writeFile(
metadata_filename,
`---
title: ${book.title}
authors: ${book.metadata.authors}
isbn: ${book.metadata.isbn}
year: ${book.metadata.year}
id: ${book.id}
fast_hash: ${book.fast_hash}
type: book
plugin: pocketbook-cloud-highlights-importer
${stringify(book_yaml_frontmatter)}
---
\`\`\`dataview
@ -55,6 +62,7 @@ SORT sort_order
`
);
//TODO: only create the CFI object once m)
try {
// if sorting works, fine. if not, also fine, using date then.
highlights.sort((a, b) => CFI.compare(this.cfi(a.quotation.begin), this.cfi(b.quotation.begin)));
@ -66,20 +74,24 @@ SORT sort_order
for (const highlight of highlights) {
i++;
const file_name = `${folder}/highlights/${highlight.uuid}.md`;
const highlight_yaml_frontmatter = {
id: highlight.uuid,
book_id: book.id,
book_fast_hash: book.fast_hash,
color: highlight.color?.value ?? 'unknown',
note: highlight.note?.text ?? '',
text: highlight.quotation?.text ?? '',
pointer: {
begin: highlight.quotation?.begin ?? '',
end: highlight.quotation?.end ?? '',
},
updated: highlight.quotation.updated,
type: 'highlight',
plugin: 'pocketbook-cloud-highlights-importer',
sort_order: i,
};
const content = `---
id: ${highlight.uuid}
book_id: ${book.id}
book_fast_hash: ${book.fast_hash}
color: ${highlight.color?.value ?? 'unknown'}
note: ${highlight.note?.text ?? ''}
text: ${highlight.quotation?.text ?? ''}
pointer:
begin: ${highlight.quotation?.begin ?? ''}
end: ${highlight.quotation?.end ?? ''}
updated: ${highlight.quotation.updated}
type: highlight
plugin: pocketbook-cloud-highlights-importer
sort_order: ${i}
${stringify(highlight_yaml_frontmatter)}
---
${highlight.quotation?.text ?? ''}

View file

@ -11,8 +11,12 @@
"id": "5a8f7af4213cc25f",
"type": "leaf",
"state": {
"type": "empty",
"state": {}
"type": "markdown",
"state": {
"file": "Highlights/From Conflict to Community/highlights/45cdb958-6059-48df-a76f-376046d03fe5.md",
"mode": "source",
"source": false
}
}
}
]
@ -81,6 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "Highlights/From Conflict to Community/highlights/45cdb958-6059-48df-a76f-376046d03fe5.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
@ -97,6 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "Highlights/From Conflict to Community/highlights/45cdb958-6059-48df-a76f-376046d03fe5.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
@ -118,7 +124,9 @@
"type": "leaf",
"state": {
"type": "outline",
"state": {}
"state": {
"file": "Highlights/From Conflict to Community/highlights/45cdb958-6059-48df-a76f-376046d03fe5.md"
}
}
}
]
@ -138,17 +146,17 @@
"command-palette:Open command palette": false
}
},
"active": "d252e907b82daee6",
"active": "5a8f7af4213cc25f",
"lastOpenFiles": [
"Highlights/From Conflict to Community/highlights/4213c9e0-2f8d-46e0-8ae0-e2fdb45cd590.md",
"Highlights/From Conflict to Community/highlights/218a7270-d3da-4eeb-ab10-019a80f46f8f.md",
"Highlights/From Conflict to Community/highlights/45cdb958-6059-48df-a76f-376046d03fe5.md",
"Highlights/Datenintensive Anwendungen designen/metadata.md",
"Highlights/From Conflict to Community/metadata.md",
"Pocketbook Cloud Highlights API.md",
"Testnote.md",
"Highlights/Reinventing Organizations A Guide to Creating Organizations Inspired by the Next Stage of Human Consciousness/metadata.md",
"Highlights/No Rules Rules/metadata.md",
"Highlights/Good StrategyBad Strategy/metadata.md",
"Highlights/Getting Things Done The Art of Stress-free Productivity/metadata.md",
"Highlights/Escaping the Build Trap/metadata.md",
"Highlights/Befriend Your Brain/metadata.md",
"Highlights/Escaping the Build Trap/highlights/6C527597-BECA-5EE3-9F72-ACFBBBCA7FE2.md",
"Highlights/Befriend Your Brain/highlights/FA4CBDE2-C8C7-5700-B4BA-4BBB1E7557D2.md",
"Highlights/Befriend Your Brain/highlights/C7A84480-2246-5AF0-B6AA-2218FD312B38.md"
"Highlights/Good StrategyBad Strategy/metadata.md"
]
}