[GH-ISSUE #927] How to fix everything what is possible when using a files option? #554

Closed
opened 2026-03-03 01:27:58 +03:00 by kerem · 6 comments
Owner

Originally created by @jcubic on GitHub (Aug 10, 2023).
Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/927

How to fix what is possible when using default: true? Do I need to duplicate every single rule to add fixInfo? There should be a way to make it fix what it can by default.

I've found this issue #663 but I'm not sure how to use this API if I have this config:

const options = {
  'files': process.argv.slice(2),
  'config': {
    'default': true,
    'line-length': {
      'heading_line_length': 100,
      'line_length': 100,
      'code_block_line_length': 100,
    },
  },
};

const fixResults = markdownlint(options, function callback(err, result) {
  if (!err) {
    console.log(result.toString());
    process.exit(1);
  }
});

markdownlintRuleHelpers.applyFixes(options, fixResults.content);

It seems that markdownlintRuleHelpers.applyFixes requires { content: string } but I have a file.

I've also tried this:

const { sync: markdownlint }  = require('markdownlint');
const markdownlintRuleHelpers = require('markdownlint-rule-helpers');

const fixResults = markdownlint(options, function callback(err, result) {
  if (!err) {
    console.log(result.toString());
    process.exit(1);
  }
});

markdownlintRuleHelpers.applyFixes(options, fixResults.content);

but this give an error:

./node_modules/markdownlint-rule-helpers/helpers.js:793
  const endings = input.match(newLineRe) || [];
                        ^

TypeError: input.match is not a function
Originally created by @jcubic on GitHub (Aug 10, 2023). Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/927 How to fix what is possible when using `default: true`? Do I need to duplicate every single rule to add fixInfo? There should be a way to make it fix what it can by default. I've found this issue #663 but I'm not sure how to use this API if I have this config: ```javascript const options = { 'files': process.argv.slice(2), 'config': { 'default': true, 'line-length': { 'heading_line_length': 100, 'line_length': 100, 'code_block_line_length': 100, }, }, }; const fixResults = markdownlint(options, function callback(err, result) { if (!err) { console.log(result.toString()); process.exit(1); } }); markdownlintRuleHelpers.applyFixes(options, fixResults.content); ``` It seems that `markdownlintRuleHelpers.applyFixes` requires `{ content: string }` but I have a file. I've also tried this: ```javascript const { sync: markdownlint } = require('markdownlint'); const markdownlintRuleHelpers = require('markdownlint-rule-helpers'); const fixResults = markdownlint(options, function callback(err, result) { if (!err) { console.log(result.toString()); process.exit(1); } }); markdownlintRuleHelpers.applyFixes(options, fixResults.content); ``` but this give an error: ``` ./node_modules/markdownlint-rule-helpers/helpers.js:793 const endings = input.match(newLineRe) || []; ^ TypeError: input.match is not a function ```
kerem 2026-03-03 01:27:58 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jcubic commented on GitHub (Aug 10, 2023):

So I was able to figure this out:

const { sync: markdownlint }  = require('markdownlint');
const fs = require('fs/promises');
const { applyFixes } = require('markdownlint-rule-helpers');

const options = {
  'files': process.argv.slice(2),
  'config': {
    'default': true,
    'line-length': {
      'heading_line_length': 100,
      'line_length': 100,
      'code_block_line_length': 100,
    },
  },
};

const fixResults = markdownlint(options, function callback(err, result) {
  if (!err) {
    console.log(result.toString());
    process.exit(1);
  }
});


Object.entries(fixResults).forEach(([filename, rules]) => {
  if (rules.length) {
    fs.readFile(filename, 'utf8').then(content => {
      const output = applyFixes(content, rules);
      fs.writeFile(filename, output);
    });
  }
});

Do you want me to add this to docs? If yes then where I should add this code?

<!-- gh-comment-id:1673417950 --> @jcubic commented on GitHub (Aug 10, 2023): So I was able to figure this out: ```javascript const { sync: markdownlint } = require('markdownlint'); const fs = require('fs/promises'); const { applyFixes } = require('markdownlint-rule-helpers'); const options = { 'files': process.argv.slice(2), 'config': { 'default': true, 'line-length': { 'heading_line_length': 100, 'line_length': 100, 'code_block_line_length': 100, }, }, }; const fixResults = markdownlint(options, function callback(err, result) { if (!err) { console.log(result.toString()); process.exit(1); } }); Object.entries(fixResults).forEach(([filename, rules]) => { if (rules.length) { fs.readFile(filename, 'utf8').then(content => { const output = applyFixes(content, rules); fs.writeFile(filename, output); }); } }); ``` Do you want me to add this to docs? If yes then where I should add this code?
Author
Owner

@DavidAnson commented on GitHub (Aug 10, 2023):

Issue #663 that you link to gives context and references a commit that adds an explanation and an example to the documentation:

<!-- gh-comment-id:1673514804 --> @DavidAnson commented on GitHub (Aug 10, 2023): Issue #663 that you link to gives context and references a commit that adds an explanation and an example to the documentation: - https://github.com/DavidAnson/markdownlint#fixing - https://github.com/DavidAnson/markdownlint/blob/main/helpers/README.md#applying-recommended-fixes
Author
Owner

@jcubic commented on GitHub (Aug 11, 2023):

But it only shows how to fix a single string. It doesn't show to fix the files. You need to figure that out on your own.
Another option is to show what is the return value of sync, which is also not documented.

<!-- gh-comment-id:1674333718 --> @jcubic commented on GitHub (Aug 11, 2023): But it only shows how to fix a single string. It doesn't show to fix the files. You need to figure that out on your own. Another option is to show what is the return value of sync, which is also not documented.
Author
Owner

@DavidAnson commented on GitHub (Aug 11, 2023):

Types for markdownlint's sync API are available here: github.com/DavidAnson/markdownlint@d641caf90c/lib/markdownlint.d.ts (L74)

And described here: https://github.com/DavidAnson/markdownlint#usage

<!-- gh-comment-id:1675030295 --> @DavidAnson commented on GitHub (Aug 11, 2023): Types for markdownlint's sync API are available here: https://github.com/DavidAnson/markdownlint/blob/d641caf90ca67660a1939e50f6cc8acb1939819f/lib/markdownlint.d.ts#L74 And described here: https://github.com/DavidAnson/markdownlint#usage
Author
Owner

@jcubic commented on GitHub (Aug 11, 2023):

Sorry, but I don't see documentation on how to call applyFixes with an output of markdownlint. As I said there are only docs how to call it with string.

<!-- gh-comment-id:1675085368 --> @jcubic commented on GitHub (Aug 11, 2023): Sorry, but I don't see documentation on how to call `applyFixes` with an output of `markdownlint`. As I said there are only docs how to call it with string.
Author
Owner

@DavidAnson commented on GitHub (Aug 11, 2023):

There is documentation to describe and demonstrate various ways of giving input into the tool as well as how to interpret the output, including fixing issues. There may not be an example that corresponds precisely to your scenario.

From what I can see above, what's missing is code to read and write each file. Those are standard Node APIs and examples exist in their documentation and elsewhere.

<!-- gh-comment-id:1675111185 --> @DavidAnson commented on GitHub (Aug 11, 2023): There is documentation to describe and demonstrate various ways of giving input into the tool as well as how to interpret the output, including fixing issues. There may not be an example that corresponds precisely to your scenario. From what I can see above, what's missing is code to read and write each file. Those are standard Node APIs and examples exist in their documentation and elsewhere.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/markdownlint#554
No description provided.