Skip to content

Commit

Permalink
feat: enable template_tags variable while inserting templates
Browse files Browse the repository at this point in the history
Fixes #48.
  • Loading branch information
nishantwrp committed Oct 9, 2021
1 parent b14d906 commit 2f9863f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Currently there are two special variables.
- If a tag specified in `template_tags` doesn't exist already, it will be created.
- You can't use these variable names i.e. `template_title` and `template_tags` for custom variables. In general, please avoid defining custom variables with `template_` prefix.
- To get the ID of a notebook, you can right click on that notebook and click on `Copy notebook ID`.
- While you are inserting the template in an existing note/to-do, `template_tags` variable is used to apply those tags to the note the template is inserted in. However, you can disable using `template_tags` while inserting templates from the plugin settings.

**Example of a template using special variables**

Expand Down
9 changes: 9 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export enum TemplateAction {

const performInsertTextAction = async (template: NewNote) => {
await joplin.commands.execute("insertText", template.body);

const applyTags = await joplin.settings.value("applyTagsWhileInserting")
if (applyTags) {
const noteId = (await joplin.workspace.selectedNote()).id;
for (const tag of template.tags) {
const tagId = (await getAnyTagWithTitle(tag)).id;
await applyTagToNote(tagId, noteId);
}
}
}

const performNewNoteAction = async (template: NewNote, isTodo: 0 | 1) => {
Expand Down
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const DOCUMENTATION_URL = "https://github.com/joplin/plugin-templates#readme";

joplin.plugins.register({
onStart: async function() {
// Register setting section
await joplin.settings.registerSection("templatesPlugin", {
label: "Templates",
});


// Register all settings
await joplin.settings.registerSettings({
"defaultNoteTemplateId": {
Expand All @@ -26,6 +32,14 @@ joplin.plugins.register({
type: SettingItemType.String,
value: null,
label: "Default to-do template ID"
},
"applyTagsWhileInserting": {
public: true,
type: SettingItemType.Bool,
value: true,
label: "Apply tags while inserting template",
description: "Apply tags using 'template_tags' variable while inserting template to notes/to-dos.",
section: "templatesPlugin"
}
});

Expand Down

0 comments on commit 2f9863f

Please sign in to comment.