lewtun HF Staff commited on
Commit
8a6f5fc
·
1 Parent(s): 77bdec5

Export code blocks properly in docx

Browse files
Files changed (1) hide show
  1. app/scripts/export-docx.mjs +34 -12
app/scripts/export-docx.mjs CHANGED
@@ -14,7 +14,7 @@
14
  * npm run export:docx
15
  */
16
 
17
- import { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType } from 'docx';
18
  import { promises as fs } from 'node:fs';
19
  import { resolve } from 'node:path';
20
  import process from 'node:process';
@@ -93,6 +93,21 @@ function parseInlineFormatting(text) {
93
  return runs.length > 0 ? runs : [new TextRun(text)];
94
  }
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  async function convertTxtToDocx(txtPath, outputPath) {
97
  console.log(`📖 Reading TXT file: ${txtPath}`);
98
  const content = await fs.readFile(txtPath, 'utf-8');
@@ -112,12 +127,25 @@ async function convertTxtToDocx(txtPath, outputPath) {
112
  }
113
 
114
  // Handle code blocks <c>...</c>
115
- if (line.trim().startsWith('<c>')) {
116
  inCodeBlock = true;
117
  codeLines = [];
118
- const firstLine = line.replace(/^<c>\s*/, '');
119
- if (firstLine && !firstLine.startsWith('</c>')) {
120
- codeLines.push(firstLine);
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  }
122
  continue;
123
  }
@@ -128,13 +156,7 @@ async function convertTxtToDocx(txtPath, outputPath) {
128
 
129
  // Add code block as paragraph(s)
130
  if (codeLines.length > 0) {
131
- paragraphs.push(new Paragraph({
132
- text: codeLines.join('\n'),
133
- font: 'Courier New',
134
- size: 20,
135
- shading: { fill: 'F5F5F5', type: 'clear' },
136
- spacing: { before: 200, after: 200 }
137
- }));
138
  }
139
 
140
  inCodeBlock = false;
 
14
  * npm run export:docx
15
  */
16
 
17
+ import { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType, LineRuleType } from 'docx';
18
  import { promises as fs } from 'node:fs';
19
  import { resolve } from 'node:path';
20
  import process from 'node:process';
 
93
  return runs.length > 0 ? runs : [new TextRun(text)];
94
  }
95
 
96
+ function codeBlockToParagraph(codeLines) {
97
+ const codeRuns = codeLines.map((codeLine, idx) => new TextRun({
98
+ break: idx === 0 ? 0 : 1,
99
+ children: [codeLine],
100
+ font: 'Courier New',
101
+ size: 20
102
+ }));
103
+
104
+ return new Paragraph({
105
+ children: codeRuns,
106
+ shading: { fill: 'F5F5F5', type: 'clear' },
107
+ spacing: { before: 200, after: 200, line: 240, lineRule: LineRuleType.AUTO }
108
+ });
109
+ }
110
+
111
  async function convertTxtToDocx(txtPath, outputPath) {
112
  console.log(`📖 Reading TXT file: ${txtPath}`);
113
  const content = await fs.readFile(txtPath, 'utf-8');
 
127
  }
128
 
129
  // Handle code blocks <c>...</c>
130
+ if (/^\s*<c>/.test(line)) {
131
  inCodeBlock = true;
132
  codeLines = [];
133
+
134
+ const firstLine = line.replace(/^\s*<c>/, '');
135
+ if (firstLine) {
136
+ // Handle single-line code blocks like: <c>code...</c>
137
+ if (firstLine.trim().endsWith('</c>')) {
138
+ const singleLine = firstLine.replace(/<\/c>\s*$/, '');
139
+ codeLines.push(singleLine);
140
+ paragraphs.push(codeBlockToParagraph(codeLines));
141
+ inCodeBlock = false;
142
+ codeLines = [];
143
+ continue;
144
+ }
145
+
146
+ if (!firstLine.startsWith('</c>')) {
147
+ codeLines.push(firstLine);
148
+ }
149
  }
150
  continue;
151
  }
 
156
 
157
  // Add code block as paragraph(s)
158
  if (codeLines.length > 0) {
159
+ paragraphs.push(codeBlockToParagraph(codeLines));
 
 
 
 
 
 
160
  }
161
 
162
  inCodeBlock = false;