Skip to content

Commit

Permalink
Feat: added black list for ignore certain nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
CatHood0 committed Sep 15, 2024
1 parent 25b3c6f commit f78c697
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/parser/html_to_delta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ class HtmlToDelta {
/// Converts HTML tags to Delta operations based on defined rules.
late HtmlOperations htmlToOp;

/// This is a list that must contains only the tag name
/// of the all HTML Nodes (`<p>`, `<div>` or `<h1>`) that will be
/// ignored and inserted as plain text
///
/// # Example
/// Assume that you want to ignore just HTML containers. Then just need
/// to do something like this:
/// __
/// ```dart
/// final List containerBlackList = ['div', 'section', 'article'];
///
/// final converter = HtmlToDelta(blackNodesList: containerBlackList);
/// final delta = converter.convert(<your_html>);
/// ```
final List<String> blackNodesList;

/// ## Optionally trims converted text
///
/// This could cause some **unexpected** behaviors since we cannot
Expand Down Expand Up @@ -77,6 +93,7 @@ class HtmlToDelta {
/// [customBlocks] allows adding custom rules for handling specific HTML tags.
HtmlToDelta({
HtmlOperations? htmlToOperations,
this.blackNodesList = const [],
this.customBlocks,
this.trimText = true,
this.replaceNormalNewLinesToBr = false,
Expand Down Expand Up @@ -230,6 +247,11 @@ class HtmlToDelta {
operations.add(Operation.insert(trimText ? node.text.trim() : node.text));
}
if (node is dom.Element) {
if (blackNodesList.contains(node.localName)) {
operations
.add(Operation.insert(trimText ? node.text.trim() : node.text));
return operations;
}
List<Operation> ops = htmlToOp.resolveCurrentElement(node);
operations.addAll(ops);
}
Expand Down

0 comments on commit f78c697

Please sign in to comment.