Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich4ab7c4e3901e
diff --git a/crates/liteparse/src/markdown_layout/headings.rs b/crates/liteparse/src/markdown_layout/headings.rs
index 5dab41b..6dcc0ca 100644
--- a/crates/liteparse/src/markdown_layout/headings.rs
+++ b/crates/liteparse/src/markdown_layout/headings.rs
@@ -1,7 +1,7 @@
use crate::types::{OutlineTarget, ParsedPage, ProjectedLine, StructNode};
use super::classify::is_rotated_line;
-use super::inline::line_uniform_style;
+use super::inline::line_all_bold;
use super::paragraphs::continues_paragraph;
use super::tables::{TABLE_MIN_COLUMNS, split_cells};
@@ -267,11 +267,24 @@ pub(super) fn looks_like_bold_heading(
if is_caption_line(text) {
return false;
}
- let style = match line_uniform_style(line) {
- Some(s) => s,
- None => return false,
- };
- if !style.bold || style.mono {
+ // Accept a line whose spans are all bold (italic may vary) and non-mono.
+ // A strict single-style check would reject headings that mix bold and
+ // bold-italic spans (e.g. "**4** ***Foo*** **Bar**"), which are common
+ // for numbered section headings.
+ if !line_all_bold(line) {
+ return false;
+ }
+ // Run-in labels — a bold lead-in that ends in a period and flows straight
+ // into the body sentence ("United Kingdom.", "Model merging.") — read as
+ // emphasis, not block headings. A trailing '.' is the giveaway; real
+ // section headings (numbered or titled) don't terminate in a period.
+ // ':' is deliberately allowed ("Reference frameworks:").
+ if text.ends_with('.') {
+ return false;
+ }
+ // Block headings are capitalized. A bold line starting lowercase is almost
+ // always a stray bold word or a borderless table cell, not a heading.
+ if text.chars().next().is_some_and(|c| c.is_lowercase()) {
return false;
}
// Reject bold-uniform lines dominated by digits / punctuation — these are
diff --git a/crates/liteparse/src/markdown_layout/inline.rs b/crates/liteparse/src/markdown_layout/inline.rs
index 67e5e52..dc2dd6d 100644
--- a/crates/liteparse/src/markdown_layout/inline.rs
+++ b/crates/liteparse/src/markdown_layout/inline.rs
@@ -219,6 +219,25 @@ pub(super) fn line_uniform_style(line: &ProjectedLine) -> Option<SpanStyle> {
Some(first)
}
+/// True when every non-whitespace span on the line is bold and non-mono.
+/// Unlike `line_uniform_style`, this tolerates per-span *italic* variation,
+/// so a heading whose spans mix bold and bold-italic (e.g.
+/// "**4** ***Foo*** **Bar**") still reads as a single bold line. Returns
+/// false for an empty / all-whitespace line.
+pub(super) fn line_all_bold(line: &ProjectedLine) -> bool {
+ let mut saw_span = false;
+ for span in &line.spans {
+ if span.text.trim().is_empty() {
+ continue;
+ }
+ if is_mono_item(span) || !is_bold_item(span) {
+ return false;
+ }
+ saw_span = true;
+ }
+ saw_span
+}
+
#[cfg(test)]
mod tests {
use super::super::test_helpers::styled_line;
diff --git a/crates/liteparse/src/markdown_layout/paragraphs.rs b/crates/liteparse/src/markdown_layout/paragraphs.rs
index 478f8a1..2d5e052 100644
--- a/crates/liteparse/src/markdown_layout/paragraphs.rs
+++ b/crates/liteparse/src/markdown_layout/paragraphs.rs
@@ -1,6 +1,6 @@
use crate::types::{Anchor, ProjectedLine};
-use super::inline::{SpanStyle, line_uniform_style, render_line_inline};
+use super::inline::{SpanStyle, line_all_bold, line_uniform_style, render_line_inline};
/// Multiplier on line height used as the paragraph-break threshold.
pub(super) const PARAGRAPH_GAP_MULTIPLIER: f32 = 1.5;
@@ -87,6 +87,13 @@ pub(super) fn continues_paragraph(prev: &ProjectedLine, cur: &ProjectedLine) ->
{
return false;
}
+ // Same intent, but italic-tolerant: an all-bold line (which may mix bold
+ // and bold-italic spans, so `line_uniform_style` yields None) adjacent to
+ // a non-all-bold line is a paragraph break. Catches a bold section heading
+ // hugging the body paragraph below it.
+ if line_all_bold(prev) != line_all_bold(cur) {
+ return false;
+ }
if prev.region_path != cur.region_path {
// Cross-region continuation: the same paragraph can wrap from the
// bottom of one column into the top of the next. Only bridge regions