added prenote script to quickly create a new note

This commit is contained in:
Wouter Groeneveld 2022-04-26 18:09:55 +02:00
parent 4ad750a8d1
commit 207514af30
3 changed files with 83 additions and 0 deletions

73
hooks/prenote.js Normal file
View File

@ -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}.`)
});

View File

@ -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"
}
}

View File

@ -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"