Refactor RenderMarkdown signature
This makes the function accept a bitmask instead of a single flag. This is a breaking change.
This commit is contained in:
parent
30a5d407e2
commit
47e056f253
2 changed files with 17 additions and 14 deletions
|
@ -98,8 +98,6 @@ var (
|
|||
topLevelPostRegex = regexp.MustCompile(contentBase + `([\w-_ ]+)/([\w-_ ]+)\.md`)
|
||||
)
|
||||
|
||||
// TODO: more meaningful errors
|
||||
|
||||
type post struct {
|
||||
Post []byte
|
||||
Metadata gemini.HugoMetadata
|
||||
|
@ -205,7 +203,7 @@ func main() {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gemText, metadata, err := gemini.RenderMarkdown(fileContent, gemini.WithoutMetadata)
|
||||
gemText, metadata, err := gemini.RenderMarkdown(fileContent, gemini.Defaults)
|
||||
// skip drafts from rendering
|
||||
if errors.Is(err, gemini.ErrPostIsDraft) {
|
||||
return nil
|
||||
|
@ -277,7 +275,7 @@ func main() {
|
|||
// skip unreadable index files
|
||||
continue
|
||||
}
|
||||
gemtext, _, err := gemini.RenderMarkdown(content, gemini.WithoutMetadata)
|
||||
gemtext, _, err := gemini.RenderMarkdown(content, gemini.Defaults)
|
||||
if errors.Is(err, gemini.ErrPostIsDraft) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
|
@ -305,7 +303,7 @@ func main() {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gemtext, _, err := gemini.RenderMarkdown(indexContent, gemini.WithoutMetadata)
|
||||
gemtext, _, err := gemini.RenderMarkdown(indexContent, gemini.Defaults)
|
||||
if err != nil && !errors.Is(err, gemini.ErrPostIsDraft) {
|
||||
panic(err)
|
||||
}
|
||||
|
|
23
render.go
23
render.go
|
@ -54,15 +54,20 @@ var yamlDelimiter = []byte("---\n")
|
|||
// supposed to be rendered.
|
||||
var ErrPostIsDraft = errors.New("post is draft")
|
||||
|
||||
// MetadataSetting defines whether or not metadata is included in the
|
||||
// rendered text.
|
||||
type MetadataSetting int
|
||||
// Settings is a bitmask for renderer preferences.
|
||||
type Settings uint
|
||||
|
||||
// Has uses AND to check whether a flag is set.
|
||||
func (s Settings) Has(setting Settings) bool {
|
||||
return (s & setting) != 0
|
||||
}
|
||||
|
||||
// Metadata settings control the inclusion of metadata in the rendered
|
||||
// text.
|
||||
const (
|
||||
WithMetadata MetadataSetting = iota
|
||||
WithoutMetadata
|
||||
// Defaults simply renders the document.
|
||||
Defaults Settings = 0b0
|
||||
// WithMetadata indicates that the metadata should be included in
|
||||
// the text produced by the renderer.
|
||||
WithMetadata Settings = 0b1
|
||||
)
|
||||
|
||||
// RenderMarkdown converts Markdown text to text/gemini using
|
||||
|
@ -74,7 +79,7 @@ const (
|
|||
//
|
||||
// Draft posts are still rendered, but with an error of type
|
||||
// ErrPostIsDraft.
|
||||
func RenderMarkdown(md []byte, metadataSetting MetadataSetting) (geminiText []byte, metadata HugoMetadata, err error) {
|
||||
func RenderMarkdown(md []byte, settings Settings) (geminiText []byte, metadata HugoMetadata, err error) {
|
||||
var (
|
||||
blockEnd int
|
||||
yamlContent []byte
|
||||
|
@ -95,7 +100,7 @@ func RenderMarkdown(md []byte, metadataSetting MetadataSetting) (geminiText []by
|
|||
parse:
|
||||
ast := markdown.Parse(md, parser.NewWithExtensions(parser.CommonExtensions))
|
||||
var geminiContent []byte
|
||||
if metadataSetting == WithMetadata && metadata.PostTitle != "" {
|
||||
if settings.Has(WithMetadata) && metadata.PostTitle != "" {
|
||||
geminiContent = markdown.Render(ast, gemini.NewRendererWithMetadata(metadata))
|
||||
} else {
|
||||
geminiContent = markdown.Render(ast, gemini.NewRenderer())
|
||||
|
|
Loading…
Reference in a new issue