Initial release
This commit is contained in:
41
parse/parse.go
Normal file
41
parse/parse.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"TheAdversary/config"
|
||||
"TheAdversary/schema"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/ast"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"os"
|
||||
)
|
||||
|
||||
func newParser() *parser.Parser {
|
||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
|
||||
|
||||
return parser.NewWithExtensions(extensions)
|
||||
}
|
||||
|
||||
func newHtmlRenderer(title string) *html.Renderer {
|
||||
renderOpts := html.RendererOptions{
|
||||
Title: title,
|
||||
Flags: html.CommonFlags | html.CompletePage,
|
||||
}
|
||||
return html.NewRenderer(renderOpts)
|
||||
}
|
||||
|
||||
func Parse(path string) (ast.Node, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return markdown.Parse(raw, newParser()), nil
|
||||
}
|
||||
|
||||
func ParseToHtml(article *schema.Article) ([]byte, error) {
|
||||
node, err := Parse(article.FilePath(config.ArticleRoot))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return markdown.Render(node, newHtmlRenderer(article.Title)), nil
|
||||
}
|
||||
Reference in New Issue
Block a user