From 667e7be221a21e488667a1af7203c1c0e81efc73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20H=C3=A9rail?= Date: Wed, 25 Jan 2023 10:52:00 +0100 Subject: [PATCH 1/2] fix: Title not being extracted when not on the first line While a page with a macro is processed, having a macro leaves an unclean buffer for further processing steps, including title extraction. The problem seems to come from `ExtractMeta`: as it runs through the metadata, it hits the first line of the macro but since it doesn't look like a complete Metadata (e.g. matching either `\[\]:\s*#\s*\(([^:]+):\s*(.*)\)` or `` in https://github.com/kovetskiy/mark/blob/master/pkg/mark/meta.go#L37:L38, it will break the loop in https://github.com/kovetskiy/mark/blob/master/pkg/mark/meta.go#L61 and return the final document as: ``` Template: ac:children Style: h2 Excerpt: none Page: TestSpace:Test Reverse: false All: true --> :children: ``` This then goes into `ExtractDocumentLeadingH1` which doesn't find a match for the regex `^#[^#]\s*(.*)\s*\n`, returning an empty title since the title is not on the first line. This commit, while it doesn't fix the unclean document, fixes the regex to properly find the title. Closes #214 --- pkg/mark/markdown.go | 2 +- pkg/mark/testdata/header.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/mark/markdown.go b/pkg/mark/markdown.go index 98576c0..ddea59f 100644 --- a/pkg/mark/markdown.go +++ b/pkg/mark/markdown.go @@ -345,7 +345,7 @@ func DropDocumentLeadingH1( // ExtractDocumentLeadingH1 will extract leading H1 heading func ExtractDocumentLeadingH1(markdown []byte) string { - h1 := regexp.MustCompile(`^#[^#]\s*(.*)\s*\n`) + h1 := regexp.MustCompile(`#[^#]\s*(.*)\s*\n`) groups := h1.FindSubmatch(markdown) if groups == nil { return "" diff --git a/pkg/mark/testdata/header.md b/pkg/mark/testdata/header.md index 9c1d2f0..81a9738 100644 --- a/pkg/mark/testdata/header.md +++ b/pkg/mark/testdata/header.md @@ -1,3 +1,4 @@ + # a ## b ### c From 1b3c7b4127c2125bfa0a780d4e65703f29ab0ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20H=C3=A9rail?= Date: Wed, 25 Jan 2023 13:08:08 +0100 Subject: [PATCH 2/2] fix: Properly handle macro when extracting Metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a macro is set in the header, only the first line will be read and then discarded. This makes sure we keep the macro in and stop processing metadata when we hit a macro. Co-authored-by: Manuel RĂ¼ger --- README.md | 3 +++ pkg/mark/meta.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e70b76..f288534 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,9 @@ be replaced with specified template: --> ``` +**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 which can be later referenced in the `` using `${}` syntax, where `` is number of a capture group in regexp (`${0}` is used for entire regexp match), diff --git a/pkg/mark/meta.go b/pkg/mark/meta.go index 2f1a301..416182b 100644 --- a/pkg/mark/meta.go +++ b/pkg/mark/meta.go @@ -34,8 +34,9 @@ type Meta struct { } var ( - reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`) - reHeaderPatternV2 = regexp.MustCompile(``) + reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`) + reHeaderPatternV2 = regexp.MustCompile(``) + reHeaderPatternMacro = regexp.MustCompile(`