diff --git a/jgclark.NoteHelpers/README.md b/jgclark.NoteHelpers/README.md index af1c9a52e..30cd2f339 100644 --- a/jgclark.NoteHelpers/README.md +++ b/jgclark.NoteHelpers/README.md @@ -35,6 +35,7 @@ This plugin (now a Core Plugin bundled with NotePlan 3.16.1 onwards) provides co - **Show This Quarter** (alias **/stq**) - **Show This Year** (alias **/sty**) - **update all indexes** (alias **uai**): updates all the existing folder index notes +- **Write changed/modified date to frontmatter** (alias **modified**): writes the modified date to frontmatter (on each save). Writes to 'modified' key. Also (optionally) writes the author's initials to the 'author' key (see plugin settings). **Tip**: some of these are even more helpful if you assign a keyboard shortcut to them, using macOS's Keyboard > Shortcuts > App Shortcuts system. For example I have mapped ⇧⌘H to `/jump to heading`. diff --git a/jgclark.NoteHelpers/plugin.json b/jgclark.NoteHelpers/plugin.json index 3be677994..6116f7f88 100644 --- a/jgclark.NoteHelpers/plugin.json +++ b/jgclark.NoteHelpers/plugin.json @@ -7,8 +7,8 @@ "plugin.author": "Jonathan Clark & Eduard Metzger", "plugin.url": "https://github.com/NotePlan/plugins/tree/main/jgclark.NoteHelpers/", "plugin.changelog": "https://github.com/NotePlan/plugins/blob/main/jgclark.NoteHelpers/CHANGELOG.md", - "plugin.version": "1.0.0", - "plugin.lastUpdateInfo": "1.0.0: move '/new note from ...' commands here from Filer plugin, and revived '/new note' command.\n0.20.3: New 'printEditorDetailed' command to log all paragraph details as well.\n0.20.2: 'printNote' extended to cover backlinks.\n0.20.1: new command 'printNote' for debugging purposes.\n0.20.0: new commands \"unlinked note finder\" and \"delete note\". Bug fix to \"rename note filename\" command.\n0.19.2: fix edge cases with \"add trigger to note\".\n0.19.1: \"add trigger to note\" command can now be run from template. Added migration message about 'open note' commands.\n0.19.0: fix to '/rename inconsistent filename' command. Moved 'open note' functions to WindowTools plugin. Updated the display of the 'index folders' command. Removed 'Show month/quarter/year' commands as they are now in the main NP menus.", + "plugin.version": "1.1.0", + "plugin.lastUpdateInfo": "1.1.0: added 'write modified' command to write the modified date to frontmatter (on each save). Can be run by hand or can be included as a trigger using the 'add trigger to note' command.\n1.0.0: move '/new note from ...' commands here from Filer plugin, and revived '/new note' command.\n0.20.3: New 'printEditorDetailed' command to log all paragraph details as well.\n0.20.2: 'printNote' extended to cover backlinks.\n0.20.1: new command 'printNote' for debugging purposes.\n0.20.0: new commands \"unlinked note finder\" and \"delete note\". Bug fix to \"rename note filename\" command.\n0.19.2: fix edge cases with \"add trigger to note\".\n0.19.1: \"add trigger to note\" command can now be run from template. Added migration message about 'open note' commands.\n0.19.0: fix to '/rename inconsistent filename' command. Moved 'open note' functions to WindowTools plugin. Updated the display of the 'index folders' command. Removed 'Show month/quarter/year' commands as they are now in the main NP menus.", "plugin.dependencies": [], "plugin.script": "script.js", "plugin.commands": [ @@ -253,6 +253,23 @@ "description": "onEditorWillSave", "jsFunction": "triggerFindUnlinkedNotes", "hidden": true + }, + { + "name": "Write changed/modified date to frontmatter", + "description": "Write the modified date to frontmatter (on each save). Writes to 'modified' key.", + "jsFunction": "writeModified", + "alias": [ + "modified" + ], + "arguments": [] + }, + { + "name": "writeModified", + "description": "Write the modified date to frontmatter (on each save). Writes to 'modified' key. Can be run by hand or can be included as a trigger using the 'add trigger to note' command.", + "jsFunction": "writeModified", + "alias": [], + "arguments": [], + "hidden": true } ], "offerToDownloadPlugin": { @@ -372,6 +389,33 @@ { "type": "separator" }, + { + "type": "heading", + "title": "'Write changed/modified date to frontmatter' command settings" + }, + { + "key": "dateFormat", + "title": "Date format", + "description": "The format of the date to write to frontmatter. Use 'ISO' for ISO 8601 format (YYYY-MM-DDTHH:MM:SS.SSSZ), or 'Local' to use your local time settings format.", + "type": "string", + "default": "ISO", + "required": false, + "choices": [ + "ISO", + "Local" + ] + }, + { + "key": "authorID", + "title": "Author ID", + "description": "Your intiials or ID to use in command 'Write changed/modified date to frontmatter'. Leave blank to not include an author ID next to the date.", + "type": "string", + "default": "", + "required": false + }, + { + "type": "separator" + }, { "type": "heading", "title": "Debugging" diff --git a/jgclark.NoteHelpers/src/index.js b/jgclark.NoteHelpers/src/index.js index c3ddadd45..a41be1568 100644 --- a/jgclark.NoteHelpers/src/index.js +++ b/jgclark.NoteHelpers/src/index.js @@ -21,15 +21,7 @@ export { titleToFilename } from './lib/commands/titleToFilename' export { filenameToTitle } from './lib/commands/filenameToTitle' export { renameInconsistentNames } from './lib/commands/renameInconsistentNames' export { newNote, newNoteFromClipboard, newNoteFromSelection } from './newNote' -export { - addTriggerToNote, - convertLocalLinksToPluginLinks, - addFrontmatterToNote, - moveNote, - logEditorNoteDetailed, - renameNoteFile, - trashNote -} from './noteHelpers' +export { addTriggerToNote, convertLocalLinksToPluginLinks, addFrontmatterToNote, moveNote, logEditorNoteDetailed, renameNoteFile, trashNote } from './noteHelpers' export { jumpToDone, jumpToHeading, @@ -46,6 +38,8 @@ export { export { findUnlinkedNotesInCurrentNote, findUnlinkedNotesInAllNotes, triggerFindUnlinkedNotes } from './unlinkedNoteFinder' export { printNote } from '@helpers/NPnote' +export { writeModified } from './writeModified' + export function resetCaches() { NotePlan.resetCaches() } diff --git a/jgclark.NoteHelpers/src/writeModified.js b/jgclark.NoteHelpers/src/writeModified.js new file mode 100644 index 000000000..a85cd7bbd --- /dev/null +++ b/jgclark.NoteHelpers/src/writeModified.js @@ -0,0 +1,39 @@ +// @flow + +import pluginJson from '../plugin.json' +import { setFrontMatterVars } from '@helpers/NPFrontMatter' +import { log, logError, logDebug, timer, clo, clof, JSP } from '@helpers/dev' + +/**************************************************************************************************************************** + * CONSTANTS + ****************************************************************************************************************************/ + +/**************************************************************************************************************************** + * LOCAL FUNCTIONS + ****************************************************************************************************************************/ + +/**************************************************************************************************************************** + * EXPORTED FUNCTIONS + ****************************************************************************************************************************/ + +/**************************************************************************************************************************** + * COMMAND ENTRYPOINTS + ****************************************************************************************************************************/ + +/** + * Writes the modified date to frontmatter (on each save). Writes to 'modified' key + * Requires the trigger onEditorWillSave + * @author @jgclark + */ +export async function writeModified(): Promise { + try { + logDebug('writeModified', 'Starting') + const { authorID, dateFormat } = await DataStore.settings + const theTime = !dateFormat || dateFormat === 'ISO' ? new Date().toISOString() : new Date().toLocaleString() + setFrontMatterVars(Editor, { + modified: authorID ? `${theTime} (${authorID})` : theTime, + }) + } catch (e) { + logError(pluginJson, JSP(e)) + } +}