mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 01:26:21 +00:00
feat: limpieza de texto
This commit is contained in:
parent
d1a87177a5
commit
b8e7e53ebd
4 changed files with 25 additions and 17 deletions
|
@ -33,10 +33,24 @@ module ApplicationHelper
|
|||
end
|
||||
end
|
||||
|
||||
# Devuelve todas las etiquetas HTML que queremos mantener
|
||||
def all_html_tags
|
||||
%w[h1 h2 h3 h4 h5 h6 p a ul ol li table tr td th tbody thead
|
||||
tfoot em strong sup blockquote cite pre section article]
|
||||
# Sanitizador que elimina todo
|
||||
#
|
||||
# @param html [String]
|
||||
# @return [String]
|
||||
def text_plain(html)
|
||||
sanitize(html, tags: [], attributes: [])
|
||||
end
|
||||
|
||||
# Sanitizador con etiquetas y atributos por defecto
|
||||
#
|
||||
# @param html [String]
|
||||
# @param options [Hash]
|
||||
# @return [String]
|
||||
def sanitize(html, options = {})
|
||||
options[:tags] ||= Sutty::ALLOWED_TAGS
|
||||
options[:attributes] ||= Sutty::ALLOWED_ATTRIBUTES
|
||||
|
||||
super(html, options)
|
||||
end
|
||||
|
||||
# Genera HTML y limpia etiquetas innecesarias
|
||||
|
|
|
@ -190,8 +190,8 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
|
|||
|
||||
sanitizer
|
||||
.sanitize(string.tr("\r", '').unicode_normalize,
|
||||
tags: allowed_tags,
|
||||
attributes: allowed_attributes)
|
||||
tags: Sutty::ALLOWED_TAGS,
|
||||
attributes: Sutty::ALLOWED_ATTRIBUTES)
|
||||
.strip
|
||||
.html_safe
|
||||
end
|
||||
|
@ -200,16 +200,6 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
|
|||
@sanitizer ||= Rails::Html::Sanitizer.safe_list_sanitizer.new
|
||||
end
|
||||
|
||||
def allowed_attributes
|
||||
@allowed_attributes ||= %w[style href src alt controls data-align data-multimedia data-multimedia-inner id
|
||||
name rel target referrerpolicy class colspan rowspan role data-turbo start type reversed].freeze
|
||||
end
|
||||
|
||||
def allowed_tags
|
||||
@allowed_tags ||= %w[strong em del u mark p h1 h2 h3 h4 h5 h6 ul ol li img iframe audio video div figure blockquote
|
||||
figcaption a sub sup small table thead tbody tfoot tr th td br code].freeze
|
||||
end
|
||||
|
||||
# Decifra el valor
|
||||
#
|
||||
# XXX: Otros tipos de valores necesitan implementar su propio método
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
post: @post, attribute: attr,
|
||||
metadata: metadata,
|
||||
site: @site,
|
||||
tags: all_html_tags,
|
||||
locale: @locale,
|
||||
dir: dir)
|
||||
|
||||
|
|
|
@ -37,6 +37,11 @@ if %w[development test].include? ENV['RAILS_ENV']
|
|||
end
|
||||
|
||||
module Sutty
|
||||
ALLOWED_ATTRIBUTES = %w[style href src alt controls data-align data-multimedia data-multimedia-inner id name rel
|
||||
target referrerpolicy class colspan rowspan role data-turbo start type reversed].freeze
|
||||
ALLOWED_TAGS = %w[strong em del u mark p h1 h2 h3 h4 h5 h6 ul ol li img iframe audio video div figure blockquote
|
||||
figcaption a sub sup small table thead tbody tfoot tr th td br code].freeze
|
||||
|
||||
# Sutty!
|
||||
class Application < Rails::Application
|
||||
# Initialize configuration defaults for originally generated Rails
|
||||
|
|
Loading…
Reference in a new issue