forked from princjef/gomarkdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plain.go
120 lines (100 loc) · 4.46 KB
/
plain.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
package format
import (
"fmt"
"github.com/chengyumeng/gomarkdoc/format/formatcore"
"github.com/chengyumeng/gomarkdoc/lang"
)
// PlainMarkdown provides a Format which is compatible with the base Markdown
// format specification.
type PlainMarkdown struct{}
// Bold converts the provided text to bold
func (f *PlainMarkdown) Bold(text string) (string, error) {
return formatcore.Bold(text), nil
}
// CodeBlock wraps the provided code as a code block. The provided language is
// ignored as it is not supported in plain markdown.
func (f *PlainMarkdown) CodeBlock(language, code string) (string, error) {
return formatcore.CodeBlock(code), nil
}
// Anchor produces an anchor for the provided link.
func (f *PlainMarkdown) Anchor(anchor string) string {
return formatcore.Anchor(anchor)
}
// AnchorHeader converts the provided text and custom anchor link into a header
// of the provided level. The level is expected to be at least 1.
func (f *PlainMarkdown) AnchorHeader(level int, text, anchor string) (string, error) {
return formatcore.AnchorHeader(level, formatcore.Escape(text), anchor)
}
// Header converts the provided text into a header of the provided level. The
// level is expected to be at least 1.
func (f *PlainMarkdown) Header(level int, text string) (string, error) {
return formatcore.Header(level, formatcore.Escape(text))
}
// RawAnchorHeader converts the provided text and custom anchor link into a
// header of the provided level without escaping the header text. The level is
// expected to be at least 1.
func (f *PlainMarkdown) RawAnchorHeader(level int, text, anchor string) (string, error) {
return formatcore.AnchorHeader(level, text, anchor)
}
// RawHeader converts the provided text into a header of the provided level
// without escaping the header text. The level is expected to be at least 1.
func (f *PlainMarkdown) RawHeader(level int, text string) (string, error) {
return formatcore.Header(level, text)
}
// LocalHref always returns the empty string, as header links are not supported
// in plain markdown.
func (f *PlainMarkdown) LocalHref(headerText string) (string, error) {
return "", nil
}
// RawLocalHref generates an href within the same document but with a direct
// link provided instead of text to slugify.
func (f *PlainMarkdown) RawLocalHref(anchor string) string {
return fmt.Sprintf("#%s", anchor)
}
// CodeHref always returns the empty string, as there is no defined file linking
// format in standard markdown.
func (f *PlainMarkdown) CodeHref(loc lang.Location) (string, error) {
return "", nil
}
// Link generates a link with the given text and href values.
func (f *PlainMarkdown) Link(text, href string) (string, error) {
return formatcore.Link(text, href), nil
}
// ListEntry generates an unordered list entry with the provided text at the
// provided zero-indexed depth. A depth of 0 is considered the topmost level of
// list.
func (f *PlainMarkdown) ListEntry(depth int, text string) (string, error) {
return formatcore.ListEntry(depth, text), nil
}
// Accordion generates a collapsible content. Since accordions are not supported
// by plain markdown, this generates a level 6 header followed by a paragraph.
func (f *PlainMarkdown) Accordion(title, body string) (string, error) {
h, err := formatcore.Header(6, title)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s", h, body), nil
}
// AccordionHeader generates the header visible when an accordion is collapsed.
// Since accordions are not supported in plain markdown, this generates a level
// 6 header.
//
// The AccordionHeader is expected to be used in conjunction with
// AccordionTerminator() when the demands of the body's rendering requires it to
// be generated independently. The result looks conceptually like the following:
//
// accordion := format.AccordionHeader("Accordion Title") + "Accordion Body" + format.AccordionTerminator()
func (f *PlainMarkdown) AccordionHeader(title string) (string, error) {
return formatcore.Header(6, title)
}
// AccordionTerminator generates the code necessary to terminate an accordion
// after the body. Since accordions are not supported in plain markdown, this
// completes a paragraph section. It is expected to be used in conjunction with
// AccordionHeader(). See AccordionHeader for a full description.
func (f *PlainMarkdown) AccordionTerminator() (string, error) {
return "\n\n", nil
}
// Escape escapes special markdown characters from the provided text.
func (f *PlainMarkdown) Escape(text string) string {
return formatcore.Escape(text)
}