Skip to content

Commit 9c7dc14

Browse files
committed
feat: update renderer for marked
1 parent 9f6262d commit 9c7dc14

File tree

4 files changed

+43
-39
lines changed

4 files changed

+43
-39
lines changed

package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"@rjsf/validator-ajv8": "^5.13.3",
8080
"@typeform/embed-react": "2.20.0",
8181
"dompurify": "^3.0.2",
82-
"marked": "4.3.0",
82+
"marked": "^13.0.3",
8383
"react": "^17.0.2",
8484
"react-dom": "^17.0.2",
8585
"react-draggable": "^4.4.5",

src/Common/Markdown/MarkDown.tsx

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { marked } from 'marked'
17+
import { marked, Tokens } from 'marked'
1818
import DOMPurify from 'dompurify'
1919
import { useEffect, useRef } from 'react'
2020
import { MarkDownProps } from './Types'
2121
import './markdown.scss'
2222

23-
const uncheckedCheckboxInputElement = `<input checked="" disabled="" type="checkbox">`
24-
const checkedCheckboxInputElement = `<input disabled="" type="checkbox">`
2523
const renderer = new marked.Renderer()
2624

2725
const MarkDown = ({ setExpandableIcon, markdown, className, breaks, disableEscapedText, ...props }: MarkDownProps) => {
@@ -39,62 +37,67 @@ const MarkDown = ({ setExpandableIcon, markdown, className, breaks, disableEscap
3937
getHeight()
4038
}, [markdown])
4139

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>`
6252
}
6353
return `<li>${text}</li>`
6454
}
6555

66-
renderer.image = (href: string, title: string, text: string) =>
56+
renderer.image = ({ href, title, text }: Tokens.Image) =>
6757
`<img src="${href}" alt="${text}" title="${title}" class="max-w-100">`
6858

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 `
7067
<div class="table-container">
7168
<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>
7475
</table>
7576
</div>
7677
`
78+
}
7779

78-
renderer.heading = (text, level) => {
80+
renderer.heading = ({ text, depth }: Tokens.Heading) => {
7981
const escapedText = disableEscapedText ? '' : text.toLowerCase().replace(/[^\w]+/g, '-')
8082

8183
return `
8284
<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">
8486
<span class="header-link"></span>
8587
${text}
86-
</h${level}>
88+
</h${depth}>
8789
</a>`
8890
}
8991

9092
marked.setOptions({
9193
renderer,
9294
gfm: true,
93-
smartLists: true,
9495
...(breaks && { breaks: true }),
9596
})
9697

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+
})
98101

99102
return (
100103
<article

src/Common/Markdown/markdown.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
border-spacing: 0;
2727
border-collapse: collapse;
2828

29-
tbody {
29+
tbody, thead {
3030
td,
3131
th {
3232
padding: 6px 13px;

0 commit comments

Comments
 (0)