Skip to content

Commit

Permalink
feat(import existing): trigger import of attached resources of a note…
Browse files Browse the repository at this point in the history
… into the note

A new feature arrived to import existing note attachment (files) to a existing note.
The attachment must be connected to the note, then the new feature is able to import and use the same logic like for new stuff. It will import the resource of a note into the note.
There are two options
Override note body
Append to note body
  • Loading branch information
makaanneo committed Dec 22, 2022
1 parent 5240bf4 commit 3579202
Show file tree
Hide file tree
Showing 23 changed files with 649 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dist/
node_modules/
publish/
build/config.gypi
coverage/
coverage/
migrateToDocumentNote/
20 changes: 10 additions & 10 deletions api/JoplinData.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ import { Path } from './types';
* ```
*/
export default class JoplinData {
private api_;
private pathSegmentRegex_;
private serializeApiBody;
private pathToString;
get(path: Path, query?: any): Promise<any>;
post(path: Path, query?: any, body?: any, files?: any[]): Promise<any>;
put(path: Path, query?: any, body?: any, files?: any[]): Promise<any>;
delete(path: Path, query?: any): Promise<any>;
itemType(itemId: string): Promise<ModelType>;
resourcePath(resourceId: string): Promise<string>;
private api_;
private pathSegmentRegex_;
private serializeApiBody;
private pathToString;
get(path: Path, query?: any): Promise<any>;
post(path: Path, query?: any, body?: any, files?: any[]): Promise<any>;
put(path: Path, query?: any, body?: any, files?: any[]): Promise<any>;
delete(path: Path, query?: any): Promise<any>;
itemType(itemId: string): Promise<ModelType>;
resourcePath(resourceId: string): Promise<string>;
}
4 changes: 4 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const FRONT_MATTER_RENDER_RULE = 'frontMatterRenderRule';
export const FILE_HASH_ALGORITHM = 'fileHashAlgorithm';
export const CODEMIRROR_FRONT_MATTER = 'codemirrorFrontMatter';
export const FOLD_FRONT_MATTER = 'foldFrontMatter';
export const PLUGIN_DATA_DIR = 'pluginDataDir';
export const DOCUMENTS_SECTION_HEADER = 'DocumentsSectionHeader';

export class pluginSettings {
ignoreFiles: string;
Expand All @@ -33,6 +35,8 @@ export class pluginSettings {
fileHashAlgorithm: string;
codemirrorFrontMatter: boolean;
foldFrontMatter: boolean;
pluginDataDir: string;
documentsSectionHeader: string;
}

export enum ContextMsgType {
Expand Down
45 changes: 45 additions & 0 deletions src/core/joplinApiBc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { injectable } from 'inversify';
import { iPreparedNote } from './iPreparedNote';
import { iJoplinResource, joplinResource } from './joplinResource';
import { iJoplinNotebook } from './JoplinNotebook';
import { iJoplinAttachment, joplinAttachment } from './joplinAttachment';

export interface iJoplinNote {
id: string;
Expand Down Expand Up @@ -42,10 +43,53 @@ export interface iJoplinApiBc {
postTagToNote(noteId: string, tagId: string): Promise<void>;
postTag(tagName: string): Promise<joplinTag>;
putNoteBody(noteId: string, noteBody: string): Promise<iJoplinNote>;
getAttachmentById(resourceId: string): Promise<iJoplinAttachment>;
getResourceById(resourceId: string): Promise<iJoplinResource>;
}

@injectable()
export class joplinApiBc implements iJoplinApiBc {
async getResourceById(resourceId: string): Promise<iJoplinResource> {
console.log(`get resource by id ${resourceId}.`);
try {
const resource = await joplin.data.get(['resources', resourceId], {
fields: ['id', 'size', 'title', 'filename', 'file_extension', 'mime']
});
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.title;
return result;
} catch (e) {
console.error('Error: get resource by id.');
console.error(e);
throw e;
}
}

async getAttachmentById(resourceId: string): Promise<iJoplinAttachment> {
console.log(`get attachment by id ${resourceId}.`);
try {
const result = await joplin.data.get(
['resources', resourceId, 'file'],
{}
);
console.info(result);
const jr: iJoplinAttachment = new joplinAttachment();
jr.type = result.type;
jr.body = result.body;
jr.contentType = result.contentType;
jr.attachmentFilename = result.attachmentFilename;
return jr;
} catch (e) {
console.error('Error: get attachment by id.');
console.error(e);
throw e;
}
}
putNoteBody(noteId: string, noteBody: string): Promise<iJoplinNote> {
console.log('Put note body to Joplin note.');
try {
Expand Down Expand Up @@ -73,6 +117,7 @@ export class joplinApiBc implements iJoplinApiBc {
) {
return null;
}
console.info(result);
const jr: Array<iJoplinResource> = result.items.map((resource) => {
const result: iJoplinResource = new joplinResource();
result.id = resource.id;
Expand Down
16 changes: 16 additions & 0 deletions src/core/joplinAttachment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { injectable } from 'inversify';

export interface iJoplinAttachment {
type: string;
body: Uint8Array;
contentType: string;
attachmentFilename: string;
}

@injectable()
export class joplinAttachment implements iJoplinAttachment {
type: string;
body: Uint8Array;
contentType: string;
attachmentFilename: string;
}
40 changes: 33 additions & 7 deletions src/core/joplinNoteBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import { iJoplinResource } from './joplinResource';
export interface iJoplinNoteBuilder {
buildNote(
loadedFile: iRawFile,
resource: iJoplinResource
resource: iJoplinResource,
headerLevel: number,
documentSection: boolean
): Promise<iPreparedNote>;
mapFileToPreparedNote(
file: iRawFile,
resource: iJoplinResource
): Promise<documentMetaData>;
perpareHeaderLevel(headerLevel: number): Promise<string>;
}

@injectable()
Expand Down Expand Up @@ -74,19 +77,36 @@ export class joplinNoteBuilder implements iJoplinNoteBuilder {
return result;
}

async perpareHeaderLevel(headerLevel: number): Promise<string> {
let result = '';
for (let loop = 0; loop < headerLevel; loop++) {
result += '#';
}
return result;
}

async buildDocumentHeaderSection(): Promise<string> {
return `# ${this._settings.Values.documentsSectionHeader}`;
}

