Skip to content

Commit 33c6678

Browse files
authored
feat: add optional remarkIgnoreMarkdown plugin to avoid markdown transform to HTML (#2872)
1 parent 2e9bc6e commit 33c6678

File tree

5 files changed

+121
-181
lines changed

5 files changed

+121
-181
lines changed

src/components/Message/renderText/__tests__/__snapshots__/renderText.test.js.snap

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,6 @@ exports[`keepLineBreaksPlugin present keeps line between lines with strong text
236236
<br />
237237
238238
239-
240-
241-
242-
243239
<p>
244240
This is the second line
245241
</p>
@@ -256,17 +252,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a blockquote 1`]
256252
<br />
257253
258254
259-
260-
261-
262-
263255
<br />
264256
265257
266-
267-
268-
269-
270258
<blockquote>
271259
272260
@@ -281,17 +269,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a blockquote 1`]
281269
<br />
282270
283271
284-
285-
286-
287-
288272
<br />
289273
290274
291-
292-
293-
294-
295275
<p>
296276
c
297277
</p>
@@ -308,17 +288,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a code block 1`]
308288
<br />
309289
310290
311-
312-
313-
314-
315291
<br />
316292
317293
318-
319-
320-
321-
322294
<p>
323295
<code>
324296
b
@@ -329,17 +301,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a code block 1`]
329301
<br />
330302
331303
332-
333-
334-
335-
336304
<br />
337305
338306
339-
340-
341-
342-
343307
<p>
344308
c
345309
</p>
@@ -356,33 +320,17 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a horizontal rule
356320
<br />
357321
358322
359-
360-
361-
362-
363323
<br />
364324
365325
366326
367327
368-
369-
370-
371-
372328
<br />
373329
374330
375-
376-
377-
378-
379331
<br />
380332
381333
382-
383-
384-
385-
386334
<p>
387335
b
388336
</p>
@@ -399,17 +347,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a strikethrough 1
399347
<br />
400348
401349
402-
403-
404-
405-
406350
<br />
407351
408352
409-
410-
411-
412-
413353
<p>
414354
~~xxx~~
415355
</p>
@@ -418,17 +358,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a strikethrough 1
418358
<br />
419359
420360
421-
422-
423-
424-
425361
<br />
426362
427363
428-
429-
430-
431-
432364
<p>
433365
b
434366
</p>
@@ -445,17 +377,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a table 1`] = `
445377
<br />
446378
447379
448-
449-
450-
451-
452380
<br />
453381
454382
455-
456-
457-
458-
459383
<p>
460384
| a | b | c | d |
461385
| - | :- | -: | :-: |
@@ -466,17 +390,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks around a table 1`] = `
466390
<br />
467391
468392
469-
470-
471-
472-
473393
<br />
474394
475395
476-
477-
478-
479-
480396
<p>
481397
c
482398
</p>
@@ -493,17 +409,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks between paragraphs 1`] =
493409
<br />
494410
495411
496-
497-
498-
499-
500412
<br />
501413
502414
503-
504-
505-
506-
507415
<p>
508416
b
509417
</p>
@@ -512,17 +420,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks between paragraphs 1`] =
512420
<br />
513421
514422
515-
516-
517-
518-
519423
<br />
520424
521425
522-
523-
524-
525-
526426
<p>
527427
c
528428
</p>
@@ -548,17 +448,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks between the items in an
548448
<br />
549449
550450
551-
552-
553-
554-
555451
<br />
556452
557453
558-
559-
560-
561-
562454
<li>
563455
564456
@@ -573,17 +465,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks between the items in an
573465
<br />
574466
575467
576-
577-
578-
579-
580468
<br />
581469
582470
583-
584-
585-
586-
587471
<li>
588472
589473
@@ -618,17 +502,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks between the items in an
618502
<br />
619503
620504
621-
622-
623-
624-
625505
<br />
626506
627507
628-
629-
630-
631-
632508
<li>
633509
634510
@@ -643,17 +519,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks between the items in an
643519
<br />
644520
645521
646-
647-
648-
649-
650522
<br />
651523
652524
653-
654-
655-
656-
657525
<li>
658526
659527
@@ -679,17 +547,9 @@ exports[`keepLineBreaksPlugin present keeps line breaks under a heading 1`] = `
679547
<br />
680548
681549
682-
683-
684-
685-
686550
<br />
687551
688552
689-
690-
691-
692-
693553
<p>
694554
a
695555
</p>

