Merge pull request #244 from xiu/fix/214

fix: Title not being extracted when not on the first line
This commit is contained in:
Manuel Rüger 2023-01-27 11:37:00 +01:00 committed by GitHub
commit adee0189bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 3 deletions

View File

@ -120,6 +120,9 @@ be replaced with specified template:
<yaml-data> --> <yaml-data> -->
``` ```
**NOTE**: Make sure to define your macros after your metadata (Title/Space),
mark will stop processing metadata if it hits a Macro.
Capture groups can be defined in the macro's <regexp> which can be later Capture groups can be defined in the macro's <regexp> which can be later
referenced in the `<yaml-data>` using `${<number>}` syntax, where `<number>` is referenced in the `<yaml-data>` using `${<number>}` syntax, where `<number>` is
number of a capture group in regexp (`${0}` is used for entire regexp match), number of a capture group in regexp (`${0}` is used for entire regexp match),

View File

@ -345,7 +345,7 @@ func DropDocumentLeadingH1(
// ExtractDocumentLeadingH1 will extract leading H1 heading // ExtractDocumentLeadingH1 will extract leading H1 heading
func ExtractDocumentLeadingH1(markdown []byte) string { func ExtractDocumentLeadingH1(markdown []byte) string {
h1 := regexp.MustCompile(`^#[^#]\s*(.*)\s*\n`) h1 := regexp.MustCompile(`#[^#]\s*(.*)\s*\n`)
groups := h1.FindSubmatch(markdown) groups := h1.FindSubmatch(markdown)
if groups == nil { if groups == nil {
return "" return ""

View File

@ -34,8 +34,9 @@ type Meta struct {
} }
var ( var (
reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`) reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`) reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
) )
func ExtractMeta(data []byte) (*Meta, []byte, error) { func ExtractMeta(data []byte) (*Meta, []byte, error) {
@ -58,6 +59,12 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
if matches == nil { if matches == nil {
matches = reHeaderPatternV1.FindStringSubmatch(line) matches = reHeaderPatternV1.FindStringSubmatch(line)
if matches == nil { if matches == nil {
matches = reHeaderPatternMacro.FindStringSubmatch(line)
// If we have a match, then we started reading a macro.
// We want to keep it in the document for it to be read by ExtractMacros
if matches != nil {
offset -= len(line) + 1
}
break break
} }

View File

@ -1,3 +1,4 @@
# a # a
## b ## b
### c ### c