mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-23 16:59:48 +01:00
Fix search highlighting conflict with jsx code (#1886)
Replaces "<>", "</>" highlighting marks with "<|[[--" and "--]]|>" to avoid conflicts when highlighting jsx code.
This commit is contained in:
2
gradle/changelog/hightlight_jsx_conflict.yaml
Normal file
2
gradle/changelog/hightlight_jsx_conflict.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
- type: fixed
|
||||||
|
description: Search highlighting in jsx ([#1886](https://github.com/scm-manager/scm-manager/pull/1886))
|
||||||
@@ -24,6 +24,9 @@
|
|||||||
|
|
||||||
import React, { FC } from "react";
|
import React, { FC } from "react";
|
||||||
|
|
||||||
|
const PRE_TAG = "<|[[--";
|
||||||
|
const POST_TAG = "--]]|>";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
value: string;
|
value: string;
|
||||||
};
|
};
|
||||||
@@ -33,14 +36,14 @@ const HighlightedFragment: FC<Props> = ({ value }) => {
|
|||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
while (content.length > 0) {
|
while (content.length > 0) {
|
||||||
const start = content.indexOf("<>");
|
const start = content.indexOf(PRE_TAG);
|
||||||
const end = content.indexOf("</>");
|
const end = content.indexOf(POST_TAG);
|
||||||
if (start >= 0 && end > 0) {
|
if (start >= 0 && end > 0) {
|
||||||
if (start > 0) {
|
if (start > 0) {
|
||||||
result.push(content.substring(0, start));
|
result.push(content.substring(0, start));
|
||||||
}
|
}
|
||||||
result.push(<strong>{content.substring(start + 2, end)}</strong>);
|
result.push(<strong>{content.substring(start + PRE_TAG.length, end)}</strong>);
|
||||||
content = content.substring(end + 3);
|
content = content.substring(end + POST_TAG.length);
|
||||||
} else {
|
} else {
|
||||||
result.push(content);
|
result.push(content);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ public final class LuceneHighlighter {
|
|||||||
fragments = keepWholeLine(value, fragments);
|
fragments = keepWholeLine(value, fragments);
|
||||||
}
|
}
|
||||||
return Arrays.stream(fragments)
|
return Arrays.stream(fragments)
|
||||||
.map(fragment -> fragment.replace(PRE_TAG, "<>").replace(POST_TAG, "</>"))
|
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class LuceneHighlighterTest {
|
|||||||
String[] snippets = highlighter.highlight("content", Indexed.Analyzer.DEFAULT, content);
|
String[] snippets = highlighter.highlight("content", Indexed.Analyzer.DEFAULT, content);
|
||||||
|
|
||||||
assertThat(snippets).hasSize(1).allSatisfy(
|
assertThat(snippets).hasSize(1).allSatisfy(
|
||||||
snippet -> assertThat(snippet).contains("<>Golgafrinchan</>")
|
snippet -> assertThat(snippet).contains("<|[[--Golgafrinchan--]]|>")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ class LuceneHighlighterTest {
|
|||||||
snippet -> assertThat(snippet.split("\n")).contains(
|
snippet -> assertThat(snippet.split("\n")).contains(
|
||||||
"\t\t\t\tint neighbors= getNeighbors(above, same, below);",
|
"\t\t\t\tint neighbors= getNeighbors(above, same, below);",
|
||||||
"\t\t\t\tif(neighbors < 2 || neighbors > 3){",
|
"\t\t\t\tif(neighbors < 2 || neighbors > 3){",
|
||||||
"\t\t\t\t\tnewGen[row]+= \"_\";//<2 or >3 neighbors -> <>die</>",
|
"\t\t\t\t\tnewGen[row]+= \"_\";//<2 or >3 neighbors -> <|[[--die--]]|>",
|
||||||
"\t\t\t\t}else if(neighbors == 3){",
|
"\t\t\t\t}else if(neighbors == 3){",
|
||||||
"\t\t\t\t\tnewGen[row]+= \"#\";//3 neighbors -> spawn/live"
|
"\t\t\t\t\tnewGen[row]+= \"#\";//3 neighbors -> spawn/live"
|
||||||
)
|
)
|
||||||
@@ -79,7 +79,7 @@ class LuceneHighlighterTest {
|
|||||||
snippet -> assertThat(snippet.split("\n")).contains(
|
snippet -> assertThat(snippet.split("\n")).contains(
|
||||||
"}) => {",
|
"}) => {",
|
||||||
" const renderIcon = () => {",
|
" const renderIcon = () => {",
|
||||||
" return <>{icon ? <Icon name={icon} color=\"<>inherit</>\" className=\"is-medium pr-1\" /> : null}</>;",
|
" return <>{icon ? <Icon name={icon} color=\"<|[[--inherit--]]|>\" className=\"is-medium pr-1\" /> : null}</>;",
|
||||||
" };"
|
" };"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -499,7 +499,7 @@ class LuceneQueryBuilderTest {
|
|||||||
|
|
||||||
JsonNode displayName = fields.get("displayName");
|
JsonNode displayName = fields.get("displayName");
|
||||||
assertThat(displayName.get("highlighted").asBoolean()).isTrue();
|
assertThat(displayName.get("highlighted").asBoolean()).isTrue();
|
||||||
assertThat(displayName.get("fragments").get(0).asText()).contains("<>Arthur</>");
|
assertThat(displayName.get("fragments").get(0).asText()).contains("<|[[--Arthur--]]|>");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user