Skip to content

Commit

Permalink
JSONfeed: prefer HTML over text content
Browse files Browse the repository at this point in the history
HTML has generally more useful information than text, so let's move it
first in the content list. Users that want text can still iterate over
the list and pick the text type.

Signed-off-by: Beat Bolli <[email protected]>
  • Loading branch information
bbolli committed Apr 22, 2024
1 parent 78b55b0 commit 97c648c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions feedparser/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ def parse_entry(self, e):
entry[dst] = e[src]

content = []
if "content_text" in e:
c = FeedParserDict()
c["value"] = e["content_text"]
c["type"] = "text"
content.append(c)
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 content:
entry["content"] = content

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"
}
]
}

0 comments on commit 97c648c

Please sign in to comment.