module StringTools::HTML
Constants
- HTML_SERIALIZE_OPTIONS
- TEXT_WITH_LINKS_MINIMUM_LENGTH
минимальная длина строки, в которой могут быть ссылки
Public Class Methods
remove_links(html, options = {})
click to toggle source
Public: Удаляет ссылки на неразрешенные домены
html - String
содержимое потенциально ненужных ссылок options - Hash
:whitelist - Array of String разрешенныe домены
Examples
html = '<a href="https://www.yandex.ru">yandex</a>' StringTools::HTML.remove_links(html, whitelist: ['google.com']) # => 'yandex' StringTools::HTML.remove_links(html, whitelist: ['yandex.ru']) # => '<a href="https://www.yandex.ru">yandex</a>' StringTools::HTML.remove_links(html, whitelist: ['www.yandex.ru']) # => '<a href="https://www.yandex.ru">yandex</a>' html = '<a href="https://yandex.ru">yandex</a>' StringTools::HTML.remove_links(html, whitelist: ['www.yandex.ru']) # => 'yandex'
Returns String
without links to external resources
# File lib/string_tools/html.rb, line 42 def self.remove_links(html, options = {}) return html if html.length < TEXT_WITH_LINKS_MINIMUM_LENGTH doc = Nokogiri::HTML::DocumentFragment.parse(html) scrubber = LinksRemoveScrubber.new(options) doc.css('a').each { |node| scrubber.call node } if scrubber.done_changes? doc.children.map { |node| node.serialize HTML_SERIALIZE_OPTIONS }.join else html end end