obsidian-digital-garden
obsidian-digital-garden copied to clipboard
FR: Batch/Bulk add "dg-publish: true"
is there a way to bulk-add this to a selection of files/search/dataview-result?
would be great as I have hundreds of files related to a project (scattered all over the place as I dont use folders to separate projects) .... thanks!
I did try to make a feature in the plugin, where one could mass select files in folders easily. But it was a mess, so I scrapped it. I might add something in the future. π
For now, the best way I've found, as described in the tips and tricks section, is using the MetaEdit plugin.
Here is a Deno script that can help...
#!/usr/bin/env -S deno run --allow-read --allow-write
import { join } from "https://deno.land/std/path/mod.ts";
import { walk } from "https://deno.land/std/fs/walk.ts";
for await (
const note of walk(Deno.cwd(), {
includeDirs: false,
exts: ["md"],
skip: [/^\.git$/, /README.md/],
})
) {
const contents = await Deno.readTextFile(note.path);
const match = contents.match(/^---\n(.+)---\n(.+)$/s);
const publishTag = "dg-publish: true";
const isHome = note.path === join(Deno.cwd(), "Home.md");
const homeTag = "\ndg-home: true";
const frontmatter = `---\n${match ? match[1] : ""}${publishTag}${
!isHome ? "" : homeTag
}\n---\n`;
Deno.writeTextFileSync(
note.path,
`${frontmatter}${match ? match[2] : contents}`,
);
}
It walks a vault (ignoring the .git
directory) and for each markdown file that's NOT named README.md
it adds dg-publish: true
to the frontmatter of all notes, while preserving any existing frontmatter (dg-*
tags are appended at the end of existing tags). For a file named Home.md
in the project root, it will add the dg-home: true
tag.
Note to self: It could be a good idea to add a view to the publication center where users can toggle files that doesn't have dg-publish set and bulk add it similarly to how you can bulk publish notes.
super simple Bash script that can help with batch editing. Isn't a perfect solution, but should help if you're starting from scratch
This assumes that
dg-publish: true
is always the 2nd line in any of your published files.
like so
---
gd-publish: true
---
#!/bin/bash
# cd /mnt/uasis5/obsidian-livesync-data/dnd/livesync-bridge-dnd/data/vault/π Campaigns/π A Dance of Matter/Itemsπ
cd /home/icicle/scripts/test-md
for file in ./*.md; do
## check if first line is "---"
FIRSTLINE=$(head -n 1 "$file")
SECONDLINE=$(sed -n '2p' "$file")
## if first line is ---
if [ "$FIRSTLINE" = "---" ]; then
## se if "dg-publish: true is already 2nd line"
if [ "$SECONDLINE" = 'dg-publish: true' ]; then
echo "Already PUBLISHED: $file"
else
sed -i '2i\dg-publish: true' "$file"
fi
else
# first line is something else
sed -i '1s/^/---\ndg-publish: true\n---\n/' "$file"
fi
done