Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
JackGruber committed Jan 9, 2021
2 parents 5d127ca + 7eb4ae0 commit a19bc99
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 18 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ Plugin to combine one or more notes to a new one.
- Select multiple notes to be combined into a new one
- Click on `Tools > Combine selected notes` or use the command `Combine selected notes` from the context menu

## Options

Go to `Tools > Options > Combine notes`

- `Create combined note as to-do`: New note is created as To-Do. Default `false`
- `Delete combined notes`: Delete combined notes, after note creation. Default `false`
- `Preserve Source URL`: The source URL will be added to the new note. Default `false`
- `Preserve Created Date`: The Created Date will be added to the new note. Default `false`
- `Preserve Updated Date`: The Updated Date will be added to the new note. Default `false`
- `Preserve Location`: The Location (Latitude, Longitude, Altitude) will be added to the new note. Default `false`
- `Metadata Prefix`: The entered text is output before the metadata (URL, Date, Location).
- `Metadata Suffix`: The entered text is output after the metadata (URL, Date, Location).

## Keyboard Shortcus

Under `Options > Keyboard Shortcus` you can assign a keyboard shortcut for the following commands:
Expand All @@ -41,6 +54,12 @@ To update the plugin framework, run `npm run update`.

## Changelog

### v0.2.0 (2021-01-09)

- Option added to create the combined note as a to-do
- Option added to preserve metadata (Source URL, Creation Date, Updated Date, Location)
- Option added to delete combined notes

### v0.1.0 (2021-01-08)

