#A heavily documented example of static site using HTML export - Typst CN News (Unofficial)

38 messages ยท Page 1 of 1 (latest)

lapis kiln
#

Thanks for typst team and @azure lodge, @timid yoke, @split pulsar, we have a very happy experience on building a static website using typst 0.13.0's HTML export. We hadn't had any frustrating problem and everything goes smooth. Impressively, for example, although typst doesn't have support to export math equations to HTML, we can support it on our side with an oneline show rule:

#let div-frame(content, attrs: (:)) = html.elem("div", html.frame(content), attrs: attrs)
show math.equation: div-frame.with(attrs: ("style": "display: flex; justify-content: center; overflow-x: auto;"))

Using 84.3% Typst, 2.3% CSS, and 0.9% HTML, it even doesn't use any static site generator or bundler, but a simple build script that calls typst compile and typst query.

What's the input:

  • assets are fonts, styles, icons, and tmTheme files used by the typst compiler.
  • typ/packages are local packages.
  • typ/templates are common template and components.
  • src/ are common source code used by documents.
  • content/{zh-CN, en} are documents in specific languages.

How it builds the site:

  • npm install to install the compiler, no any other external software and packages are required.
  • npm run build: build the output once.
  • npm run build -- --url-base=/news/: build the output once to host on my.github.io/news/
  • npm run dev: watch the whole project.
  • npm run serve: starts a local http server for preview.

To add a new content (news):

Create a typst file under the content/en/news/date/ containing the tempalte initialization:

#import "/typ/templates/news.typ": *

#show: news-template.with(
  date: "2025-02-06",
  title: "Typst 0.13.0, RC 1 released",
  lang: "en",
  tags: ("update",),
  description: "Typst 0.13.0, RC 1 was released.",
)

Other translated files can be optionally created.

Website: https://typst-doc-cn.github.io/news

cobalt tiger
lapis kiln
split pulsar
#

hope u get the Typstown pun ๐Ÿ˜†

fallow mist
lapis kiln
terse coral
#

Is it possible currently to html export and pdf export the same typ file, which also controlling <head> of the html?

lapis kiln
#

The way to detect pdf export:

#let has-html-feature = ("target" in dictionary(std))
#let target = if has-html-feature {
  std.target()
} else {
  "paged"
}
terse coral
#

but where does the pdf export get the content from? It will ignore the html.elems

lapis kiln
#

don't put html.elem in your document and make you owned elements be aware of target

terse coral
lapis kiln
#

Perhaps the workaround can be only using the has-html-feature, which doesn't acquire to be wrapped in context

#

You could explore it by yourself.

weak steppe
#

can't wait to use typst to render my website.

weak steppe
#

The inline math will not renderred correctly "This is test inline math: $A = pi r^2$. look at this: $A = pi r^2$. looks good."

fallow mist
#

Maybe it is browser-dependent behaviour, could you share the resulting html?

#

(I would also check that later)

fallow mist
#

This renders fine for me:

#show math.equation.where(block: false): it => html.elem("span", html.frame(it)) 
#show math.equation.where(block: true): it => html.frame(it) 

This is test inline math: $A = pi r^2$. look at this: $A = pi r^2$. looks good.
#

Works fine in chrome.

#

Are you sure you don't have conflicting show rules and/or css/js code?

weak steppe
#

my code

#import "/typ/templates/news.typ": *
// #import "/typ/templates/template.typ": *

#show: news-template.with(
  date: "2025-02-09",
  title: "Typst 0.13.0, RC 1 released ioui",
  lang: "en",
  tags: ("update",),
  description: "Typst 0.13.0, eeeRC 1 was released.",
)


#show math.equation.where(block: false): it => html.elem("span", html.frame(it)) 
#show math.equation.where(block: true): it => html.frame(it) 

This is test inline math: $A = pi r^2$. look at this: $A = pi r^2$. looks good.

= Math

This is test inline math: $A = pi r^2$. look at this: $A = pi r^2$. looks good.
#

my website

#

what i do is I remove chinese version. trying to fork a new one and see what happen.

weak steppe
#

Fork a new one, still doesn't work, the following is the html generated

</style>
      <h1 class="main-title">Typst 0.13.0, RC 1 released</h1>
      <div class="news-prop">Published At: 2025-02-06  Tags: update</div>
      <div class="news-body">
        <p>Typst 0.13.0, RC 1 was releases. <a href="https://staging.typst.app/docs/changelog/0.13.0">Changelog</a>.</p>
        <p>This is test inline math: <span><svg style="overflow: visible;" class="typst-doc" viewBox="0 0 0 0" width="0pt" height="0pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    <g/>
</svg>
</span>. look at this: <span><svg style="overflow: visible;" class="typst-doc" viewBox="0 0 0 0" width="0pt" height="0pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    <g/>
</svg>
</span>. looks good.</p>
        <h2>Math</h2>
        <p>This is test inline math: <span><svg style="overflow: visible;" class="typst-doc" viewBox="0 0 0 0" width="0pt" height="0pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    <g/>
</svg>
</span>. look at this: <span><svg style="overflow: visible;" class="typst-doc" viewBox="0 0 0 0" width="0pt" height="0pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    <g/>
</svg>
</span>. looks good.</p>
        <svg style="overflow: visible;" class="typst-doc" viewBox="0 0 0 0" width="0pt" height="0pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:h5="http://www.w3.org/1999/xhtml">
    <g/>
</svg>
fallow mist
#

I'm quite sure the problem is somewhere inside.

#

Maybe css, as html looks okay.

#

Okay, it's definitely not full or well-formed. The <g> tags are closing, but not opening.

weak steppe
#

I guess I may make a stupid mistake some where, but I dont what it is.

fallow mist
#

Cloned necessary repos, removed unnecessary news bases, got a blank page with several console errors. :)

weak steppe
#

Thanks for investigating it!

lapis kiln
#

the <g/> are suspicious. but I also don't know what thing produces empty svg frames in your document. they are all zero sized.