-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from lumirlumir/feature-textlint-rule-allowed-…
…uris feat: textlint-rule-allowed-uris.js
- Loading branch information
Showing
11 changed files
with
674 additions
and
177 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,72 @@ | ||
const c = require('ansi-colors'); | ||
const getUriList = require('./utils/getUriList'); | ||
|
||
/** | ||
* The main reporter function that processes the node and reports any issues based on the specified options. | ||
* | ||
* @async | ||
* @param {Object} context The context object containing the `report`, `locator`, and `RuleError`. | ||
* @param {function} context.report Function to report errors or issues. | ||
* @param {function} context.locator Function to locate the position of the node in the source. | ||
* @param {function} context.RuleError Constructor to create a new rule error. | ||
* @param {Object} options Configuration options containing `allowed` and `disallowed` URI patterns. | ||
* @param {Object} [options.allowed] Object specifying `allowed` URI patterns. | ||
* @param {RegExp[]} [options.allowed.links] Array of regular expressions for allowed links URIs. | ||
* @param {RegExp[]} [options.allowed.images] Array of regular expressions for allowed images URIs. | ||
* @param {Object} [options.disallowed] Object specifying `disallowed` URI patterns. | ||
* @param {RegExp[]} [options.disallowed.links] Array of regular expressions for disallowed links URIs. | ||
* @param {RegExp[]} [options.disallowed.images] Array of regular expressions for disallowed images URIs. | ||
* @param {Object} node The node to be processed, containing potential URIs. | ||
* @returns {Promise<void>} A `Promise` that resolves when the reporting is completed. | ||
*/ | ||
const reporter = async ({ report, locator, RuleError }, options, node) => { | ||
/* Initialize options */ | ||
const regexes = { | ||
allowed: { | ||
links: options?.allowed?.links ?? [/.*/], | ||
images: options?.allowed?.images ?? [/.*/], | ||
}, | ||
disallowed: { | ||
links: options?.disallowed?.links ?? [], | ||
images: options?.disallowed?.images ?? [], | ||
}, | ||
}; | ||
|
||
/* Report process */ | ||
(await getUriList(node)).uriList.forEach(({ uri, type }) => { | ||
Object.keys(regexes).forEach(key => { | ||
if ( | ||
key === 'allowed' | ||
? !regexes[key][`${type}s`].some(regex => regex.test(uri)) | ||
: regexes[key][`${type}s`].some(regex => regex.test(uri)) | ||
) | ||
report( | ||
node, | ||
new RuleError( | ||
`${c.red.bold(`${key}.${type}s`)}\n${c.bold.red('-')} problem: '${c.strikethrough.bold.white(uri)}'\n${c.bold.red('-')} ${key} regular expressions: '${c.bold.white(regexes[key][`${type}s`].join(' or '))}'`, | ||
{ | ||
padding: locator.at(0), | ||
}, | ||
), | ||
); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* The module export function that returns an object mapping node types to the `reporter` function. | ||
* | ||
* @param {Object} context The context object provided to the `reporter`. | ||
* @param {Object} options Configuration options for the `reporter`. | ||
* @returns {Object} An object with node type keys mapping to the `reporter` function. | ||
*/ | ||
module.exports = (context, options) => { | ||
// TODO: Error When it is offline. | ||
|
||
return Object.fromEntries( | ||
['Link', 'Image', 'Definition', 'Html'].map(type => [ | ||
type, | ||
async node => reporter(context, options, node), | ||
]), | ||
); | ||
}; |
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
Oops, something went wrong.