@alauda/doom
Version:
Doctor Doom making docs.
39 lines (38 loc) • 1.57 kB
JavaScript
import path from 'node:path';
import { lintRule } from 'unified-lint-rule';
import { visitParents } from 'unist-util-visit-parents';
import { SITES_FILE } from "../cli/constants.js";
import { pathExists } from "../utils/fs.js";
import { resolveStaticConfig } from "../utils/helpers.js";
const SITE_ELEMENTS = new Set([
'ExternalApisOverview',
'ExternalSite',
'ExternalSiteLink',
]);
const sitesConfigFilePath = path.resolve(SITES_FILE);
let sites;
export const site = lintRule('doom-lint:site', async (root, vfile) => {
if (!sites) {
if (await pathExists(sitesConfigFilePath, 'file')) {
sites = await resolveStaticConfig(sitesConfigFilePath);
}
else {
sites = [];
}
}
visitParents(root, ['mdxJsxFlowElement', 'mdxJsxTextElement'], (element, parents) => {
if (!sites?.length || !element.name || !SITE_ELEMENTS.has(element.name)) {
return;
}
const nameAttr = element.attributes.find((attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'name');
const nameVal = nameAttr?.value;
if (typeof nameVal === 'string' &&
sites.some((site) => site.name === nameVal)) {
return;
}
vfile.message(`Invalid site \`name\` property value \`${typeof nameVal === 'string' ? nameVal : nameVal?.value}\` which should be static string matching a site name from \`${SITES_FILE}\` config.`, {
ancestors: [...parents, element],
place: (nameAttr ?? element).position,
});
});
});