22import fs from 'fs/promises' ;
33import express from 'express' ;
44import { JSDOM } from 'jsdom' ;
5+ import documentBlob from './document.js' ;
56
67// In Node, we use the Editor class directly from superdoc/super-editor
78import { Editor , getStarterExtensions } from '@harbour-enterprises/superdoc/super-editor' ;
@@ -17,23 +18,26 @@ const server = express();
1718 * If no param is passed, the document will be returned as as-is (blank template with header and footer).
1819 */
1920server . get ( '/' , async ( req , res , next ) => {
20- // Load our example document - a blank template with a header and footer
21- let documentData = await fs . readFile ( './sample-document.docx' ) ;
22-
23- // Get the text and html from the query parameters
2421 const { text, html } = req . query ;
2522
26- // If we have text or html, we will to load the editor and insert the content
27- if ( text || html ) {
28- const editor = await getEditor ( documentData ) ;
23+ // Load the specified document:
24+ // if using stackblitz:
25+ let documentData = documentBlob ;
26+ const arrayBuffer = await documentData . arrayBuffer ( ) ;
27+ documentData = Buffer . from ( arrayBuffer ) ;
2928
30- if ( text ) editor . commands . insertContent ( text ) ;
31- if ( html ) editor . commands . insertContent ( html ) ;
29+ // otherwise, you can read from disk:
30+ // let documentData = await fs.readFile(`./sample-document.docx`);
31+
32+ const editor = await getEditor ( documentData ) ;
33+
34+ // If we have text or html, we will to load the editor and insert the content
35+ if ( text ) editor . commands . insertContent ( text ) ;
36+ if ( html ) editor . commands . insertContent ( html ) ;
3237
33- // Export the docx and create a buffer to return to the user
34- const zipBuffer = await editor . exportDocx ( ) ;
35- documentData = Buffer . from ( zipBuffer ) ;
36- }
38+ // Export the docx and create a buffer to return to the user
39+ const zipBuffer = await editor . exportDocx ( ) ;
40+ documentData = Buffer . from ( zipBuffer ) ;
3741
3842 // Download the file
3943 res
0 commit comments