-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwasm.go
53 lines (41 loc) · 1.33 KB
/
wasm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// +build js,wasm
package main
import (
"github.com/bradleyjkemp/godoc-playground/preview"
"regexp"
"strings"
"syscall/js"
)
var sourcePane js.Value
var previewPane js.Value
type ToastifyOptions struct {
text string
duration int
}
var updatePreview = js.NewCallback(func(args []js.Value) {
source := args[0].Get("detail").String()
page, err := preview.GetPageForFile(source)
if err != nil {
js.Global().Call("showErrorToast", err.Error())
return
}
js.Global().Call("updatePreview", sanitize(page))
})
var nonAnchorHref = regexp.MustCompile(`<a href="[^#].*?"`)
func sanitize(page string) string {
// Rewrite static assets to point to local copies
page = strings.Replace(page, "/lib/godoc", "./ext", -1)
// Remove href's which will break the iframe if clicked on
// This is any href which isn't an anchor
page = nonAnchorHref.ReplaceAllString(page, `$0 style="pointer-events:none"`)
return page
}
func main() {
sourcePane = js.Global().Get("document").Call("getElementById", "codeInput")
previewPane = js.Global().Get("document").Call("getElementById", "previewPane")
previewPane.Call("addEventListener", "updatePreview", updatePreview)
// Now that handler is registered, trigger a render to display initial content
js.Global().Call("triggerRender")
// keep program alive to process callbacks
<-make(chan struct{})
}