【发布时间】:2016-09-26 11:07:26
【问题描述】:
在 jekyll 中,您将其插入到降价的顶部,并且能够将它们插入到您的布局中:
---
layout: post
title: Blogging Like a Hacker
---
我需要在 Go 中做同样的事情,不使用框架或花哨的包。只是 Golang。
import (
"github.com/russross/blackfriday"
"html/template"
"io/ioutil"
"net/http"
)
type webPost struct {
Title string
Author string
Description string
Body template.HTML
}
func handlePost(res http.ResponseWriter, req *http.Request) {
//Read in some markdown from a file
input, _ := ioutil.ReadFile("test.md")
//Render it into HTML
output := blackfriday.MarkdownCommon(input)
//I need the first three parameters to grab the front matter from test.md
post := webPost{"title", "author", "a descritpion", template.HTML(output)}
//Serve to client a template
templates.ExecuteTemplate(res, "Post", post)
}
【问题讨论】:
-
“花式”和“非花式”包装之间的分界线究竟在哪里?
-
对于提取,您需要解析字节切片,提取 YAML 信息,并将剩余字节传递给
MarkdownCommon。 A YAML parser 可以在这里提供帮助,如果您不想自己解析它,尽管在您的情况下它可能是矫枉过正。 -
你已经在使用一个名为 blackfriday 的 fancy package。此外,
html/template、io/ioutil和net/http也都是 精美的软件包 - 只是它们包含在 Go 的安装中。只需使用github.com/go-yaml/yaml并继续前进。 -
好的,谢谢,我会查看 yaml 包。我只是不想使用像 Hugo 或 Revel 这样巨大的东西。