forked from maxence-charriere/go-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.go
402 lines (372 loc) · 9.52 KB
/
markdown.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"fmt"
"path/filepath"
"github.com/gabstv/go-app/v9/pkg/app"
"github.com/gabstv/go-app/v9/pkg/logs"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
)
type markdownPage struct {
path string
title string
description string
index []string
}
func mardownPages() map[string]markdownPage {
return map[string]markdownPage{
"/": {
path: "/web/documents/home.md",
title: defaultTitle,
description: defaultDescription,
index: []string{
"go-app",
"Declarative syntax",
"Standard HTTP",
"Other features",
"Next",
},
},
"/start": {
path: "/web/documents/start.md",
title: "Getting started building a Progressive Web App with Go and WebAssembly",
description: "Introduction about how to create a Progressive Web App displaying a simple Hello World with the Go programming language (Golang) and WebAssembly (Wasm).",
index: []string{
"Getting started",
"Prerequisite",
"Install",
"Code",
"Build and run",
" Building the client",
" Building the server",
" Launching the app",
" Tips",
"Next",
},
},
"/v7-to-v8": {
path: "/web/documents/v7-to-v8.md",
title: "Migrating a go-app Progressive Web App from V7 to V8",
description: "Guide about how to migrate a Progressive Web App built with go-app V7 to V8.",
index: []string{
"V7 to V8 migration guide",
"Build directives",
"Routing",
"Package functions",
"Component interfaces",
"Resource provider",
"Concurrency",
"Next",
},
},
"/architecture": {
path: "/web/documents/architecture.md",
title: "Understanding go-app architecture",
description: "Documentation about how go-app parts are working together to deliver Progressive Web Apps out of the box?",
index: []string{
"Architecture",
"Web browser",
"Server",
"HTML pages",
"Package resources",
"app.wasm",
"Static resources",
"Next",
},
},
"/components": {
path: "/web/documents/components.md",
title: "Building components: customizable, independent, and reusable UI elements",
description: "Documentation about building customizable, independent, and reusable UI elements.",
index: []string{
"Components",
"Create",
"Customize",
"Update",
"Fields",
" Exported fields",
" Internal fields",
"Lifecycle",
" Prerender",
" Mount",
" Nav",
" Dismount",
"Extensions",
"Next",
},
},
"/concurrency": {
path: "/web/documents/concurrency.md",
title: "Building responsive Progressive Web Apps",
description: "Documentation about go-app tools that help to build reactive and concurrency safe Progressive Web Apps.",
index: []string{
"Concurrency",
"UI goroutine",
"Async",
"Dispatch",
"Defer",
"Next",
},
},
"/syntax": {
path: "/web/documents/syntax.md",
title: "A Go syntax for building beautiful UIs",
description: "Documentation about the Go (Golang) syntax to customize go-app components look, and craft beautiful UIs only with the Go Programming Language.",
index: []string{
"Declarative syntax",
"HTML elements",
" Create",
" Standard elements",
" Self closing elements",
" Style",
" Attributes",
" Event handlers",
"Text",
"Raw elements",
"Nested components",
"Condition",
" If",
" ElseIf",
" Else",
"Range",
" Slice",
" Map",
"Form helpers",
" ValueTo",
"Next",
},
},
"/js": {
path: "/web/documents/js.md",
title: "Interoperability between Go and JavaScript",
description: "Documentation about how to interact with the webpage DOM and JavaScript libraries from Go (Golang).",
index: []string{
"JavaScript and DOM",
"Include JS files",
" Page's scope",
" Inlined in Components",
"Using window global object",
" Get element by ID",
" Create JS object",
"Cancel an event",
"Get input value",
"Next",
},
},
"/lifecycle": {
path: "/web/documents/lifecycle.md",
title: "Understanding app lifecycle in the web browser",
description: "Documentation that describes how a web browser installs and updates a go-app Progressive Web App.",
index: []string{
"Lifecycle",
" First loading",
" Recurrent loadings",
" Loading after app update",
"Listen for app updates",
"Next",
},
},
"/routing": {
path: "/web/documents/routing.md",
title: "Routing pages to go-app components",
description: "Documentation about how to associate URL paths to go-app components.",
index: []string{
"Routing",
"Define a route",
" Simple route",
" Route with regular expression",
"Detect navigation",
"Next",
},
},
"/seo": {
path: "/web/documents/seo.md",
title: "Building an SEO friendly Progressive Web App",
description: "Documentation about how to make a Progressive Web App indexable by search engines with go-app package.",
index: []string{
"SEO",
"Prerendering",
" Customizing prerendering",
" Customizing page metadata",
" Caching",
"Next",
},
},
"/static-resources": {
path: "/web/documents/static-resources.md",
title: "Dealing with static resources",
description: "Documentation that describes what are static resources, how to interact, and where to host them.",
index: []string{
"Static resources",
"Access static resources",
" In Handler",
" In components",
" In CSS files",
"Setup local web directory",
"Setup remote web directory",
"Fully static app",
"Next",
},
},
"/testing": {
path: "/web/documents/testing.md",
title: "Testing components",
description: "Documentation about how to unit test components created with go-app.",
index: []string{
"Testing",
"Component server prerendering",
"Component client lifecycle",
"Asynchronous operations",
"UI elements",
"Next",
},
},
"/built-with": {
path: "/web/documents/built-with.md",
title: "Progressive Web Apps created with go-app",
description: "An index that lists Progressive Web Apps built with the go-app package.",
index: []string{
"Built with go-app",
"Lofimusic.app",
"Murlok.io",
"Astextract",
"Liwasc",
"Next",
},
},
"/install": {
path: "/web/documents/install.md",
title: "Installing Progressive Web Apps on user devices",
description: "Documentation about how to install a Progressive Web App on a user device, from Chromium-based web browsers to mobile devices.",
index: []string{
"Install",
"Desktop",
"IOS",
"Android",
"Next",
},
},
}
}
type markdownDoc struct {
app.Compo
path string
title string
index []string
markdown string
isLoading bool
err error
}
func newMarkdownDoc() *markdownDoc {
return &markdownDoc{}
}
func (d *markdownDoc) OnPreRender(ctx app.Context) {
d.init(ctx)
}
func (d *markdownDoc) OnNav(ctx app.Context) {
d.init(ctx)
d.scroll(ctx)
}
func (d *markdownDoc) init(ctx app.Context) {
path := ctx.Page().URL().Path
if d.path == path {
return
}
page, ok := mardownPages()[ctx.Page().URL().Path]
if !ok {
app.Log(logs.New("markdown page not found").
Tag("url", ctx.Page().URL().String()),
)
return
}
d.path = path
d.title = page.path
d.index = page.index
ctx.Page().SetTitle(page.title)
ctx.Page().SetDescription(page.description)
d.load(ctx, page.path)
}
func (d *markdownDoc) load(ctx app.Context, path string) {
d.isLoading = true
d.err = nil
ctx.Async(func() {
md, err := get(ctx, path)
ctx.Dispatch(func(ctx app.Context) {
d.markdown = string(md)
d.err = err
d.isLoading = false
d.scroll(ctx)
})
})
}
func (d *markdownDoc) scroll(ctx app.Context) {
fragment := ctx.Page().URL().Fragment
if fragment == "" {
fragment = "top"
}
ctx.ScrollTo(fragment)
}
func (d *markdownDoc) Render() app.UI {
return newPage().
Index(
newIndex().Links(d.index...),
).
Content(
newMarkdownContent().
Class("hspace-out-stretch").
Markdown(d.markdown),
newLoader().
Class("page-loader").
Class("fill").
Title("Loading").
Description(filepath.Base(d.title)).
Loading(d.isLoading).
Err(d.err),
).
IssueTitle(filepath.Base(d.title))
}
type markdownContent struct {
app.Compo
Iclass string
Imd string
md string
}
func newMarkdownContent() *markdownContent {
return &markdownContent{}
}
func (m *markdownContent) Class(v string) *markdownContent {
if v == "" {
return m
}
if m.Iclass != "" {
m.Iclass += " "
}
m.Iclass += v
return m
}
func (m *markdownContent) Markdown(v string) *markdownContent {
m.Imd = v
return m
}
func (m *markdownContent) OnMount(ctx app.Context) {
m.highlightCode(ctx)
}
func (m *markdownContent) OnUpdate(ctx app.Context) {
m.highlightCode(ctx)
}
func (m *markdownContent) Render() app.UI {
return app.Div().
Class("markdown").
Class(m.Iclass).
Body(
app.Raw(fmt.Sprintf("<div>%s</div>", parseMarkdown([]byte(m.Imd)))),
)
}
func (d *markdownContent) highlightCode(ctx app.Context) {
app.Window().Get("Prism").Call("highlightAll")
}
func parseMarkdown(md []byte) []byte {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
parser := parser.NewWithExtensions(extensions)
return markdown.ToHTML(md, parser, nil)
}