diff --git a/hooks/prenote.js b/hooks/prenote.js new file mode 100644 index 00000000..9e2d1c1f --- /dev/null +++ b/hooks/prenote.js @@ -0,0 +1,73 @@ +/** + * This script auto-creates a note with correct filename in /content/notes + * It pries out the current active Firefox tab URL as webmention potential context target. + * Hooked up with Mac Shortcut CTRL+OPT+CMD+N ! + * Depends on https://github.com/andikleen/lz4json to decode FF session content (npm lz4 didn't work) + */ + +const spawn = require("child_process").spawn +const { writeFileSync, existsSync, mkdirSync } = require('fs'); +const dayjs = require('dayjs'); + +// options: +const sublPath = "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl" +const firefoxProfileDir = "/Users/wgroeneveld/Library/Application Support/Firefox/Profiles/vm6bu36u.default-release" +const notesDir = `${__dirname}/../content/notes` + +const proc = spawn('lz4jsoncat', [ + `${firefoxProfileDir}/sessionstore-backups/recovery.jsonlz4` +]) + +let output = ''; +proc.stdout.on('data', (chunk) => { + output += chunk.toString(); +}) + +function pryOutLastActiveTabFromDecodedJsonRecovery(output) { + const session = JSON.parse(output) + let lastAccessed = 0 + let url = "" + + session.windows.forEach(w => { + w.tabs.forEach(t => { + if(t.lastAccessed > lastAccessed) { + lastAccessed = t.lastAccessed + url = t.entries[t.entries.length - 1].url + } + }) + }) + return url +} + +function ensureNoteDirectoriesArePresent() { + const date = dayjs() + const year = date.format("YYYY") + const month = date.format("MM") + const path = `${notesDir}/${year}/${month}` + + if(!existsSync(`${notesDir}/${year}`)) mkdirSync(`${notesDir}/${year}`) + if(!existsSync(path)) mkdirSync(path) + return path +} + +proc.on('exit', (code) => { + const url = pryOutLastActiveTabFromDecodedJsonRecovery(output) + const date = dayjs() + + const path = ensureNoteDirectoriesArePresent() + const filename = `${date.format("DD")}h${date.format("HH")}m${date.format("mm")}s${date.format("ss")}` + const fullpath = `${path}/${filename}.md` + const mddata = `--- +date: ${date.format("YYYY-MM-DDTHH:mm:ssZ")} +context: "${url}" +--- + + +` + + writeFileSync(fullpath, mddata, 'utf-8') + spawn(sublPath, [ fullpath ]) + + console.log(`Created note ${filename} with context ${url}.`) +}); + diff --git a/package.json b/package.json index 1a4d00ff..03979629 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "lodash": ">=4.17.21" }, "scripts": { + "prenote": "node hooks/prenote.js", "precommit": "node hooks/precommit.js", "postdeploy": "node hooks/postdeploy.js" }, @@ -27,6 +28,7 @@ }, "homepage": "https://brainbaking.com", "dependencies": { + "dayjs": "^1.11.1", "jam-my-stack": "^1.0.22" } } diff --git a/yarn.lock b/yarn.lock index db33bdfd..3fdb0b1f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1549,6 +1549,7 @@ __metadata: "@babel/cli": ^7.8.4 "@babel/core": ^7.9.6 "@babel/preset-env": ^7.10.1 + dayjs: ^1.11.1 jam-my-stack: ^1.0.22 lodash: ">=4.17.21" languageName: unknown @@ -1886,6 +1887,13 @@ __metadata: languageName: node linkType: hard +"dayjs@npm:^1.11.1": + version: 1.11.1 + resolution: "dayjs@npm:1.11.1" + checksum: 45df0f7ed59ca142ae6eb377ea54aaca20b831ef4c91067133bf9703ccede6fb8af597478dfc2941f55baee72043c79c2e137ec02064fd4bc8433e22df9213e0 + languageName: node + linkType: hard + "debug@npm:^2.2.0, debug@npm:^2.3.3": version: 2.6.9 resolution: "debug@npm:2.6.9"