github.com/phsym/gomarkdoc@v0.5.4/format/plain.go (about)

     1  package format
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/phsym/gomarkdoc/format/formatcore"
     7  	"github.com/phsym/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  // Header converts the provided text into a header of the provided level. The
    26  // level is expected to be at least 1.
    27  func (f *PlainMarkdown) Header(level int, text string) (string, error) {
    28  	return formatcore.Header(level, formatcore.Escape(text))
    29  }
    30  
    31  // RawHeader converts the provided text into a header of the provided level
    32  // without escaping the header text. The level is expected to be at least 1.
    33  func (f *PlainMarkdown) RawHeader(level int, text string) (string, error) {
    34  	return formatcore.Header(level, text)
    35  }
    36  
    37  // LocalHref always returns the empty string, as header links are not supported
    38  // in plain markdown.
    39  func (f *PlainMarkdown) LocalHref(headerText string) (string, error) {
    40  	return "", nil
    41  }
    42  
    43  // CodeHref always returns the empty string, as there is no defined file linking
    44  // format in standard markdown.
    45  func (f *PlainMarkdown) CodeHref(loc lang.Location) (string, error) {
    46  	return "", nil
    47  }
    48  
    49  // Link generates a link with the given text and href values.
    50  func (f *PlainMarkdown) Link(text, href string) (string, error) {
    51  	return formatcore.Link(text, href), nil
    52  }
    53  
    54  // ListEntry generates an unordered list entry with the provided text at the
    55  // provided zero-indexed depth. A depth of 0 is considered the topmost level of
    56  // list.
    57  func (f *PlainMarkdown) ListEntry(depth int, text string) (string, error) {
    58  	return formatcore.ListEntry(depth, text), nil
    59  }
    60  
    61  // Accordion generates a collapsible content. Since accordions are not supported
    62  // by plain markdown, this generates a level 6 header followed by a paragraph.
    63  func (f *PlainMarkdown) Accordion(title, body string) (string, error) {
    64  	h, err := formatcore.Header(6, title)
    65  	if err != nil {
    66  		return "", err
    67  	}
    68  
    69  	return fmt.Sprintf("%s%s", h, formatcore.Paragraph(body)), nil
    70  }
    71  
    72  // AccordionHeader generates the header visible when an accordion is collapsed.
    73  // Since accordions are not supported in plain markdown, this generates a level
    74  // 6 header.
    75  //
    76  // The AccordionHeader is expected to be used in conjunction with
    77  // AccordionTerminator() when the demands of the body's rendering requires it to
    78  // be generated independently. The result looks conceptually like the following:
    79  //
    80  //	accordion := format.AccordionHeader("Accordion Title") + "Accordion Body" + format.AccordionTerminator()
    81  func (f *PlainMarkdown) AccordionHeader(title string) (string, error) {
    82  	return formatcore.Header(6, title)
    83  }
    84  
    85  // AccordionTerminator generates the code necessary to terminate an accordion
    86  // after the body. Since accordions are not supported in plain markdown, this
    87  // completes a paragraph section. It is expected to be used in conjunction with
    88  // AccordionHeader(). See AccordionHeader for a full description.
    89  func (f *PlainMarkdown) AccordionTerminator() (string, error) {
    90  	return "\n\n", nil
    91  }
    92  
    93  // Paragraph formats a paragraph with the provided text as the contents.
    94  func (f *PlainMarkdown) Paragraph(text string) (string, error) {
    95  	return formatcore.Paragraph(text), nil
    96  }
    97  
    98  // Escape escapes special markdown characters from the provided text.
    99  func (f *PlainMarkdown) Escape(text string) string {
   100  	return formatcore.Escape(text)
   101  }