Skip to content

Commit bd1fa35

Browse files
committed
update: split into two constant arrays
1 parent 78f6923 commit bd1fa35

File tree

5 files changed

+58
-29
lines changed

5 files changed

+58
-29
lines changed

ee/tabby-ui/components/markdown.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { ElementType, FC, memo } from 'react'
22
import ReactMarkdown, { Components, Options } from 'react-markdown'
33

4-
import { MARKDOWN_CUSTOM_TAGS } from '@/lib/constants'
4+
import {
5+
CUSTOM_HTML_BLOCK_TAGS,
6+
CUSTOM_HTML_INLINE_TAGS
7+
} from '@/lib/constants'
8+
9+
type CustomTag =
10+
| (typeof CUSTOM_HTML_BLOCK_TAGS)[number]
11+
| (typeof CUSTOM_HTML_INLINE_TAGS)[number]
512

613
type ExtendedOptions = Omit<Options, 'components'> & {
714
components: Components & {
815
// for custom html tags rendering
9-
[Tag in (typeof MARKDOWN_CUSTOM_TAGS)[number]]?: ElementType
16+
[Tag in CustomTag]?: ElementType
1017
}
1118
}
1219

Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
import { MARKDOWN_CUSTOM_TAGS } from '@/lib/constants'
21
import type { Root } from 'hast'
32
import type { Raw } from 'react-markdown/lib/ast-to-react'
43
import { visit } from 'unist-util-visit'
54

5+
function createTagFilterExpression(tagNames: string[]): RegExp {
6+
const escapedTags = tagNames
7+
.map(tag => tag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
8+
.join('|')
9+
return new RegExp(
10+
`<(/?)(?!/?(${escapedTags}))([^>]*)(?=[\\t\\n\\f\\r />])`,
11+
'gi'
12+
)
13+
}
14+
615
/**
7-
* Escape HTML tags that are not in MARKDOWN_CUSTOM_TAGS
16+
* Escape HTML tags that are not in tagNames
817
*/
9-
// const tagFilterExpression = /<(\/?)(?!\/?(think))([^>]*)(?=[\t\n\f\r />])/gi
10-
const tagFilterExpression = createTagFilterExpression(MARKDOWN_CUSTOM_TAGS)
18+
export function customStripTagsPlugin({ tagNames }: { tagNames: string[] }) {
19+
// const tagFilterExpression = /<(\/?)(?!\/?(think))([^>]*)(?=[\t\n\f\r />])/gi
20+
const tagFilterExpression = createTagFilterExpression(tagNames)
1121

12-
function createTagFilterExpression(tagNames: typeof MARKDOWN_CUSTOM_TAGS): RegExp {
13-
const escapedTags = tagNames.map(tag => tag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')
14-
return new RegExp(`<(/?)(?!/?(${escapedTags}))([^>]*)(?=[\\t\\n\\f\\r />])`, 'gi')
15-
}
16-
17-
export function customStripTagsPlugin() {
1822
return function (tree: Root) {
1923
visit(tree, 'raw', (node: Raw) => {
2024
node.value = node.value.replace(tagFilterExpression, '&lt;$2$3')
2125
})
2226
return tree
2327
}
24-
}
28+
}

ee/tabby-ui/components/message-markdown/index.tsx

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Fragment, ReactNode, useContext, useMemo, useState } from 'react'
2-
import { compact, isNil } from 'lodash-es'
2+
import { compact, flatten, isNil } from 'lodash-es'
33
import rehypeRaw from 'rehype-raw'
44
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'
55
import remarkGfm from 'remark-gfm'
@@ -21,7 +21,7 @@ import {
2121
convertFromFilepath,
2222
convertToFilepath,
2323
encodeMentionPlaceHolder,
24-
formatMarkdownCustomTags,
24+
formatCustomHTMLBlockTags,
2525
getRangeFromAttachmentCode,
2626
resolveFileNameForDisplay
2727
} from '@/lib/utils'
@@ -43,7 +43,10 @@ import {
4343
SymbolInfo
4444
} from 'tabby-chat-panel/index'
4545

46-
import { MARKDOWN_CUSTOM_TAGS } from '@/lib/constants'
46+
import {
47+
CUSTOM_HTML_BLOCK_TAGS,
48+
CUSTOM_HTML_INLINE_TAGS
49+
} from '@/lib/constants'
4750
import {
4851
MARKDOWN_CITATION_REGEX,
4952
MARKDOWN_COMMAND_REGEX,
@@ -277,8 +280,11 @@ export function MessageMarkdown({
277280
}
278281

279282
const encodedMessage = useMemo(() => {
280-
const formatedMessage = formatMarkdownCustomTags(message)
281-
return encodeMentionPlaceHolder(formatedMessage)
283+
const formattedMessage = formatCustomHTMLBlockTags(
284+
message,
285+
CUSTOM_HTML_BLOCK_TAGS as unknown as string[]
286+
)
287+
return encodeMentionPlaceHolder(formattedMessage)
282288
}, [message])
283289

284290
return (
@@ -310,16 +316,25 @@ export function MessageMarkdown({
310316
)}
311317
remarkPlugins={[remarkGfm, remarkMath]}
312318
rehypePlugins={[
313-
customStripTagsPlugin,
319+
[
320+
customStripTagsPlugin,
321+
{
322+
tagNames: flatten([
323+
CUSTOM_HTML_BLOCK_TAGS,
324+
CUSTOM_HTML_INLINE_TAGS
325+
])
326+
}
327+
],
314328
rehypeRaw,
315329
[
316330
rehypeSanitize,
317331
{
318332
...defaultSchema,
319-
tagNames: [
320-
...(defaultSchema.tagNames ?? []),
321-
...MARKDOWN_CUSTOM_TAGS
322-
]
333+
tagNames: flatten([
334+
defaultSchema.tagNames,
335+
CUSTOM_HTML_BLOCK_TAGS,
336+
CUSTOM_HTML_INLINE_TAGS
337+
])
323338
}
324339
]
325340
]}

ee/tabby-ui/lib/constants/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export const ERROR_CODE_NOT_FOUND = 'NOT_FOUND'
1414

1515
export const NEWLINE_CHARACTER = '\n'
1616

17-
export const MARKDOWN_CUSTOM_TAGS = ['think'] as const
17+
export const CUSTOM_HTML_BLOCK_TAGS = ['think'] as const
18+
export const CUSTOM_HTML_INLINE_TAGS = [] as const

ee/tabby-ui/lib/utils/chat.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from '@/lib/gql/generates/graphql'
1010
import type { MentionAttributes } from '@/lib/types'
1111

12-
import { MARKDOWN_CUSTOM_TAGS } from '../constants'
1312
import {
1413
MARKDOWN_FILE_REGEX,
1514
MARKDOWN_SOURCE_REGEX,
@@ -308,10 +307,13 @@ export function getTitleFromMessages(
308307
* @param inputString
309308
* @returns formatted markdown string
310309
*/
311-
export function formatMarkdownCustomTags(inputString: string): string {
312-
const tagPattern = MARKDOWN_CUSTOM_TAGS.map(tag =>
313-
tag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
314-
).join('|')
310+
export function formatCustomHTMLBlockTags(
311+
inputString: string,
312+
tagNames: string[]
313+
): string {
314+
const tagPattern = tagNames
315+
.map(tag => tag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
316+
.join('|')
315317
const regex = new RegExp(`(<(${tagPattern})>.*?</\\2>)`, 'gs')
316318

317319
// Adjust the newline characters for matched closing tags

0 commit comments

Comments
 (0)