11import React from "react" ;
2+ import ReactMarkdown from "react-markdown" ;
23
3- import { Markdown , MarkdownProps } from "../../cmem/markdown/Markdown " ;
4+ import { truncateMarkdown } from "../../cmem/markdown/truncateMarkdown " ;
45
56import { reduceToText } from "./reduceToText" ;
67import { truncateMarkdownDisplay } from "./truncateMarkdownDisplay" ;
78
9+ interface RawMarkdownProps {
10+ children : string ;
11+ cutOff : number ;
12+ cutOffSuffix ?: string ;
13+ allowHtml ?: boolean ;
14+ "data-test-id" ?: string ;
15+ }
16+
17+ const RawMarkdown = ( { children, cutOff, cutOffSuffix = "..." } : RawMarkdownProps ) => (
18+ < ReactMarkdown > { truncateMarkdown ( children , cutOff , cutOffSuffix ) . trim ( ) } </ ReactMarkdown >
19+ ) ;
20+
821const measureLength = ( node : React . ReactElement ) : number => reduceToText ( node ) . length ;
922
10- const makeMarkdown = ( children : string , cutOff : number , extra ?: Partial < MarkdownProps > ) =>
11- React . createElement ( Markdown , { children, cutOff, ...extra } ) as React . ReactElement <
12- MarkdownProps & { cutOff : number }
13- > ;
23+ const makeMarkdown = ( children : string , cutOff : number , extra ?: Partial < RawMarkdownProps > ) =>
24+ React . createElement ( RawMarkdown , { children, cutOff, ...extra } ) as React . ReactElement < RawMarkdownProps > ;
1425
1526describe ( "truncateMarkdownDisplay" , ( ) => {
1627 it ( "returns the untruncated element when the rendered content is already shorter than cutOff" , ( ) => {
1728 const input = makeMarkdown ( "Short text." , 1000 ) ;
1829 const result = truncateMarkdownDisplay ( input ) ;
19- expect ( ( result . props as MarkdownProps ) . cutOff ) . toBeUndefined ( ) ;
30+ expect ( ( result . props as RawMarkdownProps ) . cutOff ) . toBeUndefined ( ) ;
2031 } ) ;
2132
2233 it ( "returns an element whose rendered text length is closer to cutOff than the raw cutOff would yield" , ( ) => {
@@ -37,7 +48,7 @@ describe("truncateMarkdownDisplay", () => {
3748 const linkHeavy = Array . from ( { length : 20 } , ( _ , i ) => `[click](https://example.com/${ i } )` ) . join ( " " ) ;
3849 const input = makeMarkdown ( linkHeavy , 40 , { "data-test-id" : "md-x" , allowHtml : true } ) ;
3950 const result = truncateMarkdownDisplay ( input ) ;
40- const props = result . props as MarkdownProps ;
51+ const props = result . props as RawMarkdownProps ;
4152 expect ( props [ "data-test-id" ] ) . toBe ( "md-x" ) ;
4253 expect ( props . allowHtml ) . toBe ( true ) ;
4354 } ) ;
@@ -46,7 +57,7 @@ describe("truncateMarkdownDisplay", () => {
4657 const linkHeavy = Array . from ( { length : 20 } , ( _ , i ) => `[click](https://example.com/${ i } )` ) . join ( " " ) ;
4758 const initialCutOff = 50 ;
4859 const result = truncateMarkdownDisplay ( makeMarkdown ( linkHeavy , initialCutOff ) ) ;
49- const props = result . props as MarkdownProps ;
60+ const props = result . props as RawMarkdownProps ;
5061 // Either the element was kept (initial was already best) or cutOff was raised to compensate for syntax overhead.
5162 expect ( props . cutOff === undefined || ( typeof props . cutOff === "number" && props . cutOff >= initialCutOff ) ) . toBe (
5263 true ,
@@ -60,7 +71,7 @@ describe("truncateMarkdownDisplay", () => {
6071 const cutOff = 30 ;
6172 const input = makeMarkdown ( content , cutOff ) ;
6273 const resultDecoded = truncateMarkdownDisplay ( input , { decodeHtmlEntities : true } ) ;
63- expect ( ( resultDecoded . props as MarkdownProps ) . cutOff ) . toBeUndefined ( ) ;
74+ expect ( ( resultDecoded . props as RawMarkdownProps ) . cutOff ) . toBeUndefined ( ) ;
6475 } ) ;
6576
6677 it ( "respects maxRounds by not iterating when set to 0" , ( ) => {
@@ -69,6 +80,6 @@ describe("truncateMarkdownDisplay", () => {
6980 const input = makeMarkdown ( linkHeavy , initialCutOff ) ;
7081 const result = truncateMarkdownDisplay ( input , undefined , 0 ) ;
7182 // With no iterations allowed, the result should be the initial element (same cutOff as the input).
72- expect ( ( result . props as MarkdownProps ) . cutOff ) . toBe ( initialCutOff ) ;
83+ expect ( ( result . props as RawMarkdownProps ) . cutOff ) . toBe ( initialCutOff ) ;
7384 } ) ;
7485} ) ;
0 commit comments