Skip to content

Go package that converts string fields within a struct from Markdown to HTML in-place.

License

Notifications You must be signed in to change notification settings

herbygillot/markstruct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

markstruct Tests Docs Coverage Status Go Report Card

markstruct converts a struct's string fields from Markdown to HTML in-place.

markstruct scans a struct for tagged fields of relevant type (string, *string, []string & maps with string values), and renders the field value from Markdown to HTML in-place. That is to say the value of each field itself will be changed within the struct to be the HTML result of rendering the original value as Markdown.

markstruct uses github.com/yuin/goldmark to render Markdown, and allows for custom goldmark.Markdown objects and parse options.

Installation

go get github.com/herbygillot/markstruct

Usage

ConvertFields accepts a pointer to struct, and converts fields of relevant type (as listed above) from Markdown to HTML that are tagged with markdown:"on":


type Document struct {
  Title string                 // this field will be ignored
  Body  string `markdown:"on"` // this field will be converted
}

 doc := &Document{
     Title: "Doc *1*",
     Body:  "This is _emphasis_.",
 }

 changed, err := markstruct.ConvertFields(doc)
 ...
 fmt.Println(doc.Title) // "Doc *1*"
 fmt.Println(doc.Body)  // "<p>This is <em>emphasis</em>.</p>"

ConvertAllFields also accepts a pointer to struct, but will convert all fields of relevant type, ignoring the absence or presence of the markdown:"on" tag.

type Document struct {
  Title string                 // normally ignored, but will be converted by ConvertAllFields
  Body  string `markdown:"on"` // this field will be converted
}

 doc := &Document{
     Title: "Doc *1*",
     Body:  "This is _emphasis_.",
 }

 changed, err := markstruct.ConvertAllFields(doc)
 ...
 fmt.Println(doc.Title) // "<p>Doc <em>1</em></p>"
 fmt.Println(doc.Body)  // "<p>This is <em>emphasis</em>.</p>"

There are equivalent functions, ValidateFields and ValidateAllFields, that can be used to check if errors would occur during conversion, making no changes to the target struct. They return the exact same values as ConverFields and ConvertAllFields, respectively.

About

Go package that converts string fields within a struct from Markdown to HTML in-place.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages