Skip to content

Commit 6aa8bb9

Browse files
committed
fix: don't error on duplicate fields
Signed-off-by: Joseph Kato <joseph@jdkato.io>
1 parent 94247d4 commit 6aa8bb9

File tree

7 files changed

+74
-9
lines changed

7 files changed

+74
-9
lines changed

internal/lint/metadata.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ import (
1414
func (l Linter) lintMetadata(f *core.File) error {
1515
metadata := make(map[string]any)
1616

17-
_, err := frontmatter.Parse(strings.NewReader(f.Content), &metadata)
17+
body, err := frontmatter.Parse(strings.NewReader(f.Content), &metadata)
1818
if errors.Is(err, frontmatter.ErrNotFound) {
19-
// No front matter found, return the original content.
2019
return nil
2120
} else if err != nil {
2221
return err
2322
}
2423

25-
seen := make(map[string]int)
24+
frontmatter, fmErr := extractFrontMatter(f.Content, string(body))
25+
if fmErr != nil {
26+
return fmErr
27+
}
28+
2629
for key, value := range metadata {
2730
if s, ok := value.(string); ok {
28-
i, line := findLineBySubstring(f.Content, s, seen)
31+
i, _ := findBestLineBySubstring(frontmatter, s)
2932
if i < 0 {
30-
return core.NewE100(f.Path, fmt.Errorf("'%s' not found", s))
33+
continue
3134
}
32-
seen[line] = i
3335

3436
scope := "text.frontmatter." + key + f.RealExt
3537
block := nlp.NewLinedBlock(f.Content, s, scope, i-1)
@@ -43,3 +45,11 @@ func (l Linter) lintMetadata(f *core.File) error {
4345

4446
return nil
4547
}
48+
49+
func extractFrontMatter(file, body string) (string, error) {
50+
startIndex := strings.Index(file, body)
51+
if startIndex == -1 {
52+
return "", fmt.Errorf("body not found in the file")
53+
}
54+
return file[:startIndex], nil
55+
}

internal/lint/util.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,38 @@ package lint
33
import (
44
"strings"
55

6+
"github.com/adrg/strutil"
7+
"github.com/adrg/strutil/metrics"
8+
69
"github.com/errata-ai/vale/v3/internal/core"
710
)
811

12+
func findBestLineBySubstring(s, sub string) (int, string) {
13+
if strings.Count(sub, "\n") > 0 {
14+
sub = strings.Split(sub, "\n")[0]
15+
}
16+
17+
bestMatchLine := -1
18+
bestMatch := ""
19+
bestMatchDistance := -1.0
20+
21+
metric := metrics.NewLevenshtein()
22+
for i, line := range strings.Split(s, "\n") {
23+
if !strings.Contains(line, sub) {
24+
continue
25+
}
26+
27+
distance := strutil.Similarity(line, sub, metric)
28+
if bestMatchLine == -1 || distance < bestMatchDistance {
29+
bestMatchDistance = distance
30+
bestMatchLine = i + 1
31+
bestMatch = line
32+
}
33+
}
34+
35+
return bestMatchLine, bestMatch
36+
}
37+
938
func findLineBySubstring(s, sub string, seen map[string]int) (int, string) {
1039
if strings.Count(sub, "\n") > 0 {
1140
sub = strings.Split(sub, "\n")[0]

testdata/features/frontmatter.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ Feature: Frontmatter
4343
test.rst:81:3:vale.Annotations:'TODO' left in text
4444
test.rst:81:38:vale.Annotations:'XXX' left in text
4545
test.rst:87:10:vale.Annotations:'TODO' left in text
46+
test2.md:2:8:Meta.Title:'How the SDK works' should be in title case
47+
test2.md:5:3:vale.Annotations:'TODO' left in text
48+
test2.mdx:2:8:Meta.Title:'How the SDK works' should be in title case
4649
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
StylesPath = ../../styles
22
MinAlertLevel = suggestion
33

4-
[*.{md,rst,adoc}]
4+
[*.{md,rst,adoc,mdx}]
55
vale.Annotations = YES
66
Meta.Title = YES

testdata/fixtures/frontmatter/test.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ XXX: This is a very interesting sentence that is not in a block.
4040
this sentence has -- dashes TODO: but it should still work.
4141

4242
| TODO | XXX | FIXME |
43-
|:----:|:---:|:-----:|
43+
| :--: | :-: | :---: |
4444
| one | two | three |
4545

4646
> **Note**: one two FIXME three.
4747
4848
[![Boxy Monokai ★ Predawn][img-monokai]][img-monokai]
4949

50-
is *TODO* by
50+
is _TODO_ by
5151

5252
![TODO](/images/logo.png)
5353

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: How the SDK works
3+
description: |
4+
Understand how the Android SDK loads, tracks, and send data
5+
TODO
6+
maintainedBy:
7+
- dc.android@xx.com
8+
category: android
9+
lang: en
10+
---
11+
12+
## Introduction
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: How the SDK works
3+
description: Understand how the Android SDK loads, tracks, and send data
4+
maintainedBy:
5+
- dc.android@xx.com
6+
category: android
7+
lang: en
8+
---
9+
10+
## Introduction
11+

0 commit comments

Comments
 (0)