moul_md.gno
12.42 Kb · 431 lines
1package moul_md
2
3import "gno.land/p/moul/md"
4
5// This package demonstrates the usage of the gno.land/p/moul/md package.
6
7func Render(path string) string {
8 sections := []string{
9 renderIntro(),
10 renderTextFormatting(),
11 renderHeaders(),
12 renderLists(),
13 renderLinksAndImages(),
14 renderCode(),
15 renderBlockquotes(),
16 renderAdvancedFeatures(),
17 renderUtilityFunctions(),
18 renderPracticalExample(),
19 renderBestPractices(),
20 renderSeeAlso(),
21 }
22
23 output := ""
24 for _, section := range sections {
25 output += section
26 }
27 return output
28}
29
30func renderIntro() string {
31 return md.H1("Package Demo: `p/moul/md` ") +
32 md.Paragraph("This document demonstrates the features of the "+md.Link("gno.land/p/moul/md", "https://gno.land/p/moul/md")+" package, showing both the source code and the rendered result for each feature.") +
33 "\n" +
34 md.Blockquote(md.Bold("Note:")+" The md package provides helper functions to generate markdown programmatically, making it easier to create dynamic documentation and content.") +
35 "\n"
36}
37
38func renderTextFormatting() string {
39 return md.H2("Text Formatting") + "\n" +
40 // Bold
41 renderExample(
42 "Bold Text",
43 `md.Bold("Important text")`,
44 md.Bold("Important text"),
45 ) +
46 // Italic
47 renderExample(
48 "Italic Text",
49 `md.Italic("Emphasized text")`,
50 md.Italic("Emphasized text"),
51 ) +
52 // Strikethrough
53 renderExample(
54 "Strikethrough Text",
55 `md.Strikethrough("Deprecated feature")`,
56 md.Strikethrough("Deprecated feature"),
57 ) +
58 // Combined
59 renderExample(
60 "Combined Formatting",
61 `md.Bold(md.Italic("Very important"))`,
62 md.Bold(md.Italic("Very important")),
63 )
64}
65
66func renderHeaders() string {
67 code := `md.H1("Main Title")
68md.H2("Section")
69md.H3("Subsection")
70md.H4("Sub-subsection")
71md.H5("Minor heading")
72md.H6("Smallest heading")`
73
74 result := md.H1("Main Title") +
75 md.H2("Section") +
76 md.H3("Subsection") +
77 md.H4("Sub-subsection") +
78 md.H5("Minor heading") +
79 md.H6("Smallest heading")
80
81 return md.H2("Headers") +
82 md.Paragraph("The package supports all six levels of markdown headers:") +
83 "\n" +
84 md.LanguageCodeBlock("go", code) +
85 "\nResult:\n" +
86 result +
87 "\n"
88}
89
90func renderLists() string {
91 output := md.H2("Lists") + "\n"
92
93 // Bullet Lists
94 bulletCode := `items := []string{
95 "First item",
96 "Second item",
97 "Third item with\nmultiple lines",
98}
99md.BulletList(items)`
100
101 output += md.H3("Bullet Lists") +
102 md.LanguageCodeBlock("go", bulletCode) +
103 "\nResult:\n" +
104 md.BulletList([]string{
105 "First item",
106 "Second item",
107 "Third item with\nmultiple lines",
108 }) + "\n"
109
110 // Ordered Lists
111 orderedCode := `steps := []string{
112 "First step",
113 "Second step",
114 "Third step with\nadditional details",
115}
116md.OrderedList(steps)`
117
118 output += md.H3("Ordered Lists") +
119 md.LanguageCodeBlock("go", orderedCode) +
120 "\nResult:\n" +
121 md.OrderedList([]string{
122 "First step",
123 "Second step",
124 "Third step with\nadditional details",
125 }) + "\n"
126
127 // Todo Lists
128 todoCode := `tasks := []string{
129 "Completed task",
130 "Another completed task",
131 "Pending task",
132 "Another pending task",
133}
134completed := []bool{true, true, false, false}
135md.TodoList(tasks, completed)`
136
137 output += md.H3("Todo Lists") +
138 md.LanguageCodeBlock("go", todoCode) +
139 "\nResult:\n" +
140 md.TodoList(
141 []string{
142 "Completed task",
143 "Another completed task",
144 "Pending task",
145 "Another pending task",
146 },
147 []bool{true, true, false, false},
148 ) + "\n"
149
150 // Nested Lists
151 nestedCode := `md.BulletItem("Parent item") +
152md.Nested(
153 md.BulletItem("Nested item 1") +
154 md.BulletItem("Nested item 2") +
155 md.Nested(
156 md.BulletItem("Deeply nested"),
157 " ",
158 ),
159 " ",
160)`
161
162 nestedResult := md.BulletItem("Parent item") +
163 md.Nested(
164 md.BulletItem("Nested item 1")+
165 md.BulletItem("Nested item 2")+
166 md.Nested(
167 md.BulletItem("Deeply nested"),
168 " ",
169 ),
170 " ",
171 )
172
173 output += md.H3("Nested Lists") +
174 md.LanguageCodeBlock("go", nestedCode) +
175 "\nResult:\n" +
176 nestedResult + "\n"
177
178 return output
179}
180
181func renderLinksAndImages() string {
182 return md.H2("Links and Images") + "\n" +
183 // Regular Links
184 renderExample(
185 "Regular Links",
186 `md.Link("Gno Homepage", "https://gno.land")`,
187 md.Link("Gno Homepage", "https://gno.land"),
188 ) +
189 // User Links
190 renderExample(
191 "User Links",
192 `md.UserLink("moul")`,
193 md.UserLink("moul"),
194 ) +
195 renderExample(
196 "",
197 `md.UserLink("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")`,
198 md.UserLink("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"),
199 ) +
200 // Images
201 md.H3("Images") +
202 md.LanguageCodeBlock("go", `md.Image("Gno Logo", "/public/imgs/gnoland.svg")`) +
203 "\nResult:\n" +
204 md.Image("Gno Logo", "/public/imgs/gnoland.svg") + "\n" +
205 // Clickable Images
206 md.H3("Clickable Images") +
207 md.LanguageCodeBlock("go", `md.InlineImageWithLink("Click me!", "/public/imgs/gnoland.svg", "https://gno.land")`) +
208 "\nResult:\n" +
209 md.InlineImageWithLink("Click me!", "/public/imgs/gnoland.svg", "https://gno.land") + "\n"
210}
211
212func renderCode() string {
213 return md.H2("Code") + "\n" +
214 // Inline Code
215 renderExample(
216 "Inline Code",
217 `"Use " + md.InlineCode("gno test") + " to run tests"`,
218 "Use "+md.InlineCode("gno test")+" to run tests",
219 ) +
220 // Simple Code Block
221 md.H3("Simple Code Block") +
222 md.LanguageCodeBlock("go", `md.CodeBlock("func main() {\n println(\"Hello, Gno!\")\n}")`) +
223 "\nResult:\n" +
224 md.CodeBlock("func main() {\n println(\"Hello, Gno!\")\n}") + "\n" +
225 // Language Code Block
226 md.H3("Language-specific Code Block") +
227 md.LanguageCodeBlock("go", `md.LanguageCodeBlock("go", "package main\n\nfunc main() {\n println(\"Hello!\")\n}")`) +
228 "\nResult:\n" +
229 md.LanguageCodeBlock("go", "package main\n\nfunc main() {\n println(\"Hello!\")\n}") + "\n"
230}
231
232func renderBlockquotes() string {
233 return md.H2("Blockquotes") +
234 md.LanguageCodeBlock("go", `md.Blockquote("This is an important note.")`) +
235 "\nResult:\n" +
236 md.Blockquote("This is an important note.") + "\n" +
237 md.LanguageCodeBlock("go", `md.Blockquote("Multi-line quotes\nare also supported\nwith proper formatting.")`) +
238 "\nResult:\n" +
239 md.Blockquote("Multi-line quotes\nare also supported\nwith proper formatting.") + "\n"
240}
241
242func renderAdvancedFeatures() string {
243 output := md.H2("Advanced Features") + "\n"
244
245 // Collapsible Sections
246 output += md.H3("Collapsible Sections") +
247 md.LanguageCodeBlock("go", `md.CollapsibleSection("Click to expand", "Hidden content here!")`) +
248 "\nResult:\n" +
249 md.CollapsibleSection("Click to expand", "Hidden content here!") + "\n"
250
251 // Two Columns
252 columnsCode := `md.Columns([]string{
253 "Left column content",
254 "Right column content",
255}, false)`
256
257 output += md.H3("Two Columns") +
258 md.LanguageCodeBlock("go", columnsCode) +
259 "\nResult:\n" +
260 md.Columns([]string{
261 "Left column content",
262 "Right column content",
263 }, false) + "\n"
264
265 // Multiple Columns
266 multiColumnsCode := `md.ColumnsN([]string{
267 "Item 1", "Item 2", "Item 3",
268 "Item 4", "Item 5", "Item 6",
269}, 3, true)`
270
271 output += md.H3("Multiple Columns (3 per row)") +
272 md.LanguageCodeBlock("go", multiColumnsCode) +
273 "\nResult:\n" +
274 md.ColumnsN([]string{
275 "Item 1", "Item 2", "Item 3",
276 "Item 4", "Item 5", "Item 6",
277 }, 3, true) + "\n"
278
279 return output
280}
281
282func renderUtilityFunctions() string {
283 return md.H2("Utility Functions") + "\n" +
284 // Horizontal Rule
285 renderExampleWithRawResult(
286 "Horizontal Rule",
287 `md.HorizontalRule()`,
288 md.HorizontalRule(),
289 ) +
290 // Footnotes
291 md.H3("Footnotes") +
292 md.LanguageCodeBlock("go", `"This has a footnote[1].\n\n" + md.Footnote("1", "Footnote content here.")`) +
293 "\nResult:\n" +
294 "This has a footnote[1].\n\n" +
295 md.Footnote("1", "Footnote content here.") + "\n" +
296 // Paragraphs
297 renderExampleWithRawResult(
298 "Paragraphs",
299 `md.Paragraph("This ensures proper spacing.")`,
300 md.Paragraph("This ensures proper spacing."),
301 )
302}
303
304func renderPracticalExample() string {
305 code := `// Example function that generates a rich user profile with balanced columns
306func RenderProfile(username, name, bio string, avatar string, tasks []string, completed []bool) string {
307 // First row: Avatar/Basic Info | Bio
308 avatarSection := md.Image(name + " avatar", avatar) + "\n\n" +
309 md.H3("Basic Info") +
310 md.BulletList([]string{
311 md.Bold("Name:") + " " + name,
312 md.Bold("Username:") + " " + md.UserLink(username),
313 md.Bold("Status:") + " 🟢 Active",
314 md.Bold("Joined:") + " January 2024",
315 })
316
317 bioSection := md.H3("Bio") +
318 md.Blockquote(bio)
319
320 // Second row: Tasks | Links
321 tasksSection := md.H3("Current Tasks") +
322 md.TodoList(tasks, completed)
323
324 linksSection := md.H3("Links") +
325 md.BulletList([]string{
326 md.Link("GitHub", "https://github.com/" + username),
327 md.Link("Portfolio", "https://example.com/" + username),
328 md.Link("LinkedIn", "https://linkedin.com/in/" + username),
329 })
330
331 // Combine with main title and two sets of columns
332 return md.H1("User Profile: " + name) +
333 md.HorizontalRule() +
334 md.Columns([]string{avatarSection, bioSection}, true) +
335 "\n" +
336 md.Columns([]string{tasksSection, linksSection}, true)
337}
338
339// Usage:
340profile := RenderProfile(
341 "johndoe",
342 "John Doe",
343 "Passionate Gno developer building the future of smart contracts. Love working with blockchain technology and contributing to open source.",
344 "/public/imgs/gnoland.svg",
345 []string{"Complete Gno tutorial", "Build first realm", "Deploy to testnet", "Write documentation"},
346 []bool{true, true, false, false},
347)`
348
349 // Generate actual result - First row
350 avatarSection := md.Image("John Doe avatar", "/public/imgs/gnoland.svg") + "\n\n" +
351 md.H3("Basic Info") +
352 md.BulletList([]string{
353 md.Bold("Name:") + " John Doe",
354 md.Bold("Username:") + " " + md.UserLink("johndoe"),
355 md.Bold("Status:") + " 🟢 Active",
356 md.Bold("Joined:") + " January 2024",
357 })
358
359 bioSection := md.H3("Bio") +
360 md.Blockquote("Passionate Gno developer building the future of smart contracts. Love working with blockchain technology and contributing to open source.")
361
362 // Second row
363 tasksSection := md.H3("Current Tasks") +
364 md.TodoList(
365 []string{"Complete Gno tutorial", "Build first realm", "Deploy to testnet", "Write documentation"},
366 []bool{true, true, false, false},
367 )
368
369 linksSection := md.H3("Links") +
370 md.BulletList([]string{
371 md.Link("GitHub", "https://github.com/johndoe"),
372 md.Link("Portfolio", "https://example.com/johndoe"),
373 md.Link("LinkedIn", "https://linkedin.com/in/johndoe"),
374 })
375
376 result := md.H1("User Profile: John Doe") +
377 md.HorizontalRule() +
378 md.Columns([]string{avatarSection, bioSection}, true) +
379 "\n" +
380 md.Columns([]string{tasksSection, linksSection}, true)
381
382 return md.H2("Practical Example") +
383 md.Paragraph("Here's a complete example showing how to build a rich user profile page using columns, images, and various md features:") +
384 "\n" +
385 md.LanguageCodeBlock("go", code) +
386 "\nResult when called with the above parameters:\n" +
387 result + "\n"
388}
389
390func renderBestPractices() string {
391 practices := []string{
392 "Prioritize code readability over performance in Render() - use clear variable names, logical grouping, and readable concatenation for humans",
393 "Use the md package for all programmatic markdown generation in Render() methods",
394 "Combine functions for complex formatting: " + md.InlineCode("md.Bold(md.Italic(\"text\"))"),
395 "Use " + md.InlineCode("md.Paragraph()") + " to ensure proper spacing between elements",
396 "Leverage " + md.InlineCode("md.ColumnsN()") + " for responsive layouts",
397 }
398
399 return md.H2("Best Practices") +
400 md.OrderedList(practices) +
401 "\n"
402}
403
404func renderSeeAlso() string {
405 links := []string{
406 md.Link("gno.land/p/moul/md", "https://gno.land/p/moul/md") + " - The md package source code",
407 md.Link("Markdown on Gno", "https://gno.land/r/docs/markdown") + " - Comprehensive guide on Gno Flavored Markdown syntax",
408 }
409
410 return md.H2("See Also") +
411 md.BulletList(links)
412}
413
414// Helper function to render an example with code and result
415func renderExample(title, code, result string) string {
416 output := ""
417 if title != "" {
418 output += md.H3(title)
419 }
420 output += md.LanguageCodeBlock("go", code) + "\n"
421 output += md.Paragraph("Result: "+result) + "\n"
422 return output
423}
424
425// Helper function for examples where result needs raw output
426func renderExampleWithRawResult(title, code, result string) string {
427 output := md.H3(title)
428 output += md.LanguageCodeBlock("go", code) + "\n"
429 output += "Result:\n" + result + "\n"
430 return output
431}