fix: strip HTML tags from book descriptions for proper display

This commit is contained in:
Claude 2025-11-21 11:05:38 +00:00
parent 2b314696ac
commit d6e49238ec
No known key found for this signature in database
2 changed files with 35 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import 'package:vaani/features/item_viewer/view/library_item_sliver_app_bar.dart
import 'package:vaani/features/player/view/mini_player_bottom_padding.dart';
import 'package:vaani/router/models/library_item_extras.dart';
import 'package:vaani/shared/widgets/expandable_description.dart';
import 'package:vaani/shared/utils/html_utils.dart';
import 'library_item_actions.dart';
import 'library_item_hero_section.dart';
@ -149,9 +150,16 @@ class LibraryItemDescription extends HookConsumerWidget {
if (item == null) {
return const SizedBox();
}
// Get description and strip HTML tags
final rawDescription = item.media.metadata.description;
final cleanDescription = rawDescription != null
? HtmlUtils.stripHtml(rawDescription)
: 'Sorry, no description found';
return ExpandableDescription(
title: 'About the Book',
content: item.media.metadata.description ?? 'Sorry, no description found',
content: cleanDescription,
);
}
}

View file

@ -0,0 +1,26 @@
/// Utility functions for handling HTML content
class HtmlUtils {
/// Strips HTML tags and decodes common HTML entities from a string
static String stripHtml(String htmlString) {
// Remove HTML tags
String text = htmlString.replaceAll(RegExp(r'<[^>]*>'), '');
// Decode common HTML entities
text = text
.replaceAll('&nbsp;', ' ')
.replaceAll('&amp;', '&')
.replaceAll('&lt;', '<')
.replaceAll('&gt;', '>')
.replaceAll('&quot;', '"')
.replaceAll('&#39;', "'")
.replaceAll('&apos;', "'");
// Replace multiple spaces with single space
text = text.replaceAll(RegExp(r'\s+'), ' ');
// Trim leading/trailing whitespace
text = text.trim();
return text;
}
}