Use month week and random name

This commit is contained in:
Endeavorance 2024-10-27 08:44:15 -04:00
parent f75037547f
commit 9c65740da2
5 changed files with 3508 additions and 21 deletions

1101
adjectives.ts Normal file

File diff suppressed because it is too large Load diff

65
main.ts
View file

@ -7,6 +7,9 @@ import {
Vault, Vault,
Notice, Notice,
} from "obsidian"; } from "obsidian";
import { nouns } from "./nouns.js";
import { adjectives } from "./adjectives.js";
import { capitalize, sample } from "lodash-es";
interface ScrapsPluginSettings { interface ScrapsPluginSettings {
scrapsRootDir: string; scrapsRootDir: string;
@ -16,10 +19,16 @@ interface ScrapsPluginSettings {
const DEFAULT_SETTINGS: ScrapsPluginSettings = { const DEFAULT_SETTINGS: ScrapsPluginSettings = {
scrapsRootDir: "_Scraps", scrapsRootDir: "_Scraps",
scrapsPathFormat: "MM MMM/DD", scrapsPathFormat: "MM MMM/R",
scrapsFileName: "ddd MMM DD hhmmssa", scrapsFileName: "ddd MMM DD hhmmssa",
}; };
function getFormattedDate(format: string): string {
const momentFormatted = moment().format(format);
const currentWeek = Math.ceil(moment().date() / 7);
return momentFormatted.replace("R", `Week ${currentWeek}`);
}
async function mkdirp(vault: Vault, folderPath: string): Promise<void> { async function mkdirp(vault: Vault, folderPath: string): Promise<void> {
const pathParts = folderPath.split("/"); const pathParts = folderPath.split("/");
@ -46,19 +55,27 @@ export default class ScrapsPlugin extends Plugin {
settings: ScrapsPluginSettings; settings: ScrapsPluginSettings;
async ensureScrapDir(): Promise<string> { async ensureScrapDir(): Promise<string> {
const pathname = `${this.settings.scrapsRootDir}/${moment().format( const pathname = `${this.getScrapDir()}`;
this.settings.scrapsPathFormat
)}`;
await mkdirp(this.app.vault, pathname); await mkdirp(this.app.vault, pathname);
return pathname; return pathname;
} }
getScrapFilePath(): string { getScrapDir(): string {
return `${this.settings.scrapsRootDir}/${moment().format( return `${this.settings.scrapsRootDir}/${getFormattedDate(
this.settings.scrapsPathFormat this.settings.scrapsPathFormat
)}/${moment().format(this.settings.scrapsFileName)}.md`; )}`;
}
getScrapFileName(): string {
const adj = capitalize(sample(adjectives));
const noun = capitalize(sample(nouns));
return `Scrap ${adj} ${noun}.md`;
}
getScrapFilePath(): string {
return `${this.getScrapDir()}/${this.getScrapFileName()}`;
} }
async createScrap() { async createScrap() {
@ -71,6 +88,22 @@ export default class ScrapsPlugin extends Plugin {
await this.app.workspace.getLeaf(false).openFile(newScrap); await this.app.workspace.getLeaf(false).openFile(newScrap);
} }
async convertToScrap() {
const currentFile = this.app.workspace.getActiveFile();
if (currentFile === null) {
new Notice("No file is currently open");
return;
}
await this.ensureScrapDir();
await this.app.fileManager.renameFile(
currentFile,
this.getScrapFilePath()
);
}
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
this.addSettingTab(new ScrapsSettingTab(this.app, this)); this.addSettingTab(new ScrapsSettingTab(this.app, this));
@ -92,21 +125,11 @@ export default class ScrapsPlugin extends Plugin {
id: "scraps-convert", id: "scraps-convert",
name: "Scraps: Convert current file to Scrap", name: "Scraps: Convert current file to Scrap",
icon: "file-plus", icon: "file-plus",
editorCallback: async () => { editorCallback: async () => this.convertToScrap(),
const currentFile = this.app.workspace.getActiveFile(); });
if (currentFile === null) { this.addRibbonIcon("file-plus", "Convert file to Scrap", async () => {
new Notice("No file is currently open"); this.convertToScrap();
return;
}
await this.ensureScrapDir();
await this.app.fileManager.renameFile(
currentFile,
this.getScrapFilePath()
);
},
}); });
this.addCommand({ this.addCommand({

2335
nouns.ts Normal file

File diff suppressed because it is too large Load diff

26
package-lock.json generated
View file

@ -9,11 +9,13 @@
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/lodash-es": "^4.17.12",
"@types/node": "^16.11.6", "@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0", "@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0", "@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0", "builtin-modules": "3.3.0",
"esbuild": "0.17.3", "esbuild": "0.17.3",
"lodash-es": "^4.17.21",
"obsidian": "latest", "obsidian": "latest",
"tslib": "2.4.0", "tslib": "2.4.0",
"typescript": "4.7.4" "typescript": "4.7.4"
@ -581,6 +583,23 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/lodash": {
"version": "4.17.12",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.12.tgz",
"integrity": "sha512-sviUmCE8AYdaF/KIHLDJBQgeYzPBI0vf/17NaYehBJfYD1j6/L95Slh07NlyK2iNyBNaEkb3En2jRt+a8y3xZQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/lodash-es": {
"version": "4.17.12",
"resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.12.tgz",
"integrity": "sha512-0NgftHUcV4v34VhXm8QBSftKVXtbkBG3ViCjs6+eJ5a6y6Mi/jiFGPc1sC7QK+9BFhWrURE3EOggmWaSxL9OzQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/lodash": "*"
}
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "16.18.114", "version": "16.18.114",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.114.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.114.tgz",
@ -1779,6 +1798,13 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/lodash-es": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz",
"integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==",
"dev": true,
"license": "MIT"
},
"node_modules/lodash.merge": { "node_modules/lodash.merge": {
"version": "4.6.2", "version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",

View file

@ -12,11 +12,13 @@
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/lodash-es": "^4.17.12",
"@types/node": "^16.11.6", "@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0", "@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0", "@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0", "builtin-modules": "3.3.0",
"esbuild": "0.17.3", "esbuild": "0.17.3",
"lodash-es": "^4.17.21",
"obsidian": "latest", "obsidian": "latest",
"tslib": "2.4.0", "tslib": "2.4.0",
"typescript": "4.7.4" "typescript": "4.7.4"