2019-04-10 21:26:59 -07:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { getReferenceLinkImageData as helpersGetReferenceLinkImageData } from "../helpers/helpers.cjs";
|
|
|
|
import { filterByTypes } from "../helpers/micromark-helpers.cjs";
|
2024-08-24 22:05:16 -07:00
|
|
|
|
2025-04-20 04:57:35 +00:00
|
|
|
/** @typedef {import("markdownlint").RuleParams} RuleParams */
|
|
|
|
/** @typedef {import("markdownlint").MicromarkToken} MicromarkToken */
|
|
|
|
/** @typedef {import("markdownlint").MicromarkTokenType} MicromarkTokenType */
|
|
|
|
|
2024-08-24 22:05:16 -07:00
|
|
|
/** @type {Map<string, object>} */
|
2022-06-02 21:33:31 -07:00
|
|
|
const map = new Map();
|
2025-04-20 04:57:35 +00:00
|
|
|
/** @type {RuleParams | undefined} */
|
2024-08-24 22:05:16 -07:00
|
|
|
let params = undefined;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes (resets) the cache.
|
|
|
|
*
|
2025-04-20 04:57:35 +00:00
|
|
|
* @param {RuleParams} [p] Rule parameters object.
|
2024-08-24 22:05:16 -07:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2024-11-28 20:36:44 -08:00
|
|
|
export function initialize(p) {
|
2024-08-24 22:05:16 -07:00
|
|
|
map.clear();
|
|
|
|
params = p;
|
|
|
|
}
|
2021-06-17 22:01:27 -07:00
|
|
|
|
2025-04-20 04:57:35 +00:00
|
|
|
/**
|
|
|
|
* Gets the cached Micromark token array (for testing).
|
|
|
|
*
|
|
|
|
* @returns {MicromarkToken[]} Micromark tokens.
|
|
|
|
*/
|
|
|
|
export function micromarkTokens() {
|
|
|
|
return params?.parsers.micromark.tokens || [];
|
|
|
|
}
|
|
|
|
|
2024-08-24 22:05:16 -07:00
|
|
|
/**
|
|
|
|
* Gets a cached object value - computes it and caches it.
|
|
|
|
*
|
|
|
|
* @param {string} name Cache object name.
|
|
|
|
* @param {Function} getValue Getter for object value.
|
|
|
|
* @returns {Object} Object value.
|
|
|
|
*/
|
|
|
|
function getCached(name, getValue) {
|
|
|
|
if (map.has(name)) {
|
|
|
|
return map.get(name);
|
2022-06-01 20:23:08 -07:00
|
|
|
}
|
2024-08-24 22:05:16 -07:00
|
|
|
const value = getValue();
|
|
|
|
map.set(name, value);
|
|
|
|
return value;
|
|
|
|
}
|
2022-06-01 20:23:08 -07:00
|
|
|
|
2024-08-24 22:05:16 -07:00
|
|
|
/**
|
|
|
|
* Filters a list of Micromark tokens by type and caches the result.
|
|
|
|
*
|
2025-04-20 04:57:35 +00:00
|
|
|
* @param {MicromarkTokenType[]} types Types to allow.
|
2024-08-24 22:05:16 -07:00
|
|
|
* @param {boolean} [htmlFlow] Whether to include htmlFlow content.
|
2025-04-20 04:57:35 +00:00
|
|
|
* @returns {MicromarkToken[]} Filtered tokens.
|
2024-08-24 22:05:16 -07:00
|
|
|
*/
|
2024-11-28 20:36:44 -08:00
|
|
|
export function filterByTypesCached(types, htmlFlow) {
|
2024-08-24 22:05:16 -07:00
|
|
|
return getCached(
|
2024-10-09 22:42:36 -07:00
|
|
|
// eslint-disable-next-line prefer-rest-params
|
|
|
|
JSON.stringify(arguments),
|
2025-04-20 04:57:35 +00:00
|
|
|
() => filterByTypes(micromarkTokens(), types, htmlFlow)
|
2024-08-24 22:05:16 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a reference link and image data object.
|
|
|
|
*
|
|
|
|
* @returns {Object} Reference link and image data object.
|
|
|
|
*/
|
2024-11-28 20:36:44 -08:00
|
|
|
export function getReferenceLinkImageData() {
|
2024-08-24 22:05:16 -07:00
|
|
|
return getCached(
|
|
|
|
getReferenceLinkImageData.name,
|
2025-04-20 04:57:35 +00:00
|
|
|
() => helpersGetReferenceLinkImageData(micromarkTokens())
|
2024-08-24 22:05:16 -07:00
|
|
|
);
|
|
|
|
}
|