@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
64 lines (60 loc) • 1.83 kB
JavaScript
function getAppliedEnhancers(root) {
const data = root.data;
return data?.appliedEnhancers ?? [];
}
/**
* Records on the HAST root that an enhancer with this name has been applied,
* so subsequent passes can skip it. No-op when the enhancer has no
* `enhancerName`.
*/
export function recordEnhancerApplied(root, enhancer) {
const name = enhancer.enhancerName;
if (!name) {
return;
}
const data = root.data ?? {};
const existing = data.appliedEnhancers;
if (existing && existing.includes(name)) {
return;
}
root.data = {
...data,
appliedEnhancers: existing ? [...existing, name] : [name]
};
}
/**
* Returns true if the enhancer has a stable name that already appears in
* `root.data.appliedEnhancers`. Anonymous enhancers (no `enhancerName`) are
* never skipped.
*/
export function shouldSkipEnhancer(root, enhancer) {
const name = enhancer.enhancerName;
if (!name) {
return false;
}
return getAppliedEnhancers(root).includes(name);
}
/**
* Runs a single enhancer with the skip/record bookkeeping. Returns the
* (possibly unchanged) root; awaits the enhancer when it returns a promise.
*/
export async function applyEnhancer(root, comments, fileName, enhancer) {
if (shouldSkipEnhancer(root, enhancer)) {
return root;
}
const result = await enhancer(root, comments, fileName);
recordEnhancerApplied(result, enhancer);
return result;
}
/**
* Runs the enhancer pipeline sequentially, skipping any enhancer whose
* `enhancerName` is already recorded on the HAST root.
*/
export async function applyEnhancers(root, comments, fileName, enhancers) {
let current = root;
for (const enhancer of enhancers) {
// eslint-disable-next-line no-await-in-loop
current = await applyEnhancer(current, comments, fileName, enhancer);
}
return current;
}