diff --git a/app/models/note.rb b/app/models/note.rb index 669799ef..7f7d3ee4 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -3,8 +3,8 @@ require "twitter" require "json" require "uri" -require "html/pipeline" -require "html/pipeline/hashtag/hashtag_filter" +require "html_pipeline" +require "html_pipeline/node_filter/mention_filter" class Note < Content include PublifyGuid @@ -33,37 +33,51 @@ class Note < Content TWITTER_HTTPS_URL_LENGTH = 21 TWITTER_LINK_LENGTH = 22 - class TwitterHashtagFilter < HTML::Pipeline::HashtagFilter - def initialize(text) - super(text, - tag_url: "https://twitter.com/search?q=%%23%s&src=tren&mode=realtime", - tag_link_attr: "") + class TwitterHashtagFilter < HTMLPipeline::NodeFilter + def after_initialize + context[:tag_url] ||= "https://twitter.com/search?q=%%23%s&src=tren&mode=realtime" end - end - class TwitterMentionFilter < HTML::Pipeline::MentionFilter - def initialize(text) - super(text, base_url: "https://twitter.com") + SELECTOR = Selma::Selector.new(match_text_within: "*", + ignore_text_within: ["a"]) + + def selector + SELECTOR end - # Override base mentions finder, treating @mention just like any other @foo. - def self.mentioned_logins_in(text, username_pattern = UsernamePattern) - text.gsub MentionPatterns[username_pattern] do |match| - login = Regexp.last_match(1) - yield match, login, false + HASHTAG_PATTERN = /(?<=^|\W)#([-_A-Za-z0-9]+)(?=\W|$)/ + + def handle_text_chunk(chunk) + text = chunk.to_s + + html = text.gsub(HASHTAG_PATTERN) do |match| + tag = Regexp.last_match(1) + url = format context[:tag_url], tag: tag + "#{match}" end + + return chunk if html == text + + chunk.replace(html, as: :html) + end + end + + class TwitterMentionFilter < HTMLPipeline::NodeFilter::MentionFilter + def after_initialize + super + context[:base_url] ||= "https://twitter.com" + context[:info_url] ||= "https://foo.com" end # Override base link creator, removing the class - def link_to_mentioned_user(login) + def link_to_mentioned_user(base_url, login) result[:mentioned_usernames] |= [login] url = base_url.dup - url << "/" unless %r{[/~]\z}.match?(url) + excluded_prefixes = %r{[/(?:~|@]\z} + url << "/" unless excluded_prefixes.match?(url) - "" \ - "@#{login}" \ - "" + "@#{login}" end end @@ -82,7 +96,8 @@ def tags def generate_html(field, text = nil) if field == :in_reply_to - html = TextFilter.make_filter("none").filter_text(text) + helper = PublifyCore::ContentTextHelpers.new + html = helper.simple_format(text) html_postprocess(field, html).to_s else super @@ -93,8 +108,8 @@ def html_postprocess(field, html) helper = PublifyCore::ContentTextHelpers.new html = helper.auto_link(html) - html = TwitterHashtagFilter.new(html).call - html = TwitterMentionFilter.new(html).call.to_s + html = TwitterHashtagFilter.call(html) + html = TwitterMentionFilter.call(html).to_s super end diff --git a/publify_core.gemspec b/publify_core.gemspec index 9aedf4b9..a8bd0153 100644 --- a/publify_core.gemspec +++ b/publify_core.gemspec @@ -30,8 +30,7 @@ Gem::Specification.new do |s| s.add_dependency "devise_zxcvbn", "~> 6.0" s.add_dependency "fog-aws", "~> 3.2" s.add_dependency "fog-core", "~> 2.2" - s.add_dependency "html-pipeline", "~> 2.14" - s.add_dependency "html-pipeline-hashtag", "~> 0.1.2" + s.add_dependency "html-pipeline", "~> 3.2" s.add_dependency "jquery-rails", ">= 4.5", "< 4.7" s.add_dependency "jquery-ui-rails", ">= 7", "< 9" s.add_dependency "kaminari", ["~> 1.2", ">= 1.2.1"]