mirror of
https://github.com/shadowoption/Hunchly-obsidian-plugin.git
synced 2026-07-22 08:50:25 +00:00
Initial commit
This commit is contained in:
parent
cc1ed38ea3
commit
4c12762f43
9 changed files with 2831 additions and 231 deletions
98
README.md
98
README.md
|
|
@ -1,96 +1,14 @@
|
|||
# Obsidian Sample Plugin
|
||||
# Hunchly Obsidian Sample Plugin
|
||||
|
||||
This is a sample plugin for Obsidian (https://obsidian.md).
|
||||
This is an obsidian plugin to convert Hunchly notes and captioned images in obsidian notes. Also adds the selector as tags.
|
||||
|
||||
This project uses Typescript to provide type checking and documentation.
|
||||
The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does.
|
||||
|
||||
**Note:** The Obsidian API is still in early alpha and is subject to change at any time!
|
||||
|
||||
This sample plugin demonstrates some of the basic functionality the plugin API can do.
|
||||
- Adds a ribbon icon, which shows a Notice when clicked.
|
||||
- Adds a command "Open Sample Modal" which opens a Modal.
|
||||
- Adds a plugin setting tab to the settings page.
|
||||
- Registers a global click event and output 'click' to the console.
|
||||
- Registers a global interval which logs 'setInterval' to the console.
|
||||
|
||||
## First time developing plugins?
|
||||
|
||||
Quick starting guide for new plugin devs:
|
||||
|
||||
- Check if [someone already developed a plugin for what you want](https://obsidian.md/plugins)! There might be an existing plugin similar enough that you can partner up with.
|
||||
- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it).
|
||||
- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder.
|
||||
- Install NodeJS, then run `npm i` in the command line under your repo folder.
|
||||
- Run `npm run dev` to compile your plugin from `main.ts` to `main.js`.
|
||||
- Make changes to `main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`.
|
||||
- Reload Obsidian to load the new version of your plugin.
|
||||
- Enable plugin in settings window.
|
||||
- For updates to the Obsidian API run `npm update` in the command line under your repo folder.
|
||||
|
||||
## Releasing new releases
|
||||
|
||||
- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
|
||||
- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible.
|
||||
- Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases
|
||||
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. Note: The manifest.json file must be in two places, first the root path of your repository and also in the release.
|
||||
- Publish the release.
|
||||
|
||||
> You can simplify the version bump process by running `npm version patch`, `npm version minor` or `npm version major` after updating `minAppVersion` manually in `manifest.json`.
|
||||
> The command will bump version in `manifest.json` and `package.json`, and add the entry for the new version to `versions.json`
|
||||
|
||||
## Adding your plugin to the community plugin list
|
||||
|
||||
- Check https://github.com/obsidianmd/obsidian-releases/blob/master/plugin-review.md
|
||||
- Publish an initial version.
|
||||
- Make sure you have a `README.md` file in the root of your repo.
|
||||
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.
|
||||
Implementing using obsidian sample plugin template https://github.com/obsidianmd/obsidian-sample-plugin.git
|
||||
|
||||
## How to use
|
||||
|
||||
- Clone this repo.
|
||||
- Make sure your NodeJS is at least v16 (`node --version`).
|
||||
- `npm i` or `yarn` to install dependencies.
|
||||
- `npm run dev` to start compilation in watch mode.
|
||||
Click on the **H** ribbon icon on Obsidian.
|
||||
|
||||
## Manually installing the plugin
|
||||
|
||||
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
|
||||
|
||||
## Improve code quality with eslint (optional)
|
||||
- [ESLint](https://eslint.org/) is a tool that analyzes your code to quickly find problems. You can run ESLint against your plugin to find common bugs and ways to improve your code.
|
||||
- To use eslint with this project, make sure to install eslint from terminal:
|
||||
- `npm install -g eslint`
|
||||
- To use eslint to analyze this project use this command:
|
||||
- `eslint main.ts`
|
||||
- eslint will then create a report with suggestions for code improvement by file and line number.
|
||||
- If your source code is in a folder, such as `src`, you can use eslint with this command to analyze all files in that folder:
|
||||
- `eslint .\src\`
|
||||
|
||||
## Funding URL
|
||||
|
||||
You can include funding URLs where people who use your plugin can financially support it.
|
||||
|
||||
The simple way is to set the `fundingUrl` field to your link in your `manifest.json` file:
|
||||
|
||||
```json
|
||||
{
|
||||
"fundingUrl": "https://buymeacoffee.com"
|
||||
}
|
||||
```
|
||||
|
||||
If you have multiple URLs, you can also do:
|
||||
|
||||
```json
|
||||
{
|
||||
"fundingUrl": {
|
||||
"Buy Me a Coffee": "https://buymeacoffee.com",
|
||||
"GitHub Sponsor": "https://github.com/sponsors",
|
||||
"Patreon": "https://www.patreon.com/"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## API Documentation
|
||||
|
||||
See https://github.com/obsidianmd/obsidian-api
|
||||
In the modal,
|
||||
- Enter the location to store the notes. This is relative to the vault's root path. If empty the notes get added to vault root
|
||||
- Option to consolidate notes based on the same URL. Hunchly notes take on the same url will be consolidate in one obsidian note
|
||||
- Select the path where the hunchly export case file resides (in zip format). Usually obtained by using the `Export Case` in the Hunchly Dashboard.
|
||||
|
|
@ -15,7 +15,7 @@ const context = await esbuild.context({
|
|||
banner: {
|
||||
js: banner,
|
||||
},
|
||||
entryPoints: ["main.ts"],
|
||||
entryPoints: ["src/main.ts"],
|
||||
bundle: true,
|
||||
external: [
|
||||
"obsidian",
|
||||
|
|
|
|||
134
main.ts
134
main.ts
|
|
@ -1,134 +0,0 @@
|
|||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
interface MyPluginSettings {
|
||||
mySetting: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: MyPluginSettings = {
|
||||
mySetting: 'default'
|
||||
}
|
||||
|
||||
export default class MyPlugin extends Plugin {
|
||||
settings: MyPluginSettings;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
// This creates an icon in the left ribbon.
|
||||
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
new Notice('This is a notice!');
|
||||
});
|
||||
// Perform additional things with the ribbon
|
||||
ribbonIconEl.addClass('my-plugin-ribbon-class');
|
||||
|
||||
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||
const statusBarItemEl = this.addStatusBarItem();
|
||||
statusBarItemEl.setText('Status Bar Text');
|
||||
|
||||
// This adds a simple command that can be triggered anywhere
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-simple',
|
||||
name: 'Open sample modal (simple)',
|
||||
callback: () => {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
});
|
||||
// This adds an editor command that can perform some operation on the current editor instance
|
||||
this.addCommand({
|
||||
id: 'sample-editor-command',
|
||||
name: 'Sample editor command',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
console.log(editor.getSelection());
|
||||
editor.replaceSelection('Sample Editor Command');
|
||||
}
|
||||
});
|
||||
// This adds a complex command that can check whether the current state of the app allows execution of the command
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-complex',
|
||||
name: 'Open sample modal (complex)',
|
||||
checkCallback: (checking: boolean) => {
|
||||
// Conditions to check
|
||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
// If checking is true, we're simply "checking" if the command can be run.
|
||||
// If checking is false, then we want to actually perform the operation.
|
||||
if (!checking) {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
|
||||
// This command will only show up in Command Palette when the check function returns true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
||||
|
||||
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
|
||||
// Using this function will automatically remove the event listener when this plugin is disabled.
|
||||
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
||||
console.log('click', evt);
|
||||
});
|
||||
|
||||
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
|
||||
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
|
||||
}
|
||||
|
||||
onunload() {
|
||||
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
}
|
||||
|
||||
class SampleModal extends Modal {
|
||||
constructor(app: App) {
|
||||
super(app);
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
const {contentEl} = this;
|
||||
contentEl.setText('Woah!');
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const {contentEl} = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
|
||||
class SampleSettingTab extends PluginSettingTab {
|
||||
plugin: MyPlugin;
|
||||
|
||||
constructor(app: App, plugin: MyPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Setting #1')
|
||||
.setDesc('It\'s a secret')
|
||||
.addText(text => text
|
||||
.setPlaceholder('Enter your secret')
|
||||
.setValue(this.plugin.settings.mySetting)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.mySetting = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
{
|
||||
"id": "sample-plugin",
|
||||
"name": "Sample Plugin",
|
||||
"id": "hunchly-obsidian-plugin",
|
||||
"name": "Hunchly Obsidian Plugin",
|
||||
"version": "1.0.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
|
||||
"author": "Obsidian",
|
||||
"authorUrl": "https://obsidian.md",
|
||||
"fundingUrl": "https://obsidian.md/pricing",
|
||||
"description": "This is a plugin to import Hunchly Notes and images",
|
||||
"author": "Divya",
|
||||
"isDesktopOnly": false
|
||||
}
|
||||
|
|
|
|||
2281
package-lock.json
generated
Normal file
2281
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -13,6 +13,8 @@
|
|||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.6",
|
||||
"@types/tmp": "^0.2.5",
|
||||
"@types/unzipper": "^0.10.8",
|
||||
"@typescript-eslint/eslint-plugin": "5.29.0",
|
||||
"@typescript-eslint/parser": "5.29.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
|
|
@ -20,5 +22,11 @@
|
|||
"obsidian": "latest",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"csv-parser": "^3.0.0",
|
||||
"eslint": "^8.53.0",
|
||||
"tmp": "^0.2.1",
|
||||
"unzipper": "^0.10.14"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
59
src/fileModal.ts
Normal file
59
src/fileModal.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { App, Modal, Setting } from "obsidian";
|
||||
const {dialog} = require('electron').remote;
|
||||
export class FileModal extends Modal {
|
||||
result: { [key: string]: string }= {"location": "", "consolidate": "", "notepath": ""};
|
||||
inputString: string;
|
||||
onSubmit: (result: { [key: string]: string }) => void;
|
||||
|
||||
constructor(app: App, inputString: string, onSubmit: (result: { [key: string]: string }) => void) {
|
||||
super(app);
|
||||
this.inputString = inputString
|
||||
this.onSubmit = onSubmit;
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
const { contentEl } = this;
|
||||
|
||||
contentEl.createEl("h2", {text: "Import Hunchly Notes and Captioned Images"});
|
||||
|
||||
new Setting(contentEl)
|
||||
.setName("Notes location relative to the vault (if empty, notes get added to vault root).")
|
||||
.addText((text) =>
|
||||
text.onChange((value) => {
|
||||
this.result.location = value
|
||||
}));
|
||||
|
||||
new Setting(contentEl)
|
||||
.setName("Do you want to consolidate notes by url?")
|
||||
.addToggle((toggle) =>
|
||||
toggle.onChange((value) => {
|
||||
if (value){
|
||||
this.result.consolidate = "true"
|
||||
}else {
|
||||
this.result.consolidate = "false"
|
||||
}
|
||||
}));
|
||||
new Setting(contentEl)
|
||||
.setName(this.inputString)
|
||||
.addButton((btn) =>
|
||||
btn
|
||||
.setButtonText("Select")
|
||||
.setCta()
|
||||
.onClick(() => {
|
||||
dialog.showOpenDialog({properties: ["openDirectory","openFile"]}, function (fileNames: any) {
|
||||
return fileNames
|
||||
}).then((fileNames: any) => {
|
||||
this.result.notepath = fileNames.filePaths[0]
|
||||
this.close();
|
||||
this.onSubmit(this.result);
|
||||
});
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
93
src/main.ts
Normal file
93
src/main.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import { Notice, Plugin, PluginSettingTab, App, Setting, addIcon } from 'obsidian';
|
||||
import { FileModal } from './fileModal';
|
||||
import { Hunchly } from './processHunchly';
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
interface HunchlyObsidianPluginSettings {
|
||||
mySetting: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: HunchlyObsidianPluginSettings = {
|
||||
mySetting: 'default'
|
||||
}
|
||||
|
||||
export default class HunchlyObsidianPlugin extends Plugin {
|
||||
settings: HunchlyObsidianPluginSettings;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
//https://en.wikipedia.org/wiki/File:Eo_circle_blue_white_letter-h.svg creative common license
|
||||
addIcon("hunchly", `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" enable-background="new 0 0 64 64"><circle cx="32" cy="32" r="30" fill="#fff"/><path d="M32,2C15.432,2,2,15.432,2,32s13.432,30,30,30s30-13.432,30-30S48.568,2,32,2z M43.664,46.508h-6.023V33.555H26.361v12.953
|
||||
h-6.025V17.492h6.025v11.063h11.279V17.492h6.023V46.508z" fill="#1e88e5"/></svg>`);
|
||||
// This creates an icon in the left ribbon.
|
||||
const ribbonIconEl = this.addRibbonIcon('hunchly', 'Hunchly Obsidian Plugin', (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
new FileModal(this.app, "Select the exported hunchly case file (zip format)", (result) => {
|
||||
new Notice('Processing the hunchly notes and images in path ' + result.notepath, 5000);
|
||||
const hunchly = new Hunchly(result.notepath, result.location, result.consolidate, this)
|
||||
hunchly.process()
|
||||
}).open();
|
||||
|
||||
});
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
||||
|
||||
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
|
||||
// Using this function will automatically remove the event listener when this plugin is disabled.
|
||||
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
||||
console.log('click', evt);
|
||||
});
|
||||
|
||||
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
|
||||
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
|
||||
|
||||
// //link hotes
|
||||
// this.addCommand({
|
||||
// id: "link-notes",
|
||||
// name: "link-notes",
|
||||
// callback: () => {
|
||||
// console.log("Hey, you!");
|
||||
// },
|
||||
// });
|
||||
}
|
||||
|
||||
onunload() {
|
||||
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
}
|
||||
|
||||
class SampleSettingTab extends PluginSettingTab {
|
||||
plugin: HunchlyObsidianPlugin;
|
||||
|
||||
constructor(app: App, plugin: HunchlyObsidianPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Setting #1')
|
||||
.setDesc('It\'s a secret')
|
||||
.addText(text => text
|
||||
.setPlaceholder('Enter your secret')
|
||||
.setValue(this.plugin.settings.mySetting)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.mySetting = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
}
|
||||
}
|
||||
377
src/processHunchly.ts
Normal file
377
src/processHunchly.ts
Normal file
|
|
@ -0,0 +1,377 @@
|
|||
import * as fs_promise from 'fs/promises';
|
||||
import * as fs from 'fs';
|
||||
import { FileSystemAdapter, Plugin, Vault } from 'obsidian';
|
||||
import * as path from 'path'
|
||||
import * as unzipper from 'unzipper';
|
||||
import * as tmp from 'tmp';
|
||||
|
||||
interface IPage {
|
||||
title: string;
|
||||
url: string;
|
||||
date: string;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
interface INote {
|
||||
note: string;
|
||||
date: string;
|
||||
pageid: number;
|
||||
}
|
||||
|
||||
interface IPhoto {
|
||||
caption: string;
|
||||
date: string;
|
||||
pageid: number;
|
||||
photourl: string;
|
||||
photohash:string;
|
||||
photopath:string;
|
||||
}
|
||||
|
||||
export class Hunchly{
|
||||
hunchlyExportPath: string;
|
||||
vaultPath: string;
|
||||
plugin: Plugin
|
||||
vault: Vault
|
||||
hunchlyLocation: string
|
||||
consolidate: boolean
|
||||
|
||||
pages: Map<string, IPage>
|
||||
notes: Map<string, INote>
|
||||
selector: Map<number, string>
|
||||
selectorHits: Map<number, number[]>
|
||||
taggedPhotos: Map<string, IPhoto>
|
||||
|
||||
constructor(hunchlyExportPath: string, vaultlocation: string, consolidate: string, plugin: Plugin) {
|
||||
this.hunchlyExportPath = hunchlyExportPath;
|
||||
this.vaultPath = path.join((plugin.app.vault.adapter as FileSystemAdapter).getBasePath(), vaultlocation)
|
||||
this.plugin = plugin;
|
||||
this.vault = plugin.app.vault
|
||||
this.hunchlyLocation= vaultlocation
|
||||
|
||||
if (consolidate == "true"){
|
||||
this.consolidate = true
|
||||
} else{
|
||||
this.consolidate = false
|
||||
}
|
||||
}
|
||||
|
||||
async process() {
|
||||
const zipFilePath = this.hunchlyExportPath
|
||||
try {
|
||||
// Create a temporary directory for extraction.
|
||||
const tempDir = tmp.dirSync({ unsafeCleanup: true });
|
||||
const extractionPath = tempDir.name;
|
||||
|
||||
// Create a read stream for the zip file.
|
||||
const readStream = fs.createReadStream(zipFilePath);
|
||||
|
||||
// Pipe the read stream through unzipper to extract the contents.
|
||||
await readStream.pipe(unzipper.Extract({ path: extractionPath })).promise();
|
||||
|
||||
const pages = await extractPages(extractionPath)
|
||||
const notes = await extractNotes(extractionPath)
|
||||
const photos = await extractPhotos(extractionPath)
|
||||
const selectors = await extractSelectors(extractionPath)
|
||||
const selectorHits = await extractSelectorsHits(extractionPath)
|
||||
|
||||
await this.processNotes(notes, pages, selectors, selectorHits, extractionPath)
|
||||
await this.processImages(photos, pages, selectors, selectorHits, extractionPath)
|
||||
|
||||
setTimeout(()=>{tempDir.removeCallback();}, 3000);
|
||||
|
||||
} catch (error) {
|
||||
this.updateStatus("Error processing hunchly zip file");
|
||||
console.error(`Error extracting, analyzing, or deleting the temporary directory: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async checkFileOrFolderExistence(folder: string, filename: string): Promise<boolean> {
|
||||
const filePath = path.join(folder, filename)
|
||||
try {
|
||||
await fs_promise.access(filePath);
|
||||
return true;
|
||||
} catch (err) {
|
||||
// This adds a status bar saying invalid note path
|
||||
this.updateStatus(filePath + " does not exist");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async updateStatus(status: string) {
|
||||
const statusBarItemEl = this.plugin.addStatusBarItem();
|
||||
statusBarItemEl.setText(status);
|
||||
}
|
||||
|
||||
private async processNotes(notes: Map<number, INote>, pages: Map<number, IPage>, selectors: Map<number, string>, selectorHits: Map<number, number[]>, extractionPath : string){
|
||||
await this.createDirectoryIfNotExists(path.join("hunchly_notes", "screenshots"))
|
||||
const urlMap = new Map<string, string>()
|
||||
for (const [key, value] of notes) {
|
||||
const page = pages.get(value.pageid)
|
||||
const selectorhits = selectorHits.get(value.pageid)
|
||||
if (page) {
|
||||
let title = page.title
|
||||
let fileContent = ""
|
||||
title = title.substring(0, 65).replace(/[&/\\#,+()$~%.'":*?<>{} ]/g,'_')
|
||||
title = `${title}-notes-${key}`
|
||||
|
||||
if (!(urlMap.has(page.url) && this.consolidate)) {
|
||||
fileContent = "---\n"
|
||||
fileContent = fileContent + `Date: ${value.date}\n`
|
||||
fileContent = fileContent + `URL: ${page.url}\n`
|
||||
fileContent = fileContent + "---\n"
|
||||
if(selectorhits){
|
||||
fileContent = await addSelectors(selectorhits, selectors, fileContent)
|
||||
}
|
||||
}
|
||||
|
||||
fileContent = fileContent + `${await this.processNoteContent(value.note)}\n\n`
|
||||
fileContent = await this.addImages(path.join(extractionPath, "note_screenshots"), path.join(this.vaultPath, "hunchly_notes", "screenshots"), `${key}.jpeg`, fileContent)
|
||||
fileContent = fileContent + "\n---\n"
|
||||
|
||||
if (urlMap.has(page.url) && this.consolidate) {
|
||||
const notePath = urlMap.get(page.url)
|
||||
if (notePath){
|
||||
await updateNoteFile(notePath, fileContent)
|
||||
}
|
||||
} else {
|
||||
const thisnotepath = path.join(this.vaultPath, "hunchly_notes", `${title}.md`)
|
||||
await createNoteFile(thisnotepath, fileContent)
|
||||
urlMap.set(page.url, thisnotepath)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async processImages(photos: Map<number, IPhoto>, pages: Map<number, IPage>, selectors: Map<number, string>, selectorHits: Map<number, number[]>, extractionPath : string){
|
||||
await this.createDirectoryIfNotExists(path.join("hunchly_notes", "screenshots"))
|
||||
const urlMap = new Map<string, string>()
|
||||
for (const [key, value] of photos) {
|
||||
const page = pages.get(value.pageid)
|
||||
const selectorhits = selectorHits.get(value.pageid)
|
||||
if (page) {
|
||||
let title = page.title
|
||||
title = title.substring(0, 50).replace(/[&/\\#,+()$~%.'":*?<>{} ]/g,'_')
|
||||
title = `${title}-captioned-image-${key}`
|
||||
let fileContent = ""
|
||||
|
||||
if (!(urlMap.has(page.url) && this.consolidate)) {
|
||||
fileContent = "---\n"
|
||||
fileContent = fileContent + `Date: ${value.date}\n`
|
||||
fileContent = fileContent + `URL: ${value.photourl}\n`
|
||||
fileContent = fileContent + `HASH: ${value.photohash}\n`
|
||||
fileContent = fileContent + "---\n"
|
||||
if(selectorhits){
|
||||
fileContent = await addSelectors(selectorhits, selectors, fileContent)
|
||||
}
|
||||
}
|
||||
|
||||
fileContent = fileContent + `${await this.processNoteContent(value.caption)}\n\n`
|
||||
fileContent = await this.addImages(path.join(extractionPath, "tagged_photos"), path.join(this.vaultPath, "hunchly_notes", "screenshots"), value.photopath, fileContent)
|
||||
fileContent = fileContent + "\n---\n"
|
||||
|
||||
if (urlMap.has(page.url) && this.consolidate) {
|
||||
const notePath = urlMap.get(page.url)
|
||||
if (notePath){
|
||||
await updateNoteFile(notePath, fileContent)
|
||||
}
|
||||
} else {
|
||||
const thisnotepath = path.join(this.vaultPath, "hunchly_notes", `${title}.md`)
|
||||
await createNoteFile(thisnotepath, fileContent)
|
||||
urlMap.set(page.url, thisnotepath)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async createDirectoryIfNotExists(directoryPath: string): Promise<void> {
|
||||
try {
|
||||
await this.vault.createFolder(path.join(this.hunchlyLocation, directoryPath))
|
||||
} catch (error) {
|
||||
console.error(`Error creating the directory: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async processNoteContent(note: string){
|
||||
await this.vault.getMarkdownFiles().map((file) => {
|
||||
const searchMask = file.name.replace(".md", "")
|
||||
const regEx = new RegExp(searchMask, "ig");
|
||||
const replaceMask = ` [[${searchMask}]] `;
|
||||
note = note.replace(regEx, replaceMask)
|
||||
})
|
||||
return note
|
||||
}
|
||||
|
||||
private async addImages(source: string, destination: string, filename: string, fileContent: string) : Promise<string>{
|
||||
if (await this.checkFileOrFolderExistence(source, filename)){
|
||||
const sourceImagePath = path.join(source, filename)
|
||||
const destinationImagePath = path.join(destination, filename)
|
||||
await copyImages(sourceImagePath, destinationImagePath)
|
||||
return fileContent + `![[${path.join("hunchly_notes", "screenshots", `${filename}`)}]]\n`
|
||||
}
|
||||
return fileContent
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
async function addSelectors(selectorhits: number[], selectors: Map<number, string>, fileContent: string): Promise<string>{
|
||||
fileContent = fileContent + "#### Selectors\n\n"
|
||||
selectorhits.forEach((selector)=>{
|
||||
const sel = selectors.get(selector)
|
||||
if (sel) {
|
||||
fileContent = fileContent + `#${sel.replace(/[&/\\#,+()$~%.'":*?<>{} =]/g,'-')}\t`
|
||||
}
|
||||
})
|
||||
return fileContent + "\n\n---\n"
|
||||
}
|
||||
|
||||
async function extractSelectorsHits(zipFilePath: string): Promise<Map<number, number[]>> {
|
||||
const pagesPath = path.join(zipFilePath, "case_data", "selector_hits.json")
|
||||
const results = new Map<number, number[]>()
|
||||
|
||||
try {
|
||||
const fileContent = await fs_promise.readFile(pagesPath, 'utf8');
|
||||
const jsonData = JSON.parse(fileContent);
|
||||
|
||||
if (Array.isArray(jsonData)) {
|
||||
jsonData.forEach((item, index) => {
|
||||
if (results.has(item.PageID)){
|
||||
const temp = results.get(item.PageID)
|
||||
if (temp != undefined) {
|
||||
temp.push(item.SelectorID)
|
||||
results.set(item.PageID, temp)
|
||||
}
|
||||
} else {
|
||||
results.set(item.PageID, [item.SelectorID])
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error parsing the selector_hits.json file: ${error}`);
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
async function extractSelectors(zipFilePath: string): Promise<Map<number, string>> {
|
||||
const pagesPath = path.join(zipFilePath, "case_data", "selectors.json")
|
||||
const results = new Map<number, string>()
|
||||
|
||||
try {
|
||||
const fileContent = await fs_promise.readFile(pagesPath, 'utf8');
|
||||
const jsonData = JSON.parse(fileContent);
|
||||
|
||||
if (Array.isArray(jsonData)) {
|
||||
jsonData.forEach((item, index) => {
|
||||
results.set(item.ID, item.Selector)
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error parsing the selectors.json file: ${error}`);
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
async function extractPhotos(zipFilePath: string): Promise<Map<number, IPhoto>> {
|
||||
const pagesPath = path.join(zipFilePath, "case_data", "tagged_photos.json")
|
||||
const results = new Map<number, IPhoto>()
|
||||
|
||||
try {
|
||||
const fileContent = await fs_promise.readFile(pagesPath, 'utf8');
|
||||
const jsonData = JSON.parse(fileContent);
|
||||
|
||||
if (Array.isArray(jsonData)) {
|
||||
jsonData.forEach((item, index) => {
|
||||
const temp = {} as IPhoto
|
||||
temp.caption = item.Caption
|
||||
temp.date = item.PhotoTimestamp
|
||||
temp.pageid = item.PageId
|
||||
temp.photohash = item.PhotoHash
|
||||
temp.photourl = item.PhotoUrl
|
||||
temp.photopath = item.LocalFile
|
||||
results.set(item.ID, temp)
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error parsing the tagged_photos.JSON file: ${error}`);
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
async function extractNotes(zipFilePath: string): Promise<Map<number, INote>> {
|
||||
const pagesPath = path.join(zipFilePath, "case_data", "notes.json")
|
||||
const results = new Map<number, INote>()
|
||||
|
||||
try {
|
||||
const fileContent = await fs_promise.readFile(pagesPath, 'utf8');
|
||||
const jsonData = JSON.parse(fileContent);
|
||||
|
||||
if (Array.isArray(jsonData)) {
|
||||
jsonData.forEach((item, index) => {
|
||||
const temp = {} as INote
|
||||
temp.date = item.NoteDate
|
||||
temp.note = item.Note
|
||||
temp.pageid = item.PageId
|
||||
results.set(item.ID, temp)
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error parsing the notes JSON file: ${error}`);
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
async function extractPages(zipFilePath: string): Promise<Map<number, IPage>> {
|
||||
const pagesPath = path.join(zipFilePath, "case_data", "pages.json")
|
||||
const results = new Map<number, IPage>()
|
||||
|
||||
try {
|
||||
const fileContent = await fs_promise.readFile(pagesPath, 'utf8');
|
||||
const jsonData = await JSON.parse(fileContent);
|
||||
if (Array.isArray(jsonData)) {
|
||||
await jsonData.forEach((item, index) => {
|
||||
const temp = {} as IPage
|
||||
temp.date = item.timestamp_created
|
||||
temp.hash = item.content_hash
|
||||
temp.url = item.url
|
||||
temp.title = item.title
|
||||
results.set(item.id, temp)
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error parsing the pages JSON file: ${error}`);
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
async function copyImages(sourcePath: string, destinationPath: string): Promise<void> {
|
||||
try {
|
||||
const fileContent = await fs_promise.readFile(sourcePath);
|
||||
await fs_promise.writeFile(destinationPath, fileContent);
|
||||
} catch (error) {
|
||||
console.error(`Error copying the file: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function createNoteFile(filePath: string, content: string): Promise<void> {
|
||||
try {
|
||||
await fs_promise.writeFile(filePath, content, 'utf8');
|
||||
} catch (error) {
|
||||
console.error(`Error creating the file in ${filePath}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function updateNoteFile(filePath: string, content: string): Promise<void> {
|
||||
try {
|
||||
await fs_promise.appendFile(filePath, content, 'utf8');
|
||||
} catch (error) {
|
||||
console.error(`Error appending the file in ${filePath}: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in a new issue