comparison wdown.go @ 8:4a25b534c81c javascript-experiment

Add v8 engine and include katex
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 17 Jun 2020 21:43:52 +0200
parents a5aa39557726
children
comparison
equal deleted inserted replaced
7:a5aa39557726 8:4a25b534c81c
12 "github.com/yuin/goldmark" 12 "github.com/yuin/goldmark"
13 highlighting "github.com/yuin/goldmark-highlighting" 13 highlighting "github.com/yuin/goldmark-highlighting"
14 meta "github.com/yuin/goldmark-meta" 14 meta "github.com/yuin/goldmark-meta"
15 "github.com/yuin/goldmark/extension" 15 "github.com/yuin/goldmark/extension"
16 "github.com/yuin/goldmark/parser" 16 "github.com/yuin/goldmark/parser"
17 "rogchap.com/v8go"
17 ) 18 )
18 19
19 func main() { 20 func main() {
20 var templateFilename string 21 var templateFilename string
21 22
71 log.Fatal(err) 72 log.Fatal(err)
72 } 73 }
73 } else { 74 } else {
74 fmt.Print(htmlContent) 75 fmt.Print(htmlContent)
75 } 76 }
77
78 katexFormula := "c = \\pm\\sqrt{a^2 + b^2} = \\pm x^2"
79 katexRenderOptions := `{
80 throwOnError: false,
81 displayMode: true,
82 }
83 `
84 ctx, _ := v8go.NewContext(nil) // creates a new V8 context with a new Isolate aka VM
85 ctx.RunScript(katexSrc(), "katex/katex.js")
86 ctx.RunScript("const katexFormula = String.raw`"+katexFormula+"`", "main.js") // executes a script on the global context
87 ctx.RunScript("const mathHtml = katex.renderToString(katexFormula,"+katexRenderOptions+")", "main.js") // any functions previously added to the context can be called
88 mathHtml, _ := ctx.RunScript("mathHtml", "value.js") // return a value in JavaScript back to Go
89 fmt.Println(mathHtml)
76 } 90 }
91
92 func katexSrc() string {
93 katexBytes, err := ioutil.ReadFile("katex/katex.js")
94 if err != nil {
95 log.Fatal(err)
96 }
97
98 return string(katexBytes)
99 }