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
|
|
|
|
|
|
|
|
/** @type {Map<string, object>} */
|
2022-06-02 21:33:31 -07:00
|
|
|
const map = new Map();
|
2024-08-24 22:05:16 -07:00
|
|
|
let params = undefined;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes (resets) the cache.
|
|
|
|
|
*
|
2024-11-28 20:36:44 -08:00
|
|
|
* @param {import("./markdownlint.mjs").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
|
|
|
|
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.
|
|
|
|
|
*
|
2024-11-28 20:36:44 -08:00
|
|
|
* @param {import("./markdownlint.mjs").MicromarkTokenType[]} types Types to allow.
|
2024-08-24 22:05:16 -07:00
|
|
|
* @param {boolean} [htmlFlow] Whether to include htmlFlow content.
|
2024-11-28 20:36:44 -08:00
|
|
|
* @returns {import("./markdownlint.mjs").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),
|
2024-08-24 22:05:16 -07:00
|
|
|
() => filterByTypes(params.parsers.micromark.tokens, types, htmlFlow)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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,
|
2024-11-28 20:36:44 -08:00
|
|
|
() => helpersGetReferenceLinkImageData(params.parsers.micromark.tokens)
|
2024-08-24 22:05:16 -07:00
|
|
|
);
|
|
|
|
|
}
|