5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 07:46:21 +00:00

feat: limpieza de texto

This commit is contained in:
f 2024-03-04 13:09:36 -03:00
parent d1a87177a5
commit b8e7e53ebd
No known key found for this signature in database
4 changed files with 25 additions and 17 deletions

View file

@ -33,10 +33,24 @@ module ApplicationHelper
end end
end end
# Devuelve todas las etiquetas HTML que queremos mantener # Sanitizador que elimina todo
def all_html_tags #
%w[h1 h2 h3 h4 h5 h6 p a ul ol li table tr td th tbody thead # @param html [String]
tfoot em strong sup blockquote cite pre section article] # @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 end
# Genera HTML y limpia etiquetas innecesarias # Genera HTML y limpia etiquetas innecesarias

View file

@ -190,8 +190,8 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
sanitizer sanitizer
.sanitize(string.tr("\r", '').unicode_normalize, .sanitize(string.tr("\r", '').unicode_normalize,
tags: allowed_tags, tags: Sutty::ALLOWED_TAGS,
attributes: allowed_attributes) attributes: Sutty::ALLOWED_ATTRIBUTES)
.strip .strip
.html_safe .html_safe
end end
@ -200,16 +200,6 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
@sanitizer ||= Rails::Html::Sanitizer.safe_list_sanitizer.new @sanitizer ||= Rails::Html::Sanitizer.safe_list_sanitizer.new
end 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 # Decifra el valor
# #
# XXX: Otros tipos de valores necesitan implementar su propio método # XXX: Otros tipos de valores necesitan implementar su propio método

View file

@ -20,7 +20,6 @@
post: @post, attribute: attr, post: @post, attribute: attr,
metadata: metadata, metadata: metadata,
site: @site, site: @site,
tags: all_html_tags,
locale: @locale, locale: @locale,
dir: dir) dir: dir)

View file

@ -37,6 +37,11 @@ if %w[development test].include? ENV['RAILS_ENV']
end end
module Sutty 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! # Sutty!
class Application < Rails::Application class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails # Initialize configuration defaults for originally generated Rails