kordoc 2.2.1 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -234,20 +234,14 @@ declare function diffBlocks(blocksA: IRBlock[], blocksB: IRBlock[]): DiffResult;
234
234
  declare function extractFormFields(blocks: IRBlock[]): FormResult;
235
235
 
236
236
  /**
237
- * Markdown → HWPX 역변환 (MVP)
237
+ * Markdown → HWPX 역변환
238
238
  *
239
- * 지원: 단락, 헤딩, 테이블 (텍스트+구조만, 스타일 없음)
239
+ * 지원: 헤딩(h1~h6), 단락, 볼드, 이탤릭, 인라인코드, 코드블록,
240
+ * 순서/비순서 리스트, 수평선, 인용문, 테이블
240
241
  * jszip으로 HWPX ZIP 패키징.
241
242
  */
242
243
  /**
243
244
  * 마크다운 텍스트를 HWPX (ArrayBuffer)로 변환.
244
- *
245
- * @example
246
- * ```ts
247
- * import { markdownToHwpx } from "kordoc"
248
- * const hwpxBuffer = await markdownToHwpx("# 제목\n\n본문 텍스트")
249
- * writeFileSync("output.hwpx", Buffer.from(hwpxBuffer))
250
- * ```
251
245
  */
252
246
  declare function markdownToHwpx(markdown: string): Promise<ArrayBuffer>;
253
247
 
package/dist/index.d.ts CHANGED
@@ -234,20 +234,14 @@ declare function diffBlocks(blocksA: IRBlock[], blocksB: IRBlock[]): DiffResult;
234
234
  declare function extractFormFields(blocks: IRBlock[]): FormResult;
235
235
 
236
236
  /**
237
- * Markdown → HWPX 역변환 (MVP)
237
+ * Markdown → HWPX 역변환
238
238
  *
239
- * 지원: 단락, 헤딩, 테이블 (텍스트+구조만, 스타일 없음)
239
+ * 지원: 헤딩(h1~h6), 단락, 볼드, 이탤릭, 인라인코드, 코드블록,
240
+ * 순서/비순서 리스트, 수평선, 인용문, 테이블
240
241
  * jszip으로 HWPX ZIP 패키징.
241
242
  */
242
243
  /**
243
244
  * 마크다운 텍스트를 HWPX (ArrayBuffer)로 변환.
244
- *
245
- * @example
246
- * ```ts
247
- * import { markdownToHwpx } from "kordoc"
248
- * const hwpxBuffer = await markdownToHwpx("# 제목\n\n본문 텍스트")
249
- * writeFileSync("output.hwpx", Buffer.from(hwpxBuffer))
250
- * ```
251
245
  */
252
246
  declare function markdownToHwpx(markdown: string): Promise<ArrayBuffer>;
253
247
 
package/dist/index.js CHANGED
@@ -139,7 +139,7 @@ import { inflateRawSync } from "zlib";
139
139
  import { DOMParser } from "@xmldom/xmldom";
140
140
 
141
141
  // src/utils.ts
