Skip to content

Commit

Permalink
feat(import): modify import and fix tests
Browse files Browse the repository at this point in the history
Now import should create a note with content and pdf content as yaml code snippet
  • Loading branch information
makaanneo committed Dec 1, 2022
1 parent b48eae1 commit 4257d7b
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 130 deletions.
2 changes: 2 additions & 0 deletions src/core/iPreparedNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ export interface iPreparedNote {
Body: string;
Tags: Array<string>;
Folder: string;
created_time: Date;
}
export class preparedNote implements iPreparedNote {
Title: string;
Body: string;
Tags: string[];
Folder: string;
created_time: Date;
}
194 changes: 114 additions & 80 deletions src/core/joplinApiBc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,57 @@ export class joplinApiBc implements iJoplinApiBc {
});
return jNote;
} catch (e) {
console.error('Error on create note');
console.error('Error: Put note body to Joplin note.');
console.error(e);
return null;
throw e;
}
}
async getResourcesOfNote(noteId: string): Promise<Array<iJoplinResource>> {
console.log('get resource of note by id.');
const result = await joplin.data.get(['notes', noteId, 'resources'], {
fields: ['id', 'size', 'title', 'filename', 'file_extension', 'mime']
});
if (
typeof result === undefined ||
result == null ||
(result != null && result.items.length === 0) ||
typeof result.items[0] === undefined
) {
return null;
try {
const result = await joplin.data.get(['notes', noteId, 'resources'], {
fields: ['id', 'size', 'title', 'filename', 'file_extension', 'mime']
});
if (
typeof result === undefined ||
result == null ||
(result != null && result.items.length === 0) ||
typeof result.items[0] === undefined
) {
return null;
}
const jr: Array<iJoplinResource> = result.items.map((resource) => {
const result: iJoplinResource = new joplinResource();
result.id = resource.id;
result.filename = resource.filename;
result.mime = resource.mime;
result.file_extension = resource.file_extension;
result.size = resource.size;
result.title = resource.titel;
return result;
});
return jr;
} catch (e) {
console.error('Error: get resource of note by id.');
console.error(e);
throw e;
}
const jr: Array<iJoplinResource> = result.items.map((resource) => {
const result: iJoplinResource = new joplinResource();
result.id = resource.id;
result.filename = resource.filename;
result.mime = resource.mime;
result.file_extension = resource.file_extension;
result.size = resource.size;
result.title = resource.titel;
return result;
});
return jr;
}
async getNote(noteId: string): Promise<iJoplinNote> {
console.log('get note by id.');
const result = await joplin.data.get(['notes', noteId], {
fields: ['id', 'title', 'body', 'parent_id', 'created_time']
});
if (typeof result === undefined || result == null) {
return null;
try {
const result = await joplin.data.get(['notes', noteId], {
fields: ['id', 'title', 'body', 'parent_id', 'created_time']
});
if (typeof result === undefined || result == null) {
return null;
}
return result;
} catch (e) {
console.error('Error: get note by id.');
console.error(e);
throw e;
}
return result;
}
async postNotebook(name: string): Promise<iJoplinNotebook> {
console.log('Post notebook (folder) to Joplin.');
Expand All @@ -104,24 +116,30 @@ export class joplinApiBc implements iJoplinApiBc {
} catch (e) {
console.error('Error on create note');
console.error(e);
return null;
throw e;
}
}
async findNoteByHash(hash: string): Promise<Array<iJoplinNote>> {
console.log('Find note by hash.');
const result = await joplin.data.get(['search'], {
query: hash,
type: 'note'
});
if (
typeof result === undefined ||
result == null ||
(result != null && result.items.length === 0) ||
typeof result.items[0] === undefined
) {
return null;
try {
const result = await joplin.data.get(['search'], {
query: hash,
type: 'note'
});
if (
typeof result === undefined ||
result == null ||
(result != null && result.items.length === 0) ||
typeof result.items[0] === undefined
) {
return null;
}
return result.items;
} catch (e) {
console.error('Error: Find note by hash');
console.error(e);
throw e;
}
return result.items;
}

async postNote(note: iPreparedNote): Promise<iJoplinNote> {
Expand All @@ -130,13 +148,15 @@ export class joplinApiBc implements iJoplinApiBc {
const jNote = await joplin.data.post(['notes'], null, {
body: note.Body,
title: note.Title,
parent_id: note.Folder
parent_id: note.Folder,
user_created_time: note.created_time?.getTime(),
user_updated_time: note.created_time?.getTime()
});
return jNote;
} catch (e) {
console.error('Error on create note');
console.error('Error: Post note to Joplin');
console.error(e);
return null;
throw e;
}
}

Expand Down Expand Up @@ -164,9 +184,9 @@ export class joplinApiBc implements iJoplinApiBc {
result.title = resource.titel;
return result;
} catch (e) {
console.error('Error on create resources');
console.error('Error: Post Resource to Joplin');
console.error(e);
return null;
throw e;
}
}

Expand All @@ -177,27 +197,34 @@ export class joplinApiBc implements iJoplinApiBc {
}

console.info(`Search for folders: ${name}`);
try {
const query = await joplin.data.get(['search'], {
type: 'folder',
query: name
});

const query = await joplin.data.get(['search'], {
type: 'folder',
query: name
});
if (
typeof query === undefined ||
query == null ||
(query != null && query.length === 0) ||
typeof query.items[0] === undefined
) {
return null;
}

if (
typeof query === undefined ||
query == null ||
(query != null && query.length === 0) ||
typeof query.items[0] === undefined
) {
return null;
}

if (query.items.length > 1) {
console.warn(`Retrieved more then one folder for titel: ${query.length}`);
if (query.items.length > 1) {
console.warn(
`Retrieved more then one folder for titel: ${query.length}`
);
}
const folder = query.items[0];
console.log(`Retrieved folder ${folder.title}`);
return folder;
} catch (e) {
console.error(`Error: Search for folders: ${name}`);
console.error(e);
throw e;
}
const folder = query.items[0];
console.log(`Retrieved folder ${folder.title}`);
return folder;
}

async postTagToNote(noteId: string, tagId: string): Promise<void> {
Expand All @@ -209,25 +236,32 @@ export class joplinApiBc implements iJoplinApiBc {
} catch (e) {
console.error('note tagging error');
console.error(e);
throw e;
}
}

async findTagByName(tag: string): Promise<joplinTag> {
console.log('Find tag by name.');
const result = await joplin.data.get(['search'], {
query: tag,
type: 'tag'
});
if (
typeof result === undefined ||
result == null ||
(result != null && result.items.length === 0) ||
typeof result.items[0] === undefined
) {
return null;
try {
const result = await joplin.data.get(['search'], {
query: tag,
type: 'tag'
});
if (
typeof result === undefined ||
result == null ||
(result != null && result.items.length === 0) ||
typeof result.items[0] === undefined
) {
return null;
}
const jTag = result.items[0];
return jTag;
} catch (e) {
console.error('Error: Find tag by name.');
console.error(e);
throw e;
}
const jTag = result.items[0];
return jTag;
}

async postTag(tagName: string): Promise<joplinTag> {
Expand All @@ -238,9 +272,9 @@ export class joplinApiBc implements iJoplinApiBc {
});
return jTag;
} catch (e) {
console.error('Error on create note');
console.error('Error: Post tag to Joplin.');
console.error(e);
return null;
throw e;
}
}
}
19 changes: 9 additions & 10 deletions src/core/joplinNoteBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { inject, injectable } from 'inversify';
import { iAthenaConfiguration } from '../settings/athenaConfiguration';
import { TYPES } from '../types';
import { iRawFile } from './rawFile';
import { stringify } from 'yaml';
import YAML from 'yaml';
import { iPreparedNote, preparedNote } from './iPreparedNote';
import { documentFrontMatter } from './documentFrontMatter';
import { documentFrontMatter as documentMetaData } from './documentFrontMatter';
import { iJoplinApiBc } from './joplinApiBc';
import { iJoplinFolderProcessor } from './joplinFolderProcessor';
import { iJoplinResource } from './joplinResource';
Expand All @@ -17,7 +17,7 @@ export interface iJoplinNoteBuilder {
mapFileToPreparedNote(
file: iRawFile,
resource: iJoplinResource
): Promise<documentFrontMatter>;
): Promise<documentMetaData>;
}

