5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 07:56:23 +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
# 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

View file

@ -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

View file

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

View file

@ -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