Skip to content

Commit c969d6b

Browse files
Merge pull request #5085 from oriondesign2015/main
Escape special characters in Monaco tokens and fix black screen in application
2 parents 89be81f + e5543fc commit c969d6b

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

redisinsight/ui/src/utils/monaco/monacoRedisMonarchTokensProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import { IRedisCommand } from 'uiSrc/constants'
55

66
const STRING_DOUBLE = 'string.double'
77

8+
const escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
9+
810
export const getRedisMonarchTokensProvider = (
911
commands: IRedisCommand[],
1012
): monacoEditor.languages.IMonarchLanguage => {
1113
const commandRedisCommands = [...commands]
1214
const searchCommands = remove(commandRedisCommands, ({ token }) =>
1315
token?.startsWith(ModuleCommandPrefix.RediSearch),
1416
)
15-
const COMMON_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${commandRedisCommands.map(({ token }) => token).join('|')})\\b`
16-
const SEARCH_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${searchCommands.map(({ token }) => token).join('|')})\\b`
17+
const COMMON_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${commandRedisCommands.map(({ token }) => escapeRegex(token || '')).join('|')})\\b`
18+
const SEARCH_COMMANDS_REGEX = `^\\s*(\\d+\\s+)?(${searchCommands.map(({ token }) => escapeRegex(token || '')).join('|')})\\b`
1719

1820
return {
1921
defaultToken: '',

redisinsight/ui/src/utils/monaco/monarchTokens/redisearchTokensTemplates.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { curryRight } from 'lodash'
33
import { Maybe } from 'uiSrc/utils'
44
import { IRedisCommand } from 'uiSrc/constants'
55

6+
// Escape special regex characters in tokens
7+
const escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
8+
69
const appendToken = (token: string, name: Maybe<string>) =>
710
name ? `${token}.${name}` : token
811
export const generateQuery = (
@@ -17,7 +20,7 @@ export const generateQuery = (
1720
): languages.IMonarchLanguageRule =>
1821
args?.length
1922
? [
20-
`(${args?.map(({ token }) => token).join('|')})\\b`,
23+
`(${args?.map(({ token }) => escapeRegex(token || '')).join('|')})\\b`,
2124
{ token: 'function', next: appendTokenName(tokenName) },
2225
]
2326
: [/_/, '']

redisinsight/ui/src/utils/monaco/redisearch/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { generateQuery } from 'uiSrc/utils/monaco/monarchTokens/redisearchTokens
55
import { ICommandTokenType, IRedisCommand } from 'uiSrc/constants'
66
import { DefinedArgumentName } from 'uiSrc/pages/workbench/constants'
77

8+
const escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
9+
810
export const generateKeywords = (commands: IRedisCommand[]) =>
911
commands.map(({ name }) => name)
1012
export const generateTokens = (
@@ -153,7 +155,7 @@ export const getBlockTokens = (
153155

154156
if (tokensWithNextExpression.length) {
155157
result.push([
156-
`(${tokensWithNextExpression.map(({ token }) => token).join('|')})\\b`,
158+
`(${tokensWithNextExpression.map(({ token }) => escapeRegex(token || '')).join('|')})\\b`,
157159
{
158160
token: `argument.block.${lvl}.${name}`,
159161
next: '@query',
@@ -163,7 +165,7 @@ export const getBlockTokens = (
163165

164166
if (restTokens.length) {
165167
result.push([
166-
`(${restTokens.map(({ token }) => token).join('|')})\\b`,
168+
`(${restTokens.map(({ token }) => escapeRegex(token || '')).join('|')})\\b`,
167169
{ token: `argument.block.${lvl}.${name}`, next: '@root' },
168170
])
169171
}

0 commit comments

Comments
 (0)