if the first content line is like
# some title
it will be interpreted as
---
title: "some title"
---
---
Taking into account our conversation regarding my
previous patch, I have moved the "title shortcut"
to be a frontmatter shorthand instead.
I also updated the doc.
Let me know what you think.
Olivier
---
docs/kiln.1.scd | 11 +++++++++++frontmatter.go | 25 +++++++++++++++++++++++--frontmatter_test.go | 24 ++++++++++++++++++++++--
3 files changed, 56 insertions(+), 4 deletions(-)
diff --git a/docs/kiln.1.scd b/docs/kiln.1.scd
index f69fab2..3274902 100644
--- a/docs/kiln.1.scd+++ b/docs/kiln.1.scd
@@ -79,6 +79,17 @@ Example:
Page content
```
+In the absence of a frontmatter, the first line will be interpreted as a title if+it starts with "# ".++Example++ ```+ # Page title++ Page content+ ```+The following keys are supported:
*title*
diff --git a/frontmatter.go b/frontmatter.go
index e89433f..7042d47 100644
--- a/frontmatter.go+++ b/frontmatter.go
@@ -1,17 +1,38 @@
package main
-import "bytes"+import (+ "bytes"+ "strconv"+)var (
frontmatterOpen = []byte("---")
frontmatterClose = []byte("\n---")
lf = []byte("\n")
crlf = []byte("\r\n")
+ frontmatterShort = []byte("# "))
func extractFrontmatter(b []byte) (frontmatter, content []byte) {
if !bytes.HasPrefix(b, frontmatterOpen) {
- return nil, b++ // check for short frontmatter: # some title+ var found bool+ b, found = bytes.CutPrefix(b, frontmatterShort)+ if !found {+ return nil, b+ }++ // extract title and format it as yaml+ title := b+ i := bytes.IndexByte(b, '\n')+ if i != -1 {+ title = bytes.TrimSuffix(title[:i], []byte("\r"))+ } else {+ i = len(b) - 1+ }++ return []byte("title: " + strconv.Quote(string(title))), b[i+1:]
Overall this looks good to me. However, I wonder if using strconv.Quote can
cause unexpected issues with escaping characters. It might be better to have the
title as a separate return value instead, and handle the extra return value in
the code and test cases.