| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { readFileSync } from 'node:fs' |
| 3 | import { pathToFileURL } from 'node:url' |
| 4 | import os from 'os' |
| 5 | import path from 'path' |
| 6 | import { unzipSync } from 'fflate' |
| 7 | import type { HtmlToPptxSlide } from '@arcsin1/html2pptx' |
| 8 | |
| 9 | // 回归背景(用户反馈「标题两行间距过大」):html2pptx 把含 \n 的文本框拆成多个 |
| 10 | // <a:p> 时,会把整个文本框的 paragraphSpacingBefore/After(来自 DOM margin 的 |
| 11 | // 块级间距)写给每一个段落。PowerPoint 会把第二段的 spcBef 叠加到行距上 |
| 12 | // (实测 64pt 行高 + 24pt 段距 = 89.5pt),行距直接膨胀近一半。 |
| 13 | // 修复:spcBef 只写首段、spcAft 只写末段——\n 拆出的是同一块内的换行, |
| 14 | // 行间距离由 lnSpc 表达,块级 margin 已体现在文本框坐标里。 |
| 15 | |
| 16 | const html2pptxNode = (await import( |
| 17 | pathToFileURL(path.resolve('node_modules/@arcsin1/html2pptx/dist/node.js')).href |
| 18 | )) as { writeHtmlToPptx: (outputPath: string, document: unknown) => void } |
| 19 | |
| 20 | const writeSlide = (slide: HtmlToPptxSlide): string => { |
| 21 | const outPath = path.join(os.tmpdir(), `oh-my-ppt-para-spacing-${Date.now()}-${Math.random().toString(36).slice(2)}.pptx`) |
| 22 | html2pptxNode.writeHtmlToPptx(outPath, { title: '段落间距', slides: [slide] }) |
| 23 | return Buffer.from(unzipSync(readFileSync(outPath))['ppt/slides/slide1.xml']).toString('utf-8') |
| 24 | } |
| 25 | |
| 26 | describe('paragraph spacing survives the \\n paragraph split correctly', () => { |
| 27 | it('writes spcBef only to the first paragraph and spcAft only to the last (runs path)', () => { |
| 28 | const xml = writeSlide({ |
| 29 | texts: [ |
| 30 | { |
| 31 | text: '标题一行\n标题二行', |
| 32 | x: 1, |
| 33 | y: 1, |
| 34 | w: 9, |
| 35 | h: 2, |
| 36 | fontSize: 40, |
| 37 | fontFace: 'Noto Sans SC', |
| 38 | bold: true, |
| 39 | color: '0F172A', |
| 40 | paragraphSpacingBefore: 24, |
| 41 | paragraphSpacingAfter: 12, |
| 42 | runs: [ |
| 43 | { text: '标题一行\n', color: '0F172A', bold: true, fontSize: 40, fontFace: 'Noto Sans SC' }, |
| 44 | { text: '标题二行', color: '0F172A', bold: true, fontSize: 40, fontFace: 'Noto Sans SC' } |
| 45 | ] |
| 46 | } |
| 47 | ] |
| 48 | }) |
| 49 | |
| 50 | expect(xml.match(/<a:spcBef>/g)).toHaveLength(1) |
| 51 | expect(xml.match(/<a:spcAft>/g)).toHaveLength(1) |
| 52 | const paragraphs = xml.split('</a:p>') |
| 53 | const firstPara = paragraphs[0] |
| 54 | const lastPara = paragraphs[paragraphs.length - 2] |
| 55 | expect(firstPara).toContain('<a:spcBef>') |
| 56 | expect(firstPara).not.toContain('<a:spcAft>') |
| 57 | expect(lastPara).toContain('<a:spcAft>') |
| 58 | expect(lastPara).not.toContain('<a:spcBef>') |
| 59 | }) |
| 60 | |
| 61 | it('writes spcBef only to the first paragraph (plain text path)', () => { |
| 62 | const xml = writeSlide({ |
| 63 | texts: [ |
| 64 | { |
| 65 | text: 'A\nB\nC', |
| 66 | x: 1, |
| 67 | y: 1, |
| 68 | w: 4, |
| 69 | h: 2, |
| 70 | fontSize: 20, |
| 71 | color: '111827', |
| 72 | paragraphSpacingBefore: 18 |
| 73 | } |
| 74 | ] |
| 75 | }) |
| 76 | |
| 77 | expect(xml.match(/<a:spcBef>/g)).toHaveLength(1) |
| 78 | const paragraphs = xml.split('</a:p>') |
| 79 | expect(paragraphs[0]).toContain('<a:spcBef>') |
| 80 | expect(paragraphs[1]).not.toContain('<a:spcBef>') |
| 81 | expect(paragraphs[2]).not.toContain('<a:spcBef>') |
| 82 | }) |
| 83 | }) |
| 84 |