Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change API for simpler use by gmnhg
  • Loading branch information
tdemin committed Nov 11, 2020
1 parent 35b4168 commit 8226a20
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/md2gmn/main.go
Expand Up @@ -47,7 +47,7 @@ func main() {
panic(err)
}

geminiContent, err := gemini.RenderMarkdown(text)
geminiContent, _, err := gemini.RenderMarkdown(text, true)
if err != nil {
panic(err)
}
Expand Down
36 changes: 25 additions & 11 deletions render.go
Expand Up @@ -19,6 +19,7 @@ package gemini

import (
"bytes"
"errors"
"fmt"
"time"

Expand All @@ -28,32 +29,42 @@ import (
"gopkg.in/yaml.v2"
)

// hugoMetadata implements gemini.Metadata, providing the bare minimum
// HugoMetadata implements gemini.Metadata, providing the bare minimum
// of possible post props.
type hugoMetadata struct {
PostTitle string `yaml:"title"`
PostDate time.Time `yaml:"date"`
type HugoMetadata struct {
PostTitle string `yaml:"title"`
PostIsDraft bool `yaml:"draft"`
PostLayout string `yaml:"layout"`
PostDate time.Time `yaml:"date"`
}

func (h hugoMetadata) Title() string {
// Title returns post title.
func (h HugoMetadata) Title() string {
return h.PostTitle
}

func (h hugoMetadata) Date() time.Time {
// Date returns post date.
func (h HugoMetadata) Date() time.Time {
return h.PostDate
}

var yamlDelimiter = []byte("---\n")

// ErrPostIsDraft indicates the post rendered is a draft and is not
// supposed to be rendered.
var ErrPostIsDraft = errors.New("post is draft")

// RenderMarkdown converts Markdown text to text/gemini using
// gomarkdown, appending Hugo YAML front matter data if any is present
// to the post header.
//
// Only a subset of front matter data parsed by Hugo is included in the
// final document. At this point it's just title and date.
func RenderMarkdown(md []byte) (geminiText []byte, err error) {
//
// Draft posts are still rendered, but with an error of type
// ErrPostIsDraft.
func RenderMarkdown(md []byte, withMetadata bool) (geminiText []byte, metadata HugoMetadata, err error) {
var (
metadata hugoMetadata
blockEnd int
yamlContent []byte
)
Expand All @@ -67,16 +78,19 @@ func RenderMarkdown(md []byte) (geminiText []byte, err error) {
}
yamlContent = md[len(yamlDelimiter) : blockEnd+len(yamlDelimiter)]
if err := yaml.Unmarshal(yamlContent, &metadata); err != nil {
return nil, fmt.Errorf("invalid front matter: %w", err)
return nil, metadata, fmt.Errorf("invalid front matter: %w", err)
}
md = md[blockEnd+len(yamlDelimiter)*2:]
parse:
ast := markdown.Parse(md, parser.NewWithExtensions(parser.CommonExtensions))
var geminiContent []byte
if metadata.PostTitle != "" {
if withMetadata && metadata.PostTitle != "" {
geminiContent = markdown.Render(ast, gemini.NewRendererWithMetadata(metadata))
} else {
geminiContent = markdown.Render(ast, gemini.NewRenderer())
}
return geminiContent, nil
if metadata.PostIsDraft {
return geminiContent, metadata, fmt.Errorf("%s: %w", metadata.PostTitle, ErrPostIsDraft)
}
return geminiContent, metadata, nil
}

0 comments on commit 8226a20

Please sign in to comment.