");
tasks.push(Task::Close("div".to_owned()));
- } else if dom.has_text_boundary(node) {
+ } else if dom.has_text_boundary(node) && !crate::content::owns_boundaries(tag) {
output.push(' ');
tasks.push(Task::Literal(" "));
}
@@ -243,25 +247,42 @@ pub(crate) fn render
(
}
if tag == "img" {
let label = crate::content::image_label(dom, node);
+ let targets: Vec<_> = anchor_targets
+ .targets(dom, node)
+ .into_iter()
+ .filter(|id| emitted_anchors.insert((*id).to_owned()))
+ .collect();
if let Some(src) = crate::media::source(dom, node) {
+ for id in targets.iter().skip(1) {
+ output.push_str(&crate::anchors::markup(id));
+ }
output.push_str("
");
+ output.push('"');
+ if let Some(id) = targets.first() {
+ output.push_str(" id=\"");
+ escape(id, &mut output);
+ output.push('"');
+ }
+ if let Some(title) = dom.attribute(node, "title") {
+ output.push_str(" title=\"");
+ escape(title, &mut output);
+ output.push('"');
+ }
+ output.push('>');
} else {
+ for id in targets {
+ output.push_str(&crate::anchors::markup(id));
+ }
escape(&label, &mut output);
}
continue;
}
if matches!(tag, "iframe" | "video" | "audio") {
let urls = crate::media::sources(dom, node, tag, depth, limit);
- let poster = (tag == "video")
- .then(|| dom.attribute(node, "poster"))
- .flatten()
- .filter(|src| {
- !src.trim().is_empty() && crate::media::safe_url(src, true)
- });
+ let poster = crate::media::poster(dom, node, tag);
if !urls.is_empty() || poster.is_some() {
let block = tag == "audio";
output.push_str(if block { "" } else { " " });
@@ -272,6 +293,7 @@ pub(crate) fn render
(
escape(&crate::media::poster_label(dom, node), &mut output);
output.push_str("\"> ");
}
+ let has_source = !urls.is_empty();
for url in urls {
output.push_str("(
if block {
output.push_str(" ");
}
- continue;
+ if has_source {
+ continue;
+ }
}
}
let retained = if allowed(tag)
&& (tag != "span"
- || dom.attribute(node, "id").is_some()
+ || !anchor_targets.targets(dom, node).is_empty()
|| dom.attribute(node, "title").is_some())
{
Some(tag)
@@ -308,6 +332,14 @@ pub(crate) fn render(
) {
begin_html_line(&mut output);
}
+ let targets: Vec<_> = anchor_targets
+ .targets(dom, node)
+ .into_iter()
+ .filter(|id| emitted_anchors.insert((*id).to_owned()))
+ .collect();
+ for id in targets.iter().skip(1) {
+ output.push_str(&crate::anchors::markup(id));
+ }
output.push('<');
output.push_str(tag);
for name in [
@@ -338,11 +370,6 @@ pub(crate) fn render(
output.push('"');
}
}
- let targets: Vec<_> = anchor_targets
- .targets(dom, node)
- .into_iter()
- .filter(|id| emitted_anchors.insert((*id).to_owned()))
- .collect();
if let Some(id) = targets.first() {
output.push_str(" id=\"");
escape(id, &mut output);
@@ -368,9 +395,6 @@ pub(crate) fn render(
if tag == "pre" {
output.push('\n');
}
- for id in targets.iter().skip(1) {
- output.push_str(&crate::anchors::markup(id));
- }
if !matches!(tag, "br" | "hr" | "img") {
tasks.push(Task::Close(tag.to_owned()));
}
diff --git a/moli-html2md/src/machine.rs b/moli-html2md/src/machine.rs
index ac37853c8a..e9758d1a73 100644
--- a/moli-html2md/src/machine.rs
+++ b/moli-html2md/src/machine.rs
@@ -11,6 +11,7 @@ enum Task<'a, Id> {
MathChildren(Option, usize),
Boundary,
ChoiceBoundary,
+ InlineSpace,
Fallback(String, usize),
PopStyle,
EndLink(usize),
@@ -88,6 +89,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
| Task::MathChildren(None, _)
| Task::RawChildren(None, _, _) => {}
Task::Boundary => self.writer().boundary(2),
+ Task::InlineSpace => self.writer().text(" "),
Task::ChoiceBoundary => self.writer().boundary(1),
Task::Fallback(label, before) => {
if self.writer().content_mark() == before {
@@ -223,12 +225,13 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
if depth >= self.options.max_depth {
return;
}
- if self.dom.has_block_layout(node) {
+ let owns_boundaries = matches!(self.dom.node_kind(node), NodeKind::Element(tag) if crate::content::owns_boundaries(tag));
+ if !owns_boundaries && self.dom.has_block_layout(node) {
self.writer().boundary(2);
self.tasks.push(Task::Boundary);
- } else if self.dom.has_text_boundary(node) {
- self.writer().boundary(1);
- self.tasks.push(Task::ChoiceBoundary);
+ } else if !owns_boundaries && self.dom.has_text_boundary(node) {
+ self.writer().text(" ");
+ self.tasks.push(Task::InlineSpace);
}
let tag = match self.dom.node_kind(node) {
NodeKind::Document => {
@@ -375,6 +378,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
self.options.max_depth,
&self.anchor_targets,
self.options.default_code_language.as_deref(),
+ &mut self.emitted_anchors,
);
if tag == "pre" {
self.writer().block(html.into(), 2, 2);
@@ -416,6 +420,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
self.options.max_depth,
&self.anchor_targets,
self.options.default_code_language.as_deref(),
+ &mut self.emitted_anchors,
);
self.writer().block(html.into(), 2, 2);
return;
@@ -447,8 +452,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
);
let complex_table =
crate::html_fallback::needed(self.dom, node, depth, self.options.max_depth);
- if !presentation
- && structural_table
+ if structural_table
&& (complex_table
|| (!self.anchor_targets.is_empty()
&& crate::anchors::contains(
@@ -465,17 +469,12 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
self.options.max_depth,
&self.anchor_targets,
self.options.default_code_language.as_deref(),
+ &mut self.emitted_anchors,
);
self.writer().block(html.into(), 2, 2);
return;
}
- {
- for id in self.anchor_targets.targets(self.dom, node) {
- if self.emitted_anchors.insert(id.to_owned()) {
- self.writer().inline_html(&crate::anchors::markup(id));
- }
- }
- }
+ self.emit_own_targets(node);
if !complex_table
&& let Some(mut table) =
Table::from_dom(self.dom, node, depth, self.options.max_depth)
@@ -488,8 +487,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
}
return;
}
- if !presentation
- && structural_table
+ if structural_table
&& crate::html_fallback::has_cells(
self.dom,
node,
@@ -504,6 +502,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
self.options.max_depth,
&self.anchor_targets,
self.options.default_code_language.as_deref(),
+ &mut self.emitted_anchors,
);
self.writer().block(html.into(), 2, 2);
return;
@@ -558,9 +557,7 @@ impl<'a, D: Dom + ?Sized> Machine<'a, D> {
fn media(&mut self, node: D::NodeId, tag: &str, depth: usize) -> bool {
let urls = crate::media::sources(self.dom, node, tag, depth, self.options.max_depth);
- let poster = (tag == "video")
- .then(|| crate::media::direct_attribute(self.dom, node, "poster", true))
- .flatten();
+ let poster = crate::media::poster(self.dom, node, tag);
if urls.is_empty() && poster.is_none() {
return false;
}
@@ -831,7 +828,7 @@ fn is_block(tag: &str) -> bool {
)
}
-fn is_structural_block(tag: &str) -> bool {
+pub(crate) fn is_structural_block(tag: &str) -> bool {
matches!(
tag,
"address"
diff --git a/moli-html2md/src/media.rs b/moli-html2md/src/media.rs
index f5f1dbb034..530ddfd635 100644
--- a/moli-html2md/src/media.rs
+++ b/moli-html2md/src/media.rs
@@ -273,8 +273,12 @@ pub(crate) fn sources<'a, D: Dom + ?Sized>(
}
pub(crate) fn label<'a, D: Dom + ?Sized>(dom: &'a D, node: D::NodeId, tag: &str) -> &'a str {
- dom.attribute(node, "title")
+ dom.attribute(node, "aria-label")
.filter(|label| !label.trim().is_empty())
+ .or_else(|| {
+ dom.attribute(node, "title")
+ .filter(|label| !label.trim().is_empty())
+ })
.unwrap_or(match tag {
"video" => "Video",
"audio" => "Audio",
@@ -295,3 +299,19 @@ pub(crate) fn safe_link_url(value: &str) -> bool {
&& !normalized.starts_with("data:text/html")
&& !normalized.starts_with("data:application/xhtml+xml")
}
+
+pub(crate) fn poster_label(dom: &D, node: D::NodeId) -> String {
+ format!("{} poster", label(dom, node, "video"))
+}
+
+pub(crate) fn poster<'a, D: Dom + ?Sized>(
+ dom: &'a D,
+ node: D::NodeId,
+ tag: &str,
+) -> Option<&'a str> {
+ (tag == "video")
+ .then(|| dom.attribute(node, "poster"))
+ .flatten()
+ .map(str::trim)
+ .filter(|src| !src.is_empty() && safe_url(src, true))
+}
diff --git a/moli-html2md/tests/review_semantics.rs b/moli-html2md/tests/review_semantics.rs
index b635872e66..4a8088ceaf 100644
--- a/moli-html2md/tests/review_semantics.rs
+++ b/moli-html2md/tests/review_semantics.rs
@@ -937,3 +937,51 @@ fn merged_headerless_data_is_preserved_without_treating_empty_hints_as_headers()
assert!(result.contains("/cover.jpg"), "{result}");
assert!(result.contains("Home"), "{result}");
}
+
+#[test]
+fn fallback_targets_and_media_preserve_navigation_without_duplicate_ids() {
+ for html in [
+ "FC |
",
+ "N",
+ "XP
",
+ ] {
+ let output = markdown(html);
+ let target = if html.contains("#fig") {
+ "fig"
+ } else if html.contains("#note") {
+ "note"
+ } else {
+ "x"
+ };
+ assert_eq!(
+ output.matches(&format!("id=\"{target}\"")).count(),
+ 1,
+ "{output}"
+ );
+ if target == "fig" {
+ assert!(output.contains("title=\"Details\""), "{output}");
+ }
+ }
+ let output = markdown(
+ "12",
+ );
+ let html = rendered_html(&output);
+ assert!(
+ html.contains("href=\"/x\" id=\"one\">linked text"),
+ "{html}"
+ );
+ assert!(
+ !output.contains("plain") && !output.contains("
C |
"
+ ));
+ let html = rendered_html(&output);
+ assert!(
+ html.contains("/cover.jpg") && html.contains("href=\"/v.mp4\""),
+ "{output}"
+ );
+ }
+}
diff --git a/moli-renderer-v8/src/runtime/page_dump.rs b/moli-renderer-v8/src/runtime/page_dump.rs
index 397678970a..f5f636dd93 100644
--- a/moli-renderer-v8/src/runtime/page_dump.rs
+++ b/moli-renderer-v8/src/runtime/page_dump.rs
@@ -394,30 +394,11 @@ mod tests {
);
let before = dom.serialize_document();
let markdown = render_markdown_document(&dom);
- for content in [
- "First story",
- "42 points by",
- "8 comments",
- "Second story",
- "7 points by",
- "discuss",
- "More",
- ] {
- assert!(markdown.contains(content), "missing {content}: {markdown}");
- }
- let positions = [
- "First story",
- "42 points by",
- "Second story",
- "7 points by",
- "More",
- ]
- .map(|content| markdown.find(content).expect("checked above"));
- assert!(
- positions.windows(2).all(|pair| pair[0] < pair[1]),
- "{markdown}"
+ assert_eq!(
+ markdown,
+ include_str!("../../../moli-html2md/tests/fixtures/hacker-news-layout.md")
+ .trim_end_matches('\n')
);
- assert!(markdown.contains(" Result<()>
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
- String::from_utf8(output.stdout)?.trim_end(),
- markdown.trim_end()
+ String::from_utf8(output.stdout)?,
+ format!("{}\n", markdown.trim_end_matches('\n'))
);
Ok(())
}