@injectable()
Expand All @@ -37,16 +37,16 @@ export class joplinNoteBuilder implements iJoplinNoteBuilder {
async mapFileToPreparedNote(
file: iRawFile,
resource: iJoplinResource
): Promise<documentFrontMatter> {
): Promise<documentMetaData> {
const resourceLink = await resource.buildResourceLink(
resource.title,
file.Name,
resource.id,
resource.mime
);
return {
Name: (await file.fileNameWithoutExtension(file.Name)).trim(),
Author: (file.Metadata.Author ?? '').trim(),
Content: ` \n ${file.Content}\n`,
Content: file.Content,
Sender: '',
Captured: file.Captured,
Created: file.Metadata?.CreationDate ?? null,
Expand All @@ -61,10 +61,8 @@ export class joplinNoteBuilder implements iJoplinNoteBuilder {
};
}

async prepareMetadataBlock(
frontMatter: documentFrontMatter
): Promise<string> {
const yaml_string = stringify(frontMatter);
async prepareMetadataBlock(metadata: documentMetaData): Promise<string> {
const yaml_string = YAML.stringify(metadata, { simpleKeys: false });

let result = '';
result += '``` yaml document header';
Expand Down Expand Up @@ -118,6 +116,7 @@ export class joplinNoteBuilder implements iJoplinNoteBuilder {
resourceLink
);
note.Title = metaData.Name;
note.created_time = metaData.Created;
note.Tags = new Array<string>();
note.Folder = (
await this._jfp.getImportFolderId(this._settings.Values.importNotebook)
Expand Down
Loading

0 comments on commit 4257d7b

Please sign in to comment.