#let seshatversion = "26.7"
#let seshatbaseurl = "https://demo.seshat.software"
#let seshatgiturl = "https://codeberg.org/seshat/seshat"

#let render_code_output(output) = raw(
  output.text
      .replace("<seshatbaseurl>", seshatbaseurl)
      .replace("<seshatgiturl>", seshatgiturl)
      .replace("<seshatversion>", seshatversion),
  block: true,
  lang: "json",
)

#let commitLink(hash) = {
  let short = hash.slice(0, 9)
  link(seshatgiturl + "/commit/" + hash)[#raw(short)]
}

#let chapter_counter = counter("chapter")
#let render_chapter(chapter_text, chapter_title) = {
  let target = dictionary(std).at("target", default: () => "paged")
  chapter_counter.step()
  context if target() == "paged" {
    set page(header: align(right)[#chapter_title], numbering: "1")
  }
  context if target() == "html" {
    let n = chapter_counter.get().first()
    html.elem("div", attrs: (class: "chapter", id: "chapter-" + str(n)))[ #chapter_text ]
  } else [ #chapter_text ]
}

#let code(text) = raw(text)

#let pagebreak-when-paged() = {
    let target = dictionary(std).at("target", default: () => "paged")
    context if target() == "paged" {
        pagebreak(weak: true)
    }
}

#let silent-chapter(title) = heading(level: 1, numbering: none, outlined: true)[#title]
#let silent-section(title) = heading(level: 2, numbering: none, outlined: true)[#title]
#let silent-subsection(title) = heading(level: 3, numbering: none, outlined: true)[#title]

#let chapter-toc-css(num-chapters) = {
  let rules = range(1, num-chapters + 1).map(i =>
    "body:has(#chapter-" + str(i) + " :target) .table-of-contents > nav > ol > li:nth-child(" + str(i) + ") > ol, " +
    "body:has(#chapter-" + str(i) + ":target) .table-of-contents > nav > ol > li:nth-child(" + str(i) + ") > ol"
  ).join(",\n")
  rules + " { display: block; }"
}
