【问题标题】:How do I insert/extract front matter from Blackfriday in Go?如何在 Go 中从 Blackfriday 插入/提取前端内容?
【发布时间】: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 信息,并将剩余字节传递给 MarkdownCommonA YAML parser 可以在这里提供帮助,如果您不想自己解析它,尽管在您的情况下它可能是矫枉过正。
  • 你已经在使用一个名为 blackfriday 的 fancy package。此外,html/templateio/ioutilnet/http 也都是 精美的软件包 - 只是它们包含在 Go 的安装中。只需使用github.com/go-yaml/yaml 并继续前进。
  • 好的,谢谢,我会查看 yaml 包。我只是不想使用像 Hugo 或 Revel 这样巨大的东西。

标签: go markdown


【解决方案1】:

我使用https://github.com/gernest/front 来解析jekyll 文件。效果很好。先用front解析文件,然后将body传递给blackfriday。

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    相关资源
    最近更新 更多