main

mattermost/focalboard

Last updated at: 29/12/2023 09:48

headingDelimiterStyleStrategy.ts

TLDR

This file contains a function to create a heading delimiter style strategy for a Markdown plugin.

Methods

createHeadingDelimiterStyleStrategy

This method creates a heading delimiter style strategy for a Markdown plugin. It returns an InlineStrategy object that includes a style, a findStyleRanges method, and a styles object. The findStyleRanges method searches for heading delimiter ranges within a text block and returns an array of those ranges.

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {InlineStrategy} from '../pluginStrategy'
import findRangesWithRegex from '../utils/findRangesWithRegex'

const createHeadingDelimiterStyleStrategy = (): InlineStrategy => {
    const headingDelimiterRegex = /(^#{1,6})\s/g

    return {
        style: 'HEADING-DELIMITER',
        findStyleRanges: (block) => {
            // Skip the text search if the block isn't a header block
            if (block.getType().indexOf('header') < 0) {
                return []
            }

            const text = block.getText()
            const headingDelimiterRanges = findRangesWithRegex(
                text,
                headingDelimiterRegex,
            )
            return headingDelimiterRanges
        },
        styles: {
            opacity: 0.4,
        },
    }
}

export default createHeadingDelimiterStyleStrategy