Skip to content

Commit 726eac6

Browse files
committed
fix(files): destroy round-trip probe editor on serialization error
Wrap the probe serialize() in try/finally so the throwaway Editor is always destroyed even if setContent/getMarkdown throws (addresses Greptile). Adds a test proving PipeSafeTable escapes only interior cell pipes, not structural delimiters.
1 parent 672b297 commit 726eac6

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/round-trip-safety.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ function stripCode(content: string): string {
6060
function serialize(content: string): string {
6161
const { frontmatter, body } = splitFrontmatter(content)
6262
const editor = new Editor({ extensions: createMarkdownContentExtensions() })
63-
editor.commands.setContent(body, { contentType: 'markdown' })
64-
const out = applyFrontmatter(frontmatter, postProcessSerializedMarkdown(editor.getMarkdown()))
65-
editor.destroy()
66-
return out
63+
try {
64+
editor.commands.setContent(body, { contentType: 'markdown' })
65+
return applyFrontmatter(frontmatter, postProcessSerializedMarkdown(editor.getMarkdown()))
66+
} finally {
67+
editor.destroy()
68+
}
6769
}
6870

6971
/**

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/round-trip.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ describe('editor markdown round-trip', () => {
206206
expect(out).toContain('| --- |')
207207
})
208208

209+
it('escapes only interior cell pipes, not the structural delimiters', () => {
210+
const out = roundTrip('| a | b |\n| --- | --- |\n| one \\| two | three |')
211+
expect(out).toContain('one \\| two')
212+
expect(out).toContain('| three |')
213+
// Every row keeps exactly its two structural columns (3 pipes per line).
214+
for (const line of out.trim().split('\n')) {
215+
expect((line.match(/(?<!\\)\|/g) ?? []).length).toBe(3)
216+
}
217+
expect(roundTrip(out)).toBe(out)
218+
})
219+
209220
it('combines strikethrough with inline code (relaxed code mark)', () => {
210221
expect(roundTrip('~~`x`~~')).toContain('~~`x`~~')
211222
expect(roundTrip('# ~~`x`~~')).toContain('# ~~`x`~~')

0 commit comments

Comments
 (0)