Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 23, 2024
1 parent 1587927 commit c767af1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 52 deletions.
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,28 +567,19 @@ This setting can be useful in the presence of (custom) rules that encounter
unexpected syntax and fail. By enabling this option, the linting process
is allowed to continue and report any violations that were found.

##### options.markdownItPlugins
##### options.markdownItFactory

Type: `Array` of `Array` of `Function` and plugin parameters
Type: `Function` returning ...

Specifies additional [`markdown-it` plugins][markdown-it-plugin] to use when
parsing input. Plugins can be used to support additional syntax and features for
advanced scenarios. *Deprecated.*
Specifies ... [`markdown-it` plugins][markdown-it-plugin]

[markdown-it-plugin]: https://www.npmjs.com/search?q=keywords:markdown-it-plugin

Each item in the top-level `Array` should be of the form:

```javascript
[ require("markdown-it-plugin"), plugin_param_0, plugin_param_1, ... ]
```

> Note that `markdown-it` plugins are only called when the `markdown-it` parser
> Note that `markdown` is only used when ... the `markdown-it` parser
> is invoked. None of the built-in rules use the `markdown-it` parser, so
> `markdown-it` plugins will only be invoked when one or more
> [custom rules][custom-rules] that use the `markdown-it` parser are present.
[custom-rules]: #custom-rules
[markdown-it-plugin]: https://www.npmjs.com/search?q=keywords:markdown-it-plugin

##### options.noInlineConfig

Expand Down
2 changes: 1 addition & 1 deletion example/typescript/type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ options = {
"frontMatter": /---/,
"handleRuleFailures": false,
"noInlineConfig": false,
"markdownItPlugins": [ [ markdownItSub ] ]
"markdownItFactory": null
};

