Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom template with rehype-typst #618

Open
ZreXoc opened this issue Dec 17, 2024 · 5 comments
Open

Use custom template with rehype-typst #618

ZreXoc opened this issue Dec 17, 2024 · 5 comments

Comments

@ZreXoc
Copy link

ZreXoc commented Dec 17, 2024

Is your feature request related to a problem? Please describe.
I want to use packages (e.g. physica) in my math, and I have patch the inlineMathTemplate and displayMathTemplate with #import "@preview/physica:0.9.4": * manually.

Describe the solution you'd like
add options that can pass custom template to rehype-typst like this:

  .use(rehypeTypst, {
     displayMathTemplate:(code)=> `
#import "@preview/physica:0.9.4": *
#set page(height: auto, width: auto, margin: 0pt)

$ ${code} $
`,
     inlineMathTemplate: `...`,
  })

or just write extra lines to be added before the default templates.

.use(rehypeTypst, {
     displayMathTemplate:`#import "@preview/physica:0.9.4": *`,
     inlineMathTemplate: `#import "@preview/physica:0.9.4": *`,
  })

And I think more than just rendering math is a good idea. Such as rendering some diagrams:

  .use(rehypeTypst, {
    {
         templates: {
          cetz: (content) => `
            #import "@preview/cetz:0.3.1"

            #cetz.canvas({
              import cetz.draw: *
              ${content}`,
          diagram: (content) => `
            #import "@preview/cetz:0.3.1"

            #cetz.canvas({
              #figure(
                diagram(
                  ${content},
                ), caption: "...",
              )
            }),
            `
        }
  }
}
```typst-cetz
circle((0, 0))
line((0, 0), (2, 1))
@Myriad-Dreamin
Copy link
Owner

Myriad-Dreamin commented Dec 28, 2024

Rendering fenced code really looks like a killer feature. But we can be more fit in typst-y style setup.

If I want to extend rehype-typst to support the example in description, I might make the following design:

_
.use(rehypeTypst, {
   transformers: [
     {
       type: FencedCodeMatcher,
       language: /^typst-cetz$/,
       use: 'my-cetz-template.typ',
       show: 'typst-cetz-main',
     }
   ]
}

And implement the show rule typst-cetz-main in our "my-cetz-template.typ" file:

#let typst-cetz-main(content, caption: none, ..more-args) = {
  import "@preview/cetz:0.3.1"
  show raw.where(lang: "typst-cetz"): it => cetz.canvas({
    import cetz.draw: *
    let cetz-scope = (..);
    figure(diagram(eval(it.text, cetz-scope)), caption: caption)
  })
  content        
}

It would capture the typst-cetz content and compile the content by the following template:

#import "my-cetz-template.typ": typst-cetz-main
#show: typst-cetz-main.with(..sys.inputs)
${content}

@Myriad-Dreamin
Copy link
Owner

@Enter-tainer

@Myriad-Dreamin
Copy link
Owner

eval(it.text, cetz-scope)

Add some comment, It is fancy but in practical we cannot report the error with precise source location anymore.

@Enter-tainer
Copy link
Collaborator

my two cents:

  1. @Myriad-Dreamin 's design makes a lot of sense to me. user can register template and hook for various languages(typst-*). Just to confirm, if i understand it correctly, it is for codeblocks but not inline/block math equations.
  2. Another problem is that, current template(especially inline math) is more complex than it appears to be. To correctly layout inline equations, we use black magic to query to baseline height. I think it will be unlikely to work if arbitary template is used.

@Myriad-Dreamin Myriad-Dreamin changed the title Use custom math template with rehype-typst Use custom template with rehype-typst Dec 29, 2024
@Myriad-Dreamin
Copy link
Owner

Just to confirm, if i understand it correctly, it is for codeblocks but not inline/block math equations.

The title was unmatched. I edited the title to match their description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants