Repository navigation
Expand file tree
/
Copy pathexamples_test.go
More file actions
1402 lines (1211 loc) · 38 KB
/
Copy pathexamples_test.go
File metadata and controls
1402 lines (1211 loc) · 38 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build linux || darwin
package markdown_test
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
md "github.com/nao1215/markdown"
"github.com/nao1215/markdown/mermaid/piechart"
"github.com/nao1215/markdown/mermaid/sequence"
)
// ExampleMarkdown skips this test on Windows.
// The newline codes in the comment section where
// the expected values are written are represented as '\n',
// causing failures when testing on Windows.
func ExampleMarkdown() {
_ = md.NewMarkdown(os.Stdout).
H1("This is H1").
PlainText("This is plain text").
H2f("This is %s with text format", "H2").
PlainTextf("Text formatting, such as %s and %s, %s styles.",
md.Bold("bold"), md.Italic("italic"), md.Code("code")).
H2("Code Block").
CodeBlocks(md.SyntaxHighlightGo,
`package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}`).
H2("List").
BulletList("Bullet Item 1", "Bullet Item 2", "Bullet Item 3").
OrderedList("Ordered Item 1", "Ordered Item 2", "Ordered Item 3").
H2("CheckBox").
CheckBox([]md.CheckBoxSet{
{Checked: false, Text: md.Code("sample code")},
{Checked: true, Text: md.Link("Go", "https://golang.org")},
{Checked: false, Text: md.Strikethrough("strikethrough")},
}).
H2("Blockquote").
Blockquote("If you can dream it, you can do it.").
H3("Horizontal Rule").
HorizontalRule().
H2("Table").
Table(md.TableSet{
Header: []string{"Name", "Age", "Country"},
Rows: [][]string{
{"David", "23", "USA"},
{"John", "30", "UK"},
{"Bob", "25", "Canada"},
},
}).
H2("Image").
PlainTextf("%s", md.Image("sample_image", "./sample.png")).
Build()
// Output:
// # This is H1
// This is plain text
// ## This is H2 with text format
// Text formatting, such as **bold** and *italic*, `code` styles.
// ## Code Block
// ```go
// package main
// import "fmt"
//
// func main() {
// fmt.Println("Hello, World!")
// }
// ```
// ## List
// - Bullet Item 1
// - Bullet Item 2
// - Bullet Item 3
//
// 1. Ordered Item 1
// 2. Ordered Item 2
// 3. Ordered Item 3
//
// ## CheckBox
// - [ ] `sample code`
// - [x] [Go](https://golang.org)
// - [ ] ~~strikethrough~~
//
// ## Blockquote
// > If you can dream it, you can do it.
//
// ### Horizontal Rule
// ---
// ## Table
// | Name | Age | Country |
// |---------|---------|---------|
// | David | 23 | USA |
// | John | 30 | UK |
// | Bob | 25 | Canada |
//
// ## Image
// 
}
// ExampleNewDiagram skips this test on Windows.
// The newline codes in the comment section where
// the expected values are written are represented as '\n',
// causing failures when testing on Windows.
func ExampleNewDiagram() {
diagram := sequence.NewDiagram(os.Stdout).
Participant("Sophia").
Participant("David").
Participant("Subaru").
LF().
SyncRequest("Sophia", "David", "Please wake up Subaru").
SyncResponse("David", "Sophia", "OK").
LF().
LoopStart("until Subaru wake up").
SyncRequest("David", "Subaru", "Wake up!").
SyncResponse("Subaru", "David", "zzz").
SyncRequest("David", "Subaru", "Hey!!!").
BreakStart("if Subaru wake up").
SyncResponse("Subaru", "David", "......").
BreakEnd().
LoopEnd().
LF().
SyncResponse("David", "Sophia", "wake up, wake up").
String()
_ = md.NewMarkdown(os.Stdout).
H2("Sequence Diagram").
CodeBlocks(md.SyntaxHighlightMermaid, diagram).
Build()
// Output:
// ## Sequence Diagram
// ```mermaid
// sequenceDiagram
// participant Sophia
// participant David
// participant Subaru
//
// Sophia->>David: Please wake up Subaru
// David-->>Sophia: OK
//
// loop until Subaru wake up
// David->>Subaru: Wake up!
// Subaru-->>David: zzz
// David->>Subaru: Hey!!!
// break if Subaru wake up
// Subaru-->>David: ......
// end
// end
//
// David-->>Sophia: wake up, wake up
// ```
}
// ExampleTableAlignment demonstrates table alignment features.
func ExampleTableAlignment() {
_ = md.NewMarkdown(os.Stdout).
H2("Table with Alignments").
Table(md.TableSet{
Header: []string{"Left Align", "Center Align", "Right Align"},
Rows: [][]string{
{"Content1", "Content2", "Content3"},
{"Content4", "Content5", "Content6"},
},
Alignment: []md.TableAlignment{md.AlignLeft, md.AlignCenter, md.AlignRight},
}).
Build()
// Output:
// ## Table with Alignments
// | Left Align | Center Align | Right Align |
// |:--------|:-------:|--------:|
// | Content1 | Content2 | Content3 |
// | Content4 | Content5 | Content6 |
}
// ExampleNewMarkdown shows the shape every document has: a
// writer, a chain of calls, and Build.
func ExampleNewMarkdown() {
_ = md.NewMarkdown(os.Stdout).
H1("Release Notes").
PlainText("Everything that changed in this release.").
Build()
// Output:
// # Release Notes
// Everything that changed in this release.
}
// ExampleNewMarkdown_writerIsAnyWriter shows that the destination is an
// io.Writer, so a document can be built into memory and used as a string.
func ExampleNewMarkdown_writerIsAnyWriter() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).H2("Summary").Build(); err != nil {
fmt.Println("build:", err)
return
}
fmt.Printf("%q\n", buf.String())
// Output:
// "## Summary\n"
}
// ExampleWithBlockSpacing shows the option that puts a blank line between
// blocks. Without it the blocks are written one after another.
func ExampleWithBlockSpacing() {
_ = md.NewMarkdown(os.Stdout, md.WithBlockSpacing()).
H2("Deploy").
PlainText("Runs on every merge to main.").
H2("Rollback").
PlainText("Re-run the previous release job.").
Build()
// Output:
// ## Deploy
//
// Runs on every merge to main.
//
// ## Rollback
//
// Re-run the previous release job.
}
// ExampleMarkdown_H1 writes a level 1 heading.
func ExampleMarkdown_H1() {
_ = md.NewMarkdown(os.Stdout).H1("Heading").Build()
// Output:
// # Heading
}
// ExampleMarkdown_H1f writes a level 1 heading from a format string.
func ExampleMarkdown_H1f() {
_ = md.NewMarkdown(os.Stdout).H1f("Heading %d", 1).Build()
// Output:
// # Heading 1
}
// ExampleMarkdown_H2 writes a level 2 heading.
func ExampleMarkdown_H2() {
_ = md.NewMarkdown(os.Stdout).H2("Heading").Build()
// Output:
// ## Heading
}
// ExampleMarkdown_H2f writes a level 2 heading from a format string.
func ExampleMarkdown_H2f() {
_ = md.NewMarkdown(os.Stdout).H2f("Heading %d", 2).Build()
// Output:
// ## Heading 2
}
// ExampleMarkdown_H3 writes a level 3 heading.
func ExampleMarkdown_H3() {
_ = md.NewMarkdown(os.Stdout).H3("Heading").Build()
// Output:
// ### Heading
}
// ExampleMarkdown_H3f writes a level 3 heading from a format string.
func ExampleMarkdown_H3f() {
_ = md.NewMarkdown(os.Stdout).H3f("Heading %d", 3).Build()
// Output:
// ### Heading 3
}
// ExampleMarkdown_H4 writes a level 4 heading.
func ExampleMarkdown_H4() {
_ = md.NewMarkdown(os.Stdout).H4("Heading").Build()
// Output:
// #### Heading
}
// ExampleMarkdown_H4f writes a level 4 heading from a format string.
func ExampleMarkdown_H4f() {
_ = md.NewMarkdown(os.Stdout).H4f("Heading %d", 4).Build()
// Output:
// #### Heading 4
}
// ExampleMarkdown_H5 writes a level 5 heading.
func ExampleMarkdown_H5() {
_ = md.NewMarkdown(os.Stdout).H5("Heading").Build()
// Output:
// ##### Heading
}
// ExampleMarkdown_H5f writes a level 5 heading from a format string.
func ExampleMarkdown_H5f() {
_ = md.NewMarkdown(os.Stdout).H5f("Heading %d", 5).Build()
// Output:
// ##### Heading 5
}
// ExampleMarkdown_H6 writes a level 6 heading.
func ExampleMarkdown_H6() {
_ = md.NewMarkdown(os.Stdout).H6("Heading").Build()
// Output:
// ###### Heading
}
// ExampleMarkdown_H6f writes a level 6 heading from a format string.
func ExampleMarkdown_H6f() {
_ = md.NewMarkdown(os.Stdout).H6f("Heading %d", 6).Build()
// Output:
// ###### Heading 6
}
// ExampleMarkdown_PlainText writes a paragraph.
func ExampleMarkdown_PlainText() {
_ = md.NewMarkdown(os.Stdout).PlainText("A paragraph of text.").Build()
// Output:
// A paragraph of text.
}
// ExampleMarkdown_PlainTextf writes a paragraph from a format string.
func ExampleMarkdown_PlainTextf() {
_ = md.NewMarkdown(os.Stdout).PlainTextf("Built %d documents in %s.", 3, "2s").Build()
// Output:
// Built 3 documents in 2s.
}
// ExampleMarkdown_Blockquote writes a quotation. Each line of the text is
// prefixed, so a quotation spanning lines stays one block.
func ExampleMarkdown_Blockquote() {
_ = md.NewMarkdown(os.Stdout).
Blockquote("If you can dream it, you can do it.").
Build()
// Output:
// > If you can dream it, you can do it.
}
// ExampleMarkdown_CodeBlocks writes a fenced code block tagged with its
// language.
func ExampleMarkdown_CodeBlocks() {
_ = md.NewMarkdown(os.Stdout).
CodeBlocks(md.SyntaxHighlightGo, `fmt.Println("hello")`).
Build()
// Output:
// ```go
// fmt.Println("hello")
// ```
}
// ExampleMarkdown_HorizontalRule writes a thematic break.
func ExampleMarkdown_HorizontalRule() {
_ = md.NewMarkdown(os.Stdout).
PlainText("Above").
HorizontalRule().
PlainText("Below").
Build()
// Output:
// Above
// ---
// Below
}
// ExampleMarkdown_BlankLine writes an empty line, for the places a document
// needs one that the block spacing does not give it.
func ExampleMarkdown_BlankLine() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).PlainText("Above").BlankLine().PlainText("Below").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the empty line carries the two spaces markdown
// reads as a hard line break, and a godoc Output block cannot hold
// trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "Above\n\nBelow\n"
}
// ExampleMarkdown_LF is the older name for BlankLine and does the same thing.
func ExampleMarkdown_LF() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).PlainText("Above").LF().PlainText("Below").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the empty line carries the two spaces markdown
// reads as a hard line break, and a godoc Output block cannot hold
// trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "Above\n \nBelow\n"
}
// ExampleMarkdown_Details writes a collapsible section. Markdown has no
// syntax of its own for one, so this is the single place the library emits
// HTML.
func ExampleMarkdown_Details() {
_ = md.NewMarkdown(os.Stdout).
Details("Show the stack trace", "goroutine 1 [running]").
Build()
// Output:
// <details>
// <summary>Show the stack trace</summary>
//
// goroutine 1 [running]
//
// </details>
}
// ExampleMarkdown_Detailsf writes a collapsible section from a format string.
func ExampleMarkdown_Detailsf() {
_ = md.NewMarkdown(os.Stdout).
Detailsf("Show the log", "exited with status %d", 2).
Build()
// Output:
// <details>
// <summary>Show the log</summary>
//
// exited with status 2
//
// </details>
}
// ExampleMarkdown_BulletList writes an unordered list.
func ExampleMarkdown_BulletList() {
_ = md.NewMarkdown(os.Stdout).
BulletList("Read the spec", "Write the test", "Write the code").
Build()
// Output:
// - Read the spec
// - Write the test
// - Write the code
}
// ExampleMarkdown_OrderedList writes a numbered list. The numbers are
// written out, so the document reads the same as a plain file.
func ExampleMarkdown_OrderedList() {
_ = md.NewMarkdown(os.Stdout).
OrderedList("Clone the repository", "Run the tests", "Open a pull request").
Build()
// Output:
// 1. Clone the repository
// 2. Run the tests
// 3. Open a pull request
}
// ExampleMarkdown_CheckBox writes a task list. GitHub renders each item as a
// checkbox, and the text of an item may hold any inline markup.
func ExampleMarkdown_CheckBox() {
_ = md.NewMarkdown(os.Stdout).
CheckBox([]md.CheckBoxSet{
{Checked: true, Text: "Write the proposal"},
{Checked: false, Text: md.Code("go test ./...")},
}).
Build()
// Output:
// - [x] Write the proposal
// - [ ] `go test ./...`
}
// ExampleMarkdown_Table writes a table. Every row must have as many cells as
// the header, and a row that does not records an error rather than writing a
// broken table.
func ExampleMarkdown_Table() {
_ = md.NewMarkdown(os.Stdout).
Table(md.TableSet{
Header: []string{"Name", "Age"},
Rows: [][]string{
{"David", "23"},
{"John", "30"},
},
}).
Build()
// Output:
// | Name | Age |
// |---------|---------|
// | David | 23 |
// | John | 30 |
}
// ExampleMarkdown_CustomTable writes a table with the alignment row spelled
// out. Without the options every column is left aligned.
func ExampleMarkdown_CustomTable() {
_ = md.NewMarkdown(os.Stdout).
CustomTable(md.TableSet{
Header: []string{"Name", "Age"},
Rows: [][]string{
{"David", "23"},
{"John", "30"},
},
}, md.TableOptions{
AutoFormatHeaders: false,
AutoWrapText: false,
}).
Build()
// Output:
// | Name | Age |
// |-------|-----|
// | David | 23 |
// | John | 30 |
}
// ExampleTableSet_ValidateColumns reports a row whose cell count does not
// match the header. Table calls it, so this is only worth calling directly
// when the table is assembled before the document is.
func ExampleTableSet_ValidateColumns() {
set := md.TableSet{
Header: []string{"Name", "Age"},
Rows: [][]string{{"only one cell"}},
}
fmt.Println(set.ValidateColumns())
// Output:
// number of columns in the record doesn't match the header
}
// ExampleEscapeTableCell escapes the characters that would end a table cell
// early. A pipe closes the cell it is written in, so text arriving from a
// database or a command's output needs this before it reaches a row.
func ExampleEscapeTableCell() {
fmt.Println(md.EscapeTableCell("a|b"))
fmt.Println(md.EscapeTableCell("multi\nline"))
// Output:
// a\|b
// multi<br>line
}
// ExampleMarkdown_TableOfContents writes a table of contents built from the
// headings of the document. It may be called before the headings it lists:
// the list is filled in at Build.
func ExampleMarkdown_TableOfContents() {
_ = md.NewMarkdown(os.Stdout).
H1("Guide").
TableOfContents(md.TableOfContentsDepthH3).
H2("Install").
H3("From source").
H2("Usage").
Build()
// Output:
// # Guide
// <!-- BEGIN_TOC -->
// - [Guide](#guide)
// - [Install](#install)
// - [From source](#from-source)
// - [Usage](#usage)
// <!-- END_TOC -->
//
// ## Install
// ### From source
// ## Usage
}
// ExampleMarkdown_TableOfContentsWithRange writes a table of contents holding
// only the heading levels between the two given, which is how a document
// leaves its own title out of its contents.
func ExampleMarkdown_TableOfContentsWithRange() {
_ = md.NewMarkdown(os.Stdout).
H1("Guide").
TableOfContentsWithRange(md.TableOfContentsDepthH2, md.TableOfContentsDepthH2).
H2("Install").
H3("From source").
H2("Usage").
Build()
// Output:
// # Guide
// <!-- BEGIN_TOC -->
// - [Install](#install)
// - [Usage](#usage)
// <!-- END_TOC -->
//
// ## Install
// ### From source
// ## Usage
}
// ExampleMarkdown_Note writes a GitHub NOTE alert.
func ExampleMarkdown_Note() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Note("Useful information a reader should know even when skimming.").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!NOTE] \n> Useful information a reader should know even when skimming.\n"
}
// ExampleMarkdown_Notef writes a GitHub NOTE alert from a format string.
func ExampleMarkdown_Notef() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Notef("Note takes %d minutes.", 5).Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!NOTE] \n> Note takes 5 minutes.\n"
}
// ExampleMarkdown_Tip writes a GitHub TIP alert.
func ExampleMarkdown_Tip() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Tip("Helpful advice for doing things better.").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!TIP] \n> Helpful advice for doing things better.\n"
}
// ExampleMarkdown_Tipf writes a GitHub TIP alert from a format string.
func ExampleMarkdown_Tipf() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Tipf("Tip takes %d minutes.", 5).Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!TIP] \n> Tip takes 5 minutes.\n"
}
// ExampleMarkdown_Important writes a GitHub IMPORTANT alert.
func ExampleMarkdown_Important() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Important("Key information a reader needs to succeed.").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!IMPORTANT] \n> Key information a reader needs to succeed.\n"
}
// ExampleMarkdown_Importantf writes a GitHub IMPORTANT alert from a format string.
func ExampleMarkdown_Importantf() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Importantf("Important takes %d minutes.", 5).Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!IMPORTANT] \n> Important takes 5 minutes.\n"
}
// ExampleMarkdown_Warning writes a GitHub WARNING alert.
func ExampleMarkdown_Warning() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Warning("Urgent information needing immediate attention.").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!WARNING] \n> Urgent information needing immediate attention.\n"
}
// ExampleMarkdown_Warningf writes a GitHub WARNING alert from a format string.
func ExampleMarkdown_Warningf() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Warningf("Warning takes %d minutes.", 5).Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!WARNING] \n> Warning takes 5 minutes.\n"
}
// ExampleMarkdown_Caution writes a GitHub CAUTION alert.
func ExampleMarkdown_Caution() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Caution("Advises about the risks of an action.").Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!CAUTION] \n> Advises about the risks of an action.\n"
}
// ExampleMarkdown_Cautionf writes a GitHub CAUTION alert from a format string.
func ExampleMarkdown_Cautionf() {
buf := &bytes.Buffer{}
if err := md.NewMarkdown(buf).Cautionf("Caution takes %d minutes.", 5).Build(); err != nil {
fmt.Println("build:", err)
return
}
// Printed quoted because the keyword line ends with the two spaces
// markdown reads as a hard line break, and a godoc Output block cannot
// hold trailing whitespace.
fmt.Printf("%q\n", buf.String())
// Output:
// "> [!CAUTION] \n> Caution takes 5 minutes.\n"
}
// ExampleMarkdown_RedBadge writes a red badge, which is an image served by
// shields.io rather than markdown of its own.
func ExampleMarkdown_RedBadge() {
_ = md.NewMarkdown(os.Stdout).RedBadge("build").Build()
// Output:
// 
}
// ExampleMarkdown_RedBadgef writes a red badge from a format string.
func ExampleMarkdown_RedBadgef() {
_ = md.NewMarkdown(os.Stdout).RedBadgef("coverage %d%%", 96).Build()
// Output:
// 
}
// ExampleMarkdown_YellowBadge writes a yellow badge, which is an image served by
// shields.io rather than markdown of its own.
func ExampleMarkdown_YellowBadge() {
_ = md.NewMarkdown(os.Stdout).YellowBadge("build").Build()
// Output:
// 
}
// ExampleMarkdown_YellowBadgef writes a yellow badge from a format string.
func ExampleMarkdown_YellowBadgef() {
_ = md.NewMarkdown(os.Stdout).YellowBadgef("coverage %d%%", 96).Build()
// Output:
// 
}
// ExampleMarkdown_GreenBadge writes a green badge, which is an image served by
// shields.io rather than markdown of its own.
func ExampleMarkdown_GreenBadge() {
_ = md.NewMarkdown(os.Stdout).GreenBadge("build").Build()
// Output:
// 
}
// ExampleMarkdown_GreenBadgef writes a green badge from a format string.
func ExampleMarkdown_GreenBadgef() {
_ = md.NewMarkdown(os.Stdout).GreenBadgef("coverage %d%%", 96).Build()
// Output:
// 
}
// ExampleMarkdown_BlueBadge writes a blue badge, which is an image served by
// shields.io rather than markdown of its own.
func ExampleMarkdown_BlueBadge() {
_ = md.NewMarkdown(os.Stdout).BlueBadge("build").Build()
// Output:
// 
}
// ExampleMarkdown_BlueBadgef writes a blue badge from a format string.
func ExampleMarkdown_BlueBadgef() {
_ = md.NewMarkdown(os.Stdout).BlueBadgef("coverage %d%%", 96).Build()
// Output:
// 
}
// ExampleBold returns the inline markup rather than writing it, so it can be
// put inside any text a builder takes.
func ExampleBold() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("This word is %s.", md.Bold("important")).
Build()
// Output:
// This word is **important**.
}
// ExampleItalic returns the inline markup rather than writing it, so it can be
// put inside any text a builder takes.
func ExampleItalic() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("This word is %s.", md.Italic("emphasis")).
Build()
// Output:
// This word is *emphasis*.
}
// ExampleBoldItalic returns the inline markup rather than writing it, so it can be
// put inside any text a builder takes.
func ExampleBoldItalic() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("This word is %s.", md.BoldItalic("both")).
Build()
// Output:
// This word is ***both***.
}
// ExampleCode returns the inline markup rather than writing it, so it can be
// put inside any text a builder takes.
func ExampleCode() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("This word is %s.", md.Code("go test ./...")).
Build()
// Output:
// This word is `go test ./...`.
}
// ExampleStrikethrough returns the inline markup rather than writing it, so it can be
// put inside any text a builder takes.
func ExampleStrikethrough() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("This word is %s.", md.Strikethrough("removed")).
Build()
// Output:
// This word is ~~removed~~.
}
// ExampleHighlight returns the inline markup rather than writing it, so it can be
// put inside any text a builder takes.
func ExampleHighlight() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("This word is %s.", md.Highlight("marked")).
Build()
// Output:
// This word is ==marked==.
}
// ExampleLink returns an inline link.
func ExampleLink() {
_ = md.NewMarkdown(os.Stdout).
PlainText(md.Link("The Go Programming Language", "https://go.dev")).
Build()
// Output:
// [The Go Programming Language](https://go.dev)
}
// ExampleImage returns an inline image.
func ExampleImage() {
_ = md.NewMarkdown(os.Stdout).
PlainText(md.Image("The Go gopher", "./gopher.png")).
Build()
// Output:
// 
}
// ExampleReferenceLink returns a link that points at a definition written
// elsewhere in the document, which keeps a long URL out of the sentence.
func ExampleReferenceLink() {
_ = md.NewMarkdown(os.Stdout).
PlainText(md.ReferenceLink("The Go Programming Language", "go")).
PlainText(md.ReferenceLinkDefinition("go", "https://go.dev")).
Build()
// Output:
// [The Go Programming Language][go]
// [go]: https://go.dev
}
// ExampleReferenceLinkDefinition writes the definition a reference link points
// at. The optional third argument is the title a browser shows on hover.
func ExampleReferenceLinkDefinition() {
fmt.Println(md.ReferenceLinkDefinition("go", "https://go.dev"))
fmt.Println(md.ReferenceLinkDefinition("go", "https://go.dev", "The Go website"))
// Output:
// [go]: https://go.dev
// [go]: https://go.dev "The Go website"
}
// ExampleFootnoteReference returns the marker that points at a footnote.
func ExampleFootnoteReference() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("Generated by this library%s.", md.FootnoteReference("1")).
PlainText(md.FootnoteDefinition("1", "github.com/nao1215/markdown")).
Build()
// Output:
// Generated by this library[^1].
// [^1]: github.com/nao1215/markdown
}
// ExampleFootnoteDefinition writes the text a footnote reference points at.
func ExampleFootnoteDefinition() {
fmt.Println(md.FootnoteDefinition("1", "github.com/nao1215/markdown"))
// Output:
// [^1]: github.com/nao1215/markdown
}
// ExampleInlineMath returns a mathematical expression that sits inside a
// sentence. GitHub renders it with KaTeX.
func ExampleInlineMath() {
_ = md.NewMarkdown(os.Stdout).
PlainTextf("The area is %s.", md.InlineMath("\\pi r^2")).
Build()
// Output:
// The area is $\pi r^2$.
}
// ExampleBlockMath returns a mathematical expression that stands on its own.
func ExampleBlockMath() {
_ = md.NewMarkdown(os.Stdout).
PlainText(md.BlockMath("\\int_0^1 x^2 dx = \\frac{1}{3}")).
Build()
// Output:
// $$
// \int_0^1 x^2 dx = \frac{1}{3}
// $$
}
// ExampleMarkdown_Build writes the document and reports the first error the
// chain recorded. Nothing in the chain panics on bad input, so one check at
// the end is enough.
func ExampleMarkdown_Build() {
err := md.NewMarkdown(os.Stdout).
H1("Report").
Table(md.TableSet{
Header: []string{"Name", "Age"},
Rows: [][]string{{"only one cell"}},
}).
Build()
fmt.Println("error:", err)
// Output:
// # Report
// error: failed to validate columns: number of columns in the record doesn't match the header