From 7f4d20ed9d3231af81a5e4e4d2e08a463676002d Mon Sep 17 00:00:00 2001 From: David Lacroix Date: Sun, 15 Nov 2026 18:28:01 -0500 Subject: [PATCH] fix(googlebooks): show descriptions as plain text, not HTML (#1723) With Google Books as the metadata provider, every description shows its HTML, starting with a literal `

`. The API returns HTML, `_parse_volume` passes it on with the comment "leave as-is for UI to sanitize", and the UI renders `{book.description}` as text in both `ReleaseModal.tsx` and `DetailsModal.tsx`. This turns the description into plain text in the provider: `
` becomes a line break, the end of a block element a blank line, other tags are dropped and entities decoded. The details modal uses `whitespace-pre-line`, so the breaks survive there. Text that only looks like a tag is left alone (`2 < 3 and a > b`). Tests are in `tests/metadata/test_googlebooks_parse.py`. With the source change reverted all three fail. `make python-checks` is clean and `tests/metadata` passes. I did not reuse `_blurb_to_text` from the DNB provider: it is specific to DNB blurb pages. ## Summary by CodeRabbit * **Bug Fixes** * Google Books descriptions are cleaned up for display: HTML formatting is removed, entities are decoded, and paragraph breaks are preserved. Empty descriptions are treated as missing. --- shelfmark/metadata_providers/googlebooks.py | 36 +++++++++++++++++- tests/metadata/test_googlebooks_parse.py | 41 +++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/shelfmark/metadata_providers/googlebooks.py b/shelfmark/metadata_providers/googlebooks.py index d7d24652..bfcdf27c 100644 --- a/shelfmark/metadata_providers/googlebooks.py +++ b/shelfmark/metadata_providers/googlebooks.py @@ -6,6 +6,8 @@ Requires a free API key from Google Cloud Console (~1000 requests/day quota). API Documentation: https://developers.google.com/books/docs/v1/using """ +import html +import re from contextlib import suppress from dataclasses import replace from http import HTTPStatus @@ -43,6 +45,36 @@ from shelfmark.metadata_providers import ( logger = setup_logger(__name__) +# A tag starts with a letter after "<" or " b" is left alone. +_HTML_TAG_RE = re.compile(r"]*>") +# A br with attributes is still a line break; the word boundary keeps out any other tag +# whose name only starts with "br". +_LINE_BREAK_RE = re.compile(r"]*>", re.IGNORECASE) +_BLOCK_END_RE = re.compile(r"", re.IGNORECASE) +_TRAILING_SPACE_RE = re.compile(r"[ \t]+\n") +_EXTRA_BLANK_LINES_RE = re.compile(r"\n{3,}") + + +def _description_to_text(description: object) -> str | None: + """Turn a Google Books description, which is HTML, into plain text. + + The UI renders a description as text, never as markup, so leaving the HTML in showed + every description with its tags, starting with a literal "

". Converting it here + keeps the paragraph and line breaks, which the details view preserves, and avoids + putting markup from the API into the page. + """ + if not isinstance(description, str): + return None + text = _LINE_BREAK_RE.sub("\n", description) + text = _BLOCK_END_RE.sub("\n\n", text) + text = _HTML_TAG_RE.sub("", text) + # Entities are decoded after the tags are gone, so an escaped "<b>" stays text. + text = html.unescape(text) + text = _TRAILING_SPACE_RE.sub("\n", text) + text = _EXTRA_BLANK_LINES_RE.sub("\n\n", text).strip() + return text or None + + _HTTP_STATUS_FORBIDDEN = HTTPStatus.FORBIDDEN _HTTP_STATUS_BAD_REQUEST = HTTPStatus.BAD_REQUEST _HTTP_STATUS_NOT_FOUND = HTTPStatus.NOT_FOUND @@ -341,8 +373,8 @@ class GoogleBooksProvider(MetadataProvider): # Genres/categories (limit to 5) genres = volume_info.get("categories", [])[:5] - # Description (may contain HTML - leave as-is for UI to sanitize) - description = volume_info.get("description") + # Description: the API sends HTML, the UI shows text + description = _description_to_text(volume_info.get("description")) # Source URL source_url = volume_info.get("infoLink") diff --git a/tests/metadata/test_googlebooks_parse.py b/tests/metadata/test_googlebooks_parse.py index ee719dbc..56766c64 100644 --- a/tests/metadata/test_googlebooks_parse.py +++ b/tests/metadata/test_googlebooks_parse.py @@ -174,6 +174,47 @@ class TestGoogleBooksParseVolume: assert result is None + @staticmethod + def _description_of(description): + provider = GoogleBooksProvider(api_key="test-key") + result = provider._parse_volume( + { + "id": "volume-3", + "volumeInfo": {"title": "Described Book", "description": description}, + } + ) + assert result is not None + return result.description + + def test_parse_volume_turns_html_description_into_plain_text(self): + """Google Books sends HTML and the UI renders it as text: users saw a literal

.""" + description = self._description_of( + "

First paragraph.

Second, with bold & italic." + "
A new line.

" + ) + + assert description == "First paragraph.\n\nSecond, with bold & italic.\nA new line." + + def test_parse_volume_keeps_a_line_break_written_with_attributes(self): + """A `
` was dropped as a plain tag and joined the lines ("FirstSecond").""" + assert self._description_of('First
Second') == "First\nSecond" + + def test_parse_volume_keeps_an_uppercase_self_closing_line_break(self): + """Tag names are case-insensitive in HTML, and `
` is common in older blurbs.""" + assert self._description_of("First
Second") == "First\nSecond" + + def test_parse_volume_keeps_angle_brackets_that_are_not_tags(self): + """Only something shaped like a tag is removed, never text around a < or >. + + A looser `<[^>]+>` would eat "< 3 and a >" here along with the real tags. + """ + assert self._description_of("

2 < 3 and a > b

") == "2 < 3 and a > b" + + def test_parse_volume_reports_an_empty_description_as_missing(self): + """A description with nothing left to show must not render as a blank block.""" + assert self._description_of("") is None + assert self._description_of("


") is None + class _FullPageSession: def __init__(self, count):