How to refactor the following typ code, I see code duplication. It is possible to
combine the 3 show rules into a single show rule? Thank you
#show "one": name => text(green)[#name]
#show "two": name => text(red)[#name]
#show "three": name => text(orange)[#name]
Dummy text one, Dummy text two, Dummy text three
#show regex("\b(?:one|two|three)\b"): name => {
if name == [one] {
text(blue)[#name]
}
else if name == [two] {
text(red)[#name]
}
else if name == [three] {
text(green)[#name]
}
}
#[Dummy text one, Dummy text two, Dummy text three]
Another option using the same method but collecting all the text->color conversions into a table:
#let colors = (
"one": blue,
"two": red,
"three": green
)
#show regex("\b(?:" + colors.keys().join("|") + ")\b"): name => {
let key = name.text
let color = colors.at(key)
set text(color)
name
}
Dummy text one, Dummy text two, Dummy text three
The following topic also mentions another way of achieving this, using selector.or():
A single regex show rules might be more expensive than several plain text show rules.
I haven’t benchmarked but from the way the compiler is implemented, a single regex show rule is very likely to be faster than multiple plain text rules.
The regex crate used by Typst supports only “true” regular expressions that can be implemented efficiently with finite automata. This way they can guarantees O(m*n) performance (regex of size m, string of size n). You’d still need to benchmark to make sure what is faster, but at least it tells us we won’t get into pathological cases as can happen with Perl-compatible regular expressions, as explained in this article.