I'm writing a custom rule that validates front matter content (for example, requiring a title key and checking date formats).
As noted in #1202, a custom rule can inspect front matter via params.frontMatterLines. However, there is currently no way to report an error at a precise location within the front matter, nor to fix it: onError only accepts lineNumber values that refer to params.lines (the Markdown body), and fixInfo is likewise limited to body lines.
I'd like to propose a small, opt-in extension to RuleOnErrorInfo:
onError({
frontMatter: true, // new: lineNumber refers to params.frontMatterLines
lineNumber: 2, // 1-based index into frontMatterLines
range: [1, 5],
fixInfo: { editColumn: 1, deleteCount: 5, insertText: "title" }
});
When frontMatter is true:
lineNumber (and fixInfo.lineNumber, if specified) are validated against frontMatterLines.length instead of lines.length; when producing the absolute file line number, the usual front matter offset is not added
range, fixInfo.editColumn, and fixInfo.deleteCount are validated against the corresponding line in frontMatterLines
- Using
frontMatter: true when there is no front matter fails validation, consistent with other onError validation
As far as I can tell, no change to the output format is needed: LintError.lineNumber is already absolute within the file, and applyFixes already operates on the full input, including front matter. Existing rules would be unaffected because the flag is opt-in.
One implementation detail is enabledRulesPerLineNumber: its entries corresponding to front matter are currently empty, so the rule-enablement check in onError would also need to account for front matter errors.
What do you think of this approach? If it sounds reasonable, I'd be happy to open a pull request.
I'm writing a custom rule that validates front matter content (for example, requiring a
titlekey and checking date formats).As noted in #1202, a custom rule can inspect front matter via
params.frontMatterLines. However, there is currently no way to report an error at a precise location within the front matter, nor to fix it:onErroronly acceptslineNumbervalues that refer toparams.lines(the Markdown body), andfixInfois likewise limited to body lines.I'd like to propose a small, opt-in extension to
RuleOnErrorInfo:When
frontMatteristrue:lineNumber(andfixInfo.lineNumber, if specified) are validated againstfrontMatterLines.lengthinstead oflines.length; when producing the absolute file line number, the usual front matter offset is not addedrange,fixInfo.editColumn, andfixInfo.deleteCountare validated against the corresponding line infrontMatterLinesfrontMatter: truewhen there is no front matter fails validation, consistent with otheronErrorvalidationAs far as I can tell, no change to the output format is needed:
LintError.lineNumberis already absolute within the file, andapplyFixesalready operates on the full input, including front matter. Existing rules would be unaffected because the flag is opt-in.One implementation detail is
enabledRulesPerLineNumber: its entries corresponding to front matter are currently empty, so the rule-enablement check inonErrorwould also need to account for front matter errors.What do you think of this approach? If it sounds reasonable, I'd be happy to open a pull request.