-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
fix: preserve markdown tables in RichInput Source/Edit roundtrip #6441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import Placeholder from '@tiptap/extension-placeholder' | |
| import { mergeAttributes } from '@tiptap/core' | ||
| import StarterKit from '@tiptap/starter-kit' | ||
| import { Markdown } from '@tiptap/markdown' | ||
| import { Table, TableCell, TableHeader, TableRow } from '@tiptap/extension-table' | ||
| import { styled } from '@mui/material/styles' | ||
| import { Box } from '@mui/material' | ||
| import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight' | ||
|
|
@@ -17,6 +18,41 @@ import { isHtmlContent, escapeXmlTags, unescapeXmlEntities, unescapeXmlTags } fr | |
|
|
||
| const lowlight = createLowlight(common) | ||
|
|
||
| const MarkdownTable = Table.extend({ | ||
| renderMarkdown: (node, h) => { | ||
| const rows = | ||
| node.content?.map((rowNode) => | ||
| (rowNode.content || []).map((cellNode) => { | ||
| const text = (cellNode.content || []) | ||
| .map((childNode) => h.renderChildren(childNode)) | ||
| .join(' ') | ||
| .replace(/\s+/g, ' ') | ||
| .trim() | ||
| return { text, isHeader: cellNode.type === 'tableHeader' } | ||
| }) | ||
| ) || [] | ||
| const columnCount = rows.reduce((max, row) => Math.max(max, row.length), 0) | ||
|
|
||
| if (!columnCount) return '' | ||
|
|
||
| const renderRow = (row = []) => | ||
| `| ${new Array(columnCount) | ||
| .fill(0) | ||
| .map((_, index) => row[index]?.text || '') | ||
| .join(' | ')} |` | ||
|
|
||
| const headerRow = rows[0] || [] | ||
| const hasHeader = headerRow.some((cell) => cell.isHeader) | ||
| const bodyRows = hasHeader ? rows.slice(1) : rows | ||
|
|
||
| return [ | ||
| renderRow(hasHeader ? headerRow : []), | ||
| `| ${new Array(columnCount).fill('---').join(' | ')} |`, | ||
| ...bodyRows.map(renderRow) | ||
| ].join('\n') | ||
| } | ||
| }) | ||
|
Comment on lines
+21
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current markdown table serializer has two issues:
We can simplify the code and address both issues by removing the const MarkdownTable = Table.extend({
renderMarkdown: (node, h) => {
const rows =
node.content?.map((rowNode) =>
(rowNode.content || []).map((cellNode) =>
(cellNode.content || [])
.map((childNode) => h.renderChildren(childNode))
.join(' ')
.replace(/\s+/g, ' ')
.trim()
.replace(/\|/g, '\\|')
)
) || []
const columnCount = rows.reduce((max, row) => Math.max(max, row.length), 0)
if (!columnCount) return ''
const renderRow = (row = []) =>
'| ' + new Array(columnCount)
.fill(0)
.map((_, index) => row[index] || '')
.join(' | ') + ' |'
const headerRow = rows[0] || []
const bodyRows = rows.slice(1)
return [
renderRow(headerRow),
'| ' + new Array(columnCount).fill('---').join(' | ') + ' |',
...bodyRows.map(renderRow)
].join('\n')
}
})References
|
||
|
|
||
| // define your extension array | ||
| const extensions = ( | ||
| availableNodesForVariable, | ||
|
|
@@ -27,11 +63,20 @@ const extensions = ( | |
| isNodeInsideInteration, | ||
| useMarkdown | ||
| ) => [ | ||
| Markdown, | ||
| StarterKit.configure({ | ||
| codeBlock: false, | ||
| ...(!useMarkdown && { link: false }) | ||
| }), | ||
| MarkdownTable, | ||
| TableRow, | ||
| TableHeader, | ||
| TableCell, | ||
| Markdown.configure({ | ||
| markedOptions: { | ||
| gfm: true, | ||
| breaks: false | ||
| } | ||
| }), | ||
| CustomMention.configure({ | ||
| HTMLAttributes: { | ||
| class: 'variable' | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current markdown table serializer has two issues:
|), it will break the markdown table structure. We should escape|as\|inside the cell text.hasHeaderis false), the current logic renders an empty header row and shifts all actual data to the body. Since Markdown tables require a header row, we should fallback to treating the first row as the header row.We can simplify the code and address both issues by removing the
isHeadertracking and always treating the first row as the header row, which is the standard fallback for Markdown tables.References