Skip to content

Commit

Permalink
minib3: nbTextToHtml
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroppeter committed Feb 28, 2024
1 parent d47349e commit fd2dc27
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sandbox/minib3/minib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type
backend: NbRender

# nimib.nim
import markdown

template nbInit* =
var nb {. inject .}: Nb
nb.doc = NbDoc()
Expand All @@ -39,11 +41,17 @@ func nbImageToHtml*(blk: NbBlock): string =
let blk = blk.NbImage
"<img src= '" & blk.url & "'>"

func nbTextToHtml*(blk: NbBlock): string =
let blk = blk.NbText
{.cast(noSideEffect).}: # not sure why markdown is marked with side effects
markdown(blk.text, config=initGfmConfig())

template addToBackend*(kind: string, f: NbRenderFunc) =
nb.backend.funcs[kind] = f

template nbInitBackend* =
addToBackend("NbImage", nbImageToHtml)
addToBackend("NbImage", nbImageToHtml)
addToBackend("NbText", nbTextToHtml)

func render(nb: Nb, blk: NbBlock): string =
if blk.kind in nb.backend.funcs:
Expand All @@ -58,13 +66,14 @@ when isMainModule:
import print
# hello.nim
nbInit
nbText: "hi"
nbText: "*hi*"
nbImage("img.png")
nbSave

print nb
print nb.doc.blocks[0]
print nb.doc.blocks[0].NbText
print nb.render nb.doc.blocks[0]
# print nb.blocks[0].NbImage # correctly fails at runtime
print nb.doc.blocks[1].NbImage
print nb.render nb.doc.blocks[1]

0 comments on commit fd2dc27

Please sign in to comment.