-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewnote
More file actions
executable file
·72 lines (55 loc) · 1.72 KB
/
Copy pathnewnote
File metadata and controls
executable file
·72 lines (55 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const { exec } = require("child_process");
// ===== Helper functions =====
function ask(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise((resolve) => rl.question(question, (ans) => {
rl.close();
resolve(ans);
}));
}
function formatDate(input) {
if (!input) return new Date().toISOString().split("T")[0];
const [dd, mm, yyyy] = input.split("/");
if (!dd || !mm || !yyyy) {
console.error("❌ Invalid date format. Use dd/mm/yyyy");
process.exit(1);
}
return `${yyyy}-${mm.padStart(2, "0")}-${dd.padStart(2, "0")}`;
}
function slugify(text) {
return text
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}
// ===== Main =====
(async () => {
const title = await ask("Title: ");
const description = await ask("Description: ");
const customDate = await ask("Date (dd/mm/yyyy) [press enter for today]: ");
const date = formatDate(customDate);
const slug = slugify(title);
const timestamp = Date.now();
const filename = `${date}-${slug}--${timestamp}.md`;
// Get target directory from CLI arg or fallback to ~/notes
const targetDir = process.argv[2] || path.join(process.env.HOME || ".", "notes");
const filepath = path.join(targetDir, filename);
const frontmatter = `---
title: "${title}"
date: ${date}
description: "${description}"
---
# ${title}
`;
fs.mkdirSync(targetDir, { recursive: true });
fs.writeFileSync(filepath, frontmatter, "utf8");
exec(`nvim ${filepath}`, (err) => {
if (err) {
console.error("❌ Couldn't open in Vim:", err.message);
}
});
})();