Skip to content
Merged
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
2 changes: 1 addition & 1 deletion examples/nodejs-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HTML only: http://localhost:3000?html=<p>I am a paragraph</p><p></p><p><strong>I
```

## Additional docs
Please see [SuperDoc docs](https://docs.superdoc.dev/components/#supereditor) for additinoal editor commands and hooks.
Please see [SuperDoc docs](https://docs.superdoc.dev/guide/components#superdoc) for additinoal editor commands and hooks.

You can get a list of all available editor commands from editor.commands as well. For instnace, commands such as the examples below all will work while using the SuperDoc editor in the backend:
```
Expand Down
1 change: 1 addition & 0 deletions examples/nodejs-example/document-b64.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions examples/nodejs-example/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import b64 from './document-b64.js';

const base64ToDocx = (base64String, filename) => {
try {
const binaryString = atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}

const blob = new Blob([bytes], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});

const file = new File([blob], filename, {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});

return file;
} catch (error) {
console.error('Error converting base64 to DOCX:', error);
}
};

export default base64ToDocx(b64, 'sample-document.docx');
30 changes: 17 additions & 13 deletions examples/nodejs-example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fs from 'fs/promises';
import express from 'express';
import { JSDOM } from 'jsdom';
import documentBlob from './document.js';

// In Node, we use the Editor class directly from superdoc/super-editor
import { Editor, getStarterExtensions } from '@harbour-enterprises/superdoc/super-editor';
Expand All @@ -17,23 +18,26 @@ const server = express();
* If no param is passed, the document will be returned as as-is (blank template with header and footer).
*/
server.get('/', async (req, res, next) => {
// Load our example document - a blank template with a header and footer
let documentData = await fs.readFile('./sample-document.docx');

// Get the text and html from the query parameters
const { text, html } = req.query;

// If we have text or html, we will to load the editor and insert the content
if (text || html) {
const editor = await getEditor(documentData);
// Load the specified document:
// if using stackblitz:
let documentData = documentBlob;
const arrayBuffer = await documentData.arrayBuffer();
documentData = Buffer.from(arrayBuffer);

if (text) editor.commands.insertContent(text);
if (html) editor.commands.insertContent(html);
// otherwise, you can read from disk:
// let documentData = await fs.readFile(`./sample-document.docx`);

const editor = await getEditor(documentData);

// If we have text or html, we will to load the editor and insert the content
if (text) editor.commands.insertContent(text);
if (html) editor.commands.insertContent(html);

// Export the docx and create a buffer to return to the user
const zipBuffer = await editor.exportDocx();
documentData = Buffer.from(zipBuffer);
}
// Export the docx and create a buffer to return to the user
const zipBuffer = await editor.exportDocx();
documentData = Buffer.from(zipBuffer);

// Download the file
res
Expand Down
Loading