Skip to content

World's first open-source enterprise world model.

An HTML table keeps its caption (#1287)

Maya Kidcd1d58a6f045
diff --git a/crates/utopia-ingest/src/html.rs b/crates/utopia-ingest/src/html.rs
index a6c42d49..69cf41f5 100644
--- a/crates/utopia-ingest/src/html.rs
+++ b/crates/utopia-ingest/src/html.rs
@@ -47,6 +47,22 @@ mod table_tests {
assert!(md.contains("b"), "有内容的列不能砍: {md}");
}
+ /// 页面标题「Pricing」又出现在表名「Pricing tiers」里:那是表的名字,不是正文里的标题
+ #[test]
+ fn a_caption_that_repeats_the_page_title_keeps_the_title_heading() {
+ let page = "<html><head><title>Pricing</title></head><body><article>\
+ <p>Every plan includes unlimited projects, daily backups and email support. Teams that \
+ pay for a year are charged for ten months, and the discount applies again at each \
+ renewal. Amounts are in US dollars before tax; invoices list the tax of the country \
+ the account is billed in. A plan can be changed at any time, prorated to the day.</p>\
+ <table><caption>Pricing tiers</caption><tr><th>Plan</th><th>Monthly</th></tr>\
+ <tr><td>Basic</td><td>10</td></tr><tr><td>Team</td><td>25</td></tr></table>\
+ </article></body></html>";
+ let md = super::page_to_markdown(page, Some("https://example.com/pricing")).unwrap();
+ assert!(md.starts_with("# Pricing\n\n"), "{md}");
+ assert!(md.contains("Pricing tiers\n\n| Plan | Monthly |"), "{md}");
+ }
+
#[test]
fn a_table_that_already_has_headers_is_untouched() {
let html = "<table><thead><tr><th>a</th></tr></thead><tr><td>1</td></tr></table>";
@@ -167,9 +183,11 @@ pub fn page_to_markdown(html: &str, base_url: Option<&str>) -> Result<String, Ht
{
return markdown_from_html(html);
}
- let markdown = markdown_from_html(&article.content)?;
+ let (markdown, prose) = markdown_and_prose(&article.content)?;
let title = article.title.trim();
- if title.is_empty() || markdown.contains(title) {
+ // 只在 htmd 写的正文里找标题,不看换回来的表:表前的说明句(<caption>、标题行)
+ // 常常重复页面标题,算进去就不补 `# 标题`,每一块的面包屑里也就没有它
+ if title.is_empty() || prose.contains(title) {
Ok(markdown)
} else {
Ok(sanitize_markdown_links(&format!("# {title}\n\n{markdown}")))
@@ -488,6 +506,11 @@ fn prune_empty_table_columns(markdown: &str) -> String {
}
fn markdown_from_html(html: &str) -> Result<String, HtmlError> {
+ markdown_and_prose(html).map(|(markdown, _)| markdown)
+}
+
+/// 转出的 Markdown,和表换回去之前 htmd 写出的那份(表还是占位段落)
+fn markdown_and_prose(html: &str) -> Result<(String, String), HtmlError> {
// 最外层的表先从 DOM 渲染成带真表头的 Markdown(见 `table`),htmd 只看到占位段落;
// 套着的表还走老路,所以第一行提表头的补丁留着给它们
let (html, tables) = crate::table::lift_tables(html);
@@ -508,8 +531,11 @@ fn markdown_from_html(html: &str) -> Result<String, HtmlError> {
{
return Err(HtmlError::Interstitial);
}
- let markdown = crate::table::restore_tables(&markdown, &tables);
- normalize_markdown(&prune_empty_table_columns(&markdown))
+ let restored = crate::table::restore_tables(&markdown, &tables);
+ Ok((
+ normalize_markdown(&prune_empty_table_columns(&restored))?,
+ markdown,
+ ))
}
/// Normalize direct Markdown with the same link policy as HTML conversion.
diff --git a/crates/utopia-ingest/src/table.rs b/crates/utopia-ingest/src/table.rs
index cafbb83e..985dd7b0 100644
--- a/crates/utopia-ingest/src/table.rs
+++ b/crates/utopia-ingest/src/table.rs
@@ -354,7 +354,44 @@ fn depth_of(cell: &Cell) -> u32 {
/// 一张表 → 说明句(可选)+ 带真表头的 Markdown 表,连同它用的列头(给续表接着用)。
/// 表里什么都没有时返回 None。
fn render_table(tbl: &Selection<'_>, inherited: &[String]) -> Option<(String, Vec<String>)> {
- render_rows(grid(tbl), inherited, None)
+ let (md, headers) = render_rows(grid(tbl), inherited, None)?;
+ // <caption> 不在任何 tr 里,读网格时看不见;整张表又换成了占位段落,htmd 本来会写出的
+ // 这句话也跟着没了。照 htmd 和 Word 题注的样子写回表前,单独一段,不像标题行那样硬加冒号:
+ // 短的由分块器当说明句带进表的每一块,长的(表名后面还跟着几句说明的)和别的长段落一样只写
+ // 一次。硬加了冒号,长的也得每块重复,300 token 的预算剩不下几行。
+ // 用 formatted_text:text() 把 <br> 两边的字首尾相接
+ let captions: String = tbl
+ .select("caption")
+ .iter()
+ .map(|c| clean(&c.formatted_text()))
+ .filter(|t| !t.is_empty())
+ .map(|t| as_paragraph(&t) + "\n\n")
+ .collect();
+ Some((captions + &md, headers))
+}
+
+/// 以 Markdown 块标记开头的说明句(「# of wells」「1. Demographics」「- Totals」「> 5 years」
+/// 「~~~ Notes」)会读成标题、列表、引用或代码块:分块器只认段落作说明句,表的续块就没有它;
+/// 读成标题还会顶替后面每一块的面包屑,读成代码块则没有收尾,把表和后文一直吞到文末。照 htmd
+/// 转义正文的办法,在标记前加反斜杠;只是像标记的(「#1 well」「~5 km」)照原样写
+fn as_paragraph(line: &str) -> String {
+ let b = line.as_bytes();
+ let spaced = |i: usize| b.get(i).is_none_or(|&c| c == b' ');
+ let hashes = b.iter().take_while(|&&c| c == b'#').count();
+ let digits = b.iter().take_while(|c| c.is_ascii_digit()).count();
+ let at = match b.first() {
+ Some(b'>') => 0,
+ Some(b'-' | b'+' | b'*') if spaced(1) => 0,
+ Some(b'#') if spaced(hashes) => 0,
+ Some(b'`' | b'~') if b.get(1) == b.first() && b.get(2) == b.first() => 0,
+ Some(b'0'..=b'9')
+ if digits <= 9 && matches!(b.get(digits), Some(b'.' | b')')) && spaced(digits + 1) =>
+ {
+ digits
+ }
+ _ => return line.to_string(),
+ };
+ format!("{}\\{}", &line[..at], &line[at..])
}
/// 别的解析器用的入口:一行是若干 (文字, 跨几列, 左内边距 pt)。`first_is_header` 为真时,
@@ -796,6 +833,58 @@ mod tests {
);
}
+ /// <caption> 是表写明的名字:单独一段写在标题行前面,和 Word 的题注一样不加冒号;
+ /// 模板常留的空 <caption> 什么都不写
+ #[test]
+ fn an_html_table_keeps_its_caption() {
+ let table = |caption: &str| {
+ render(&format!(
+ "<table><caption>{caption}</caption>\
+ <tr><td colspan=\"2\">Spring survey 2026</td></tr>\
+ <tr><th>Well</th><th>Depth</th></tr><tr><td>North</td><td>1200</td></tr></table>"
+ ))
+ };
+ let rest = "Spring survey 2026:\n\n| Well | Depth |\n| --- | --- |\n| North | 1200 |";
+ assert_eq!(
+ table("Table 2<br>Readings by well."),
+ format!("Table 2 Readings by well.\n\n{rest}")
+ );
+ assert_eq!(table("&nbsp;"), rest);
+ }
+
+ #[test]
+ fn a_caption_that_starts_like_markdown_stays_a_paragraph() {
+ let table = |caption: &str| {
+ render(&format!(
+ "<table><caption>{caption}</caption>\
+ <tr><th>Well</th><th>Depth</th></tr><tr><td>North</td><td>1200</td></tr></table>"
+ ))
+ };
+ for caption in [
+ "# of wells",
+ "1. Demographics",
+ "2) Totals",
+ "- Totals",
+ "+ Totals",
+ "* Preliminary",
+ "&gt; 5 years in service",
+ "~~~ Notes",
+ "``` Notes",
+ ] {
+ let md = table(caption);
+ assert_eq!(
+ crate::blocks::blocks(&md)[0].kind,
+ crate::blocks::Kind::Paragraph,
+ "{md}"
+ );
+ }
+ // 只是像标记的照原样写,块里的原文不多出反斜杠
+ for caption in ["#1 well", "~5 km"] {
+ let md = table(caption);
+ assert!(md.starts_with(&format!("{caption}\n\n")), "{md}");
+ }
+ }
+
#[test]
fn a_first_row_of_words_is_the_header_when_the_table_has_none() {
let html = "<table><tr><td>Revenue</td><td>Q2 FY27</td><td>Q1 FY27</td></tr>\