Improve formatting options

This commit is contained in:
Endeavorance 2024-10-28 07:34:16 -04:00
parent 9c65740da2
commit 8549e949dd
2 changed files with 74 additions and 20 deletions

51
format.ts Normal file
View file

@ -0,0 +1,51 @@
import { moment } from "obsidian";
import { adjectives } from "adjectives.js";
import { capitalize, sample } from "lodash-es";
import { nouns } from "nouns.js";
interface FormatOptions {
ext?: string;
}
export function format(str: string, options?: FormatOptions): string {
let formatted = str.trim();
let extension = options?.ext ?? "";
if (extension.length > 0 && !extension.startsWith(".")) {
extension = `.${extension}`;
}
// Replace {N} with a random noun
formatted = formatted.replace(/{N}/g, () => {
return capitalize(sample(nouns));
});
// Replace {n} with a random noun (lower case)
formatted = formatted.replace(/{n}/g, () => {
return sample(nouns);
});
// Replace {A} with a random adjective
formatted = formatted.replace(/{A}/g, () => {
return capitalize(sample(adjectives));
});
// Replace {a} with a random adjective (lower case)
formatted = formatted.replace(/{a}/g, () => {
return sample(adjectives);
});
// Replace {MW} with the current month week
const monthWeek = `${Math.ceil(new Date().getDate() / 7)}`;
formatted = formatted.replace(/{MW}/g, monthWeek);
// Replace {time <moment format>} with the moment-formatted string
formatted = formatted.replace(
/{time (.+?)}/g,
(_: string, pattern: string) => {
return moment().format(pattern);
}
);
return formatted + extension;
}

43
main.ts
View file

@ -3,13 +3,10 @@ import {
Plugin,
PluginSettingTab,
Setting,
moment,
Vault,
Notice,
} from "obsidian";
import { nouns } from "./nouns.js";
import { adjectives } from "./adjectives.js";
import { capitalize, sample } from "lodash-es";
import { format } from "./format.js";
interface ScrapsPluginSettings {
scrapsRootDir: string;
@ -19,16 +16,10 @@ interface ScrapsPluginSettings {
const DEFAULT_SETTINGS: ScrapsPluginSettings = {
scrapsRootDir: "_Scraps",
scrapsPathFormat: "MM MMM/R",
scrapsFileName: "ddd MMM DD hhmmssa",
scrapsPathFormat: "{time YYYY} Week {time WW MMM}",
scrapsFileName: "{time DDDD} {A} {N}",
};
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> {
const pathParts = folderPath.split("/");
@ -63,15 +54,13 @@ export default class ScrapsPlugin extends Plugin {
}
getScrapDir(): string {
return `${this.settings.scrapsRootDir}/${getFormattedDate(
return `${this.settings.scrapsRootDir}/${format(
this.settings.scrapsPathFormat
)}`;
}
getScrapFileName(): string {
const adj = capitalize(sample(adjectives));
const noun = capitalize(sample(nouns));
return `Scrap ${adj} ${noun}.md`;
return format(this.settings.scrapsFileName, { ext: "md" });
}
getScrapFilePath(): string {
@ -197,30 +186,44 @@ class ScrapsSettingTab extends PluginSettingTab {
})
);
new Setting(containerEl)
const pathFormatSetting = new Setting(containerEl)
.setName("Scraps Path Format")
.setDesc("The format of the path to the scrap")
.setDesc(
`Preview: ${format(this.plugin.settings.scrapsPathFormat)}`
)
.addText((text) =>
text
.setPlaceholder("Enter format")
.setValue(this.plugin.settings.scrapsPathFormat)
.onChange(async (value) => {
this.plugin.settings.scrapsPathFormat = value;
pathFormatSetting.setDesc(`Preview: ${format(value)}`);
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
const fileFormatSetting = new Setting(containerEl)
.setName("Scraps File Name Format")
.setDesc("The format of the filename")
.setDesc(
`Preview: ${format(this.plugin.settings.scrapsFileName, {
ext: "md",
})}`
)
.addText((text) =>
text
.setPlaceholder("Enter format")
.setValue(this.plugin.settings.scrapsFileName)
.onChange(async (value) => {
this.plugin.settings.scrapsFileName = value;
fileFormatSetting.setDesc(
`Preview: ${format(value, { ext: "md" })}`
);
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Formatting Help")
.setDesc("Tokens: {time <moment format>}, {A}, {N}, {MW}");
}
}