#metadata("start:body")
Jacob was here
#metadata("end:body")
The document is passed to a template (e.g. see here). How to get the content between those two tags?
Edit:
found a solution but it is too slow for larg(er) documents
let start_body = doc.children.position(i => i == metadata("start:body"))
let end_body = doc.children.position(i => i == metadata("end:body"))
let show_content = start_body != none and end_body != none and start_body < end_body
if show_content {
body = doc.children.slice(start_body, end_body).join()
}
So basically this is what I came up with. But as I mentioned it is way too slow.
At first I had different files for each “user-input-section” of my template. That means a file for each abstract, the main part of the document, and the appendix. But I don’t like to write text in different files. So I thought this would be a good solution. We had something like this in our Latex-template.
It doesn’t have to be a metadata-tag. I just want to write in the main document and control what goes where in the template with something like those tags.
Do you know a better solution? Those (multiple) iterations over doc.children are too much for real-time compilation.
So, are you absolutely positive that your template inputs must be written in a single file, correct? I think I had something similar once, and for readability I had to get one template/function input as include "somefile.typ":
#function(
a: [...],
b: include "file.typ",
c: [...],
)
Oh, if you mean that you have template parameters to which you feed in the whole content of your document and nothing goes below:
then this is probably not the most convenient/simple way to structure/write a document. Instead, you should include the parts directly (with or without include):
Forcing Typst to split your file into artificial sections to then merge them anyway (without any preprocessing?) is a waste of resources. To achieve the best performance you should write/structure your document the Typst way.
I might have some additional tips if you can provide some additional information, like why you want to split your document in sections in the first place? You can always give a hypothetical/generalized structure of your document if you can’t share the whole thing.
As a side note, I’m pretty sure the questing has already been answered by yourself. So it’s better to later rephrase your question and the title or create a new question or general topic. I vote for a separate topic, because it can get very messy and not straightforward. You decide.
Just as a note why there can’t be a general way to do this – what would happen if your content looked like this?
#metadata("start:body")
#block[
Jacob was
#metadata("end:body")
here
]
Should that work at all? Should the block be truncated? Two pieces of metadata don’t need to be “aligned” at the same nesting level, so this leads to problems.
An alternative approach that makes doing this impossible could look like this:
#lorem(10)
#[
Jacob was here
] <body>
#lorem(10)
This is the saved content: #context query(<body>).first()
I.e. you put the body in a scope and give that whole thing a label. Naturally, this also doesn’t work over file boundaries; crossing file boundaries by its nature can’t guarantee that start and end are at the same nesting depth.