Documentation ¶
Overview ¶
Package markdown is markdown builder that includes to convert Markdown to HTML.
Example ¶
Examle is example code. Skip 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.
package main import ( "os" md "github.com/go-spectest/markdown" ) func main() { 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(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 ![sample_image](./sample.png)
Index ¶
- Variables
- func Bold(text string) string
- func BoldItalic(text string) string
- func Code(text string) string
- func GenerateIndex(targetDir string, opts ...IndexOption) error
- func Image(text, url string) string
- func Italic(text string) string
- func Link(text, url string) string
- func Strikethrough(text string) string
- type CheckBoxSet
- type Index
- type IndexOption
- type Markdown
- func (m *Markdown) Blockquote(text string) *Markdown
- func (m *Markdown) Build() error
- func (m *Markdown) BulletList(text ...string) *Markdown
- func (m *Markdown) Caution(text string) *Markdown
- func (m *Markdown) Cautionf(format string, args ...interface{}) *Markdown
- func (m *Markdown) CheckBox(set []CheckBoxSet) *Markdown
- func (m *Markdown) CodeBlocks(lang SyntaxHighlight, text string) *Markdown
- func (m *Markdown) CustomTable(t TableSet, options TableOptions) *Markdown
- func (m *Markdown) Details(summary, text string) *Markdown
- func (m *Markdown) Detailsf(summary, format string, args ...interface{}) *Markdown
- func (m *Markdown) Error() error
- func (m *Markdown) GreenBadge(text string) *Markdown
- func (m *Markdown) GreenBadgef(format string, args ...interface{}) *Markdown
- func (m *Markdown) H1(text string) *Markdown
- func (m *Markdown) H1f(format string, args ...interface{}) *Markdown
- func (m *Markdown) H2(text string) *Markdown
- func (m *Markdown) H2f(format string, args ...interface{}) *Markdown
- func (m *Markdown) H3(text string) *Markdown
- func (m *Markdown) H3f(format string, args ...interface{}) *Markdown
- func (m *Markdown) H4(text string) *Markdown
- func (m *Markdown) H4f(format string, args ...interface{}) *Markdown
- func (m *Markdown) H5(text string) *Markdown
- func (m *Markdown) H5f(format string, args ...interface{}) *Markdown
- func (m *Markdown) H6(text string) *Markdown
- func (m *Markdown) H6f(format string, args ...interface{}) *Markdown
- func (m *Markdown) HorizontalRule() *Markdown
- func (m *Markdown) Important(text string) *Markdown
- func (m *Markdown) Importantf(format string, args ...interface{}) *Markdown
- func (m *Markdown) LF() *Markdown
- func (m *Markdown) Note(text string) *Markdown
- func (m *Markdown) Notef(format string, args ...interface{}) *Markdown
- func (m *Markdown) OrderedList(text ...string) *Markdown
- func (m *Markdown) PlainText(text string) *Markdown
- func (m *Markdown) PlainTextf(format string, args ...interface{}) *Markdown
- func (m *Markdown) RedBadge(text string) *Markdown
- func (m *Markdown) RedBadgef(format string, args ...interface{}) *Markdown
- func (m *Markdown) String() string
- func (m *Markdown) Table(t TableSet) *Markdown
- func (m *Markdown) Tip(text string) *Markdown
- func (m *Markdown) Tipf(format string, args ...interface{}) *Markdown
- func (m *Markdown) Warning(text string) *Markdown
- func (m *Markdown) Warningf(format string, args ...interface{}) *Markdown
- func (m *Markdown) YellowBadge(text string) *Markdown
- func (m *Markdown) YellowBadgef(format string, args ...interface{}) *Markdown
- type SyntaxHighlight
- type TableOptions
- type TableSet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMismatchColumn is returned when the number of columns in the record doesn't match the header. ErrMismatchColumn = errors.New("number of columns in the record doesn't match the header") // ErrInitMarkdownIndex is returned when the index can't be initialized. ErrInitMarkdownIndex = errors.New("markdown index can't be initialized") // ErrCreateMarkdownIndex is returned when the index can't be created. ErrCreateMarkdownIndex = errors.New("markdown index can't be created") // ErrWriteMarkdownIndex is returned when the index can't be written. ErrWriteMarkdownIndex = errors.New("markdown index can't be written") )
Functions ¶
func Bold ¶
Bold return text with bold format. If you set text "Hello", it will be converted to "**Hello**".
func BoldItalic ¶
BoldItalic return text with bold and italic format. If you set text "Hello", it will be converted to "***Hello***".
func Code ¶
Code return text with code format. If you set text "Hello", it will be converted to "`Hello`".
func GenerateIndex ¶
func GenerateIndex(targetDir string, opts ...IndexOption) error
GenerateIndex generates an index of all markdown files in the target directory. The index is written to the provided io.Writer.
func Image ¶
Image return text with image format. If you set text "Hello" and url "https://example.com/image.png", it will be converted to "![Hello](https://example.com/image.png)".
func Italic ¶
Italic return text with italic format. If you set text "Hello", it will be converted to "*Hello*".
func Link ¶
Link return text with link format. If you set text "Hello" and url "https://example.com", it will be converted to "[Hello](https://example.com)".
func Strikethrough ¶
Strikethrough return text with strikethrough format. If you set text "Hello", it will be converted to "~~Hello~~".
Types ¶
type CheckBoxSet ¶
type CheckBoxSet struct { // Checked is whether checked or not. Checked bool // Text is checkbox text. Text string }
CheckBoxSet is markdown checkbox list.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index represents an Index of all markdown files in a directory.
type IndexOption ¶
IndexOption are options for generating an index.
func WithDescription ¶
func WithDescription(description []string) IndexOption
WithDescription sets the description of the index.
func WithWriter ¶
func WithWriter(w io.Writer) IndexOption
WithWriter sets the writer to write the index to.
type Markdown ¶
type Markdown struct {
// contains filtered or unexported fields
}
Markdown is markdown text.
func (*Markdown) Blockquote ¶
Blockquote is markdown blockquote. If you set text "Hello", it will be converted to "> Hello".
func (*Markdown) BulletList ¶
BulletList is markdown bullet list. If you set text "Hello", it will be converted to "- Hello".
func (*Markdown) CheckBox ¶
func (m *Markdown) CheckBox(set []CheckBoxSet) *Markdown
CheckBox is markdown CheckBox.
func (*Markdown) CodeBlocks ¶
func (m *Markdown) CodeBlocks(lang SyntaxHighlight, text string) *Markdown
CodeBlocks is code blocks. If you set text "Hello" and lang "go", it will be converted to "```go Hello ```".
func (*Markdown) CustomTable ¶
func (m *Markdown) CustomTable(t TableSet, options TableOptions) *Markdown
CustomTable is markdown table. This is so not break the original Table function. with Possible breaking changes.
func (*Markdown) GreenBadge ¶
GreenBadge set text with green badge format.
func (*Markdown) GreenBadgef ¶
GreenBadgef set text with green badge format. It is similar to fmt.Sprintf.
func (*Markdown) H1 ¶
H1 is markdown header. If you set text "Hello", it will be converted to "# Hello".
func (*Markdown) H1f ¶
H1f is markdown header with format. If you set format "%s", text "Hello", it will be converted to "# Hello".
func (*Markdown) H2 ¶
H2 is markdown header. If you set text "Hello", it will be converted to "## Hello".
func (*Markdown) H2f ¶
H2f is markdown header with format. If you set format "%s", text "Hello", it will be converted to "## Hello".
func (*Markdown) H3 ¶
H3 is markdown header. If you set text "Hello", it will be converted to "### Hello".
func (*Markdown) H3f ¶
H3f is markdown header with format. If you set format "%s", text "Hello", it will be converted to "### Hello".
func (*Markdown) H4 ¶
H4 is markdown header. If you set text "Hello", it will be converted to "#### Hello".
func (*Markdown) H4f ¶
H4f is markdown header with format. If you set format "%s", text "Hello", it will be converted to "#### Hello".
func (*Markdown) H5 ¶
H5 is markdown header. If you set text "Hello", it will be converted to "##### Hello".
func (*Markdown) H5f ¶
H5f is markdown header with format. If you set format "%s", text "Hello", it will be converted to "##### Hello".
func (*Markdown) H6 ¶
H6 is markdown header. If you set text "Hello", it will be converted to "###### Hello".
func (*Markdown) H6f ¶
H6f is markdown header with format. If you set format "%s", text "Hello", it will be converted to "###### Hello".
func (*Markdown) HorizontalRule ¶
HorizontalRule is markdown horizontal rule. It will be converted to "---".
func (*Markdown) Importantf ¶
Importantf set text with important format. It is similar to fmt.Sprintf.
func (*Markdown) OrderedList ¶
OrderedList is markdown number list. If you set text "Hello", it will be converted to "1. Hello".
func (*Markdown) PlainTextf ¶
PlainTextf set plain text with format
func (*Markdown) RedBadgef ¶
RedBadgef set text with red badge format. It is similar to fmt.Sprintf.
func (*Markdown) YellowBadge ¶
YellowBadge set text with yellow badge format.
func (*Markdown) YellowBadgef ¶
YellowBadgef set text with yellow badge format. It is similar to fmt.Sprintf.
type SyntaxHighlight ¶
type SyntaxHighlight string
SyntaxHighlight is syntax highlight language.
const ( // SyntaxHighlightNone is no syntax highlight. SyntaxHighlightNone SyntaxHighlight = "" // SyntaxHighlightText is syntax highlight for text. SyntaxHighlightText SyntaxHighlight = "text" // SyntaxHighlightAPIBlueprint is syntax highlight for API Blueprint. SyntaxHighlightAPIBlueprint SyntaxHighlight = "markdown" // SyntaxHighlightShell is syntax highlight for Shell. SyntaxHighlightShell SyntaxHighlight = "shell" // SyntaxHighlightGo is syntax highlight for Go. SyntaxHighlightGo SyntaxHighlight = "go" // SyntaxHighlightJSON is syntax highlight for JSON. SyntaxHighlightJSON SyntaxHighlight = "json" // SyntaxHighlightYAML is syntax highlight for YAML. SyntaxHighlightYAML SyntaxHighlight = "yaml" // SyntaxHighlightXML is syntax highlight for XML. SyntaxHighlightXML SyntaxHighlight = "xml" // SyntaxHighlightHTML is syntax highlight for HTML. SyntaxHighlightHTML SyntaxHighlight = "html" // SyntaxHighlightCSS is syntax highlight for CSS. SyntaxHighlightCSS SyntaxHighlight = "css" // SyntaxHighlightJavaScript is syntax highlight for JavaScript. SyntaxHighlightJavaScript SyntaxHighlight = "javascript" // SyntaxHighlightTypeScript is syntax highlight for TypeScript. SyntaxHighlightTypeScript SyntaxHighlight = "typescript" // SyntaxHighlightSQL is syntax highlight for SQL. SyntaxHighlightSQL SyntaxHighlight = "sql" // SyntaxHighlightC is syntax highlight for C. SyntaxHighlightC SyntaxHighlight = "c" // SyntaxHighlightCSharp is syntax highlight for C#. SyntaxHighlightCSharp SyntaxHighlight = "csharp" // SyntaxHighlightCPlusPlus is syntax highlight for C++. SyntaxHighlightCPlusPlus SyntaxHighlight = "cpp" // SyntaxHighlightJava is syntax highlight for Java. SyntaxHighlightJava SyntaxHighlight = "java" // SyntaxHighlightKotlin is syntax highlight for Kotlin. SyntaxHighlightKotlin SyntaxHighlight = "kotlin" // SyntaxHighlightPHP is syntax highlight for PHP. SyntaxHighlightPHP SyntaxHighlight = "php" // SyntaxHighlightPython is syntax highlight for Python. SyntaxHighlightPython SyntaxHighlight = "python" // SyntaxHighlightRuby is syntax highlight for Ruby. SyntaxHighlightRuby SyntaxHighlight = "ruby" // SyntaxHighlightSwift is syntax highlight for Swift. SyntaxHighlightSwift SyntaxHighlight = "swift" // SyntaxHighlightScala is syntax highlight for Scala. SyntaxHighlightScala SyntaxHighlight = "scala" // SyntaxHighlightRust is syntax highlight for Rust. SyntaxHighlightRust SyntaxHighlight = "rust" // SyntaxHighlightObjectiveC is syntax highlight for Objective-C. SyntaxHighlightObjectiveC SyntaxHighlight = "objectivec" // SyntaxHighlightPerl is syntax highlight for Perl. SyntaxHighlightPerl SyntaxHighlight = "perl" // SyntaxHighlightLua is syntax highlight for Lua. SyntaxHighlightLua SyntaxHighlight = "lua" // SyntaxHighlightDart is syntax highlight for Dart. SyntaxHighlightDart SyntaxHighlight = "dart" // SyntaxHighlightClojure is syntax highlight for Clojure. SyntaxHighlightClojure SyntaxHighlight = "clojure" // SyntaxHighlightGroovy is syntax highlight for Groovy. SyntaxHighlightGroovy SyntaxHighlight = "groovy" // SyntaxHighlightR is syntax highlight for R. SyntaxHighlightR SyntaxHighlight = "r" // SyntaxHighlightHaskell is syntax highlight for Haskell. SyntaxHighlightHaskell SyntaxHighlight = "haskell" // SyntaxHighlightErlang is syntax highlight for Erlang. SyntaxHighlightErlang SyntaxHighlight = "erlang" // SyntaxHighlightElixir is syntax highlight for Elixir. SyntaxHighlightElixir SyntaxHighlight = "elixir" // SyntaxHighlightOCaml is syntax highlight for OCaml. SyntaxHighlightOCaml SyntaxHighlight = "ocaml" // SyntaxHighlightJulia is syntax highlight for Julia. SyntaxHighlightJulia SyntaxHighlight = "julia" // SyntaxHighlightScheme is syntax highlight for Scheme. SyntaxHighlightScheme SyntaxHighlight = "scheme" // SyntaxHighlightFSharp is syntax highlight for F#. SyntaxHighlightFSharp SyntaxHighlight = "fsharp" // SyntaxHighlightCoffeeScript is syntax highlight for CoffeeScript. SyntaxHighlightCoffeeScript SyntaxHighlight = "coffeescript" // SyntaxHighlightVBNet is syntax highlight for VB.NET. SyntaxHighlightVBNet SyntaxHighlight = "vbnet" // SyntaxHighlightTeX is syntax highlight for TeX. SyntaxHighlightTeX SyntaxHighlight = "tex" // SyntaxHighlightDiff is syntax highlight for Diff. SyntaxHighlightDiff SyntaxHighlight = "diff" // SyntaxHighlightApache is syntax highlight for Apache. SyntaxHighlightApache SyntaxHighlight = "apache" // SyntaxHighlightDockerfile is syntax highlight for Dockerfile. SyntaxHighlightDockerfile SyntaxHighlight = "dockerfile" // SyntaxHighlightMermaid is syntax highlight for Mermaid. SyntaxHighlightMermaid SyntaxHighlight = "mermaid" )
type TableOptions ¶
type TableOptions struct { // AutoWrapText is whether to wrap the text automatically. AutoWrapText bool }