package notes
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"local/notes-server/filetree"
"path"
"regexp"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
func (n *Notes) File(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("path is dir")
}
b, _ := ioutil.ReadFile(p.Local)
renderer := html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags | html.TOC,
RenderNodeHook: n.commentFormer(urlPath, b),
})
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
content := markdown.ToHTML(b, parser, renderer)
return string(content) + "\n", nil
}
func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
urlPath = strings.TrimPrefix(urlPath, "/")
urlPath = strings.TrimPrefix(urlPath, strings.Split(urlPath, "/")[0])
lines := bytes.Split(md, []byte("\n"))
cur := -1
nextHeader := func() {
cur++
for cur < len(lines) {
for _, opener_closer := range [][]string{{"```", "```"}, {`