Skip to content

Commit

Permalink
fix: Properly handle macro when extracting Metadata
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
xiu and mrueg committed Jan 26, 2023
1 parent 667e7be commit 1b3c7b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 9 additions & 2 deletions pkg/mark/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type Meta struct {
}

var (
reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
reHeaderPatternMacro = regexp.MustCompile(`<!-- Macro: .*`)
)

func ExtractMeta(data []byte) (*Meta, []byte, error) {
Expand All @@ -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
}

Expand Down

0 comments on commit 1b3c7b4

Please sign in to comment.