linguabot commited on
Commit
dfc9deb
·
verified ·
1 Parent(s): 6336297

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. 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
- const parts = diffWords(prev, current);
90
-
91
- // Build paragraph runs and attach side comments for added/removed
92
- let nextCommentId = 0;
93
- const comments = new Comments();
94
-
95
- const children = [];
96
- parts.forEach((p) => {
97
- const value = p.value || '';
98
- if (!value) return;
99
- if (p.added) {
100
- const cid = nextCommentId++;
101
- comments.create(cid, authorName, authorInitials, new Date(), [
102
- new Paragraph({ children: [ new TextRun({ text: 'Added: ', bold: true }), new TextRun({ text: value }) ] })
103
- ]);
104
- children.push(new CommentRangeStart(cid));
105
- children.push(new TextRun({ text: value, underline: {}, color: '00A65A' }));
106
- children.push(new CommentRangeEnd(cid));
107
- children.push(new CommentReference(cid));
108
- return;
109
- }
110
- if (p.removed) {
111
- const cid = nextCommentId++;
112
- comments.create(cid, authorName, authorInitials, new Date(), [
113
- new Paragraph({ children: [ new TextRun({ text: 'Removed: ', bold: true }), new TextRun({ text: value }) ] })
114
- ]);
115
- children.push(new CommentRangeStart(cid));
116
- children.push(new TextRun({ text: value, strike: true, color: 'D14343' }));
117
- children.push(new CommentRangeEnd(cid));
118
- children.push(new CommentReference(cid));
119
- return;
120
- }
121
- children.push(new TextRun({ text: value }));
122
- });
123
-
124
- const doc = new Document({
125
- sections: [
126
- { properties: {}, children: [ new Paragraph({ children }) ] }
127
- ],
128
- comments,
129
- });
130
-
131
- const buffer = await Packer.toBuffer(doc);
132
- res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
133
- res.setHeader('Content-Disposition', `attachment; filename="${outName}"`);
134
- res.send(Buffer.from(buffer));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  }