~xigoi/xidoc

564728c68d213dc3202b2c68a1988453d2a4b54b — Adam Blažek 2 years ago 2d211b3 table-of-contents
Start of table of contents
1 files changed, 29 insertions(+), 0 deletions(-)

A src/xidocpkg/table_of_contents.nim
A src/xidocpkg/table_of_contents.nim => src/xidocpkg/table_of_contents.nim +29 -0
@@ 0,0 1,29 @@
from std/htmlgen as htg import nil
import ./types
import std/strutils
import std/sugar
import std/typetraits

type
  TableOfContentsEntry = object
    name: string
    link: string
    children: seq[TableOfContentsEntry]
  TableOfContents* = distinct seq[TableOfContentsEntry]

func renderHtml*(entries: seq[TableOfContentsEntry]): string =
  let renderedEntries = collect:
    for entry in entries:
      htg.li:
        if entry.link == "":
          entry.name
        else:
          htg.a(href = entry.link, entry.name)
  renderedEntries.join

func render*(toc: TableOfContents, target: Target): string =
  case target
  of tHtml:
    let renderedChildren = toc.distinctBase.renderHtml
    result = htg.ol(class = "xd-table-of-contents", renderedChildren)
  else: discard