assertLintResults(lintSync(options));
Expand Down
14 changes: 4 additions & 10 deletions lib/markdownit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,15 @@ function annotateAndFreezeTokens(tokens, lines) {
/**
* Gets an array of markdown-it tokens for the input.
*
* @param {Plugin[]} markdownItPlugins Additional plugins.
* @param {Function} markdownItFactory ...
* @param {string} content Markdown content.
* @param {string[]} lines Lines of Markdown content.
* @returns {MarkdownItToken} Array of markdown-it tokens.
*/
function getMarkdownItTokens(markdownItPlugins, content, lines) {
const markdownit = require("markdown-it");
const md = markdownit({ "html": true });
for (const plugin of markdownItPlugins) {
// @ts-ignore
md.use(...plugin);
}
const tokens = md.parse(content, {});
function getMarkdownItTokens(markdownItFactory, content, lines) {
const markdownIt = markdownItFactory();
const tokens = markdownIt.parse(content, {});
annotateAndFreezeTokens(tokens, lines);
// @ts-ignore
return tokens;
};

Expand Down
8 changes: 6 additions & 2 deletions lib/markdownlint.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ export type Rule = {
*/
function: RuleFunction;
};
/**
* ...
*/
export type MarkdownItFactory = () => any;
/**
* Configuration options.
*/
Expand Down Expand Up @@ -390,9 +394,9 @@ export type Options = {
*/
handleRuleFailures?: boolean;
/**
* Additional plugins.
* ...
*/
markdownItPlugins?: Plugin[];
markdownItFactory?: MarkdownItFactory;
/**
* True to ignore HTML directives.
*/
Expand Down
30 changes: 18 additions & 12 deletions lib/markdownlint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ function getEnabledRulesPerLineNumber(
* names.
* @param {string} name Identifier for the content.
* @param {string} content Markdown content.
* @param {Plugin[]} markdownItPlugins Additional plugins.
* @param {MarkdownItFactory} markdownItFactory ...
* @param {Configuration} config Configuration object.
* @param {ConfigurationParser[] | null} configParsers Configuration parsers.
* @param {RegExp | null} frontMatter Regular expression for front matter.
Expand All @@ -461,7 +461,7 @@ function lintContent(
aliasToRuleNames,
name,
content,
markdownItPlugins,
markdownItFactory,
config,
configParsers,
frontMatter,
Expand Down Expand Up @@ -501,7 +501,7 @@ function lintContent(
// Parse content into lines and get markdown-it tokens
const lines = content.split(helpers.newLineRe);
const markdownitTokens = needMarkdownItTokens ?
dynamicRequire("./markdownit.cjs").getMarkdownItTokens(markdownItPlugins, preClearedContent, lines) :
dynamicRequire("./markdownit.cjs").getMarkdownItTokens(markdownItFactory, preClearedContent, lines) :
[];
// Create (frozen) parameters for rules
/** @type {MarkdownParsers} */
Expand Down Expand Up @@ -753,10 +753,9 @@ function lintContent(
* Lints a file containing Markdown content.
*
* @param {Rule[]} ruleList List of rules.
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule
* names.
* @param {Object.<string, string[]>} aliasToRuleNames Map of alias to rule names.
* @param {string} file Path of file to lint.
* @param {Plugin[]} markdownItPlugins Additional plugins.
* @param {MarkdownItFactory} markdownItFactory ...
* @param {Configuration} config Configuration object.
* @param {ConfigurationParser[] | null} configParsers Configuration parsers.
* @param {RegExp | null} frontMatter Regular expression for front matter.
Expand All @@ -772,7 +771,7 @@ function lintFile(
ruleList,
aliasToRuleNames,
file,
markdownItPlugins,
markdownItFactory,
config,
configParsers,
frontMatter,
Expand All @@ -792,7 +791,7 @@ function lintFile(
aliasToRuleNames,
file,
content,
markdownItPlugins,
markdownItFactory,
config,
configParsers,
frontMatter,
Expand Down Expand Up @@ -859,7 +858,7 @@ function lintInput(options, synchronous, callback) {
const resultVersion = (options.resultVersion === undefined) ?
3 :
options.resultVersion;
const markdownItPlugins = options.markdownItPlugins || [];
const markdownItFactory = options.markdownItFactory || (() => { throw new Error("BAD"); });

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 23)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 23)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 23)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

This line has 2 statements. Maximum allowed is 1

Check failure on line 861 in lib/markdownlint.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

This line has 2 statements. Maximum allowed is 1
const fs = options.fs || nodeFs;
const aliasToRuleNames = mapAliasToRuleNames(ruleList);
const results = newResults(ruleList);
Expand Down Expand Up @@ -891,7 +890,7 @@ function lintInput(options, synchronous, callback) {
ruleList,
aliasToRuleNames,
currentItem,
markdownItPlugins,
markdownItFactory,
config,
configParsers,
frontMatter,
Expand All @@ -910,7 +909,7 @@ function lintInput(options, synchronous, callback) {
aliasToRuleNames,
currentItem,
strings[currentItem] || "",
markdownItPlugins,
markdownItFactory,
config,
configParsers,
frontMatter,
Expand Down Expand Up @@ -1475,6 +1474,13 @@ export function getVersion() {
* @property {RuleFunction} function Rule implementation.
*/

/**
* ...
*
* @callback MarkdownItFactory
* @returns {import("markdown-it").markdownit}
*/

/**
* Configuration options.
*
Expand All @@ -1486,7 +1492,7 @@ export function getVersion() {
* @property {RegExp | null} [frontMatter] Front matter pattern.
* @property {Object} [fs] File system implementation.
* @property {boolean} [handleRuleFailures] True to catch exceptions.
* @property {Plugin[]} [markdownItPlugins] Additional plugins.
* @property {MarkdownItFactory} [markdownItFactory] ...
* @property {boolean} [noInlineConfig] True to ignore HTML directives.
* @property {number} [resultVersion] Results object version.
* @property {Object.<string, string>} [strings] Strings to lint.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"node": ">=18"
},
"dependencies": {
"markdown-it": "14.1.0",
"micromark": "4.0.1",
"micromark-extension-directive": "3.0.2",
"micromark-extension-gfm-autolink-literal": "2.1.0",
Expand All @@ -97,6 +96,7 @@
"js-yaml": "4.1.0",
"json-schema-to-typescript": "15.0.3",
"jsonc-parser": "3.3.1",
"markdown-it": "14.1.0",
"markdown-it-for-inline": "2.0.1",
"markdown-it-sub": "2.0.0",
"markdown-it-sup": "2.0.0",
Expand Down
10 changes: 8 additions & 2 deletions test/markdownlint-test-custom-rules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "node:fs/promises";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
import test from "ava";
import markdownIt from "markdown-it";
import { lint as lintAsync } from "markdownlint/async";
import { lint as lintPromise } from "markdownlint/promise";
import { lint as lintSync } from "markdownlint/sync";
Expand Down Expand Up @@ -376,6 +377,7 @@ test("customRulesNpmPackage", (t) => new Promise((resolve) => {
require("./rules/npm"),
require("markdownlint-rule-extended-ascii")
],
"markdownItFactory": () => markdownIt({ "html": true }),
"strings": {
"string": "# Text\n\n---\n\nText ✅\n"
},
Expand Down Expand Up @@ -557,7 +559,9 @@ test("customRulesParserUndefined", (t) => {
],
"strings": {
"string": "# Heading\n"
}
},
"markdownItFactory": () => markdownIt({ "html": true })

};
return lintPromise(options).then(() => null);
});
Expand Down Expand Up @@ -608,7 +612,8 @@ test("customRulesParserMarkdownIt", (t) => {
],
"strings": {
"string": "# Heading\n"
}
},
"markdownItFactory": () => markdownIt({ "html": true })
};
return lintPromise(options).then(() => null);
});
Expand Down Expand Up @@ -657,6 +662,7 @@ test("customRulesMarkdownItParamsTokensSameObject", (t) => {
}
}
],
"markdownItFactory": () => markdownIt({ "html": true }),
"strings": {
"string": "# Heading\n"
}
Expand Down
31 changes: 21 additions & 10 deletions test/markdownlint-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ test("readmeHeadings", (t) => new Promise((resolve) => {
"##### options.frontMatter",
"##### options.fs",
"##### options.handleRuleFailures",
"##### options.markdownItPlugins",
"##### options.markdownItFactory",
"##### options.noInlineConfig",
"##### options.resultVersion",
"##### options.strings",
Expand Down Expand Up @@ -1104,6 +1104,16 @@ test("someCustomRulesHaveValidUrl", (t) => {
}
});

function getMarkdownItFactory(markdownItPlugins) {

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 23)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 23)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 23)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Missing JSDoc comment

Check warning on line 1107 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Missing JSDoc comment
return () => {
const md = markdownIt({ "html": true });
for (const markdownItPlugin of markdownItPlugins) {
// @ts-ignore
md.use(...markdownItPlugin);
}
return md;
}

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 23)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 23)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 23)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Missing semicolon

Check failure on line 1115 in test/markdownlint-test.mjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Missing semicolon
}
test("markdownItPluginsSingle", (t) => new Promise((resolve) => {
t.plan(4);
lintAsync({
Expand All @@ -1112,9 +1122,9 @@ test("markdownItPluginsSingle", (t) => new Promise((resolve) => {
},
// Use a markdown-it custom rule so the markdown-it plugin will be run
"customRules": customRules.anyBlockquote,
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginInline, "check_text_plugin", "text", () => t.true(true) ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
Expand All @@ -1131,12 +1141,12 @@ test("markdownItPluginsMultiple", (t) => new Promise((resolve) => {
},
// Use a markdown-it custom rule so the markdown-it plugin will be run
"customRules": customRules.anyBlockquote,
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginSub ],
[ pluginSup ],
[ pluginInline, "check_sub_plugin", "sub_open", () => t.true(true) ],
[ pluginInline, "check_sup_plugin", "sup_open", () => t.true(true) ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
Expand All @@ -1151,9 +1161,9 @@ test("markdownItPluginsNoMarkdownIt", (t) => new Promise((resolve) => {
"strings": {
"string": "# Heading\n\nText\n"
},
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginInline, "check_text_plugin", "text", () => t.fail() ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
Expand All @@ -1173,9 +1183,9 @@ test("markdownItPluginsUnusedUncalled", (t) => new Promise((resolve) => {
},
// Use a markdown-it custom rule so the markdown-it plugin will be run
"customRules": customRules.anyBlockquote,
"markdownItPlugins": [
"markdownItFactory": getMarkdownItFactory([
[ pluginInline, "check_text_plugin", "text", () => t.fail() ]
]
])
}, function callback(err, actual) {
t.falsy(err);
const expected = { "string": [] };
Expand Down Expand Up @@ -1242,7 +1252,8 @@ test("token-map-spans", (t) => {
}
}
],
"files": [ "./test/token-map-spans.md" ]
"files": [ "./test/token-map-spans.md" ],
"markdownItFactory": () => markdownIt({ "html": true })
};
lintSync(options);
});
Expand Down

0 comments on commit c767af1

Please sign in to comment.