Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- routes/refinity.js +63 -46
routes/refinity.js
CHANGED
|
@@ -86,52 +86,69 @@ router.post('/track-changes-comments', async (req, res) => {
|
|
| 86 |
const authorName = String(req.body?.authorName || 'Refinity');
|
| 87 |
const authorInitials = String(req.body?.authorInitials || (authorName.split(/\s+/).map(s=>s[0]||'').join('').slice(0,3).toUpperCase()) || 'RF');
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
} catch (e) {
|
| 136 |
res.status(500).json({ error: 'Failed to generate document with comments' });
|
| 137 |
}
|
|
|
|
| 86 |
const authorName = String(req.body?.authorName || 'Refinity');
|
| 87 |
const authorInitials = String(req.body?.authorInitials || (authorName.split(/\s+/).map(s=>s[0]||'').join('').slice(0,3).toUpperCase()) || 'RF');
|
| 88 |
|
| 89 |
+
try {
|
| 90 |
+
const parts = diffWords(prev, current);
|
| 91 |
+
// Build paragraph runs and attach side comments for added/removed
|
| 92 |
+
let nextCommentId = 0;
|
| 93 |
+
const comments = new Comments();
|
| 94 |
+
const children = [];
|
| 95 |
+
parts.forEach((p) => {
|
| 96 |
+
const value = p.value || '';
|
| 97 |
+
if (!value) return;
|
| 98 |
+
if (p.added) {
|
| 99 |
+
const cid = nextCommentId++;
|
| 100 |
+
comments.create(cid, authorName, authorInitials, new Date(), [
|
| 101 |
+
new Paragraph({ children: [ new TextRun({ text: 'Added: ', bold: true }), new TextRun({ text: value }) ] })
|
| 102 |
+
]);
|
| 103 |
+
children.push(new CommentRangeStart(cid));
|
| 104 |
+
children.push(new TextRun({ text: value, underline: {}, color: '00A65A' }));
|
| 105 |
+
children.push(new CommentRangeEnd(cid));
|
| 106 |
+
children.push(new CommentReference(cid));
|
| 107 |
+
return;
|
| 108 |
+
}
|
| 109 |
+
if (p.removed) {
|
| 110 |
+
const cid = nextCommentId++;
|
| 111 |
+
comments.create(cid, authorName, authorInitials, new Date(), [
|
| 112 |
+
new Paragraph({ children: [ new TextRun({ text: 'Removed: ', bold: true }), new TextRun({ text: value }) ] })
|
| 113 |
+
]);
|
| 114 |
+
children.push(new CommentRangeStart(cid));
|
| 115 |
+
children.push(new TextRun({ text: value, strike: true, color: 'D14343' }));
|
| 116 |
+
children.push(new CommentRangeEnd(cid));
|
| 117 |
+
children.push(new CommentReference(cid));
|
| 118 |
+
return;
|
| 119 |
+
}
|
| 120 |
+
children.push(new TextRun({ text: value }));
|
| 121 |
+
});
|
| 122 |
+
|
| 123 |
+
const doc = new Document({
|
| 124 |
+
sections: [
|
| 125 |
+
{ properties: {}, children: [ new Paragraph({ children }) ] }
|
| 126 |
+
],
|
| 127 |
+
comments,
|
| 128 |
+
});
|
| 129 |
+
const buffer = await Packer.toBuffer(doc);
|
| 130 |
+
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
| 131 |
+
res.setHeader('Content-Disposition', `attachment; filename="${outName}"`);
|
| 132 |
+
res.send(Buffer.from(buffer));
|
| 133 |
+
return;
|
| 134 |
+
} catch (e1) {
|
| 135 |
+
// Fallback: simple in-line diff without comments to avoid 500
|
| 136 |
+
const parts = diffWords(prev, current);
|
| 137 |
+
const runs = [];
|
| 138 |
+
parts.forEach(p => {
|
| 139 |
+
const value = p.value || '';
|
| 140 |
+
if (!value) return;
|
| 141 |
+
if (p.added) runs.push(new TextRun({ text: value, color: '00A65A' }));
|
| 142 |
+
else if (p.removed) runs.push(new TextRun({ text: value, color: 'D14343', strike: true }));
|
| 143 |
+
else runs.push(new TextRun({ text: value }));
|
| 144 |
+
});
|
| 145 |
+
const doc = new Document({ sections: [ { properties: {}, children: [ new Paragraph({ children: runs }) ] } ] });
|
| 146 |
+
const buffer = await Packer.toBuffer(doc);
|
| 147 |
+
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
| 148 |
+
res.setHeader('Content-Disposition', `attachment; filename="${outName}"`);
|
| 149 |
+
res.send(Buffer.from(buffer));
|
| 150 |
+
return;
|
| 151 |
+
}
|
| 152 |
} catch (e) {
|
| 153 |
res.status(500).json({ error: 'Failed to generate document with comments' });
|
| 154 |
}
|