Content is not really meant to be manipulated, usually the better way is to only create content from data that has already been processed in any way necessary. This is often the right advice for e.g. tables – however, it is not really applicable to your use case because the data you have is fundamentally already formatted text.
The next best way, though, is not manipulating the full hierarchy of the content value you’re dealing with, but applying a rule. Your specific case can be handled by this code:
#show regex("\b\w+\b"): word => {
let (first, ..rest) = word.text.clusters()
first = upper(first)
(first, ..rest).join()
// or instead:
// str(_plugin.titlecase(bytes(word.text)))
}
some *important* text
The general principle here is: access deeply nested data using Typst’s tools (show and set rules), not by inspecting values.
In cases where even that doesn’t work, the approach you have is the one you need to go with. If that wasn’t already where you got your inspiration, there’s this example in the Typst examples book. You can still make your life a bit easier compared to the code you wrote, here’s an excerpt:
let update-dict(dict) = {
for (k, v) in dict { ((k): titlecase(v)) }
// or this:
// dict.pairs().map(((k, v)) => ((k): titlecase(v))).join()
}
// ...
if data.has("children") {
let (children, ..fields) = data.fields()
children = children.map(titlecase)
fields = update-dict(fields)
return data.func()(children, ..fields)
}
if data.has("body") {
let (body, ..fields) = data.fields()
body = titlecase(body)
fields = update-dict(fields)
return data.func()(body, ..fields)
}
One part that you probably missed is how to use variables as dictionary keys; see How can I instantiate a dictionary with a variable key name?