From ad746e9beb38dc5efc9fe89d1dc2f55b700b8cfb Mon Sep 17 00:00:00 2001 From: Rion Hwang <111563519+RionHwang0@users.noreply.github.com> Date: Sun, 6 Dec 2026 13:48:02 +0900 Subject: [PATCH 1/2] fix(ebook): keep the text of nested tables in TXT and Markdown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PlainTextWriter and MarkdownWriter built a cell's text from its paragraphs only, so a table nested in a cell (HTML, EPUB) vanished from HTML → TXT and e-book → TXT / Markdown. The nested table's text now stays in its outer cell, in reading order: cells separated by a space and rows by " / ". --- src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs | 23 +++++++++---- src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs | 17 ++++++++-- tests/Filee.Engines.Tests/HtmlReaderTests.cs | 33 +++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs index 91613ad82..bcc4845ce 100644 --- a/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/MarkdownWriter.cs @@ -129,12 +129,7 @@ private void Table(HTable table, StringBuilder sb, string prefix) column++; if (column >= columns) break; - // Header cells are bold by nature: no ** inside them. - var header = table.Rows[r].Header; - cells[column] = string.Join(" ", cell.Blocks.OfType() - .Select(p => header ? p.Inlines.Select(i => i is HText t ? new HText(t.Text, t.Format with { Bold = null }) : i).ToList() : p.Inlines) - .Select(inlines => Inlines(inlines).Replace("\\\n", " ", StringComparison.Ordinal).Replace("\n", " ", StringComparison.Ordinal))) - .Replace("|", "\\|", StringComparison.Ordinal); + cells[column] = CellText(cell.Blocks, table.Rows[r].Header).Replace("|", "\\|", StringComparison.Ordinal); for (var cc = column; cc < Math.Min(columns, column + cell.ColSpan); cc++) freeFrom[cc] = Math.Max(freeFrom[cc], r + cell.RowSpan); column += cell.ColSpan; @@ -149,6 +144,22 @@ private void Table(HTable table, StringBuilder sb, string prefix) sb.Append('\n'); } + /// + /// The Markdown of a cell on one line. A table nested in the cell keeps its text in reading order: its cells + /// separated by a space and its rows by " / ", since a pipe table cannot hold a table or a line break. + /// + private string CellText(List blocks, bool header) => + string.Join(" ", blocks.Select(block => block switch + { + // Header cells are bold by nature: no ** inside them. + HParagraph p => Inlines(header ? p.Inlines.Select(i => i is HText t ? new HText(t.Text, t.Format with { Bold = null }) : i).ToList() : p.Inlines) + .Replace("\\\n", " ", StringComparison.Ordinal).Replace("\n", " ", StringComparison.Ordinal), + HTable table => string.Join(" / ", table.Rows + .Select(row => string.Join(" ", row.Cells.Select(c => CellText(c.Blocks, header || row.Header)).Where(t => t.Length > 0))) + .Where(t => t.Length > 0)), + _ => "", + }).Where(t => t.Length > 0)); + private string Inlines(List inlines) { var sb = new StringBuilder(); diff --git a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs index da6714b9f..867515ced 100644 --- a/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs +++ b/src/Filee.Engines/Hwp/Hwpx/PlainTextWriter.cs @@ -73,8 +73,21 @@ private static void Blocks(List blocks, StringBuilder sb, List n private static bool IsCode(HParagraph paragraph) => paragraph.Inlines.Count > 0 && paragraph.Inlines.All(i => i is HText { Format.Shade: HtmlReader.CodeShade }); - private static string Cell(HCell cell, List notes) => - string.Join(" ", cell.Blocks.OfType().Select(p => Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' ')).Where(t => t.Length > 0)); + private static string Cell(HCell cell, List notes) => CellText(cell.Blocks, notes); + + /// + /// The text of a cell on one line. A table nested in the cell keeps its text in reading order: its cells separated + /// by a space and its rows by " / ", since a tab or a line break would break the outer table's row. + /// + private static string CellText(List blocks, List notes) => + string.Join(" ", blocks.Select(block => block switch + { + HParagraph p => Inlines(p.Inlines, notes).Replace('\n', ' ').Replace('\t', ' '), + HTable table => string.Join(" / ", table.Rows + .Select(row => string.Join(" ", row.Cells.Select(c => CellText(c.Blocks, notes)).Where(t => t.Length > 0))) + .Where(t => t.Length > 0)), + _ => "", + }).Where(t => t.Length > 0)); private static string Inlines(List inlines, List notes) { diff --git a/tests/Filee.Engines.Tests/HtmlReaderTests.cs b/tests/Filee.Engines.Tests/HtmlReaderTests.cs index 8bc785469..1d6397b7e 100644 --- a/tests/Filee.Engines.Tests/HtmlReaderTests.cs +++ b/tests/Filee.Engines.Tests/HtmlReaderTests.cs @@ -493,4 +493,37 @@ await File.WriteAllTextAsync(html, """ if (pages is not null) Assert.Equal(2, pages); } + + [Fact] + public async Task Nested_table_text_reaches_text_and_markdown_from_html_and_epub() + { + var html = Path.Combine(fx.NewFolder(), "일정.html"); + await File.WriteAllTextAsync(html, """ + 일정 + + + +
구분내용
일정개요 +
1일차개막식
2일차현장 견학
+
+ + """, TestContext.Current.CancellationToken); + async Task Convert(string input, string target) + { + var job = await fx.ConvertAsync([input], new Preset { TargetFormat = target }); + Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}"))); + return job.Outputs.Single(); + } + var epub = await Convert(html, "epub"); + + foreach (var (source, target) in new[] { (html, "txt"), (epub, "txt"), (epub, "md") }) + { + var text = await File.ReadAllTextAsync(await Convert(source, target), TestContext.Current.CancellationToken); + + // The nested table stays in its outer cell, on that cell's line: cells by a space, rows by " / ". + var row = text.Split('\n').Single(line => line.Contains("개요", StringComparison.Ordinal)); + Assert.Contains("일정", row, StringComparison.Ordinal); + Assert.Contains("개요 1일차 개막식 / 2일차 현장 견학", row, StringComparison.Ordinal); + } + } } From 282e21bfd16055ca535e802fcc3622c7dfa98387 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Sun, 6 Dec 2026 15:09:34 +0900 Subject: [PATCH 2/2] docs(changelog): nested table text kept in TXT and Markdown (#2106) Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d74db2be..434f63b4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -673,6 +673,9 @@ zh: 修复了 HTML 和 Word 转 PDF、PDF/A 表单值、PDF 页面编辑的书 - **Table captions kept in TXT and Markdown** (by @RionHwang0, #2105). HTML to TXT, and e-books to TXT or Markdown, dropped the caption of a table. It is now written right before the table. +- **Tables inside table cells kept in TXT and Markdown** (by @RionHwang0, #2106). HTML to TXT, and e-books to TXT or + Markdown, dropped a table nested in a cell with all its text. Its text now stays in that cell, on one line. + ## 2.5.0