From c1afdb9bd6979dac2b5bbaf40b4b948adfe780f0 Mon Sep 17 00:00:00 2001 From: JimmFly <447268514@qq.com> Date: Tue, 5 Mar 2024 10:02:21 +0000 Subject: [PATCH] fix(core): unexpected line breaks (#6019) --- .../src/components/pure/cmdk/use-highlight.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/frontend/core/src/components/pure/cmdk/use-highlight.ts b/packages/frontend/core/src/components/pure/cmdk/use-highlight.ts index 2d72d7b151..e9808a7b17 100644 --- a/packages/frontend/core/src/components/pure/cmdk/use-highlight.ts +++ b/packages/frontend/core/src/components/pure/cmdk/use-highlight.ts @@ -1,43 +1,44 @@ import { useMemo } from 'react'; function* highlightTextFragmentsGenerator(text: string, query: string) { - const lowerCaseText = text.replace(/\r?\n|\r/g, '').toLowerCase(); + const cleanedText = text.replace(/\r?\n|\r|\t/g, ''); + const lowerCaseText = cleanedText.toLowerCase(); query = query.toLowerCase(); let startIndex = lowerCaseText.indexOf(query); if (startIndex !== -1) { if (startIndex > 0) { - yield { text: text.substring(0, startIndex), highlight: false }; + yield { text: cleanedText.substring(0, startIndex), highlight: false }; } yield { - text: text.substring(startIndex, startIndex + query.length), + text: cleanedText.substring(startIndex, startIndex + query.length), highlight: true, }; - if (startIndex + query.length < text.length) { + if (startIndex + query.length < cleanedText.length) { yield { - text: text.substring(startIndex + query.length), + text: cleanedText.substring(startIndex + query.length), highlight: false, }; } } else { startIndex = 0; for (const char of query) { - const pos = text.toLowerCase().indexOf(char, startIndex); + const pos = cleanedText.toLowerCase().indexOf(char, startIndex); if (pos !== -1) { if (pos > startIndex) { yield { - text: text.substring(startIndex, pos), + text: cleanedText.substring(startIndex, pos), highlight: false, }; } - yield { text: text.substring(pos, pos + 1), highlight: true }; + yield { text: cleanedText.substring(pos, pos + 1), highlight: true }; startIndex = pos + 1; } } - if (startIndex < text.length) { - yield { text: text.substring(startIndex), highlight: false }; + if (startIndex < cleanedText.length) { + yield { text: cleanedText.substring(startIndex), highlight: false }; } } }