Better errors, frontmatter and tables fix
Anirudh Oppiliappan x@icyphox.sh
Mon, 04 Apr 2022 15:01:03 +0530
7 files changed,
72 insertions(+),
74 deletions(-)
M
commands/build.go
→
commands/build.go
@@ -71,7 +71,9 @@ return err
} out := markdown.Output{} - out.RenderMarkdown(fb) + if err = out.RenderMarkdown(fb); err != nil { + return err + } if err = out.RenderHTML( htmlFile, TEMPLATES,@@ -126,7 +128,9 @@ return err
} out := markdown.Output{} - out.RenderMarkdown(fb) + if err := out.RenderMarkdown(fb); err != nil { + return err + } if err = out.RenderHTML( htmlFile, TEMPLATES,@@ -160,7 +164,9 @@ if err != nil {
return err } out := markdown.Output{} - out.RenderMarkdown(indexMd) + if err := out.RenderMarkdown(indexMd); err != nil { + return err + } out.RenderHTML(indexHTML, TEMPLATES, struct { Cfg config.ConfigYaml
A
flake.lock
@@ -0,0 +1,25 @@
+{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1648219316, + "narHash": "sha256-Ctij+dOi0ZZIfX5eMhgwugfvB+WZSrvVNAyAuANOsnQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "30d3d79b7d3607d56546dd2a6b49e156ba0ec634", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +}
A
flake.nix
@@ -0,0 +1,17 @@
+{ + description = "vite"; + + outputs = { self, nixpkgs }: { + devShell.x86_64-linux = + let + pkgs = nixpkgs.legacyPackages.x86_64-linux; + in + pkgs.mkShell { + buildInputs = with pkgs; [ + go + gotools + gnumake + ]; + }; + }; +}
M
go.sum
→
go.sum
@@ -1,5 +1,9 @@
+github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Depado/bfchroma v1.3.0 h1:zz14vpvySU6S0CL6yGPr1vkFevQecIt8dJdCsMS2JpM= github.com/Depado/bfchroma v1.3.0/go.mod h1:c0bFk0tFmT+clD3TIGurjWCfD/QV8/EebfM3JGr+98M= +github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4= +github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE= github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= github.com/alecthomas/chroma v0.7.3/go.mod h1:sko8vR34/90zvl5QdcUdvzL3J8NKjAUx9va9jPuFNoM= github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=@@ -37,6 +41,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
M
markdown/frontmatter.go
→
markdown/frontmatter.go
@@ -1,12 +1,9 @@
package markdown import ( - "bufio" "bytes" - "errors" - "fmt" - "gopkg.in/yaml.v3" + "github.com/adrg/frontmatter" ) type Matter map[string]string@@ -15,68 +12,13 @@ type MarkdownDoc struct {
Frontmatter Matter Body []byte } - -const ( - yamlDelim = "---" -) func (md *MarkdownDoc) Extract(source []byte) error { - bufsize := 1024 * 1024 - buf := make([]byte, bufsize) - - input := bytes.NewReader(source) - s := bufio.NewScanner(input) - s.Buffer(buf, bufsize) - - matter := []byte{} - body := []byte{} - - s.Split(splitFunc) - n := 0 - for s.Scan() { - if n == 0 { - matter = s.Bytes() - } else if n == 1 { - body = s.Bytes() - } - n++ - } - if err := s.Err(); err != nil { - return fmt.Errorf("error: failed to scan text") - } - if err := yaml.Unmarshal(matter, &md.Frontmatter); err != nil { - return fmt.Errorf("error: failed to parse yaml") - } - md.Body = body - return nil -} - -func splitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) { - if atEOF && len(data) == 0 { - return 0, nil, nil - } - delim, err := sniffDelim(data) + r := bytes.NewReader(source) + rest, err := frontmatter.Parse(r, &md.Frontmatter) if err != nil { - return 0, nil, err + return err } - if delim != yamlDelim { - return 0, nil, fmt.Errorf("error: %s is not a supported delimiter", delim) - } - if x := bytes.Index(data, []byte(delim)); x >= 0 { - if next := bytes.Index(data[x+len(delim):], []byte(delim)); next > 0 { - return next + len(delim), bytes.TrimSpace(data[:next+len(delim)]), nil - } - return len(data), bytes.TrimSpace(data[x+len(delim):]), nil - } - if atEOF { - return len(data), data, nil - } - return 0, nil, nil -} - -func sniffDelim(input []byte) (string, error) { - if len(input) < 4 { - return "", errors.New("error: input is empty") - } - return string(input[:3]), nil + md.Body = rest + return nil }
M
markdown/markdown.go
→
markdown/markdown.go
@@ -1,6 +1,7 @@
package markdown import ( + "fmt" "os" gotmpl "text/template" "time"@@ -14,11 +15,8 @@ bf "github.com/russross/blackfriday/v2"
) var ( - bfFlags = bf.UseXHTML | bf.Smartypants | bf.SmartypantsFractions | - bf.SmartypantsDashes | bf.NofollowLinks - bfExts = bf.NoIntraEmphasis | bf.Tables | bf.FencedCode | bf.Autolink | - bf.Strikethrough | bf.SpaceHeadings | bf.BackslashLineBreak | - bf.HeadingIDs | bf.Footnotes | bf.NoEmptyLineBeforeBlock + bfFlags = bf.CommonHTMLFlags | bf.FootnoteReturnLinks | bf.SmartypantsFractions + bfExts = bf.CommonExtensions | bf.AutoHeadingIDs | bf.Footnotes | bf.NoEmptyLineBeforeBlock ) type Output struct {@@ -27,9 +25,11 @@ Meta Matter
} // Renders markdown to html, and fetches metadata. -func (out *Output) RenderMarkdown(source []byte) { +func (out *Output) RenderMarkdown(source []byte) error { md := MarkdownDoc{} - md.Extract(source) + if err := md.Extract(source); err != nil { + return fmt.Errorf("markdown: %w", err) + } out.HTML = bf.Run( md.Body,@@ -49,6 +49,7 @@ ),
bf.WithExtensions(bfExts), ) out.Meta = md.Frontmatter + return nil } // Renders out.HTML into dst html file, using the template specified