forked from bitwiseops/obsidian-kobo-highlights-import
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
190 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,3 @@ export default class KoboHighlightsImporter extends Plugin { | |
await this.saveData(this.settings); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// source: https://github.com/liamcain/obsidian-periodic-notes/blob/04965a1e03932d804f6dd42c2e5dba0ede010d79/src/ui/file-suggest.ts | ||
|
||
import { TAbstractFile, TFile } from "obsidian"; | ||
import { TextInputSuggest } from "./Suggest"; | ||
|
||
export class FileSuggestor extends TextInputSuggest<TFile> { | ||
getSuggestions(inputStr: string): TFile[] { | ||
const abstractFiles = this.app.vault.getAllLoadedFiles(); | ||
const files: TFile[] = []; | ||
const lowerCaseInputStr = inputStr.toLowerCase(); | ||
|
||
abstractFiles.forEach((file: TAbstractFile) => { | ||
if ( | ||
file instanceof TFile && | ||
file.extension === "md" && | ||
file.path.toLowerCase().contains(lowerCaseInputStr) | ||
) { | ||
files.push(file); | ||
} | ||
}); | ||
|
||
return files; | ||
} | ||
|
||
renderSuggestion(file: TFile, el: HTMLElement): void { | ||
el.setText(file.path); | ||
} | ||
|
||
selectSuggestion(file: TFile): void { | ||
this.inputEl.value = file.path; | ||
this.inputEl.trigger("input"); | ||
this.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as chai from 'chai'; | ||
import { applyTemplateTransformations, defaultTemplate } from './template'; | ||
|
||
|
||
describe('template', async function () { | ||
it('applyTemplateTransformations default', async function () { | ||
const content = applyTemplateTransformations(defaultTemplate, "test") | ||
|
||
chai.expect(content).deep.eq("test") | ||
}); | ||
|
||
const templates = new Map<string, string[]>([ | ||
[ | ||
"default", | ||
[defaultTemplate, "test"] | ||
], | ||
[ | ||
"with front matter", | ||
[ | ||
` | ||
--- | ||
tag: [tags] | ||
--- | ||
{{highlights}} | ||
`, | ||
`--- | ||
tag: [tags] | ||
--- | ||
test` | ||
] | ||
], | ||
]) | ||
|
||
for (const [title, t] of templates) { | ||
it(`applyTemplateTransformations ${title}`, async function () { | ||
const content = applyTemplateTransformations(t[0], "test") | ||
|
||
chai.expect(content).deep.eq(t[1]) | ||
}); | ||
} | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const defaultTemplate = ` | ||
{{highlights}} | ||
` | ||
|
||
export function applyTemplateTransformations(rawTemaple: string, highlights: string): string { | ||
return rawTemaple.replace( | ||
/{{\s*highlights\s*}}/gi, | ||
highlights, | ||
).trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Inspired by https://github.com/liamcain/obsidian-periodic-notes/blob/04965a1e03932d804f6dd42c2e5dba0ede010d79/src/utils.ts | ||
|
||
import { App, normalizePath, Notice } from "obsidian"; | ||
import { defaultTemplate } from "./template"; | ||
|
||
export async function getTemplateContents( | ||
app: App, | ||
templatePath: string | undefined | ||
): Promise<string> { | ||
const { metadataCache, vault } = app; | ||
const normalizedTemplatePath = normalizePath(templatePath ?? ""); | ||
if (normalizedTemplatePath === "/") { | ||
return defaultTemplate; | ||
} | ||
|
||
try { | ||
const templateFile = metadataCache.getFirstLinkpathDest(normalizedTemplatePath, ""); | ||
return templateFile ? vault.cachedRead(templateFile) : defaultTemplate; | ||
} catch (err) { | ||
console.error( | ||
`Failed to read the kobo highlight exporter template '${normalizedTemplatePath}'`, | ||
err | ||
); | ||
new Notice("Failed to read the kobo highlight exporter template"); | ||
|
||
return ""; | ||
} | ||
} |