- First version
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "joplin-plugin-combine-notes",
"version": "0.0.1",
"version": "0.2.0",
"scripts": {
"dist": "webpack",
"prepare": "npm run dist",
Expand Down
200 changes: 185 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,89 @@
import joplin from "api";
import { MenuItemLocation } from "api/types";
import { MenuItemLocation, SettingItemType } from "api/types";

joplin.plugins.register({
onStart: async function () {
console.info("Combine plugin started");

await joplin.settings.registerSection("combineNoteSection", {
label: "Combine notes",
iconName: "fas fa-layer-group",
});

await joplin.settings.registerSetting("asToDo", {
value: false,
type: SettingItemType.Bool,
section: "combineNoteSection",
public: true,
label: "Create combined note as to-do",
});

await joplin.settings.registerSetting("deleteCombinedNotes", {
value: false,
type: SettingItemType.Bool,
section: "combineNoteSection",
public: true,
label: "Delete combined notes",
});

await joplin.settings.registerSetting("preserveMetadataSourceUrl", {
value: false,
type: SettingItemType.Bool,
section: "combineNoteSection",
public: true,
label: "Preserve Source URL",
description:
"Preserve the Source by inserting them under the header from the note.",
});

await joplin.settings.registerSetting("preserveMetadataCreatedDate", {
value: false,
type: SettingItemType.Bool,
section: "combineNoteSection",
public: true,
label: "Preserve Created Date",
description:
"Preserve the Created Date by inserting them under the header from the note.",
});

await joplin.settings.registerSetting("preserveMetadataUpdatedDate", {
value: false,
type: SettingItemType.Bool,
section: "combineNoteSection",
public: true,
label: "Preserve Updated Date",
description:
"Preserve the Updated Date by inserting them under the header from the note.",
});

await joplin.settings.registerSetting("preserveMetadataLocation", {
value: false,
type: SettingItemType.Bool,
section: "combineNoteSection",
public: true,
label: "Preserve Location",
description:
"Preserve the Location Date by inserting them under the header from the note.",
});

await joplin.settings.registerSetting("preserveMetadataPrefix", {
value: "",
type: SettingItemType.String,
section: "combineNoteSection",
public: true,
label: "Metadata Prefix",
description: "Prefix for the Metadata section.",
});

await joplin.settings.registerSetting("preserveMetadataSuffix", {
value: "",
type: SettingItemType.String,
section: "combineNoteSection",
public: true,
label: "Metadata Suffix",
description: "Suffix for the Metadata section.",
});

await joplin.commands.register({
name: "CombineNotes",
label: "Combine selected notes",
Expand All @@ -14,13 +93,93 @@ joplin.plugins.register({
const newNoteBody = [];
let notebookId = null;
const newTags = [];
let createdDate = null;
let updatedDate = null;
let preserveMetadata = [];
const preserveUrl = await joplin.settings.value(
"preserveMetadataSourceUrl"
);
const preserveCreatedDate = await joplin.settings.value(
"preserveMetadataCreatedDate"
);
const preserveUpdatedDate = await joplin.settings.value(
"preserveMetadataUpdatedDate"
);
const preserveMetadataLocation = await joplin.settings.value(
"preserveMetadataLocation"
);
const preserveMetadataPrefix = await joplin.settings.value(
"preserveMetadataPrefix"
);
const preserveMetadataSuffix = await joplin.settings.value(
"preserveMetadataSuffix"
);

// collect note data
for (const noteId of ids) {
preserveMetadata = [];
const note = await joplin.data.get(["notes", noteId], {
fields: ["title", "body", "parent_id"],
fields: [
"title",
"body",
"parent_id",
"source_url",
"created_time",
"updated_time",
"latitude",
"longitude",
"altitude",
],
});
newNoteBody.push(["# " + note.title, "", note.body].join("\n"));
newNoteBody.push("# " + note.title + "\n");

// Preserve metadata
if (preserveUrl === true && note.source_url != "") {
preserveMetadata.push("[Source](" + note.source_url + ")");
}

if (preserveCreatedDate === true) {
createdDate = new Date(note.created_time);
preserveMetadata.push("Created: " + createdDate + ")");
}

if (preserveUpdatedDate === true) {
updatedDate = new Date(note.updated_time);
preserveMetadata.push("Updated: " + updatedDate + ")");
}

if (
preserveMetadataLocation === true &&
(note.latitude != "0.00000000" ||
note.longitude != "0.00000000" ||
note.altitude != "0.0000")
) {
preserveMetadata.push(
[
"Location:",
"Lat:",
note.latitude,
"Lon:",
note.longitude,
"Altitude:",
note.altitude,
].join(" ")
);
}

if (preserveMetadata.length > 0) {
if (preserveMetadataPrefix != "") {
newNoteBody.push(preserveMetadataPrefix + "\n");
}

newNoteBody.push(preserveMetadata.join("\n") + "\n");

if (preserveMetadataSuffix != "") {
newNoteBody.push(preserveMetadataSuffix + "\n");
}
}

newNoteBody.push(note.body + "\n");

let pageNum = 0;
do {
Expand All @@ -30,33 +189,44 @@ joplin.plugins.register({
page: pageNum++,
});
for (const tag of noteTags.items) {
if(newTags.indexOf(tag.id) === -1) {
newTags.push(tag.id)
}
if (newTags.indexOf(tag.id) === -1) {
newTags.push(tag.id);
}
}
} while (noteTags.has_more);

if (!notebookId) notebookId = note.parent_id;
}

const asToDo = await joplin.settings.value("asToDo");

// create new note
const newNoteData = {
title: "Combined note",
body: newNoteBody.join("\n\n"),
body: newNoteBody.join("\n"),
parent_id: notebookId,
is_todo: asToDo,
};
const newNote = await joplin.data.post(["notes"], null, newNoteData);

// Add Tags
for (const tag of newTags) {
await joplin.data.post(
["tags", tag, "notes"],
null,
{ id: newNote.id }
);
await joplin.data.post(["tags", tag, "notes"], null, {
id: newNote.id,
});
}

// delete combined notes
const deleteNotes = await joplin.settings.value(
"deleteCombinedNotes"
);
if (deleteNotes === true) {
for (const noteId of ids) {
await joplin.data.delete(["notes", noteId]);
}
}

await joplin.commands.execute('openNote', newNote.id);
await joplin.commands.execute("openNote", newNote.id);
}
},
});
Expand All @@ -67,8 +237,8 @@ joplin.plugins.register({
MenuItemLocation.Tools
);
await joplin.views.menuItems.create(
'contextMenuItemconcatCombineNotes',
'CombineNotes',
"contextMenuItemconcatCombineNotes",
"CombineNotes",
MenuItemLocation.NoteListContextMenu
);
},
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 1,
"id": "io.github.jackgruber.combine-notes",
"app_min_version": "1.6",
"version": "1.0.0",
"version": "0.2.0",
"name": "combine",
"description": "Combine one or more notes",
"author": "JackGruber",
Expand Down

0 comments on commit a19bc99

Please sign in to comment.