Skip to content

Commit 485b4a4

Browse files
Div627kimteayon
andauthored
fix(XMarkdown): openLinksInNewTab 属性配置失效 (#1253)
* fxi: fix openLinksInNewTab is filtered by dompurify * fix: fix ci --------- Co-authored-by: Mickey <951203214@qq.com>
1 parent 8f16477 commit 485b4a4

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/x-markdown/src/XMarkdown/__test__/index.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,41 @@ describe('XMarkdown', () => {
195195
expect(wrapper).toHaveClass('ant-x-markdown');
196196
expect(wrapper.innerHTML).toBe('<div>This is a paragraph.</div>\n');
197197
});
198+
199+
describe('openLinksInNewTab', () => {
200+
it('should add target="_blank" and rel="noopener noreferrer" to links with title when openLinksInNewTab is true', () => {
201+
const { container } = render(
202+
<XMarkdown content='[Google](https://www.google.com "Google Search")' openLinksInNewTab />,
203+
);
204+
205+
const link = container.querySelector('a');
206+
expect(link).toHaveAttribute('href', 'https://www.google.com');
207+
expect(link).toHaveAttribute('target', '_blank');
208+
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
209+
expect(link).toHaveAttribute('title', 'Google Search');
210+
expect(link).toHaveTextContent('Google');
211+
});
212+
213+
it('should not add target="_blank" when openLinksInNewTab is false', () => {
214+
const { container } = render(
215+
<XMarkdown content="[Google](https://www.google.com)" openLinksInNewTab={false} />,
216+
);
217+
218+
const link = container.querySelector('a');
219+
expect(link).toHaveAttribute('href', 'https://www.google.com');
220+
expect(link).not.toHaveAttribute('target');
221+
expect(link).not.toHaveAttribute('rel');
222+
expect(link).toHaveTextContent('Google');
223+
});
224+
225+
it('should not add target="_blank" when openLinksInNewTab is not provided', () => {
226+
const { container } = render(<XMarkdown content="[Google](https://www.google.com)" />);
227+
228+
const link = container.querySelector('a');
229+
expect(link).toHaveAttribute('href', 'https://www.google.com');
230+
expect(link).not.toHaveAttribute('target');
231+
expect(link).not.toHaveAttribute('rel');
232+
expect(link).toHaveTextContent('Google');
233+
});
234+
});
198235
});

packages/x-markdown/src/XMarkdown/core/Renderer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ class Renderer {
5959
const customComponents = Object.keys(this.options.components || {});
6060
const userConfig = this.options.dompurifyConfig || {};
6161

62+
const allowedTags = Array.isArray(userConfig.ALLOWED_TAGS) ? userConfig.ALLOWED_TAGS : [];
63+
const addAttr = Array.isArray(userConfig.ADD_ATTR) ? userConfig.ADD_ATTR : [];
64+
6265
return {
6366
...userConfig,
64-
ADD_TAGS: [...new Set([...customComponents, ...(userConfig.ALLOWED_TAGS || [])])],
67+
ADD_TAGS: Array.from(new Set([...customComponents, ...allowedTags])),
68+
ADD_ATTR: Array.from(new Set(['target', 'rel', ...addAttr])),
6569
};
6670
}
6771

0 commit comments

Comments
 (0)