File size: 2,007 Bytes
13555f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {InlineStrategy} from '../pluginStrategy'
import findRangesWithRegex from '../utils/findRangesWithRegex'

// Bold can be delimited by: **, __, ***, and ___
const createBoldStyleStrategy = (): InlineStrategy => {
    const asteriskDelimitedRegex =
		'(\\*\\*\\*)(.+?)(\\*\\*\\*)|(\\*\\*)(.+?)(\\*\\*)(?!\\*)'
    const underscoreDelimitedRegex = '(___)(.+?)(___)|(__)(.+?)(__)(?!_)'
    const boldRegex = new RegExp(
        `${asteriskDelimitedRegex}|${underscoreDelimitedRegex}`,
        'g',
    )
    const boldDelimiterRegex = /^(\*\*\*|\*\*|___|__)|(\*\*\*|\*\*|___|__)$/g

    return {
        style: 'BOLD',
        delimiterStyle: 'BOLD-DELIMITER',
        findStyleRanges: (block) => {
            // Return an array of arrays containing start and end indices for ranges of
            // text that should be bolded
            // e.g. [[0,6], [10,20]]
            const text = block.getText()
            const boldRanges = findRangesWithRegex(text, boldRegex)
            return boldRanges
        },
        findDelimiterRanges: (block, styleRanges) => {
            // Find ranges for delimiters at the beginning/end of styled text ranges
            // Returns an array of arrays containing start and end indices for delimiters
            const text = block.getText()
            let boldDelimiterRanges: number[][] = []
            styleRanges.forEach((styleRange) => {
                const delimiterRange = findRangesWithRegex(
                    text.substring(styleRange[0], styleRange[1] + 1),
                    boldDelimiterRegex,
                ).map((indices) => indices.map((x) => x + styleRange[0]))
                boldDelimiterRanges = boldDelimiterRanges.concat(delimiterRange)
            })
            return boldDelimiterRanges
        },
        delimiterStyles: {
            opacity: 0.4,
        },
    }
}

export default createBoldStyleStrategy