Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@tiptap/extension-code-block-lowlight": "^3.20.4",
"@tiptap/extension-mention": "^3.20.4",
"@tiptap/extension-placeholder": "^3.20.4",
"@tiptap/extension-table": "^3.20.4",
"@tiptap/markdown": "^3.20.4",
"@tiptap/pm": "^3.20.4",
"@tiptap/react": "^3.20.4",
Expand Down
47 changes: 46 additions & 1 deletion packages/ui/src/ui-component/dialog/ExpandRichInputDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
import { common, createLowlight } from 'lowlight'
import { suggestionOptions } from '@/ui-component/input/suggestionOption'
Expand All @@ -27,6 +28,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 +31 to +64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current markdown table serializer has two issues:

  1. If a table cell contains a pipe character (|), it will break the markdown table structure. We should escape | as \| inside the cell text.
  2. If a table has no explicit header cells (hasHeader is 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 isHeader tracking and always treating the first row as the header row, which is the standard fallback for Markdown tables.

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
  1. When using a heuristic for detection, ensure a safe fallback mechanism is in place to correctly handle cases where the heuristic fails.


// Store
import { HIDE_CANVAS_DIALOG, SHOW_CANVAS_DIALOG } from '@/store/actions'

Expand Down Expand Up @@ -115,7 +151,16 @@ const extensions = (availableNodesForVariable, availableState, acceptNodeOutputA
StarterKit.configure({
codeBlock: false
}),
Markdown,
MarkdownTable,
TableRow,
TableHeader,
TableCell,
Markdown.configure({
markedOptions: {
gfm: true,
breaks: false
}
}),
CustomMention.configure({
HTMLAttributes: {
class: 'variable'
Expand Down
47 changes: 46 additions & 1 deletion packages/ui/src/ui-component/input/RichInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current markdown table serializer has two issues:

  1. If a table cell contains a pipe character (|), it will break the markdown table structure. We should escape | as \| inside the cell text.
  2. If a table has no explicit header cells (hasHeader is 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 isHeader tracking and always treating the first row as the header row, which is the standard fallback for Markdown tables.

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
  1. When using a heuristic for detection, ensure a safe fallback mechanism is in place to correctly handle cases where the heuristic fails.


// define your extension array
const extensions = (
availableNodesForVariable,
Expand All @@ -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'
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.