Skip to content

Commit 2e7072e

Browse files
committed
NoteHelpers 1.3.3 New 'add to frontmatter' command
1 parent 32e5577 commit 2e7072e

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

jgclark.NoteHelpers/plugin.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"plugin.author": "Jonathan Clark & Eduard Metzger",
88
"plugin.url": "https://github.com/NotePlan/plugins/tree/main/jgclark.NoteHelpers/",
99
"plugin.changelog": "https://github.com/NotePlan/plugins/blob/main/jgclark.NoteHelpers/CHANGELOG.md",
10-
"plugin.version": "1.3.2",
10+
"plugin.version": "1.3.3",
1111
"plugin.releaseStatus": "full",
12-
"plugin.lastUpdateInfo": "1.3.2: Add more detail to /log Editor Note, to help flush out a long-standing API bug.\n1.3.1: Improvements and fixes to 'list inconsistent note filenames' command.\n1.3.0: New 'duplicate note' command.\n1.2.5: Fix regression in folder chooser.\n1.2.4: Small improvements to 'jump to heading' and 'jump to note heading' commands.\n1.2.3: Fix to 'move note' command for iOS/iPadOS.\n1.2.2: Fix regression in folder picker.\n1.2.1: Allow 'delete note' command to run on Teamspace notes. Stop 'inconsistent file name' commands from running on Teamspace notes. Bug fixes.\n1.2.0: improvements to displays in 'jump to heading' and 'jump to note heading' commands, and include Teamspace notes. Improvements to 'inconsistent file name' commands.\n1.1.1: add initial support for Teamspaces + bug fix.\n1.1.0: new 'list published notes' command.",
12+
"plugin.lastUpdateInfo": "1.3.3: New 'add to frontmatter' command.\n1.3.2: Add more detail to /log Editor Note, to help flush out a long-standing API bug.\n1.3.1: Improvements and fixes to 'list inconsistent note filenames' command.\n1.3.0: New 'duplicate note' command.\n1.2.5: Fix regression in folder chooser.\n1.2.4: Small improvements to 'jump to heading' and 'jump to note heading' commands.\n1.2.3: Fix to 'move note' command for iOS/iPadOS.\n1.2.2: Fix regression in folder picker.\n1.2.1: Allow 'delete note' command to run on Teamspace notes. Stop 'inconsistent file name' commands from running on Teamspace notes. Bug fixes.\n1.2.0: improvements to displays in 'jump to heading' and 'jump to note heading' commands, and include Teamspace notes. Improvements to 'inconsistent file name' commands.\n1.1.1: add initial support for Teamspaces + bug fix.\n1.1.0: new 'list published notes' command.",
1313
"plugin.dependencies": [],
1414
"plugin.script": "script.js",
1515
"plugin.commands": [
@@ -35,6 +35,18 @@
3535
"trigger string (e.g. 'onEditorWillSave => jgclark.DashboardReact.decideWhetherToUpdateDashboard')"
3636
]
3737
},
38+
{
39+
"name": "add to frontmatter",
40+
"alias": [
41+
"addFM"
42+
],
43+
"description": "Add a key:value pair to the frontmatter of the current note",
44+
"jsFunction": "addItemToFrontmatter",
45+
"parameters": [
46+
"key",
47+
"value"
48+
]
49+
},
3850
{
3951
"name": "convert to frontmatter",
4052
"alias": [

jgclark.NoteHelpers/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export { renameInconsistentNames } from './lib/commands/renameInconsistentNames'
2323
export { titleToFilename } from './lib/commands/titleToFilename'
2424
export { listPublishedNotes } from './listPublishedNotes'
2525
export { newNote, newNoteFromClipboard, newNoteFromSelection } from './newNote'
26-
export { addTriggerToNote, convertLocalLinksToPluginLinks, addFrontmatterToNote, moveNote, logEditorNoteDetailed, renameNoteFile, trashNote } from './noteHelpers'
26+
export { addTriggerToNote, convertLocalLinksToPluginLinks, addFrontmatterToNote, addItemToFrontmatter, moveNote, logEditorNoteDetailed, renameNoteFile, trashNote } from './noteHelpers'
2727
export {
2828
jumpToDone,
2929
jumpToHeading,

jgclark.NoteHelpers/src/noteHelpers.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,64 @@ export async function renameNoteFile(): Promise<void> {
329329
}
330330
}
331331

332+
/**
333+
* Add a key:value pair to the frontmatter for 'note', or if none open, the current Editor.
334+
* @param {TNote?} note optional note to add the item to, or if none open, the current Editor.
335+
* @param {string?} key the key to add to the frontmatter
336+
* @param {string?} value the value to add to the frontmatter
337+
* @returns {boolean} true if the item was added to the frontmatter successfully, false if failed for some reason
338+
*/
339+
export async function addItemToFrontmatter(note: ?TNote, key: ?string, value: ?string): Promise<boolean> {
340+
try {
341+
let thisNote: TNote
342+
let thisKey: string = ''
343+
let thisValue: string = ''
344+
if (note == null) {
345+
if (Editor == null) {
346+
throw new Error(`No note open to convert. Stopping.`)
347+
}
348+
if (!Editor.note) {
349+
throw new Error(`No note open in the Editor. Stopping.`)
350+
}
351+
thisNote = Editor.note
352+
} else {
353+
thisNote = note
354+
}
355+
if (!thisNote) {
356+
throw new Error(`No note supplied, and can't find Editor either.`)
357+
}
358+
if (!key || key === '') {
359+
const inputKey = await getInput(`Please enter the key to add to the frontmatter`, 'OK', 'Add Item to Frontmatter', '')
360+
if (typeof inputKey !== 'string' || inputKey === '') {
361+
throw new Error(`Empty key supplied. Stopping.`)
362+
}
363+
thisKey = inputKey
364+
} else {
365+
thisKey = key
366+
}
367+
if (!thisValue || thisValue === '') {
368+
const inputValue = await getInput(`Please enter the value for key '${thisKey}'`, 'OK', 'Add Item to Frontmatter', '')
369+
if (typeof inputValue !== 'string' || inputValue === '') {
370+
throw new Error(`Empty value supplied. Stopping.`)
371+
}
372+
thisValue = inputValue
373+
} else {
374+
thisValue = value
375+
}
376+
const res = updateFrontMatterVars(thisNote, { [thisKey]: thisValue })
377+
if (res) {
378+
logDebug('note/addItemToFrontmatter', `addItemToFrontmatter(${thisKey}: ${thisValue}) returned ${String(res)}.`)
379+
} else {
380+
throw new Error(`Failed to add item to frontmatter. Stopping.`)
381+
}
382+
return res
383+
} catch (error) {
384+
logError(pluginJson, JSP(error))
385+
await showMessage(error.message)
386+
return false
387+
}
388+
}
389+
332390
/**
333391
* Convert the note to using frontmatter Syntax
334392
* If the plugin settings contains default frontmatter, this is added to the frontmatter.
@@ -354,7 +412,7 @@ export async function addFrontmatterToNote(note: TNote): Promise<void> {
354412
}
355413
const config = await getSettings()
356414
const res = await convertNoteToFrontmatter(thisNote, config.defaultFMText ?? '')
357-
logDebug('note/convertNoteToFrontmatter', `ensureFrontmatter() returned ${String(res)}.`)
415+
logDebug('note/convertNoteToFrontmatter', `addFrontmatterToNote() returned ${String(res)}.`)
358416
} catch (error) {
359417
logError(pluginJson, JSP(error))
360418
await showMessage(error.message)

0 commit comments

Comments
 (0)