Skip to content

Commit 79c5332

Browse files
lint rule - term inside definition not allowed
1 parent adfac3b commit 79c5332

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const BASIC_TERM_REGEXP = '\\[([^\\[]+)\\](\\(\\*(\\w+)\\))';

src/transform/plugins/term/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Token from 'markdown-it/lib/token';
44
import {MarkdownItPluginCb} from '../typings';
55
import {generateID} from '../utils';
66
import {termDefinitions} from './termDefinitions';
7+
import {BASIC_TERM_REGEXP} from './constants';
78

89
const term: MarkdownItPluginCb = (md) => {
910
const escapeRE = md.utils.escapeRE;
@@ -68,7 +69,7 @@ const term: MarkdownItPluginCb = (md) => {
6869
nodes = [];
6970

7071
// Find terms without definitions
71-
const regexAllTerms = new RegExp('\\[([^\\[]+)\\](\\(\\*(\\w+)\\))', 'gm');
72+
const regexAllTerms = new RegExp(BASIC_TERM_REGEXP, 'gm');
7273
const uniqueTerms = [
7374
...new Set([...text.matchAll(regexAllTerms)].map((el) => `:${el[3]}`)),
7475
];

src/transform/plugins/term/termDefinitions.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import StateBlock from 'markdown-it/lib/rules_block/state_block';
22
import {MarkdownIt} from '../../typings';
3+
import {BASIC_TERM_REGEXP} from './constants';
34

45
export function termDefinitions(md: MarkdownIt) {
56
return (state: StateBlock, startLine: number, endLine: number, silent: boolean) => {
@@ -90,6 +91,16 @@ function processTermDefinition(
9091
state.env.terms = {};
9192
}
9293

94+
const basicTermDefinitionRegexp = new RegExp(BASIC_TERM_REGEXP, 'gm');
95+
// If term inside definition
96+
if (basicTermDefinitionRegexp.test(title)) {
97+
token = new state.Token('__yfm_lint', '', 0);
98+
token.hidden = true;
99+
token.map = [currentLine, endLine];
100+
token.attrSet('YFM008', 'true');
101+
state.tokens.push(token);
102+
}
103+
93104
// If term definition duplicated
94105
if (state.env.terms[':' + label]) {
95106
token = new state.Token('__yfm_lint', '', 0);

src/transform/yfmlint/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ import union from 'lodash/union';
44
import attrs from 'markdown-it-attrs';
55
import baseDefaultLintConfig from './yfmlint';
66

7-
import {yfm001, yfm002, yfm003, yfm004, yfm005, yfm006, yfm007} from './markdownlint-custom-rule';
7+
import {
8+
yfm001,
9+
yfm002,
10+
yfm003,
11+
yfm004,
12+
yfm005,
13+
yfm006,
14+
yfm007,
15+
yfm008,
16+
} from './markdownlint-custom-rule';
817

918
import {errorToString, getLogLevel} from './utils';
1019
import {Options} from './typings';
1120
import {Dictionary} from 'lodash';
1221
import {Logger, LogLevels} from '../log';
1322

14-
const defaultLintRules = [yfm001, yfm002, yfm003, yfm004, yfm005, yfm006, yfm007];
23+
const defaultLintRules = [yfm001, yfm002, yfm003, yfm004, yfm005, yfm006, yfm007, yfm008];
1524

1625
const lintCache = new Set();
1726

src/transform/yfmlint/markdownlint-custom-rule/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export {yfm004} from './yfm004';
55
export {yfm005} from './yfm005';
66
export {yfm006} from './yfm006';
77
export {yfm007} from './yfm007';
8+
export {yfm008} from './yfm008';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Rule} from 'markdownlint';
2+
3+
export const yfm008: Rule = {
4+
names: ['YFM008', 'term-inside-definition-not-allowed'],
5+
description: 'Term inside definition not allowed',
6+
tags: ['term'],
7+
function: function YFM008(params, onError) {
8+
const {config} = params;
9+
if (!config) {
10+
return;
11+
}
12+
params.tokens
13+
.filter((token) => {
14+
return token.type === '__yfm_lint';
15+
})
16+
.forEach((term) => {
17+
// @ts-expect-error bad markdownlint typings
18+
if (term.attrGet('YFM008')) {
19+
onError({
20+
lineNumber: term.lineNumber,
21+
context: term.line,
22+
});
23+
}
24+
});
25+
},
26+
};

src/transform/yfmlint/yfmlint.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const index: LintConfig = {
6060
YFM005: LogLevels.ERROR, // Tab list not closed
6161
YFM006: LogLevels.WARN, // Term definition duplicated
6262
YFM007: LogLevels.WARN, // Term used without definition
63+
YFM008: LogLevels.WARN, // Term inside definition not allowed
6364
},
6465

6566
// Inline code length

0 commit comments

Comments
 (0)