All files / validation/rules heading-rules.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 4/4
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 562x                   108x   108x 60x 60x 60x   60x 18x               108x     2x       27x       2x       27x             2x       27x      
import {
    WIKI_BOLD_REGEX,
    WIKI_FOOTNOTE_REGEX,
    WIKI_HEADING_REGEX,
    WIKI_ITALIC_REGEX,
    WIKI_LINK_REGEX,
} from '../constants/wiki-patterns';
import { RuleMatch, ValidationRule } from '../models/validation-rule.model';
 
function findInHeadings(text: string, pattern: RegExp, message: string): readonly RuleMatch[] {
    const matches: RuleMatch[] = [];
 
    for (const headingMatch of text.matchAll(WIKI_HEADING_REGEX)) {
        const content = headingMatch[1] ?? headingMatch[2];
        const markerLen = headingMatch[1] ? 3 : 2;
        const contentStart = headingMatch.index + markerLen;
 
        for (const innerMatch of content.matchAll(pattern)) {
            matches.push({
                from: contentStart + innerMatch.index,
                to: contentStart + innerMatch.index + innerMatch[0].length,
                message,
            });
        }
    }
 
    return matches;
}
 
export const headingNoLinks: ValidationRule = {
    id: 'heading-no-links',
    defaultSeverity: 'warning',
    validate(text: string): readonly RuleMatch[] {
        return findInHeadings(text, WIKI_LINK_REGEX, 'Ссылки запрещены в заголовках');
    },
};
 
export const headingNoFormatting: ValidationRule = {
    id: 'heading-no-formatting',
    defaultSeverity: 'warning',
    validate(text: string): readonly RuleMatch[] {
        return [
            ...findInHeadings(text, WIKI_BOLD_REGEX, 'Жирный текст запрещён в заголовках'),
            ...findInHeadings(text, WIKI_ITALIC_REGEX, 'Курсив запрещён в заголовках'),
        ];
    },
};
 
export const headingNoFootnotes: ValidationRule = {
    id: 'heading-no-footnotes',
    defaultSeverity: 'warning',
    validate(text: string): readonly RuleMatch[] {
        return findInHeadings(text, WIKI_FOOTNOTE_REGEX, 'Сноски запрещены в заголовках');
    },
};