Added cashtag support for statuses

• Added:
- cashtag support for statuses

Ref: e23931b255
This commit is contained in:
mgabdev 2020-10-30 14:01:55 -05:00
parent 9f712198dc
commit 6db1cf421b
6 changed files with 67 additions and 18 deletions

View File

@ -54,6 +54,10 @@ function hashtagStrategy(contentBlock, callback, contentState) {
findWithRegex(HASHTAG_REGEX, contentBlock, callback) findWithRegex(HASHTAG_REGEX, contentBlock, callback)
} }
function cashtagStrategy(contentBlock, callback, contentState) {
findWithRegex(CASHTAG_REGEX, contentBlock, callback)
}
function urlStrategy(contentBlock, callback, contentState) { function urlStrategy(contentBlock, callback, contentState) {
findWithRegex(urlRegex, contentBlock, callback) findWithRegex(urlRegex, contentBlock, callback)
} }
@ -87,6 +91,10 @@ const compositeDecorator = new CompositeDecorator([
strategy: hashtagStrategy, strategy: hashtagStrategy,
component: HighlightedSpan, component: HighlightedSpan,
}, },
{
strategy: cashtagStrategy,
component: HighlightedSpan,
},
{ {
strategy: urlStrategy, strategy: urlStrategy,
component: HighlightedSpan, component: HighlightedSpan,
@ -109,6 +117,7 @@ const styleMap = {
const GROUP_HANDLE_REGEX = /\g\/[\w]+/g const GROUP_HANDLE_REGEX = /\g\/[\w]+/g
const HANDLE_REGEX = /\@[\w]+/g const HANDLE_REGEX = /\@[\w]+/g
const HASHTAG_REGEX = /\#[\w\u0590-\u05ff]+/g const HASHTAG_REGEX = /\#[\w\u0590-\u05ff]+/g
const CASHTAG_REGEX = /\$[\w\u0590-\u05ff]+/g
class Composer extends React.PureComponent { class Composer extends React.PureComponent {

View File

@ -50,7 +50,7 @@ class StatusContent extends ImmutablePureComponent {
link.addEventListener('click', this.onMentionClick.bind(this, mention), false) link.addEventListener('click', this.onMentionClick.bind(this, mention), false)
link.setAttribute('title', mention.get('acct')) link.setAttribute('title', mention.get('acct'))
link.removeAttribute('target') link.removeAttribute('target')
} else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) { } else if (['#', '$'].includes(link.textContent[0]) || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false) link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false)
link.removeAttribute('target') link.removeAttribute('target')
} else { } else {
@ -85,7 +85,7 @@ class StatusContent extends ImmutablePureComponent {
} }
onHashtagClick = (hashtag, e) => { onHashtagClick = (hashtag, e) => {
hashtag = hashtag.replace(/^#/, '').toLowerCase() hashtag = hashtag.replace(/^(#|\$)/, '').toLowerCase()
if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) { if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
e.preventDefault() e.preventDefault()

View File

@ -13,10 +13,12 @@ module Extractor
text.to_s.scan(Account::MENTION_RE) do |screen_name, _| text.to_s.scan(Account::MENTION_RE) do |screen_name, _|
match_data = $LAST_MATCH_INFO match_data = $LAST_MATCH_INFO
after = $' after = $'
unless after =~ Twitter::Regex[:end_mention_match] unless after =~ Twitter::Regex[:end_mention_match]
start_position = match_data.char_begin(1) - 1 start_position = match_data.char_begin(1) - 1
end_position = match_data.char_end(1) end_position = match_data.char_end(1)
possible_entries << { possible_entries << {
screen_name: screen_name, screen_name: screen_name,
indices: [start_position, end_position], indices: [start_position, end_position],
@ -24,26 +26,25 @@ module Extractor
end end
end end
if block_given? possible_entries.each { |mention| yield mention[:screen_name], mention[:indices].first, mention[:indices].last } if block_given?
possible_entries.each do |mention|
yield mention[:screen_name], mention[:indices].first, mention[:indices].last
end
end
possible_entries possible_entries
end end
# :yields: hashtag, start, end
def extract_hashtags_with_indices(text, **) def extract_hashtags_with_indices(text, **)
return [] unless text =~ /#/ return [] unless text =~ /#/
tags = [] tags = []
text.scan(Tag::HASHTAG_RE) do |hash_text, _| text.scan(Tag::HASHTAG_RE) do |hash_text, _|
match_data = $LAST_MATCH_INFO match_data = $LAST_MATCH_INFO
start_position = match_data.char_begin(1) - 1 start_position = match_data.char_begin(1) - 1
end_position = match_data.char_end(1) end_position = match_data.char_end(1)
after = $' after = $'
if after =~ %r{\A://} if after =~ %r{\A://}
hash_text.match(/(.+)(https?\Z)/) do |matched| hash_text.match(/(.+)(https?\Z)/) do |matched|
hash_text = matched[1] hash_text = matched[1]
end_position -= matched[2].char_length end_position -= matched[2].char_length
end end
end end
@ -58,7 +59,34 @@ module Extractor
tags tags
end end
def extract_cashtags_with_indices(_text) # :yields: cashtag, start, end
[] # always returns empty array def extract_cashtags_with_indices(text)
return [] unless text =~ /\$/
tags = []
text.scan(Tag::CASHTAG_RE) do |cash_text, _|
match_data = $LAST_MATCH_INFO
start_position = match_data.char_begin(1) - 1
end_position = match_data.char_end(1)
after = $'
next if cash_text.size > 5
if after =~ %r{\A://}
cash_text.match(/(.+)(https?\Z)/) do |matched|
cash_text = matched[1]
end_position -= matched[2].char_length
end
end
tags << {
cashtag: cash_text,
indices: [start_position, end_position],
}
end
tags.each { |tag| yield tag[:cashtag], tag[:indices].first, tag[:indices].last } if block_given?
tags
end end
end end

View File

@ -258,6 +258,8 @@ class Formatter
link_to_url(entity, options) link_to_url(entity, options)
elsif entity[:hashtag] elsif entity[:hashtag]
link_to_hashtag(entity) link_to_hashtag(entity)
elsif entity[:cashtag]
link_to_cashtag(entity)
elsif entity[:screen_name] elsif entity[:screen_name]
link_to_mention(entity, accounts) link_to_mention(entity, accounts)
end end
@ -396,6 +398,7 @@ class Formatter
end end
entities = Extractor.extract_hashtags_with_indices(escaped, :check_url_overlap => false) + entities = Extractor.extract_hashtags_with_indices(escaped, :check_url_overlap => false) +
Extractor.extract_cashtags_with_indices(escaped, :check_url_overlap => false) +
Extractor.extract_mentions_or_lists_with_indices(escaped) Extractor.extract_mentions_or_lists_with_indices(escaped)
Extractor.remove_overlapping_entities(entities).map do |extract| Extractor.remove_overlapping_entities(entities).map do |extract|
pos = extract[:indices].first pos = extract[:indices].first
@ -440,6 +443,10 @@ class Formatter
hashtag_html(entity[:hashtag]) hashtag_html(entity[:hashtag])
end end
def link_to_cashtag(entity)
cashtag_html(entity[:cashtag])
end
def link_html(url) def link_html(url)
url = Addressable::URI.parse(url).to_s url = Addressable::URI.parse(url).to_s
prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s
@ -454,6 +461,10 @@ class Formatter
"<a data-focusable=\"true\" role=\"link\" href=\"#{encode(tag_url(tag.downcase))}\" class=\"mention hashtag\" rel=\"tag\">##{encode(tag)}</a>" "<a data-focusable=\"true\" role=\"link\" href=\"#{encode(tag_url(tag.downcase))}\" class=\"mention hashtag\" rel=\"tag\">##{encode(tag)}</a>"
end end
def cashtag_html(tag)
"<a data-focusable=\"true\" role=\"link\" href=\"#{encode(tag_url(tag.downcase))}\" class=\"mention hashtag cashtag\" rel=\"tag\">$#{encode(tag)}</a>"
end
def mention_html(account) def mention_html(account)
return "<span>@#{encode(account.acct)}</span>" unless account.local? return "<span>@#{encode(account.acct)}</span>" unless account.local?
"<a data-focusable=\"true\" role=\"link\" href=\"#{encode(TagManager.instance.url_for(account))}\" class=\"u-url mention\">@#{encode(account.acct)}</a>" "<a data-focusable=\"true\" role=\"link\" href=\"#{encode(TagManager.instance.url_for(account))}\" class=\"u-url mention\">@#{encode(account.acct)}</a>"

View File

@ -18,7 +18,8 @@ class Tag < ApplicationRecord
has_one :account_tag_stat, dependent: :destroy has_one :account_tag_stat, dependent: :destroy
HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_·][[:word:]_]*' HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_·][[:word:]_]*'
HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i.freeze
CASHTAG_RE = /(?:^|[^\/\)\w])\$([a-zA-Z]{2,5})/.freeze
validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i } validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i }

View File

@ -2,7 +2,7 @@
class ProcessHashtagsService < BaseService class ProcessHashtagsService < BaseService
def call(status, tags = []) def call(status, tags = [])
tags = Extractor.extract_hashtags(status.text) if status.local? tags = Extractor.extract_hashtags(status.text) + Extractor.extract_cashtags(status.text) if status.local?
records = [] records = []
tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |name| tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |name|