Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 105 additions & 5 deletions pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2678,10 +2678,9 @@ fn convert_doc_to_bytes(name: &str, doc: &serde_json::Value) -> Result<Vec<u8>,
/// Inline atoms carry no `content`, so recursion alone loses them.
/// Pinned representations (#1491): an `image` contributes its `alt`
/// text verbatim when non-empty, otherwise its `src` verbatim when
/// non-empty, otherwise nothing; a `front_matter` block contributes
/// its `text` attr verbatim (#5639). Known remaining gap: `footnote_reference`
/// atoms are not yet rendered — they contribute nothing; that is
/// explicitly out of scope for the #1491 fix.
/// non-empty, otherwise nothing; a `footnote_reference` contributes
/// `[^<label>]` when its label is non-empty. A `front_matter` block
/// contributes its `text` attr verbatim (#5639).
fn extract_plain_text(doc: &serde_json::Value) -> String {
let mut lines = Vec::new();
if let Some(content) = doc.get("content").and_then(|c| c.as_array()) {
Expand Down Expand Up @@ -2758,7 +2757,9 @@ fn collect_plain_text_lines(nodes: &[serde_json::Value], lines: &mut Vec<String>
/// as a newline, and any other inline node recursed into (its own
/// inline content contributes its text instead of being dropped).
/// The `image` atom has no content — its payload is `attrs`, rendered
/// per the pinned policy on `extract_plain_text` (#1491).
/// per the pinned policy on `extract_plain_text` (#1491). A
/// `footnote_reference` atom similarly renders its non-empty label
/// using the Markdown footnote-reference form.
fn extract_inline_text(node: &serde_json::Value) -> String {
let mut out = String::new();
if let Some(content) = node.get("content").and_then(|c| c.as_array()) {
Expand All @@ -2785,6 +2786,18 @@ fn extract_inline_text(node: &serde_json::Value) -> String {
out.push_str(src);
}
}
"footnote_reference" => {
if let Some(label) = inline
.get("attrs")
.and_then(|attrs| attrs.get("label"))
.and_then(serde_json::Value::as_str)
.filter(|label| !label.is_empty())
{
out.push_str("[^");
out.push_str(label);
out.push(']');
}
}
_ => out.push_str(&extract_inline_text(inline)),
}
}
Expand Down Expand Up @@ -4190,6 +4203,7 @@ async fn identity_update_name_handler(
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use proptest::prelude::*;

fn raw_doc(blocks: &[&[&str]]) -> serde_json::Value {
let content: Vec<serde_json::Value> = blocks
Expand Down Expand Up @@ -4393,6 +4407,10 @@ mod tests {
serde_json::json!({"type": "image", "attrs": attrs})
}

fn footnote_reference(label: &str) -> serde_json::Value {
serde_json::json!({"type": "footnote_reference", "attrs": {"label": label}})
}

#[test]
fn test_extract_plain_text_image_only_paragraph_saves_as_alt() {
// #1491: an image atom's only text is its alt.
Expand Down Expand Up @@ -4447,6 +4465,88 @@ mod tests {
assert_eq!(extract_plain_text(&doc), "xy");
}

#[test]
fn test_extract_plain_text_footnote_reference_between_text_runs() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add property coverage for the new serialization invariant

The new rendering behavior has a precise serializer invariant: every non-empty string label is preserved verbatim inside [^…], without changing surrounding text. However, the added tests cover only fixed examples. Root AGENTS.md requires property tests for meaningful serializer invariants and explicitly says example-based tests do not replace them. Add a proptest generating labels and surrounding text, using the expected concatenation as the oracle, including placement inside recursive containers.

AGENTS.md reference: AGENTS.md:L41-L45

Useful? React with 👍 / 👎.

let doc = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [
{"type": "text", "text": "before "},
footnote_reference("note:α"),
{"type": "text", "text": " after"}
]})]);
assert_eq!(extract_plain_text(&doc), "before [^note:α] after");
}

#[test]
fn test_extract_plain_text_footnote_reference_in_recursive_container() {
let doc = plain_doc(vec![serde_json::json!({"type": "bullet_list", "content": [
{"type": "list_item", "content": [serde_json::json!({"type": "paragraph", "content": [
{"type": "text", "text": "listed "},
footnote_reference("1"),
{"type": "text", "text": " text"}
]})]}
]})]);
assert_eq!(extract_plain_text(&doc), "listed [^1] text");
}

#[test]
fn test_extract_plain_text_footnote_reference_missing_or_empty_label_is_nothing() {
for reference in [
serde_json::json!({"type": "footnote_reference"}),
serde_json::json!({"type": "footnote_reference", "attrs": {}}),
serde_json::json!({"type": "footnote_reference", "attrs": {"label": ""}}),
] {
let doc = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [
{"type": "text", "text": "x"},
reference,
{"type": "text", "text": "y"}
]})]);
assert_eq!(extract_plain_text(&doc), "xy");
}
}

// Oracle for the two proptests below (#5366, Codex P1 on PR #6427):
// plain-text serialization concatenates inline text runs verbatim, so
// a footnote_reference with a non-empty label L between runs B and A
// must serialize to exactly B + "[^" + L + "]" + A, at any nesting
// depth — the implementation only ever push_str's the label between
// the fixed "[^" and "]" markers.
proptest! {
#[test]
fn prop_extract_plain_text_footnote_reference_label_verbatim(
before in "\\PC{0,24}",
label in "\\PC{1,24}",
after in "\\PC{0,24}",
) {
let doc = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [
{"type": "text", "text": before.clone()},
footnote_reference(&label),
{"type": "text", "text": after.clone()}
]})]);
prop_assert_eq!(
extract_plain_text(&doc),
format!("{before}[^{label}]{after}")
);
}

#[test]
fn prop_extract_plain_text_footnote_reference_in_recursive_container(
before in "\\PC{0,24}",
label in "\\PC{1,24}",
after in "\\PC{0,24}",
) {
let doc = plain_doc(vec![serde_json::json!({"type": "bullet_list", "content": [
{"type": "list_item", "content": [serde_json::json!({"type": "paragraph", "content": [
{"type": "text", "text": before.clone()},
footnote_reference(&label),
{"type": "text", "text": after.clone()}
]})]}
]})]);
prop_assert_eq!(
extract_plain_text(&doc),
format!("{before}[^{label}]{after}")
);
}
}

#[test]
fn test_classify_tag_primary() {
let (kind, ts) = classify_tag("my-document.md");
Expand Down
Loading