I also encountered this problem when I first used Typst.
The quirk is that Typst has three syntactical modes: markup, math, and code. Markup mode is the default in a Typst document, and it’s also the mode inside[…]; code mode is the mode after#and inside{…}. In markup mode, spaces and newlines become spaces; but in code mode, they vanish.To see that, you can copy the following Typst document to your editor, and hover the variable
markup-modeorcode-mode. You’ll see that the value ofmarkup-modeis a sequence of space, A, space, B, space, and that ofcode-modeis A and B concatenated without any space in between.#let a = "A" #let b = "B" #let markup-mode = [ #a #b ] #let code-mode = { a b }
(Copied from How can I add a dotted line like bibliography between two texts that are left and right aligned? - #5 by Y.D.X)
In your case, try replacing html.html(…)[…] with html.html(…, {…}):
#let itemplate(
title: "Document",
lang: "en",
contents,
) = {
html.html(lang: lang, {
html.head({
html.meta(charset: "utf-8")
html.title(title)
})
html.body(
html.div(class: ("container1", "container2"), {
html.span(class: ("text",))[some text]
contents
}),
)
})
}

