end
text_node.replace(replacement)
end
+
+ @tree.css('spoiler-text').each do |spoiler_node|
+ # Replace each +<spoiler-text>+ node with a span which reflects
+ # it.
+ #
+ # Note that this elimanates any markup within the
+ # +<spoiler-text>+ node.
+ content = spoiler_node.content
+ elt = Nokogiri::XML::Element.new('span', document)
+ elt['property'] = 'tag:ns.1024.gdn,2022-11-11:spoiler_text'
+ elt['content'] = content
+ elt << Nokogiri::XML::Text.new(
+ encode_spoiler(content),
+ document
+ )
+ spoiler_node.replace(elt)
+ end
end
@tree
end
private
+ def encode_spoiler(text)
+ result = ''.dup
+ text.unicode_normalize(:nfkd).each_char do |char|
+ result << if /[A-Ma-m]/.match?(char)
+ char.codepoints[0] + 13
+ elsif /[N-Zn-z]/.match?(char)
+ char.codepoints[0] - 13
+ elsif /[[:alpha:]]/.match?(char)
+ 0xFFFD
+ else
+ char
+ end
+ end
+ result
+ end
+
def format_markdown(html)
html = markdown_formatter.render(html)
html.delete("\r").delete("\n")
node['class'] = class_list.join(' ')
end
+ ##
+ # Deletes the +property+ and +content+ properties if the value of
+ # +property+ is not a recognized IRI.
+ PROPERTY_ALLOWLIST_TRANSFORMER = lambda do |env|
+ node = env[:node]
+ return if node['property'].present? && %w(
+ tag:ns.1024.gdn,2022-11-11:spoiler_text
+ ).include?(node['property'])
+ node.remove_attribute('property')
+ node.remove_attribute('content')
+ end
+
IMG_TAG_TRANSFORMER = lambda do |env|
node = env[:node]
attributes: {
'a' => %w(href rel class title),
- 'span' => %w(class),
+ 'span' => %w(class property content),
'abbr' => %w(title),
'blockquote' => %w(cite),
'ol' => %w(start reversed),
transformers: [
CLASS_WHITELIST_TRANSFORMER,
+ PROPERTY_ALLOWLIST_TRANSFORMER,
IMG_TAG_TRANSFORMER,
UNSUPPORTED_HREF_TRANSFORMER,
]
end
MASTODON_OUTGOING ||= freeze_config MASTODON_STRICT.merge(
+ elements: MASTODON_STRICT[:elements] + %w(spoiler-text),
attributes: merge(
MASTODON_STRICT[:attributes],
- 'a' => %w(href rel class title target)
+ 'a' => %w(href rel class title target),
+ 'span' => %w(class) # do not allow manual property setting
),
add_attributes: {},