src/components/Message/renderText/__tests__/renderText.test.js

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
imageToLink,
88
keepLineBreaksPlugin,
99
plusPlusToEmphasis,
10+
remarkIgnoreMarkdown,
1011
} from '../remarkPlugins';
1112
import { defaultAllowedTagNames, renderText } from '../renderText';
1213
import '@testing-library/jest-dom';
@@ -306,19 +307,19 @@ describe(`renderText`, () => {
306307
});
307308
});
308309

309-
describe('keepLineBreaksPlugin', () => {
310-
const lineBreaks = '\n\n\n';
311-
const paragraphText = `a${lineBreaks}b${lineBreaks}c`;
312-
const unorderedListText = `* item 1${lineBreaks}* item 2${lineBreaks}* item 3`;
313-
const orderedListText = `1. item 1${lineBreaks}2. item 2${lineBreaks}3. item 3`;
314-
const headingText = `## Heading${lineBreaks}a`;
315-
const codeBlockText = 'a\n\n\n```b```\n\n\nc';
316-
const horizontalRuleText = `a${lineBreaks}---${lineBreaks}b`;
317-
const blockquoteText = `a${lineBreaks}>b${lineBreaks}c`;
318-
const withStrikeThroughText = `a${lineBreaks}${strikeThroughText}${lineBreaks}b`;
319-
const tableText = `a${lineBreaks}| a | b | c | d |\n| - | :- | -: | :-: |\n| a | b | c | d |${lineBreaks}c`;
320-
const multilineWithStrongText = 'This is **the first** line\n\nThis is the second line';
310+
const lineBreaks = '\n\n\n';
311+
const paragraphText = `a${lineBreaks}b${lineBreaks}c`;
312+
const unorderedListText = `* item 1${lineBreaks}* item 2${lineBreaks}* item 3`;
313+
const orderedListText = `1. item 1${lineBreaks}2. item 2${lineBreaks}3. item 3`;
314+
const headingText = `## Heading${lineBreaks}a`;
315+
const codeBlockText = 'a\n\n\n```b```\n\n\nc';
316+
const horizontalRuleText = `a${lineBreaks}---${lineBreaks}b`;
317+
const blockquoteText = `a${lineBreaks}>b${lineBreaks}c`;
318+
const withStrikeThroughText = `a${lineBreaks}${strikeThroughText}${lineBreaks}b`;
319+
const tableText = `a${lineBreaks}| a | b | c | d |\n| - | :- | -: | :-: |\n| a | b | c | d |${lineBreaks}c`;
320+
const multilineWithStrongText = 'This is **the first** line\n\nThis is the second line';
321321

322+
describe('keepLineBreaksPlugin', () => {
322323
const doRenderText = (text, present) => {
323324
const Markdown = renderText(
324325
text,
@@ -500,3 +501,37 @@ describe('imageToLink', () => {
500501
).not.toBeInTheDocument();
501502
});
502503
});
504+
505+
describe('remarkIgnoreMarkdown', () => {
506+
const text = [
507+
headingText,
508+
paragraphText,
509+
unorderedListText,
510+
orderedListText,
511+
codeBlockText,
512+
horizontalRuleText,
513+
blockquoteText,
514+
withStrikeThroughText,
515+
tableText,
516+
multilineWithStrongText,
517+
].join('\n');
518+
519+
const renderWithPlugin = (plugins = []) => {
520+
const Markdown = renderText(
521+
text,
522+
{},
523+
{ getRemarkPlugins: () => [...plugins, remarkIgnoreMarkdown] },
524+
);
525+
return render(Markdown).container;
526+
};
527+
528+
it('skips the markdown transformation and keeps the original escaped text and lines', () => {
529+
expect(renderWithPlugin().innerHTML).toBe(`<p>${text.replace(/>/g, '&gt;')}</p>`);
530+
});
531+
532+
it('keeps line without keepLineBreaksPlugin', () => {
533+
expect(renderWithPlugin([keepLineBreaksPlugin]).innerHTML).toBe(
534+
renderWithPlugin().innerHTML,
535+
);
536+
});
537+
});

src/components/Message/renderText/remarkPlugins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './htmlToTextPlugin';
22
export * from './imageToLink';
33
export * from './keepLineBreaksPlugin';
44
export * from './plusPlusToEmphasis';
5+
export * from './remarkIgnoreMarkdown';

0 commit comments

Comments
 (0)