forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport 'Fix for internal links not displaying on page title' to v0.…
…26 (decidim#9407) * Fix for internal links not displaying on page title (decidim#9228) * Display proposal's/meeting's title as a href when referring to internal links in proposal's description meeting's body and comments. * Fix typo from renderer spec Fix rubocop issues Refactoring * Fixing double break line rendering issue on resource * Running Linters * Fix link display on "compare proposals" step. Check organization host when parsing internal links. * Rewrite the organization check Co-authored-by: Alexandru-Emil Lupu <[email protected]> * Fix rubocop offence Co-authored-by: roxanaopr <[email protected]> Co-authored-by: Alexandru-Emil Lupu <[email protected]>
- Loading branch information
1 parent
b9783f9
commit a6bddc3
Showing
29 changed files
with
457 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
decidim-core/lib/decidim/content_parsers/resource_parser.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ContentParsers | ||
# A parser that searches mentions of Resources in content. | ||
# | ||
# @see BaseParser Examples of how to use a content parser | ||
class ResourceParser < BaseParser | ||
# Matches a URL | ||
URL_REGEX_SCHEME = '(?:http(s)?:\/\/)' | ||
URL_REGEX_CONTENT = '[\w.-]+[\w\-\._~:\/?#\[\]@!\$&\'\(\)\*\+,;=.]+' | ||
URL_REGEX_END_CHAR = '[\d]' | ||
# Matches a mentioned resource ID (~(d)+ expression) | ||
ID_REGEX = /~(\d+)/.freeze | ||
|
||
# Replaces found mentions matching an existing | ||
# Resource with a global id for that Resource. Other mentions found that doesn't | ||
# match an existing Resource are returned as they are. | ||
# | ||
# @return [String] the content with the valid mentions replaced by a global id. | ||
def rewrite | ||
rewrited_content = parse_for_urls(content) | ||
parse_for_ids(rewrited_content) | ||
end | ||
|
||
private | ||
|
||
def parse_for_urls(content) | ||
content.gsub(url_regex) do |match| | ||
resource = resource_from_url_match(match) | ||
if resource | ||
update_metadata(resource) | ||
resource.to_global_id | ||
else | ||
match | ||
end | ||
end | ||
end | ||
|
||
def parse_for_ids(content) | ||
content.gsub(ID_REGEX) do |match| | ||
resource = resource_from_id_match(Regexp.last_match(1)) | ||
if resource | ||
update_metadata(resource) | ||
resource.to_global_id | ||
else | ||
match | ||
end | ||
end | ||
end | ||
|
||
def resource_from_url_match(match) | ||
uri = URI.parse(match) | ||
return if uri.path.blank? | ||
return unless find_organization(uri.host) | ||
|
||
resource_id = uri.path.split("/").last | ||
find_resource_by_id(resource_id) | ||
rescue URI::InvalidURIError | ||
Rails.logger.error("#{e.message}=>#{e.backtrace}") | ||
nil | ||
end | ||
|
||
def resource_from_id_match(match) | ||
resource_id = match | ||
find_resource_by_id(resource_id) | ||
end | ||
|
||
def find_resource_by_id(id) | ||
if id.present? | ||
spaces = Decidim.participatory_space_manifests.flat_map do |manifest| | ||
manifest.participatory_spaces.call(context[:current_organization]).public_spaces | ||
end | ||
components = Component.where(participatory_space: spaces).published | ||
model_class.constantize.where(component: components).find_by(id: id) | ||
end | ||
end | ||
|
||
def find_organization(uri_host) | ||
current_organization = context[:current_organization] | ||
(current_organization.host == uri_host) || current_organization.secondary_hosts.include?(uri_host) | ||
end | ||
|
||
def url_regex | ||
raise "Not implemented" | ||
end | ||
|
||
def model_class | ||
raise "Not implemented" | ||
end | ||
|
||
def update_metadata(resource) | ||
# code to update metadata - needs to be overwritten | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
decidim-core/lib/decidim/content_renderers/resource_renderer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ContentRenderers | ||
class ResourceRenderer < BaseRenderer | ||
# Matches a global id representing a Decidim::User | ||
|
||
# Replaces found Global IDs matching an existing resource with | ||
# a link to its show page. The Global IDs representing an | ||
# invalid Resource are replaced with '???' string. | ||
# | ||
# @return [String] the content ready to display (contains HTML) | ||
def render(_options = nil) | ||
return content unless content.respond_to?(:gsub) | ||
|
||
content.gsub(regex) do |resource_gid| | ||
resource = GlobalID::Locator.locate(resource_gid) | ||
resource.presenter.display_mention | ||
rescue ActiveRecord::RecordNotFound | ||
resource_id = resource_gid.split("/").last | ||
"~#{resource_id}" | ||
end | ||
end | ||
|
||
def regex | ||
raise "Not implemented" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
decidim-meetings/lib/decidim/content_parsers/meeting_parser.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ContentParsers | ||
# A parser that searches mentions of Meetings in content. | ||
# | ||
# @see BaseParser Examples of how to use a content parser | ||
class MeetingParser < ResourceParser | ||
private | ||
|
||
def url_regex | ||
%r{#{URL_REGEX_SCHEME}#{URL_REGEX_CONTENT}/meetings/#{URL_REGEX_END_CHAR}+}i | ||
end | ||
|
||
def model_class | ||
"Decidim::Meetings::Meeting" | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
decidim-meetings/lib/decidim/content_renderers/meeting_renderer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ContentRenderers | ||
# A renderer that searches Global IDs representing meetings in content | ||
# and replaces it with a link to their show page. | ||
# | ||
# e.g. gid://<APP_NAME>/Decidim::Meetings::Meeting/1 | ||
# | ||
# @see BaseRenderer Examples of how to use a content renderer | ||
class MeetingRenderer < ResourceRenderer | ||
def regex | ||
%r{gid://([\w-]*/Decidim::Meetings::Meeting/(\d+))}i | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.