How to customize codly raw blocks?

I recently asked the question How can I include user privilege indicators in shell prompts while keeping syntax highlighting enabled in codeblocks? The solution I recieved works well for regular raw blocks. My question is, how can I apply it to codly?

To recap, I would like to place $ or # at the beginning of Linux shell prompt snippets while keeping syntax highlighting, like so:

$ chmod test.sh
# chown user test.sh

Currently, when I try the show rule included in the answer, it seems to disable codly’s formatting for subsequent raw blocks.


#import "@preview/codly:1.2.0": *
#import "@preview/codly-languages:0.1.1": *
#show: codly-init.with()

Before applying custom show rule:

```console
$ chmod test.sh
# chown user test.sh
```

#show raw: set text(font: "DejaVu Sans Mono", size: 1.11em)
#show raw.where(lang: "console"): it => {
  let user = emph(raw("$ "))
  let root = emph(raw("# "))
  it
    .text
    .split("\n")
    .map(line => {
      let starts(pattern) = line.starts-with(pattern)
      let prefix = if starts("$ ") { user } else if starts("# ") { root }
      let text = line.replace(regex("^[$#] "), "")
      prefix + raw(text, lang: "sh")
    })
    .join("\n")
}

After applying custom show rule:

```console
$ chmod test.sh
# chown user test.sh
```
Screenshot

Yeah, I think Codly is not meant to be used with custom raw show rules. I also had an issue when I tried to do similar stuff. I even tried using __codly-raw, but it doesn’t work, because it’s meant to be use on raw element and not any content, like in the custom show rule you show. So I think you’re better off recreating parts of Codly yourself or finding other packages. showybox is good. Initially I hand-rolled my frame and layout, but it was absolutely horribly written, so at some point I switched to showybox, but it was still messy, so I finally just switched to Codly, and I like it.

You can open an issue in its repository and ask if there can be a solution for this.

@Dherse (I hope you don’t mind getting pinged here): Do you have any suggestions about this issue?

I like the idea actually, I am thinking of adding a “transformer” API to allow people to “transform” content, but I don’t know how and where to include it. The problem of your show rule is that it does not produce a new raw block. You should use raw line show rules: Typst

1 Like

Thank you for your reply (and also for this wonderful package)!

I would like to use syntax highlighting that is normally applied for the lang setting sh (except it should be isolated from $ and #).

When I try editing the project you shared on my machine in order to achieve that, I get the error maximum show rule depth exceeded. I modified line 24 like so:

  #show raw: set text(font: "DejaVu Sans Mono", size: 1.11em)
  #show raw.where(lang: "console"): it => {
    show raw.line: it => {
      let user = emph(("$ "))
      let root = emph(("# "))
      it
        .text
        .split("\n")
        .map(line => {
          let starts(pattern) = line.starts-with(pattern)
          let prefix = if starts("$ ") { user } else if starts("# ") { root }
          let text = line.replace(regex("^[$#] "), "")
-         prefix + text
+         prefix + raw(text, lang: "sh")
        })
        .join("\n")
    }
    it
  }

That is exactly what I was talking about here:

Oh I see what you guys mean, lemme try something

I updated the example, it should mostly work but only on single line syntaxes, syntax highlighting that spans multiple lines would not work.

1 Like

So was the solution to do this?

  show raw.line: it => {
    show raw.line: it => it.body

This is actually next level hackery.

1 Like

Nice! It’s almost perfect for my needs!

The only remaining thing I’m wondering about is how to make the font size the same as for other codly raw blocks. I’m noticing that both in the screenshot (and also on my machine, after I compile the code), that the font size is slightly bigger, which is especially noticable when I compare the word console on the right between the two snippets.

(Setting size exactly to 1.11em was my idea, originating from the other thread about regular (non-Codly) raw blocks, as it looked very similar in size to vanilla raw blocks.)