UNPKG

eslint-plugin-mdx

Version:
54 lines • 1.82 kB
import { ANY_OPTION_SCHEMA, getNodeText, getRuleConfig } from './helpers.js'; export const maximumHeadingLength = { meta: { type: 'problem', docs: { description: 'Enforce a maximum heading length', }, schema: [ { anyOf: [ { type: 'number' }, { additionalProperties: false, properties: { size: { type: ['number', 'null'] }, stringLength: ANY_OPTION_SCHEMA, }, type: 'object', }, ], }, ], }, create(context) { const config = getRuleConfig(context, 'maximum-heading-length'); if (!config) { return {}; } const size = typeof config.options === 'object' ? config.options.size : config.options; const expected = typeof size === 'number' ? size : 60; const report = (node) => { const actual = [...getNodeText(node)].length; if (actual > expected) { context.report({ loc: node.loc, message: `Unexpected \`${actual}\` characters in heading, expected at most \`${expected}\` characters`, }); } }; return { MDHeading(node) { report(node); }, JSXElement(node) { const name = node.openingElement.name; if (name.type !== 'JSXIdentifier' || !/^h[1-6]$/u.test(name.name)) { return; } report(node); }, }; }, }; //# sourceMappingURL=maximum-heading-length.js.map