43 lines
707 B
Go
43 lines
707 B
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/russross/blackfriday"
|
|
)
|
|
|
|
func Article(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
id = strings.Replace(id, "-", "/", -1)
|
|
|
|
articleBasePath := "./article"
|
|
|
|
articlePath := articleBasePath + "/" + id + ".md"
|
|
|
|
if _, err := os.Stat(articlePath); os.IsNotExist(err) {
|
|
c.String(200, "文章不存在")
|
|
|
|
return
|
|
}
|
|
|
|
// 读取Go Markdown文件
|
|
input, err := ioutil.ReadFile(articlePath)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 转换为HTML
|
|
output := blackfriday.MarkdownCommon(input)
|
|
|
|
c.HTML(200, "article.html", gin.H{
|
|
"content": template.HTML(string(output)),
|
|
})
|
|
}
|