Skip to content

Allow custom rules to report and fix errors in front matter #2239

Description

@todesking

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions