mirror of
https://github.com/kovetskiy/mark.git
synced 2025-06-07 23:12:42 +08:00
fix: Properly handle macro when extracting Metadata
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 <manuel@rueg.eu>
This commit is contained in:
parent
667e7be221
commit
1b3c7b4127
@ -120,6 +120,9 @@ be replaced with specified template:
|
||||
<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
|
||||
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),
|
||||
|
@ -36,6 +36,7 @@ type Meta struct {
|
||||
var (
|
||||
reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
|
||||
reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
|
||||
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
|
||||
)
|
||||
|
||||
func ExtractMeta(data []byte) (*Meta, []byte, error) {
|
||||
@ -58,6 +59,12 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
|
||||
if matches == nil {
|
||||
matches = reHeaderPatternV1.FindStringSubmatch(line)
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user