Skip to content

Commit ae1ccfd

Browse files
committed
refactor: replace underline plugin with highlight plugin in Markdown component
- Removed the rehypeHighlightUnderline plugin and replaced it with rehypeHighlight for improved text formatting. - Eliminated the UnderlineComponent from the Markdown rendering logic. - Updated the markdown serializer to remove underline syntax handling for better consistency.
1 parent 805f10d commit ae1ccfd

File tree

3 files changed

+10
-52
lines changed

3 files changed

+10
-52
lines changed
Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import type { Node } from 'unist';
55
type Plugin = () => (tree: any) => void;
66

77
/**
8-
* Rehype plugin to transform text nodes with ==highlight== and __underline__ syntax
9-
* to proper HTML elements.
8+
* Rehype plugin to transform text nodes with ==highlight== syntax to proper HTML elements.
109
*/
11-
const rehypeHighlightUnderline: Plugin = () => {
10+
const rehypeHighlight: Plugin = () => {
1211
return (tree: Node) => {
1312
// Process text nodes for special syntax
1413
visit(tree, 'text', (node: any, index: number | null, parent: any) => {
@@ -17,23 +16,21 @@ const rehypeHighlightUnderline: Plugin = () => {
1716
const { value } = node;
1817
if (typeof value !== 'string') return;
1918

20-
// Match patterns for highlight and underline
19+
// Match patterns for highlight
2120
const highlightRegex = /==(.*?)==/g;
22-
const underlineRegex = /__(.*?)__/g;
2321

2422
// Check if we have any matches
25-
if (!highlightRegex.test(value) && !underlineRegex.test(value)) return;
23+
if (!highlightRegex.test(value)) return;
2624

2725
// Reset regex lastIndex
2826
highlightRegex.lastIndex = 0;
29-
underlineRegex.lastIndex = 0;
3027

3128
// Process the text to create HTML elements
3229
const parts = [];
3330
let lastIndex = 0;
3431
const text = value;
3532

36-
// First process highlights
33+
// Process highlights
3734
let match: any = highlightRegex.exec(text);
3835
while (match !== null) {
3936
// Add text before the match
@@ -58,40 +55,6 @@ const rehypeHighlightUnderline: Plugin = () => {
5855
parts.push({ type: 'text', value: text.slice(lastIndex) });
5956
}
6057

61-
// If we found any highlights, replace the original node
62-
if (parts.length > 0) {
63-
parent.children.splice(index, 1, ...parts);
64-
return;
65-
}
66-
67-
// Otherwise, check for underlines
68-
lastIndex = 0;
69-
parts.length = 0;
70-
71-
match = underlineRegex.exec(text);
72-
while (match !== null) {
73-
// Add text before the match
74-
if (match.index > lastIndex) {
75-
parts.push({ type: 'text', value: text.slice(lastIndex, match.index) });
76-
}
77-
78-
// Add the underline element
79-
parts.push({
80-
type: 'element',
81-
tagName: 'u',
82-
properties: {},
83-
children: [{ type: 'text', value: match[1] }],
84-
});
85-
86-
lastIndex = match.index + match[0].length;
87-
match = underlineRegex.exec(text);
88-
}
89-
90-
// Add any remaining text
91-
if (lastIndex < text.length) {
92-
parts.push({ type: 'text', value: text.slice(lastIndex) });
93-
}
94-
9558
// Replace the original node with our processed parts
9659
if (parts.length > 0) {
9760
parent.children.splice(index, 1, ...parts);
@@ -100,4 +63,4 @@ const rehypeHighlightUnderline: Plugin = () => {
10063
};
10164
};
10265

103-
export default rehypeHighlightUnderline;
66+
export default rehypeHighlight;

packages/ai-workspace-common/src/components/markdown/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { cn, markdownCitationParse } from '@refly/utils';
99

1010
// plugins
1111
import LinkElement from './plugins/link';
12-
import rehypeHighlightUnderline from './custom-plugins/rehype-highlight-underline';
12+
import rehypeHighlight from './custom-plugins/rehype-highlight';
1313

1414
// styles
1515
import './styles/markdown.scss';
@@ -57,10 +57,6 @@ const HighlightComponent = ({ children }: { children: React.ReactNode }) => (
5757
</mark>
5858
);
5959

60-
const UnderlineComponent = ({ children }: { children: React.ReactNode }) => (
61-
<span className="underline">{children}</span>
62-
);
63-
6460
const StrikethroughComponent = ({ children }: { children: React.ReactNode }) => (
6561
<del>{children}</del>
6662
);
@@ -147,7 +143,7 @@ export const Markdown = memo(
147143
remarkPlugins={[RemarkBreaks, plugins.RemarkMath, RemarkGfm]}
148144
rehypePlugins={[
149145
...rehypePlugins,
150-
rehypeHighlightUnderline,
146+
rehypeHighlight,
151147
plugins.RehypeKatex,
152148
[
153149
plugins.RehypeHighlight,
@@ -163,7 +159,6 @@ export const Markdown = memo(
163159
img: MarkdownImage,
164160
mark: HighlightComponent,
165161
del: StrikethroughComponent,
166-
u: UnderlineComponent,
167162
}}
168163
linkTarget={'_blank'}
169164
>

packages/utils/src/editor/to_markdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ export const defaultMarkdownSerializer = new MarkdownSerializer(
231231
expelEnclosingWhitespace: true,
232232
},
233233
underline: {
234-
open: '__',
235-
close: '__',
234+
open: '',
235+
close: '',
236236
mixable: true,
237237
expelEnclosingWhitespace: true,
238238
},

0 commit comments

Comments
 (0)