// 获取 markdown 文件夹下所有文件 func Marklist() (mdlist []string) { mddir := conf.DirMark() filepath.Walk(mddir, func(path string, f os.FileInfo, err error) error { if err != nil { //忽略错误 return err } if f.IsDir() { return nil } if strings.ToLower(f.Name()) == "readme.md" { return nil } if f.Name() == "about.md" { return nil } if strings.HasSuffix(f.Name(), ".md") { mdlist = append(mdlist, path) } return nil }) return mdlist }
func ReadMuCtx(path string) (ctx *mustring, err error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() br := bufio.NewReader(f) line, err := br.ReadString('\n') if err != nil { return nil, err } if !strings.HasPrefix(line, "---") { err = errors.New("Not Start with --- : " + path) return nil, err } buf := bytes.NewBuffer(nil) for { line, err = br.ReadString('\n') if err != nil { if err != io.EOF { return nil, err } } if strings.HasPrefix(line, "---") { break } buf.WriteString(line) } err = yaml.Unmarshal(buf.Bytes(), &ctx) content, err := ioutil.ReadAll(br) if err != nil { return nil, err } fi, _ := f.Stat() if ctx.Title == "" { ctx.Title = strings.Replace(strings.TrimRight(fi.Name(), ".md"), conf.DirMark()+"/", "", 1) } if ctx.Date == "" { ctx.Date = utils.Format(fi.ModTime().Unix()) } ctx.Content = string(content) return }
// 获取about内容 func GetAbout() (art *Article, err error) { art = &Article{} about := path.Join(conf.DirMark(), "/about.md") if _, err := os.Stat(about); os.IsNotExist(err) { return art, nil } content, err := ioutil.ReadFile(about) if err != nil { return nil, err } art.Title = "" art.Content = utils.MarkdownToHtml(string(content)) art.CreatedAt = time.Now().Unix() return art, nil }