Hi everybody!
I’m using Typst for my work and I have some code blocks to show. Inside this code blocks, I want to highlight some parts that are important. In order to do this, I’m using two main functions
- One is used to make text Bold (I’m not using
* Bold *
markdown) - One contains a Regex and I’m looking for the bold_function and I apply a formatting rule to make the content of the bold_function bold
When I don’t choose any language for formatting the code blocks (add some color to my code), everything is working. But when I want to put some colors by adding a language to the raw function, my custom formatting isn’t working.
If I don’t use any language when declaring the raw block, it’s working well:
` ``
dfir@dfir-mbp ~ % #text_bold[hello]
` ``
But when I add the language details in the raw block, the language formatting is erasing my custom one:
` ``bash
dfir@dfir-mbp ~ % #text_bold[hello]
` ``
This is a part of my code, I’m using a function to do this:
//------------------------------------------------------------
// Bold Function
//------------------------------------------------------------
#let text_bold(body) = {text( weight: "bold", [#body])}
//------------------------------------------------------------
// Raw converter Function
//------------------------------------------------------------
#let raw_converter(body) = {
//----------------------------------------------------------
// Find all matches with function #text_bold
//----------------------------------------------------------
let matches_text_bold = body.text.matches(regex("#text_bold\[[^]]+\]{1,10}"))
//----------------------------------------------------------
// Adding the rules
//----------------------------------------------------------
show raw.where(): show_rule => {
//--------------------------------------------------------
// Set the #text_bold rule
//--------------------------------------------------------
for it in range(matches_text_bold.len()) {
// Creating the value inside the #text_bold function
let text_value = matches_text_bold.at(it).text.replace("#text_bold[","").replace("]","")
show_rule = {
// Remove the function to only keep the text inside
show matches_text_bold.at(it).text: text_bold(text_value)
show_rule
}
}
show_rule
}
body
}
#let terminalbox(
title: "",
body_box,
) = {
[... Creating the header of the Box ...]
//------------------------------------------------------
// Body of the Box
//------------------------------------------------------
rect(
width: 100%,
radius: (bottom-right: 6pt, bottom-left: 6pt),
fill: body_color
inset: (y: 8pt, x: 5pt),
stroke: (top: 0.8pt + line_color)
text(
box_text_size,
raw_converter(body_box),
fill: box_text_color,
font: box_text_type,
),
),
)
)
}
#terminalbox(title-bash: "Terminal",
` ` `bash
dfir@dfir-mbp ~ % #text_bold[hello]
` ` `)