14
14
* limitations under the License.
15
15
*/
16
16
17
- import { marked } from 'marked'
17
+ import { marked , Tokens } from 'marked'
18
18
import DOMPurify from 'dompurify'
19
19
import { useEffect , useRef } from 'react'
20
20
import { MarkDownProps } from './Types'
21
21
import './markdown.scss'
22
22
23
- const uncheckedCheckboxInputElement = `<input checked="" disabled="" type="checkbox">`
24
- const checkedCheckboxInputElement = `<input disabled="" type="checkbox">`
25
23
const renderer = new marked . Renderer ( )
26
24
27
25
const MarkDown = ( { setExpandableIcon, markdown, className, breaks, disableEscapedText, ...props } : MarkDownProps ) => {
@@ -39,62 +37,67 @@ const MarkDown = ({ setExpandableIcon, markdown, className, breaks, disableEscap
39
37
getHeight ( )
40
38
} , [ markdown ] )
41
39
42
- const isReadmeInputCheckbox = ( text : string ) => {
43
- if ( text . includes ( uncheckedCheckboxInputElement ) || text . includes ( checkedCheckboxInputElement ) ) {
44
- return true
45
- }
46
- return false
47
- }
48
-
49
- renderer . listitem = ( text : string ) => {
50
- if ( isReadmeInputCheckbox ( text ) ) {
51
- // eslint-disable-next-line no-param-reassign
52
- text = text
53
- . replace (
54
- uncheckedCheckboxInputElement ,
55
- '<input type="checkbox" style="margin: 0 0.2em 0.25em -1.4em;" class="dc__vertical-align-middle" checked disabled>' ,
56
- )
57
- . replace (
58
- checkedCheckboxInputElement ,
59
- '<input type="checkbox" style="margin: 0 0.2em 0.25em -1.4em;" class="dc__vertical-align-middle" disabled>' ,
60
- )
61
- return `<li style="list-style: none">${ text } </li>`
40
+ renderer . listitem = ( { text, task, checked } : Tokens . ListItem ) => {
41
+ if ( task ) {
42
+ if ( checked ) {
43
+ return `<li style="list-style: none">
44
+ <input disabled type="checkbox" checked class="dc__vertical-align-middle" style="margin: 0 0.2em 0.25em -1.4em">
45
+ ${ text }
46
+ </li>`
47
+ }
48
+ return `<li style="list-style: none">
49
+ <input disabled type="checkbox" class="dc__vertical-align-middle" style="margin: 0 0.2em 0.25em -1.4em">
50
+ ${ text }
51
+ </li>`
62
52
}
63
53
return `<li>${ text } </li>`
64
54
}
65
55
66
- renderer . image = ( href : string , title : string , text : string ) =>
56
+ renderer . image = ( { href , title, text } : Tokens . Image ) =>
67
57
`<img src="${ href } " alt="${ text } " title="${ title } " class="max-w-100">`
68
58
69
- renderer . table = ( header , body ) => `
59
+ renderer . table = ( { header, rows } : Tokens . Table ) => {
60
+ const renderTableRow = ( row : Tokens . TableCell [ ] ) => `
61
+ <tr>
62
+ ${ row . map ( ( rowCell ) => `<td align="${ rowCell . align } ">${ marked ( rowCell . text ) } </td>` ) . join ( '' ) }
63
+ </tr>
64
+ `
65
+
66
+ return `
70
67
<div class="table-container">
71
68
<table>
72
- ${ header }
73
- ${ body }
69
+ <thead>
70
+ <tr>${ header . map ( ( headerCell ) => `<th align="${ headerCell . align } ">${ headerCell . text } </th>` ) . join ( '' ) } </tr>
71
+ </thead>
72
+ <tbody>
73
+ ${ rows . map ( ( row ) => renderTableRow ( row ) ) . join ( '' ) }
74
+ </tbody>
74
75
</table>
75
76
</div>
76
77
`
78
+ }
77
79
78
- renderer . heading = ( text , level ) => {
80
+ renderer . heading = ( { text, depth } : Tokens . Heading ) => {
79
81
const escapedText = disableEscapedText ? '' : text . toLowerCase ( ) . replace ( / [ ^ \w ] + / g, '-' )
80
82
81
83
return `
82
84
<a name="${ escapedText } " rel="noreferrer noopener" class="anchor" href="#${ escapedText } ">
83
- <h${ level } data-testid="deployment-template-readme-version">
85
+ <h${ depth } data-testid="deployment-template-readme-version">
84
86
<span class="header-link"></span>
85
87
${ text }
86
- </h${ level } >
88
+ </h${ depth } >
87
89
</a>`
88
90
}
89
91
90
92
marked . setOptions ( {
91
93
renderer,
92
94
gfm : true ,
93
- smartLists : true ,
94
95
...( breaks && { breaks : true } ) ,
95
96
} )
96
97
97
- const createMarkup = ( ) => ( { __html : DOMPurify . sanitize ( marked ( markdown ) , { USE_PROFILES : { html : true } } ) } )
98
+ const createMarkup = ( ) => ( {
99
+ __html : DOMPurify . sanitize ( marked ( markdown ) as string , { USE_PROFILES : { html : true } } ) ,
100
+ } )
98
101
99
102
return (
100
103
< article
0 commit comments