mirror of
https://github.com/kovetskiy/mark.git
synced 2026-04-20 05:12:35 +00:00
Space from config and title from h1 (#153)
* Add option to define Space. Add option to use H1 heading to define page title. * Update readme
This commit is contained in:
@@ -147,3 +147,14 @@ func DropDocumentLeadingH1(
|
||||
markdown = h1.ReplaceAll(markdown, []byte(""))
|
||||
return markdown
|
||||
}
|
||||
|
||||
// ExtractDocumentLeadingH1 will extract leading H1 heading
|
||||
func ExtractDocumentLeadingH1(markdown []byte) string {
|
||||
h1 := regexp.MustCompile(`^#[^#]\s*(.*)\s*\n`)
|
||||
groups := h1.FindSubmatch(markdown)
|
||||
if groups == nil {
|
||||
return ""
|
||||
} else {
|
||||
return string(groups[1])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,3 +48,16 @@ func TestCompileMarkdown(t *testing.T) {
|
||||
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractDocumentLeadingH1(t *testing.T) {
|
||||
filename := "testdata/header.md"
|
||||
|
||||
markdown, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
actual := ExtractDocumentLeadingH1(markdown)
|
||||
|
||||
assert.Equal(t, "a", actual)
|
||||
}
|
||||
|
||||
@@ -128,19 +128,5 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
|
||||
return nil, data, nil
|
||||
}
|
||||
|
||||
if meta.Space == "" {
|
||||
return nil, nil, fmt.Errorf(
|
||||
"space key is not set (%s header is not set)",
|
||||
HeaderSpace,
|
||||
)
|
||||
}
|
||||
|
||||
if meta.Title == "" {
|
||||
return nil, nil, fmt.Errorf(
|
||||
"page title is not set (%s header is not set)",
|
||||
HeaderTitle,
|
||||
)
|
||||
}
|
||||
|
||||
return meta, data[offset:], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user