async prepareNoteBody(
metadataBlockText: string,
resourceTitle: string,
resourceLink: string
resourceLink: string,
headerLevel: number,
documentSection: boolean
): Promise<string> {
let bodyText = '';

if (documentSection) {
bodyText += await this.buildDocumentHeaderSection();
bodyText += '\n';
}
bodyText += resourceTitle;
bodyText += '\n';
bodyText += resourceLink;
bodyText += '\n';
bodyText += '\n';
bodyText += '# metadata';
bodyText += `${await this.perpareHeaderLevel(headerLevel)} metadata`;
bodyText += '\n';
bodyText += metadataBlockText;
bodyText += '\n';
Expand All @@ -96,13 +116,17 @@ export class joplinNoteBuilder implements iJoplinNoteBuilder {

async buildNote(
loadedFile: iRawFile,
resource: iJoplinResource
resource: iJoplinResource,
headerLevel: number,
documentSection: boolean
): Promise<iPreparedNote> {
console.log('START build note.');
const targetHeaderLevel = documentSection ? headerLevel + 1 : headerLevel;
const metaData = await this.mapFileToPreparedNote(loadedFile, resource);
const metaDataText = await this.prepareMetadataBlock(metaData);
const resourceTitleBlock = await resource.buildResourceTitle(
loadedFile.Name
loadedFile.Name,
await this.perpareHeaderLevel(targetHeaderLevel)
);
const resourceLink = await resource.buildResourceLink(
loadedFile.Name,
Expand All @@ -113,7 +137,9 @@ export class joplinNoteBuilder implements iJoplinNoteBuilder {
note.Body = await this.prepareNoteBody(
metaDataText,
resourceTitleBlock,
resourceLink
resourceLink,
targetHeaderLevel,
documentSection
);
note.Title = metaData.Name;
note.created_time = metaData.Created;
Expand Down
2 changes: 1 addition & 1 deletion src/core/joplinNoteProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class joplinNoteProcessor implements iJoplinNoteProcessor {
return;
}
const jRes = await this._jdapi.postResource(file);
const preparedNote = await this._nb.buildNote(lodedFile, jRes);
const preparedNote = await this._nb.buildNote(lodedFile, jRes, 1, true);
const jNote = await this._jdapi.postNote(preparedNote);

if (this._settings.Values.tagNewFiles) {
Expand Down
8 changes: 5 additions & 3 deletions src/core/joplinResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export interface iJoplinResource {
size: string;
filename: string;
file_extension: string;
body: Uint8Array;
buildResourceLink(name: string, id: string, mime: string): Promise<string>;
buildResourceTitle(name: string): Promise<string>;
buildResourceTitle(name: string, headerBlock: string): Promise<string>;
}

@injectable()
Expand All @@ -19,6 +20,7 @@ export class joplinResource implements iJoplinResource {
size: string;
filename: string;
file_extension: string;
body: Uint8Array;

async buildResourceLink(
name: string,
Expand All @@ -32,7 +34,7 @@ export class joplinResource implements iJoplinResource {
return link;
}

async buildResourceTitle(name: string): Promise<string> {
return `# ${name}`;
async buildResourceTitle(name: string, headerBlock: string): Promise<string> {
return `${headerBlock} ${name}`;
}
}
Loading

0 comments on commit 3579202

Please sign in to comment.