Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog.d/20240422_124749_bbolli_jsonfeed_content.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed
-----

* JSONfeed item content is now a list that prefers HTML content.
* A JSONfeed item without content sets the bozo flag.
19 changes: 13 additions & 6 deletions feedparser/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,23 @@ def parse_entry(self, e):
if src in e:
entry[dst] = e[src]

if "content_text" in e:
entry["content"] = c = FeedParserDict()
c["value"] = e["content_text"]
c["type"] = "text"
elif "content_html" in e:
entry["content"] = c = FeedParserDict()
entry["content"] = content = []
if "content_html" in e:
c = FeedParserDict()
c["value"] = sanitize_html(
e["content_html"], self.encoding, "application/json"
)
c["type"] = "html"
content.append(c)
if "content_text" in e:
c = FeedParserDict()
c["value"] = e["content_text"]
c["type"] = "text"
content.append(c)
if not content:
raise ValueError(
f"item {entry['id']=} has neither 'content_text' nor 'content_html'"
)

if "date_published" in e:
entry["published"] = e["date_published"]
Expand Down
18 changes: 18 additions & 0 deletions tests/json/html_first.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"__TEST__": "Description: basic JSON tests Expect: not bozo and items[0].content[0].type == 'html' -->",
"version": "https://jsonfeed.org/version/1",
"title": "html_preferred",
"home_page_url": "https://example.org/",
"feed_url": "https://example.org/feed.json",
"icon": "https://example.org/feed.png",
"author": { "name": "me" },
"items": [
{
"id": "1",
"author": { "name": "you", "url": "http://example.net/~you" },
"content_text": "Hello, world!\n",
"content_html": "<p>Hello, world!</p>\n\n<script>alert();</script>",
"url": "https://example.org/initial-post"
}
]
}
17 changes: 17 additions & 0 deletions tests/json/no_content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"__TEST__": "Description: basic JSON tests Expect: bozo and 'neither' in str(bozo_exception) -->",
"version": "https://jsonfeed.org/version/1",
"title": "no content",
"home_page_url": "https://example.org/",
"feed_url": "https://example.org/feed.json",
"icon": "https://example.org/feed.png",
"author": { "name": "me" },
"items": [
{
"id": "1",
"author": { "name": "you", "url": "http://example.net/~you" },
"summary": "Hello, world!\n",
"url": "https://example.org/initial-post"
}
]
}