Add metadata fields using dataviewjs

Hi. I need to calculate values that I want to display on my page at different spots.
I have a set of Modifier pages, which contain metadata like hp-modifier and speed-modifier.
I want to sum up all of the hps and speeds.

Using DQL I managed to calculate the values, but I cannot store them, to dispay them on various different places. Also a plus would be if I could query these values. So i want to store them as metadata. My DQL expression:

TABLE WITHOUT ID 
sum(map(rows, (r) => default(r.hp-modifier, 0))) + this.STR AS "HP",
sum(map(rows, (r) => default(r.speed-modifier, 0))) + this.DEX AS "SPEED"
FROM "Worlds/Compendium/Modifiers"
WHERE contains(this.modifiers, file.link)
GROUP BY true

Is there a way to add multiple metadata fields to my page in one block.
An alternative would be using inline dataviewjs like so:
[hp:: $= dv.query(<DQL>)], but this would have to run the query for all the new fields.

I’ve been fidgeting with the following:

use dv.paragraph to define metadata on page with the [xd::value] syntax

const mods = dv.pages('"Worlds/Compendium/Modifiers"')
const hp_sum = mods.hp.array().reduce((acc, val) => acc + val, 0)
dv.paragraph("[HP::" + hp_sum + "]")

HP: =this.hp

While the codeblock renders nicely like a [xd::value] originally would, the this.hp returns the value like this:
image

Is there a way to add a metadata definition using dv.paragraph? Or maybe any alternative?

I ended up using obsidian’s processFrontMatter function and it works perfectly.

This is what I ended up with and I’m very happy with the results. This code adds together the values and lists of the given property names and adds them to the frontmatter of one file.

const curr = dv.current();
const currentFile = app.vault.getAbstractFileByPath(curr.file.path);
const config = dv.page("Worlds/Compendium/Rules/Config.md")

const mods = dv.pages(config.modifiers_path)
	.where(p => curr.modifiers
		.some(mod => dv.equal(mod, p.file.link)));

await app.fileManager.processFrontMatter(currentFile, (fm) => {
fm["hp"] = mods.hp.array().reduce((acc, e) => acc + e, 0);
fm["spells"] = new Set(mods.spells.path.map(p => "[[" + dv.page(p).file.name + "]]"))
});
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.