142
- var VERSION = true ? "2.2.1" : "0.0.0-dev";
142
+ var VERSION = true ? "2.2.2" : "0.0.0-dev";
143
143
  function toArrayBuffer(buf) {
144
144
  if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
145
145
  return buf.buffer;
@@ -6261,6 +6261,23 @@ var NS_HEAD = "http://www.hancom.co.kr/hwpml/2011/head";
6261
6261
  var NS_OPF = "http://www.idpf.org/2007/opf/";
6262
6262
  var NS_HPF = "http://www.hancom.co.kr/schema/2011/hpf";
6263
6263
  var NS_OCF = "urn:oasis:names:tc:opendocument:xmlns:container";
6264
+ var CHAR_NORMAL = 0;
6265
+ var CHAR_BOLD = 1;
6266
+ var CHAR_ITALIC = 2;
6267
+ var CHAR_BOLD_ITALIC = 3;
6268
+ var CHAR_CODE = 4;
6269
+ var CHAR_H1 = 5;
6270
+ var CHAR_H2 = 6;
6271
+ var CHAR_H3 = 7;
6272
+ var CHAR_H4 = 8;
6273
+ var PARA_NORMAL = 0;
6274
+ var PARA_H1 = 1;
6275
+ var PARA_H2 = 2;
6276
+ var PARA_H3 = 3;
6277
+ var PARA_H4 = 4;
6278
+ var PARA_CODE = 5;
6279
+ var PARA_QUOTE = 6;
6280
+ var PARA_LIST = 7;
6264
6281
  async function markdownToHwpx(markdown) {
6265
6282
  const blocks = parseMarkdownToBlocks(markdown);
6266
6283
  const sectionXml = blocksToSectionXml(blocks);
@@ -6282,6 +6299,25 @@ function parseMarkdownToBlocks(md) {
6282
6299
  i++;
6283
6300
  continue;
6284
6301
  }
6302
+ const fenceMatch = line.match(/^(`{3,}|~{3,})(.*)$/);
6303
+ if (fenceMatch) {
6304
+ const fence = fenceMatch[1];
6305
+ const lang = fenceMatch[2].trim();
6306
+ const codeLines = [];
6307
+ i++;
6308
+ while (i < lines.length && !lines[i].startsWith(fence)) {
6309
+ codeLines.push(lines[i]);
6310
+ i++;
6311
+ }
6312
+ if (i < lines.length) i++;
6313
+ blocks.push({ type: "code_block", text: codeLines.join("\n"), lang });
6314
+ continue;
6315
+ }
6316
+ if (/^(\*{3,}|-{3,}|_{3,})\s*$/.test(line.trim())) {
6317
+ blocks.push({ type: "hr" });
6318
+ i++;
6319
+ continue;
6320
+ }
6285
6321
  const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
6286
6322
  if (headingMatch) {
6287
6323
  blocks.push({ type: "heading", text: headingMatch[2].trim(), level: headingMatch[1].length });
@@ -6300,19 +6336,101 @@ function parseMarkdownToBlocks(md) {
6300
6336
  if (cells.length > 0) tableRows.push(cells);
6301
6337
  i++;
6302
6338
  }
6303
- if (tableRows.length > 0) {
6304
- blocks.push({ type: "table", rows: tableRows });
6339
+ if (tableRows.length > 0) blocks.push({ type: "table", rows: tableRows });
6340
+ continue;
6341
+ }
6342
+ if (line.trimStart().startsWith("> ")) {
6343
+ const quoteLines = [];
6344
+ while (i < lines.length && (lines[i].trimStart().startsWith("> ") || lines[i].trimStart().startsWith(">"))) {
6345
+ quoteLines.push(lines[i].replace(/^>\s?/, ""));
6346
+ i++;
6347
+ }
6348
+ for (const ql of quoteLines) {
6349
+ blocks.push({ type: "blockquote", text: ql.trim() || "" });
6305
6350
  }
6306
6351
  continue;
6307
6352
  }
6353
+ const listMatch = line.match(/^(\s*)([-*+]|\d+[.)]) (.+)$/);
6354
+ if (listMatch) {
6355
+ const indent = Math.floor(listMatch[1].length / 2);
6356
+ const ordered = /\d/.test(listMatch[2]);
6357
+ blocks.push({ type: "list_item", text: listMatch[3].trim(), ordered, indent });
6358
+ i++;
6359
+ continue;
6360
+ }
6308
6361
  blocks.push({ type: "paragraph", text: line.trim() });
6309
6362
  i++;
6310
6363
  }
6311
6364
  return blocks;
6312
6365
  }
6366
+ function parseInlineMarkdown(text) {
6367
+ text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1");
6368
+ text = text.replace(/\[([^\]]*)\]\(([^)]*)\)/g, (_, t, u) => t || u);
6369
+ text = text.replace(/~~([^~]+)~~/g, "$1");
6370
+ const spans = [];
6371
+ const regex = /(`[^`]+`|\*{3}[^*]+\*{3}|\*{2}[^*]+\*{2}|\*[^*]+\*|_{2}[^_]+_{2}|_[^_]+_)/g;
6372
+ let lastIdx = 0;
6373
+ for (const match of text.matchAll(regex)) {
6374
+ const idx = match.index;
6375
+ if (idx > lastIdx) {
6376
+ spans.push({ text: text.slice(lastIdx, idx), bold: false, italic: false, code: false });
6377
+ }
6378
+ const raw = match[0];
6379
+ if (raw.startsWith("`")) {
6380
+ spans.push({ text: raw.slice(1, -1), bold: false, italic: false, code: true });
6381
+ } else if (raw.startsWith("***") || raw.startsWith("___")) {
6382
+ spans.push({ text: raw.slice(3, -3), bold: true, italic: true, code: false });
6383
+ } else if (raw.startsWith("**") || raw.startsWith("__")) {
6384
+ spans.push({ text: raw.slice(2, -2), bold: true, italic: false, code: false });
6385
+ } else {
6386
+ spans.push({ text: raw.slice(1, -1), bold: false, italic: true, code: false });
6387
+ }
6388
+ lastIdx = idx + raw.length;
6389
+ }
6390
+ if (lastIdx < text.length) {
6391
+ spans.push({ text: text.slice(lastIdx), bold: false, italic: false, code: false });
6392
+ }
6393
+ if (spans.length === 0) {
6394
+ spans.push({ text, bold: false, italic: false, code: false });
6395
+ }
6396
+ return spans;
6397
+ }
6398
+ function spanToCharPrId(span) {
6399
+ if (span.code) return CHAR_CODE;
6400
+ if (span.bold && span.italic) return CHAR_BOLD_ITALIC;
6401
+ if (span.bold) return CHAR_BOLD;
6402
+ if (span.italic) return CHAR_ITALIC;
6403
+ return CHAR_NORMAL;
6404
+ }
6313
6405
  function escapeXml(text) {
6314
6406
  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
6315
6407
  }
6408
+ function generateRuns(text, defaultCharPr = CHAR_NORMAL) {
6409
+ const spans = parseInlineMarkdown(text);
6410
+ return spans.map((span) => {
6411
+ const charId = span.code || span.bold || span.italic ? spanToCharPrId(span) : defaultCharPr;
6412
+ return `<hp:run charPrIDRef="${charId}"><hp:t>${escapeXml(span.text)}</hp:t></hp:run>`;
6413
+ }).join("");
6414
+ }
6415
+ function generateParagraph(text, paraPrId = PARA_NORMAL, charPrId = CHAR_NORMAL) {
6416
+ if (paraPrId === PARA_CODE) {
6417
+ return `<hp:p paraPrIDRef="${paraPrId}" styleIDRef="0"><hp:run charPrIDRef="${CHAR_CODE}"><hp:t>${escapeXml(text)}</hp:t></hp:run></hp:p>`;
6418
+ }
6419
+ const runs = generateRuns(text, charPrId);
6420
+ return `<hp:p paraPrIDRef="${paraPrId}" styleIDRef="0">${runs}</hp:p>`;
6421
+ }
6422
+ function headingParaPrId(level) {
6423
+ if (level === 1) return PARA_H1;
6424
+ if (level === 2) return PARA_H2;
6425
+ if (level === 3) return PARA_H3;
6426
+ return PARA_H4;
6427
+ }
6428
+ function headingCharPrId(level) {
6429
+ if (level === 1) return CHAR_H1;
6430
+ if (level === 2) return CHAR_H2;
6431
+ if (level === 3) return CHAR_H3;
6432
+ return CHAR_H4;
6433
+ }
6316
6434
  function generateContainerXml() {
6317
6435
  return `<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
6318
6436
  <ocf:container xmlns:ocf="${NS_OCF}" xmlns:hpf="${NS_HPF}">
@@ -6334,21 +6452,50 @@ function generateManifest() {
6334
6452
  </opf:spine>
6335
6453
  </opf:package>`;
6336
6454
  }
6455
+ function charPr(id, height, bold, italic, fontId = 0) {
6456
+ const boldAttr = bold ? ` bold="1"` : "";
6457
+ const italicAttr = italic ? ` italic="1"` : "";
6458
+ return ` <hh:charPr id="${id}" height="${height}" textColor="#000000" shadeColor="none" useFontSpace="0" useKerning="0" symMark="NONE" borderFillIDRef="0"${boldAttr}${italicAttr}>
6459
+ <hh:fontRef hangul="${fontId}" latin="${fontId}" hanja="${fontId}" japanese="${fontId}" other="${fontId}" symbol="${fontId}" user="${fontId}"/>
6460
+ <hh:ratio hangul="100" latin="100" hanja="100" japanese="100" other="100" symbol="100" user="100"/>
6461
+ <hh:spacing hangul="0" latin="0" hanja="0" japanese="0" other="0" symbol="0" user="0"/>
6462
+ <hh:relSz hangul="100" latin="100" hanja="100" japanese="100" other="100" symbol="100" user="100"/>
6463
+ <hh:offset hangul="0" latin="0" hanja="0" japanese="0" other="0" symbol="0" user="0"/>
6464
+ </hh:charPr>`;
6465
+ }
6466
+ function paraPr(id, opts = {}) {
6467
+ const { align = "JUSTIFY", spaceBefore = 0, spaceAfter = 0, lineSpacing = 160, indent = 0 } = opts;
6468
+ return ` <hh:paraPr id="${id}" tabPrIDRef="0" condense="0" fontLineHeight="0" snapToGrid="1" suppressLineNumbers="0" checked="0" textDir="AUTO">
6469
+ <hh:align horizontal="${align}" vertical="BASELINE"/>
6470
+ <hh:heading type="NONE" idRef="0" level="0"/>
6471
+ <hh:breakSetting breakLatinWord="KEEP_WORD" breakNonLatinWord="BREAK_WORD" widowOrphan="0" keepWithNext="0" keepLines="0" pageBreakBefore="0" lineWrap="BREAK"/>
6472
+ <hh:autoSpacing eAsianEng="0" eAsianNum="0"/>
6473
+ <hh:margin indent="${indent}" left="0" right="0" prev="${spaceBefore}" next="${spaceAfter}"/>
6474
+ <hh:lineSpacing type="PERCENT" value="${lineSpacing}"/>
6475
+ <hh:border borderFillIDRef="0" offsetLeft="0" offsetRight="0" offsetTop="0" offsetBottom="0" connect="0" ignoreMargin="0"/>
6476
+ </hh:paraPr>`;
6477
+ }
6337
6478
  function generateHeaderXml() {
6338
6479
  return `<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
6339
6480
  <hh:head xmlns:hh="${NS_HEAD}" xmlns:hp="${NS_PARA}" version="1.4" secCnt="1">
6340
6481
  <hh:beginNum page="1" footnote="1" endnote="1" pic="1" tbl="1" equation="1"/>
6341
6482
  <hh:refList>
6342
6483
  <hh:fontfaces itemCnt="7">
6343
- <hh:fontface lang="HANGUL" fontCnt="1">
6484
+ <hh:fontface lang="HANGUL" fontCnt="2">
6344
6485
  <hh:font id="0" face="\uD568\uCD08\uB86C\uBC14\uD0D5" type="TTF" isEmbedded="0">
6345
6486
  <hh:typeInfo familyType="FCAT_GOTHIC" weight="6" proportion="4" contrast="0" strokeVariation="1" armStyle="1" letterform="1" midline="1" xHeight="1"/>
6346
6487
  </hh:font>
6488
+ <hh:font id="1" face="\uD568\uCD08\uB86C\uB3CB\uC6C0" type="TTF" isEmbedded="0">
6489
+ <hh:typeInfo familyType="FCAT_GOTHIC" weight="6" proportion="4" contrast="0" strokeVariation="1" armStyle="1" letterform="1" midline="1" xHeight="1"/>
6490
+ </hh:font>
6347
6491
  </hh:fontface>
6348
- <hh:fontface lang="LATIN" fontCnt="1">
6492
+ <hh:fontface lang="LATIN" fontCnt="2">
6349
6493
  <hh:font id="0" face="Times New Roman" type="TTF" isEmbedded="0">
6350
6494
  <hh:typeInfo familyType="FCAT_OLDSTYLE" weight="5" proportion="4" contrast="2" strokeVariation="0" armStyle="0" letterform="0" midline="0" xHeight="4"/>
6351
6495
  </hh:font>
6496
+ <hh:font id="1" face="Consolas" type="TTF" isEmbedded="0">
6497
+ <hh:typeInfo familyType="FCAT_MODERN" weight="5" proportion="0" contrast="0" strokeVariation="0" armStyle="0" letterform="0" midline="0" xHeight="0"/>
6498
+ </hh:font>
6352
6499
  </hh:fontface>
6353
6500
  <hh:fontface lang="HANJA" fontCnt="1">
6354
6501
  <hh:font id="0" face="\uD568\uCD08\uB86C\uBC14\uD0D5" type="TTF" isEmbedded="0">
@@ -6380,34 +6527,37 @@ function generateHeaderXml() {
6380
6527
  <hh:borderFill id="0" threeD="0" shadow="0" centerLine="0" breakCellSeparateLine="0">
6381
6528
  <hh:slash type="NONE" Crooked="0" isCounter="0"/>
6382
6529
  <hh:backSlash type="NONE" Crooked="0" isCounter="0"/>
6383
- <hh:leftBorder type="NONE" width="0.1mm" color="0"/>
6384
- <hh:rightBorder type="NONE" width="0.1mm" color="0"/>
6385
- <hh:topBorder type="NONE" width="0.1mm" color="0"/>
6386
- <hh:bottomBorder type="NONE" width="0.1mm" color="0"/>
6387
- <hh:diagonal type="NONE" width="0.1mm" color="0"/>
6530
+ <hh:leftBorder type="NONE" width="0.1mm" color="#000000"/>
6531
+ <hh:rightBorder type="NONE" width="0.1mm" color="#000000"/>
6532
+ <hh:topBorder type="NONE" width="0.1mm" color="#000000"/>
6533
+ <hh:bottomBorder type="NONE" width="0.1mm" color="#000000"/>
6534
+ <hh:diagonal type="NONE" width="0.1mm" color="#000000"/>
6388
6535
  <hh:fillInfo/>
6389
6536
  </hh:borderFill>
6390
6537
  </hh:borderFills>
6391
- <hh:charProperties itemCnt="1">
6392
- <hh:charPr id="0" height="1000" textColor="0" shadeColor="-1" useFontSpace="0" useKerning="0" symMark="NONE" borderFillIDRef="0">
6393
- <hh:fontRef hangul="0" latin="0" hanja="0" japanese="0" other="0" symbol="0" user="0"/>
6394
- <hh:ratio hangul="100" latin="100" hanja="100" japanese="100" other="100" symbol="100" user="100"/>
6395
- <hh:spacing hangul="0" latin="0" hanja="0" japanese="0" other="0" symbol="0" user="0"/>
6396
- <hh:relSz hangul="100" latin="100" hanja="100" japanese="100" other="100" symbol="100" user="100"/>
6397
- <hh:offset hangul="0" latin="0" hanja="0" japanese="0" other="0" symbol="0" user="0"/>
6398
- </hh:charPr>
6538
+ <hh:charProperties itemCnt="9">
6539
+ ${charPr(0, 1e3, false, false)}
6540
+ ${charPr(1, 1e3, true, false)}
6541
+ ${charPr(2, 1e3, false, true)}
6542
+ ${charPr(3, 1e3, true, true)}
6543
+ ${charPr(4, 900, false, false, 1)}
6544
+ ${charPr(5, 1800, true, false, 1)}
6545
+ ${charPr(6, 1400, true, false, 1)}
6546
+ ${charPr(7, 1200, true, false, 1)}
6547
+ ${charPr(8, 1100, true, false, 1)}
6399
6548
  </hh:charProperties>
6400
6549
  <hh:tabProperties itemCnt="0"/>
6401
6550
  <hh:numberings itemCnt="0"/>
6402
6551
  <hh:bullets itemCnt="0"/>
6403
- <hh:paraProperties itemCnt="1">
6404
- <hh:paraPr id="0" tabIDRef="0" condense="0" fontLineHeight="0" snapToGrid="0" suppressOverlap="0" checked="0">
6405
- <hh:parLineBreak lineBreak="BREAK_LINE" wordBreak="BREAK_WORD" breakLatinWord="BREAK_WORD" breakNonLatinWord="BREAK_WORD"/>
6406
- <hh:parMargin left="0" right="0" prev="0" next="0" indent="0"/>
6407
- <hh:parBorder borderFillIDRef="0" offsetLeft="0" offsetRight="0" offsetTop="0" offsetBottom="0" connect="0" ignoreMargin="0"/>
6408
- <hh:parShade borderFillIDRef="0"/>
6409
- <hh:parTabList/>
6410
- </hh:paraPr>
6552
+ <hh:paraProperties itemCnt="8">
6553
+ ${paraPr(0)}
6554
+ ${paraPr(1, { align: "LEFT", spaceBefore: 800, spaceAfter: 200, lineSpacing: 180 })}
6555
+ ${paraPr(2, { align: "LEFT", spaceBefore: 600, spaceAfter: 150, lineSpacing: 170 })}
6556
+ ${paraPr(3, { align: "LEFT", spaceBefore: 400, spaceAfter: 100, lineSpacing: 160 })}
6557
+ ${paraPr(4, { align: "LEFT", spaceBefore: 300, spaceAfter: 100, lineSpacing: 160 })}
6558
+ ${paraPr(5, { align: "LEFT", lineSpacing: 130, indent: 400 })}
6559
+ ${paraPr(6, { align: "LEFT", lineSpacing: 150, indent: 600 })}
6560
+ ${paraPr(7, { align: "LEFT", lineSpacing: 160, indent: 600 })}
6411
6561
  </hh:paraProperties>
6412
6562
  <hh:styles itemCnt="1">
6413
6563
  <hh:style id="0" type="PARA" name="\uBC14\uD0D5\uAE00" engName="Normal" paraPrIDRef="0" charPrIDRef="0" nextStyleIDRef="0" langIDRef="1042" lockForm="0"/>
@@ -6416,34 +6566,78 @@ function generateHeaderXml() {
6416
6566
  <hh:compatibleDocument targetProgram="HWP2018"/>
6417
6567
  </hh:head>`;
6418
6568
  }
6419
- function generateParagraph(text) {
6420
- return `<hp:p paraPrIDRef="0" styleIDRef="0"><hp:run charPrIDRef="0"><hp:t>${escapeXml(text)}</hp:t></hp:run></hp:p>`;
6569
+ function generateSecPr() {
6570
+ return `<hp:secPr textDirection="HORIZONTAL" spaceColumns="1134" tabStop="8000" outlineShapeIDRef="0" memoShapeIDRef="0" textVerticalWidthHead="0" masterPageCnt="0"><hp:grid lineGrid="0" charGrid="0" wonggojiFormat="0"/><hp:startNum pageStartsOn="BOTH" page="0" pic="0" tbl="0" equation="0"/><hp:visibility hideFirstHeader="0" hideFirstFooter="0" hideFirstMasterPage="0" border="SHOW_ALL" fill="SHOW_ALL" hideFirstPageNum="0" hideFirstEmptyLine="0" showLineNumber="0"/><hp:pagePr landscape="WIDELY" width="59528" height="84188" gutterType="LEFT_ONLY"><hp:margin header="2835" footer="2835" gutter="0" left="5670" right="4252" top="8504" bottom="4252"/></hp:pagePr><hp:footNotePr><hp:autoNumFormat type="DIGIT" userChar="" prefixChar="" suffixChar=")" supscript="0"/><hp:noteLine length="-1" type="SOLID" width="0.12 mm" color="#000000"/><hp:noteSpacing betweenNotes="283" belowLine="567" aboveLine="850"/><hp:numbering type="CONTINUOUS" newNum="1"/><hp:placement place="EACH_COLUMN" beneathText="0"/></hp:footNotePr><hp:endNotePr><hp:autoNumFormat type="DIGIT" userChar="" prefixChar="" suffixChar=")" supscript="0"/><hp:noteLine length="14692344" type="SOLID" width="0.12 mm" color="#000000"/><hp:noteSpacing betweenNotes="0" belowLine="567" aboveLine="850"/><hp:numbering type="CONTINUOUS" newNum="1"/><hp:placement place="END_OF_DOCUMENT" beneathText="0"/></hp:endNotePr></hp:secPr>`;
6421
6571
  }
6422
6572
  function generateTable(rows) {
6423
6573
  const trElements = rows.map((row) => {
6424
- const tdElements = row.map(
6425
- (cell) => `<hp:tc><hp:cellSpan colSpan="1" rowSpan="1"/>${generateParagraph(cell)}</hp:tc>`
6426
- ).join("");
6574
+ const tdElements = row.map((cell) => {
6575
+ const runs = generateRuns(cell);
6576
+ return `<hp:tc><hp:cellSpan colSpan="1" rowSpan="1"/><hp:p paraPrIDRef="0" styleIDRef="0">${runs}</hp:p></hp:tc>`;
6577
+ }).join("");
6427
6578
  return `<hp:tr>${tdElements}</hp:tr>`;
6428
6579
  }).join("");
6429
6580
  return `<hp:tbl>${trElements}</hp:tbl>`;
6430
6581
  }
6431
6582
  function blocksToSectionXml(blocks) {
6432
- const body = blocks.map((block) => {
6583
+ const paraXmls = [];
6584
+ let isFirst = true;
6585
+ for (const block of blocks) {
6586
+ let xml = "";
6433
6587
  switch (block.type) {
6434
- case "heading":
6435
- return generateParagraph(block.text || "");
6436
- case "table":
6437
- return block.rows ? generateTable(block.rows) : "";
6588
+ case "heading": {
6589
+ const pId = headingParaPrId(block.level || 1);
6590
+ const cId = headingCharPrId(block.level || 1);
6591
+ xml = generateParagraph(block.text || "", pId, cId);
6592
+ break;
6593
+ }
6438
6594
  case "paragraph":
6439
- return generateParagraph(block.text || "");
6440
- default:
6441
- return "";
6595
+ xml = generateParagraph(block.text || "");
6596
+ break;
6597
+ case "code_block": {
6598
+ const codeLines = (block.text || "").split("\n");
6599
+ xml = codeLines.map((line) => generateParagraph(line || " ", PARA_CODE)).join("\n ");
6600
+ break;
6601
+ }
6602
+ case "blockquote":
6603
+ xml = generateParagraph(block.text || "", PARA_QUOTE);
6604
+ break;
6605
+ case "list_item": {
6606
+ const marker = block.ordered ? `${(block.indent || 0) + 1}. ` : "\xB7 ";
6607
+ const indentPrefix = " ".repeat(block.indent || 0);
6608
+ xml = generateParagraph(indentPrefix + marker + (block.text || ""), PARA_LIST);
6609
+ break;
6610
+ }
6611
+ case "hr":
6612
+ xml = `<hp:p paraPrIDRef="0" styleIDRef="0"><hp:run charPrIDRef="0"><hp:t>\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500</hp:t></hp:run></hp:p>`;
6613
+ break;
6614
+ case "table":
6615
+ if (block.rows) {
6616
+ if (isFirst) {
6617
+ const secRun = `<hp:run charPrIDRef="0">${generateSecPr()}<hp:t></hp:t></hp:run>`;
6618
+ paraXmls.push(`<hp:p paraPrIDRef="0" styleIDRef="0">${secRun}</hp:p>`);
6619
+ isFirst = false;
6620
+ }
6621
+ xml = generateTable(block.rows);
6622
+ }
6623
+ break;
6442
6624
  }
6443
- }).join("\n ");
6625
+ if (!xml) continue;
6626
+ if (isFirst && block.type !== "table") {
6627
+ xml = xml.replace(
6628
+ /<hp:run charPrIDRef="(\d+)">/,
6629
+ `<hp:run charPrIDRef="$1">${generateSecPr()}`
6630
+ );
6631
+ isFirst = false;
6632
+ }
6633
+ paraXmls.push(xml);
6634
+ }
6635
+ if (paraXmls.length === 0) {
6636
+ paraXmls.push(`<hp:p paraPrIDRef="0" styleIDRef="0"><hp:run charPrIDRef="0">${generateSecPr()}<hp:t></hp:t></hp:run></hp:p>`);
6637
+ }
6444
6638
  return `<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
6445
6639
  <hs:sec xmlns:hs="${NS_SECTION}" xmlns:hp="${NS_PARA}">
6446
- ${body}
6640
+ ${paraXmls.join("\n ")}
6447
6641
  </hs:sec>`;
6448
6642
  }
6449
6643