1
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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)),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Index(c *gin.Context) {
|
||||
|
||||
articleBasePath := "./article"
|
||||
|
||||
list := make([]string, 0)
|
||||
|
||||
err := filepath.Walk(articleBasePath, func(path string, info os.FileInfo, err error) error {
|
||||
// 如果是目录,直接返回
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果文件扩展名不是 .md,直接返回
|
||||
if filepath.Ext(path) != ".md" {
|
||||
return nil
|
||||
}
|
||||
|
||||
filePath := path
|
||||
uri := filePath[:len(filePath)-len(filepath.Ext(filePath))]
|
||||
uri = strings.Replace(uri, "article\\", "", -1)
|
||||
uri = strings.Replace(uri, "\\", "-", -1)
|
||||
list = append(list, uri)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.HTML(200, "index.html", gin.H{
|
||||
"list": list,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user