-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow users to override comment delimiters (#900)
Allow users to specify the comment delimiters they are using in their documentation. vale replaces these with HTML comment tags before linting, making it possible to control style rules for specific passages of prose in file formats that use non-HTML comment syntax. This is critical for controlling style rules within a page in MDX, and potentially other formats as well. This example configures the `CommentDelimiters` field for `*.md` files, indicating that `{/*` and `*/}` are the custom comment delimiters: ```ini [*.md] CommentDelimiters = "{/*,*/}" ``` Internally, custom delimiters are represented as a `[2]string`, and it is only possible to configure one set of custom comment delimiters for a given format block. More specific changes: - Refactor `applyPatterns`. Remove the method receiver and take only the necessary fields of `*core.Config` as parameters. This makes it easier to test `applyPatterns` without mocking an entire `*core.Config`. Also extract functions for `applyInlinePatterns` and `applyBlockPatterns` so we can use fewer arguments in a single function. - Add `applyCommentPatterns`, which works similarly to `applyInlinePatterns` and `applyBlockPatterns`, but for substituting comments. - For tests, import `https//github.com/stretchr/testify/assert`, a popular testing library, to get richer test output. Closes #762
- Loading branch information
Showing
8 changed files
with
400 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_processConfig_commentDelimiters(t *testing.T) { | ||
cases := []struct { | ||
description string | ||
body string | ||
expected map[string][2]string | ||
}{ | ||
{ | ||
description: "custom comment delimiters for markdown", | ||
body: `[*.md] | ||
CommentDelimiters = "{/*,*/}" | ||
`, | ||
expected: map[string][2]string{ | ||
"*.md": [2]string{"{/*", "*/}"}, | ||
}, | ||
}, | ||
{ | ||
description: "not set", | ||
body: `[*.md] | ||
TokenIgnores = (\$+[^\n$]+\$+) | ||
`, | ||
expected: map[string][2]string{}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.description, func(t *testing.T) { | ||
uCfg, err := shadowLoad([]byte(c.body)) | ||
assert.NoError(t, err) | ||
conf, err := NewConfig(&CLIFlags{}) | ||
assert.NoError(t, err) | ||
_, err = processConfig(uCfg, conf, false) | ||
assert.NoError(t, err) | ||
actual := conf.CommentDelimiters | ||
assert.Equal(t, c.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func Test_processConfig_commentDelimiters_error(t *testing.T) { | ||
cases := []struct { | ||
description string | ||
body string | ||
expectedErr string | ||
}{ | ||
{ | ||
description: "global custom comment delimiters", | ||
body: `[*] | ||
CommentDelimiters = "{/*,*/}" | ||
`, | ||
expectedErr: "syntax-specific option", | ||
}, | ||
{ | ||
description: "more than two delimiters", | ||
body: `[*.md] | ||
CommentDelimiters = "{/*,*/},<<,>>" | ||
`, | ||
expectedErr: "CommentDelimiters must be a comma-separated list of two delimiters, but got 4 items", | ||
}, | ||
{ | ||
description: "more than two delimiters (shadow)", | ||
body: `[*.md] | ||
CommentDelimiters = "{/*,*/}" | ||
[*.md] | ||
CommentDelimiters = "<<,>>" | ||
`, | ||
expectedErr: "CommentDelimiters must be a comma-separated list of two delimiters, but got 4 items", | ||
}, | ||
{ | ||
description: "one delimiter is empty", | ||
body: `[*.md] | ||
CommentDelimiters = "{/*" | ||
`, | ||
expectedErr: "CommentDelimiters must be a comma-separated list of two delimiters, but got 1 items", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.description, func(t *testing.T) { | ||
uCfg, err := shadowLoad([]byte(c.body)) | ||
assert.NoError(t, err) | ||
conf, err := NewConfig(&CLIFlags{}) | ||
assert.NoError(t, err) | ||
_, err = processConfig(uCfg, conf, false) | ||
assert.ErrorContains(t, err, c.expectedErr) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.