There are some properties I use with Obsidian Publish, and I wanted to automate collecting them a little bit, so I don't end up forgetting. So, I decided to create a small quickadd script to use when I want to publish a note. The script will:
- Change `publish` to `true`
- Add `date` to current date (so it's publish date)
- Ask for `image`, `description` and `permalink`
- Permalink defaults to note title, and then it'll lowercase and replace spaces with `+`
- Also check which `cssClasses` I want to use, using sliders
I also use the `linter` plugin to add `date created` and `date modified`.
```javascript
module.exports = async (params) => {
const {
quickAddApi: { inputPrompt, checkboxPrompt },
} = params;
const noteFile = app.workspace.getActiveFile(); // Currently Open Note
if (!noteFile.name) return; // Nothing Open
const propertiesToUpdate = {};
const fmc = app.metadataCache.getFileCache(noteFile)?.frontmatter;
propertiesToUpdate["publish"] = true;
if (!fmc["date"]) {
propertiesToUpdate["date"] = moment().format("YYYY-MM-DD");
}
if (!fmc["image"]) {
let newImage = await inputPrompt("Image?");
if (newImage) {
propertiesToUpdate["image"] = newImage.includes("/")
? newImage
: "assets/" + newImage;
}
}
if (!fmc["permalink"]) {
let newPermalink = await inputPrompt(
"Want a permalink?",
noteFile.basename,
noteFile.basename
);
newPermalink = newPermalink.replaceAll(" ", "+").toLowerCase();
// TODO: check if permalink already exists
// TODO: check if permalink has invalid URL characters
if (newPermalink) propertiesToUpdate["permalink"] = newPermalink;
}
if (!fmc["description"]) {
const newDescription = await inputPrompt("Description?");
if (newDescription) {
propertiesToUpdate["description"] = newDescription;
}
}
if (!fmc["cssclasses"]) {
const newClasses = await checkboxPrompt(
["img-zoom", "img-grid", "hero-banner"],
[]
);
if (newClasses && newClasses.length > 0) {
propertiesToUpdate["cssclasses"] = newClasses;
}
}
await app.fileManager.processFrontMatter(noteFile, (frontmatter) => {
for (const [key, value] of Object.entries(propertiesToUpdate)) {
frontmatter[key] = value;
}
});
};
```