Browse Source

pkg: markup: implement markdown frontmatter parser

Uses the page parser from Hugo, which supports yaml, toml, and json, to
seperate frontmatter from content. In the case of an error, the page is
constructed as if no parsing was done.

Uses `github.com/barracks510/go-table` to convert from page metadata to
HTML tables. go-table was written specifically for this use case.

Resolves #2931.

Signed-off-by: Dennis Chen <barracks510@gmail.com>
pull/4643/head
Dennis Chen 7 years ago
parent
commit
aeeaa79f36
  1. 20
      pkg/markup/markdown.go

20
pkg/markup/markdown.go

@ -12,10 +12,12 @@ import (
"regexp"
"strings"
"github.com/barracks510/go-table"
"github.com/gohugoio/hugo/parser"
"github.com/russross/blackfriday"
"github.com/gogits/gogs/pkg/tool"
"github.com/gogits/gogs/pkg/setting"
"github.com/gogits/gogs/pkg/tool"
)
// IsMarkdownFile reports whether name looks like a Markdown file based on its extension.
@ -157,8 +159,20 @@ func RawMarkdown(body []byte, urlPrefix string) []byte {
extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
}
body = blackfriday.Markdown(body, renderer, extensions)
return body
page, err := parser.ReadFrom(bytes.NewBuffer(body))
if err != nil {
return blackfriday.Markdown(body, renderer, extensions)
}
body = blackfriday.Markdown(page.Content(), renderer, extensions)
data, err := page.Metadata()
if err != nil {
return body
}
table, err := table.MakeTable(data)
if err != nil {
return body
}
return append(table.(*bytes.Buffer).Bytes(), body...)
}
// Markdown takes a string or []byte and renders to HTML in Markdown syntax with special links.

Loading…
Cancel
Save