Skip to content

Commit 99e5219

Browse files
committed
fix(text): varying styles for text runs
1 parent 067504e commit 99e5219

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

__tests__/replace-multi-text-html.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ test('create presentation, replace multi text from HTML string.', async () => {
88

99
const pres = automizer.loadRoot(`RootTemplate.pptx`).load(`TextReplace.pptx`);
1010

11-
const html =
12-
'<html><body>\n' +
13-
'<p><span style="font-size: 24px;">Testing layouts and exporting them.</span></p>\n' +
11+
const html = '<html><body><p>First Line 14pt</p>\n' +
12+
'<p><span style="font-size: 12px;">2nd line 12pt <strong>bold</strong> <em>italics</em></span></p>\n' +
1413
'<ul>\n' +
15-
'<li>level 1 - 1</li>\n' +
16-
'<li>level 1 - 2</li>\n' +
14+
'<li><span style="font-size: 14px;">bullet 1 level 1</span></li>\n' +
1715
'<ul>\n' +
18-
'<li>level 1-2-1 <em>italics</em></li>\n' +
16+
'<li><span style="font-size: 14px;">bullet 1 level 2</span></li>\n' +
1917
'</ul>\n' +
20-
'<li>level 1 - 3</li>\n' +
18+
'<li><span style="font-size: 14px;">bullet 2 level 1</span></li>\n' +
2119
'<ul>\n' +
22-
'<li>level 1 - 3 - 1</li>\n' +
20+
'<li><span style="font-size: 14px;">bullet 2 level 2</span></li>\n' +
21+
'<ul>\n' +
22+
'<li><span style="font-size: 14px;">bullet 2 level 3</span></li>\n' +
23+
'</ul>\n' +
24+
'<li><span style="font-size: 14px;">bullet 2 level 2</span></li>\n' +
25+
'<li><span style="font-size: 14px;"><ins>bullet</ins> <em>mixed</em> <strong><em>formatting</em></strong></span></li>\n' +
2326
'</ul>\n' +
2427
'</ul>\n' +
25-
'<p>Testing testing testing</p>\n' +
26-
'<p><strong>bold text</strong></p>\n' +
27-
'</body></html>\n';
28+
'<p><span style="font-size: 14px;"><strong><em>Text </em></strong>after bullet list</span></p></body></html>\n'
2829

2930
await pres
3031
.addSlide('TextReplace.pptx', 1, (slide) => {

src/helper/html-to-multitext-helper.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ export class HtmlToMultiTextHelper {
134134
return;
135135
}
136136

137-
const run = this.processTextNode(child);
138-
if (run.text) {
139-
textRuns.push(run);
137+
const result = this.processTextNode(child);
138+
if (Array.isArray(result)) {
139+
textRuns.push(...result.filter((run) => run.text));
140+
} else if (result.text) {
141+
textRuns.push(result);
140142
}
141143
});
142144

@@ -176,22 +178,31 @@ export class HtmlToMultiTextHelper {
176178
// Process all child nodes to create text runs
177179
// Using Array.from to convert NodeList to array that has forEach
178180
Array.from(node.childNodes).forEach((child) => {
179-
const run = this.processTextNode(child);
180-
if (run.text) {
181-
textRuns.push(run);
181+
const result = this.processTextNode(child);
182+
if (Array.isArray(result)) {
183+
textRuns.push(...result.filter((run) => run.text));
184+
} else if (result.text) {
185+
textRuns.push(result);
182186
}
183187
});
184188

185189
return textRuns;
186190
}
187191

188192
/**
189-
* Processes a text node and creates a TextRun
193+
* Processes a text node and creates a TextRun or array of TextRuns
190194
*/
191-
private processTextNode(node: ChildNode, style: TextStyle = {}): TextRun {
195+
private processTextNode(
196+
node: ChildNode,
197+
style: TextStyle = {},
198+
): TextRun | TextRun[] {
192199
// If this is a text node, return its content
193200
if (node.nodeType === Node.TEXT_NODE) {
194-
return { text: node.textContent || '', style };
201+
const text = node.textContent || '';
202+
if (text.trim() === '') {
203+
return { text: text, style };
204+
}
205+
return { text, style };
195206
}
196207

197208
// If this is an element, handle specific styling
@@ -260,25 +271,20 @@ export class HtmlToMultiTextHelper {
260271
private processElementWithChildren(
261272
element: Element,
262273
style: TextStyle,
263-
): TextRun {
274+
): TextRun[] {
264275
const runs: TextRun[] = [];
265276

266277
Array.from(element.childNodes).forEach((child) => {
267278
const childRun = this.processTextNode(child, style);
268-
if (childRun.text) {
279+
if (Array.isArray(childRun)) {
280+
// If the result is an array of runs, add them all
281+
runs.push(...childRun.filter((run) => run.text));
282+
} else if (childRun.text) {
283+
// If it's a single run, add it
269284
runs.push(childRun);
270285
}
271286
});
272287

273-
// If we have a single run, return it directly
274-
if (runs.length === 1) {
275-
return runs[0];
276-
}
277-
278-
// If we have multiple runs, concatenate the text
279-
return {
280-
text: runs.map((run) => run.text).join(''),
281-
style,
282-
};
288+
return runs;
283289
}
284290
}

0 commit comments

Comments
 (0)