eslint-plugin-mdx
Version:
ESLint Plugin for MDX
54 lines • 1.91 kB
JavaScript
import { ENABLED_OPTION_SCHEMA, getParserOptions, getRuleConfig, isMdxFile, } from './helpers.js';
const defaultHttp = 'http://';
const defaultMailto = 'mailto:';
export const noLiteralUrls = {
meta: {
type: 'problem',
docs: {
description: 'Disallow literal URLs',
},
schema: [ENABLED_OPTION_SCHEMA],
},
create(context) {
const config = getRuleConfig(context, 'no-literal-urls');
if (!config) {
return {};
}
const isMdx = isMdxFile(context.filename, getParserOptions(context).extensions);
return {
MDLink(node) {
const raw = node.raw;
if (raw.startsWith('[')) {
return;
}
let protocol;
switch (true) {
case raw === node.url: {
break;
}
case defaultHttp + raw === node.url: {
protocol = defaultHttp;
break;
}
case defaultMailto + raw === node.url: {
protocol = defaultMailto;
break;
}
default: {
return;
}
}
const expectedKind = isMdx ? 'regular link' : 'regular autolink';
const marker = protocol ?? '';
const suggestion = isMdx
? 'use `[text](url)` instead'
: `add \`<${marker}\` before and \`>\` after`;
context.report({
loc: node.loc,
message: `Unexpected GFM autolink literal, expected ${expectedKind}, ${suggestion}`,
});
},
};
},
};
//# sourceMappingURL=no-literal-urls.js.map