# All files / src/helpers string.helper.ts

100% Statements 63/63
100% Branches 17/17
100% Functions 4/4
100% Lines 63/63

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
    56
    57
    58
    59
    60
    61
    62
    63
    641x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    62x
    62x
    62x
    62x
    62x
    62x
    62x
    62x
    62x
    62x
    1x
    41x
    41x
    41x
    2x
    41x
    9x
    9x
    9x
    2x
    8x
    7x
    7x
    39x
    30x
    9x
    9x
    30x
    8x
    8x
    30x
    32x
    32x
    1x
    1x
    1x
    1x
    1x
    1x
    44x
    44x
    44x
    1x
    6x
    6x
    6x
    6x
    4x
    4x
    2x
    2x
    2x
     const chalkStringStyleRegExp = /(?<!\\){([^}]+?)[ \n\r](.+?[^\\])}/gms;
    const newLineRegExp = /\n/g;

    const highlightModifier = 'highlight';
    const codeModifier = 'code';

    /**
     * Converts a string with chalk formatting into a string safe for markdown
     *
     * @param input
     */
    export function convertChalkStringToMarkdown(input: string): string {
        return (
            input
                .replace(chalkStringStyleRegExp, replaceChalkFormatting)
                //replace new line with 2 spaces then new line
                .replace(newLineRegExp, '  \n')
                .replace(/\\{/g, '{')
                .replace(/\\}/g, '}')
        );
    }

    function replaceChalkFormatting(_substring: string, ...matches: string[]): string {
        let modifier = '';
        if (matches[0].indexOf(highlightModifier) >= 0) {
            modifier = '`';
        } else if (matches[0].indexOf(codeModifier) >= 0) {
            const codeOptions = matches[0].split('.');
            modifier = '\n```';
            if (codeOptions[1] != null) {
                return `${modifier}${codeOptions[1]}\n${matches[1]}${modifier}\n`;
            } else {
                return `${modifier}\n${matches[1]}${modifier}\n`;
            }
        } else {
            if (matches[0].indexOf('bold') >= 0) {
                modifier += '**';
            }
            if (matches[0].indexOf('italic') >= 0) {
                modifier += '*';
            }
        }
        return `${modifier}${matches[1]}${modifier}`;
    }

    /**
     * Removes formatting not supported by chalk
     *
     * @param input
     */
    export function removeAdditionalFormatting(input: string): string {
        return input.replace(chalkStringStyleRegExp, removeNonChalkFormatting);
    }

    function removeNonChalkFormatting(substring: string, ...matches: string[]): string {
        const nonChalkFormats = [highlightModifier, codeModifier];

        if (nonChalkFormats.some((format) => matches[0].indexOf(format) >= 0)) {
            return matches[1];
        }

        return substring;
    }