main

mattermost/focalboard

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

inlineCodeStyleStrategy.ts

TLDR

This file contains a function createInlineCodeStyleStrategy that returns an inline code style strategy object. The strategy defines how to find and style inline code within a block of text.

Methods

createInlineCodeStyleStrategy

This method returns an object that represents an inline code style strategy. The strategy is used to find and style inline code within a block of text. The strategy object has the following properties:

  • style: This property specifies the style type as 'INLINE-CODE'.
  • findStyleRanges(block): This method is used to find the ranges in the block of text where the inline code is located. It returns an array of ranges.
  • styles: This property contains the styling properties for the inline code, such as the fontFamily property set to 'monospace'.

END

// 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 createInlineCodeStyleStrategy = (): InlineStrategy => {
    const codeRegex = /(`)([^\n\r`]+?)(`)/g

    return {
        style: 'INLINE-CODE',
        findStyleRanges: (block) => {
            // Don't allow inline code inside of code blocks
            if (block.getType() === 'code-block') {
                return []
            }

            const text = block.getText()
            const codeRanges = findRangesWithRegex(text, codeRegex)
            return codeRanges
        },
        styles: {
            fontFamily: 'monospace',
        },
    }
}

export default createInlineCodeStyleStrategy