|
| 1 | +/** @jsx jsx */ |
| 2 | + |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +import { cleanDocx } from '@platejs/docx'; |
| 7 | +import type { SlatePlugin, TNode, Value } from 'platejs'; |
| 8 | +import { createSlateEditor } from 'platejs'; |
| 9 | +import { serializeHtml } from 'platejs/static'; |
| 10 | +import { jsx } from '@platejs/test-utils'; |
| 11 | +import mammoth from 'mammoth'; |
| 12 | +import { BaseEditorKit } from 'www/src/registry/components/editor/editor-base-kit'; |
| 13 | +import { DocxExportKit } from 'www/src/registry/components/editor/plugins/docx-export-kit'; |
| 14 | + |
| 15 | +import { htmlToDocxBlob } from '../html-to-docx'; |
| 16 | +import { preprocessMammothHtml } from '../preprocessMammothHtml'; |
| 17 | + |
| 18 | +jsx; |
| 19 | + |
| 20 | +const editorPlugins = [...BaseEditorKit, ...DocxExportKit] as SlatePlugin[]; |
| 21 | + |
| 22 | +const createTestEditor = (value?: Value) => |
| 23 | + createSlateEditor({ |
| 24 | + plugins: editorPlugins, |
| 25 | + value, |
| 26 | + }); |
| 27 | + |
| 28 | +const readDocxFixture = (filename: string): Buffer => { |
| 29 | + const docxTestDir = path.resolve( |
| 30 | + __dirname, |
| 31 | + '../../../../docx/src/lib/__tests__' |
| 32 | + ); |
| 33 | + const filepath = path.join(docxTestDir, `${filename}.docx`); |
| 34 | + |
| 35 | + return fs.readFileSync(filepath); |
| 36 | +}; |
| 37 | + |
| 38 | +const importDocxBuffer = async ( |
| 39 | + editor: ReturnType<typeof createTestEditor>, |
| 40 | + buffer: Buffer |
| 41 | +): Promise<TNode[]> => { |
| 42 | + const mammothResult = await mammoth.convertToHtml( |
| 43 | + { buffer }, |
| 44 | + { styleMap: ['comment-reference => sup'] } |
| 45 | + ); |
| 46 | + |
| 47 | + const { html: preprocessedHtml } = preprocessMammothHtml(mammothResult.value); |
| 48 | + const cleanedHtml = cleanDocx(preprocessedHtml, ''); |
| 49 | + |
| 50 | + const doc = new DOMParser().parseFromString(cleanedHtml, 'text/html'); |
| 51 | + |
| 52 | + return editor.api.html.deserialize({ element: doc.body }) as TNode[]; |
| 53 | +}; |
| 54 | + |
| 55 | +const exportNodesToDocx = async (nodes: TNode[]): Promise<Buffer> => { |
| 56 | + // Create a static editor for serialization with registered components |
| 57 | + const staticEditor = createTestEditor(nodes as Value); |
| 58 | + |
| 59 | + // Serialize nodes to HTML using platejs/static |
| 60 | + const html = await serializeHtml(staticEditor); |
| 61 | + |
| 62 | + // Convert HTML to DOCX blob |
| 63 | + const blob = await htmlToDocxBlob(html); |
| 64 | + |
| 65 | + // Convert Blob to Buffer for reimport |
| 66 | + const arrayBuffer = await blob.arrayBuffer(); |
| 67 | + |
| 68 | + return Buffer.from(arrayBuffer); |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Roundtrip test: import → export → reimport |
| 73 | + * |
| 74 | + * Verifies that export doesn't lose data that import can parse. |
| 75 | + * B === D means the roundtrip preserves data. |
| 76 | + * |
| 77 | + * Original .docx → import → Plate nodes (B) → export → new .docx → reimport → Plate nodes (D) |
| 78 | + * |
| 79 | + * Known limitations in roundtrip: |
| 80 | + * - inline_formatting: loses some marks due to HTML serialization/deserialization |
| 81 | + * - Line breaks (\n) may be converted to spaces |
| 82 | + */ |
| 83 | +describe('docx roundtrip', () => { |
| 84 | + // Fixtures that pass full roundtrip (B === D) |
| 85 | + // Note: 'links' has minor URL normalization (trailing slash) so tested separately |
| 86 | + const roundtripFixtures = ['headers', 'block_quotes', 'tables']; |
| 87 | + |
| 88 | + roundtripFixtures.forEach((name) => { |
| 89 | + it(`should preserve data for ${name}`, async () => { |
| 90 | + const editor = createTestEditor(); |
| 91 | + |
| 92 | + // 1. Import original .docx |
| 93 | + const buffer = readDocxFixture(name); |
| 94 | + const nodesB = await importDocxBuffer(editor, buffer); |
| 95 | + |
| 96 | + // 2. Export to new .docx |
| 97 | + const exportedBuffer = await exportNodesToDocx(nodesB); |
| 98 | + |
| 99 | + // 3. Reimport the exported .docx |
| 100 | + const nodesD = await importDocxBuffer(editor, exportedBuffer); |
| 101 | + |
| 102 | + // 4. Compare - should be identical |
| 103 | + expect(nodesD).toEqual(nodesB); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + // Test links - passes but with minor URL normalization (trailing slash added) |
| 108 | + it('should preserve data for links (with URL normalization)', async () => { |
| 109 | + const editor = createTestEditor(); |
| 110 | + |
| 111 | + const buffer = readDocxFixture('links'); |
| 112 | + const nodesB = await importDocxBuffer(editor, buffer); |
| 113 | + const exportedBuffer = await exportNodesToDocx(nodesB); |
| 114 | + const nodesD = await importDocxBuffer(editor, exportedBuffer); |
| 115 | + |
| 116 | + // Normalize URLs for comparison (add trailing slash to domain-only URLs) |
| 117 | + const normalizeUrls = (nodes: TNode[]) => |
| 118 | + JSON.parse( |
| 119 | + JSON.stringify(nodes).replaceAll( |
| 120 | + /"url":"(https?:\/\/[^"/]+)"/g, |
| 121 | + '"url":"$1/"' |
| 122 | + ) |
| 123 | + ); |
| 124 | + |
| 125 | + expect(normalizeUrls(nodesD)).toEqual(normalizeUrls(nodesB)); |
| 126 | + }); |
| 127 | + |
| 128 | + // Test that inline_formatting can be exported and reimported (with known data loss) |
| 129 | + it('should export and reimport inline_formatting (with known loss)', async () => { |
| 130 | + const editor = createTestEditor(); |
| 131 | + |
| 132 | + // 1. Import original .docx |
| 133 | + const buffer = readDocxFixture('inline_formatting'); |
| 134 | + const nodesB = await importDocxBuffer(editor, buffer); |
| 135 | + |
| 136 | + // 2. Export to new .docx |
| 137 | + const exportedBuffer = await exportNodesToDocx(nodesB); |
| 138 | + |
| 139 | + // 3. Reimport the exported .docx - should not throw |
| 140 | + const nodesD = await importDocxBuffer(editor, exportedBuffer); |
| 141 | + |
| 142 | + // 4. Verify we got some content back (not empty) |
| 143 | + expect(nodesD.length).toBeGreaterThan(0); |
| 144 | + // Note: nodesD won't equal nodesB due to mark/linebreak loss |
| 145 | + }); |
| 146 | +}); |
0 commit comments