github.com/dkischenko/gomarkdoc@v0.0.0-20230516135336-e40deae8a495/format/plain.go (about) 1 package format 2 3 import ( 4 "fmt" 5 6 "github.com/dkischenko/gomarkdoc/format/formatcore" 7 "github.com/dkischenko/gomarkdoc/lang" 8 ) 9 10 // PlainMarkdown provides a Format which is compatible with the base Markdown 11 // format specification. 12 type PlainMarkdown struct{} 13 14 // Bold converts the provided text to bold 15 func (f *PlainMarkdown) Bold(text string) (string, error) { 16 return formatcore.Bold(text), nil 17 } 18 19 // CodeBlock wraps the provided code as a code block. The provided language is 20 // ignored as it is not supported in plain markdown. 21 func (f *PlainMarkdown) CodeBlock(language, code string) (string, error) { 22 return formatcore.CodeBlock(code), nil 23 } 24 25 // Anchor produces an anchor for the provided link. 26 func (f *PlainMarkdown) Anchor(anchor string) string { 27 return formatcore.Anchor(anchor) 28 } 29 30 // AnchorHeader converts the provided text and custom anchor link into a header 31 // of the provided level. The level is expected to be at least 1. 32 func (f *PlainMarkdown) AnchorHeader(level int, text, anchor string) (string, error) { 33 return formatcore.AnchorHeader(level, formatcore.Escape(text), anchor) 34 } 35 36 // Header converts the provided text into a header of the provided level. The 37 // level is expected to be at least 1. 38 func (f *PlainMarkdown) Header(level int, text string) (string, error) { 39 return formatcore.Header(level, formatcore.Escape(text)) 40 } 41 42 // RawAnchorHeader converts the provided text and custom anchor link into a 43 // header of the provided level without escaping the header text. The level is 44 // expected to be at least 1. 45 func (f *PlainMarkdown) RawAnchorHeader(level int, text, anchor string) (string, error) { 46 return formatcore.AnchorHeader(level, text, anchor) 47 } 48 49 // RawHeader converts the provided text into a header of the provided level 50 // without escaping the header text. The level is expected to be at least 1. 51 func (f *PlainMarkdown) RawHeader(level int, text string) (string, error) { 52 return formatcore.Header(level, text) 53 } 54 55 // LocalHref always returns the empty string, as header links are not supported 56 // in plain markdown. 57 func (f *PlainMarkdown) LocalHref(headerText string) (string, error) { 58 return "", nil 59 } 60 61 // RawLocalHref generates an href within the same document but with a direct 62 // link provided instead of text to slugify. 63 func (f *PlainMarkdown) RawLocalHref(anchor string) string { 64 return fmt.Sprintf("#%s", anchor) 65 } 66 67 // CodeHref always returns the empty string, as there is no defined file linking 68 // format in standard markdown. 69 func (f *PlainMarkdown) CodeHref(loc lang.Location) (string, error) { 70 return "", nil 71 } 72 73 // Link generates a link with the given text and href values. 74 func (f *PlainMarkdown) Link(text, href string) (string, error) { 75 return formatcore.Link(text, href), nil 76 } 77 78 // ListEntry generates an unordered list entry with the provided text at the 79 // provided zero-indexed depth. A depth of 0 is considered the topmost level of 80 // list. 81 func (f *PlainMarkdown) ListEntry(depth int, text string) (string, error) { 82 return formatcore.ListEntry(depth, text), nil 83 } 84 85 // Accordion generates a collapsible content. Since accordions are not supported 86 // by plain markdown, this generates a level 6 header followed by a paragraph. 87 func (f *PlainMarkdown) Accordion(title, body string) (string, error) { 88 h, err := formatcore.Header(6, title) 89 if err != nil { 90 return "", err 91 } 92 93 return fmt.Sprintf("%s%s", h, body), nil 94 } 95 96 // AccordionHeader generates the header visible when an accordion is collapsed. 97 // Since accordions are not supported in plain markdown, this generates a level 98 // 6 header. 99 // 100 // The AccordionHeader is expected to be used in conjunction with 101 // AccordionTerminator() when the demands of the body's rendering requires it to 102 // be generated independently. The result looks conceptually like the following: 103 // 104 // accordion := format.AccordionHeader("Accordion Title") + "Accordion Body" + format.AccordionTerminator() 105 func (f *PlainMarkdown) AccordionHeader(title string) (string, error) { 106 return formatcore.Header(6, title) 107 } 108 109 // AccordionTerminator generates the code necessary to terminate an accordion 110 // after the body. Since accordions are not supported in plain markdown, this 111 // completes a paragraph section. It is expected to be used in conjunction with 112 // AccordionHeader(). See AccordionHeader for a full description. 113 func (f *PlainMarkdown) AccordionTerminator() (string, error) { 114 return "\n\n", nil 115 } 116 117 // Escape escapes special markdown characters from the provided text. 118 func (f *PlainMarkdown) Escape(text string) string { 119 return formatcore.Escape(text) 120 }