Skip to content

Commit 29681a4

Browse files
MattFisherclaude
andcommitted
Fix captionedImage to preserve nested image2.attrs structure
Restore the schema used by post.py's captioned_image() method: captionedImage wraps an image2 node whose attrs dict carries src, alt, href and the remaining layout fields. Update mdrender tests to match. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0276674 commit 29681a4

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

‎substack/nodes.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,29 @@ def code_block(code: str, language: Optional[str] = None) -> Dict:
101101
def captioned_image(
102102
src: str, alt: Optional[str] = None, href: Optional[str] = None
103103
) -> Dict:
104-
node: Dict = {"type": NodeType.CAPTIONED_IMAGE, "src": src}
105-
if alt:
106-
node["alt"] = alt
107-
if href:
108-
node["href"] = href
109-
return node
104+
return {
105+
"type": NodeType.CAPTIONED_IMAGE,
106+
"content": [
107+
{
108+
"type": "image2",
109+
"attrs": {
110+
"src": src,
111+
"fullscreen": False,
112+
"imageSize": "normal",
113+
"height": 819,
114+
"width": 1456,
115+
"resizeWidth": 728,
116+
"bytes": None,
117+
"alt": alt,
118+
"title": None,
119+
"type": None,
120+
"href": href,
121+
"belowTheFold": False,
122+
"internalRedirect": None,
123+
},
124+
}
125+
],
126+
}
110127

111128

112129
def footnote_anchor(number: int) -> Dict:

‎tests/substack/test_from_markdown_features.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,17 @@ def test_image(self):
133133
post.from_markdown("![alt](https://example.com/img.png)")
134134
block = body(post)[0]
135135
assert block["type"] == "captionedImage"
136-
assert block["src"] == "https://example.com/img.png"
137-
assert block["alt"] == "alt"
136+
assert block["content"][0]["type"] == "image2"
137+
attrs = block["content"][0]["attrs"]
138+
assert attrs["src"] == "https://example.com/img.png"
139+
assert attrs["alt"] == "alt"
138140

139141
def test_linked_image(self):
140142
post = make_post()
141143
post.from_markdown("[![alt](https://i/x.png)](https://link)")
142144
block = body(post)[0]
143145
assert block["type"] == "captionedImage"
144-
assert block["src"] == "https://i/x.png"
145-
assert block["href"] == "https://link"
146+
assert block["content"][0]["type"] == "image2"
147+
attrs = block["content"][0]["attrs"]
148+
assert attrs["src"] == "https://i/x.png"
149+
assert attrs["href"] == "https://link"

0 commit comments

Comments
 (0)