package notes

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"path"

	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/html"
	"github.com/gomarkdown/markdown/parser"
)

func notesFile(p Path, w http.ResponseWriter, r *http.Request) {
	b, _ := ioutil.ReadFile(p.Local)
	notesFileHead(p, w)
	renderer := html.NewRenderer(html.RendererOptions{
		Flags: html.CommonFlags | html.TOC,
	})
	parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
	content := markdown.ToHTML(b, parser, renderer)
	fmt.Fprintf(w, "%s\n", content)
}

func notesFileHead(p Path, w http.ResponseWriter) {
	fmt.Fprintf(w, `
      <a href=%q><input type="button" value="Edit"></input></a>
   `, path.Join("/edit/", p.BaseHREF))
}
