Allow Wiki links to pages that have number signs in title

This commit is contained in:
JuliusR 2019-06-15 16:21:02 +02:00 committed by wvengen
parent 8b0c749c5b
commit 3ade87167a

View file

@ -9,13 +9,13 @@ module FoodsoftWiki
url_for page url_for page
end end
link_attributes_for do |page_anchor| link_attributes_for do |page|
page, anchor = page_anchor.split(/#/, 2)
permalink = Page.permalink(page) permalink = Page.permalink(page)
if page.empty? if Page.exists?(:permalink => permalink)
{ href: '#' + anchor } { href: url_for(:wiki_page_path, permalink: permalink) }
elsif Page.exists?(:permalink => permalink) elsif page.include? '#'
{ href: url_for(:wiki_page_path, permalink: permalink, anchor: anchor) } # If "Foo#Bar" does not exist then consider "Foo" with anchor.
link_attributes_if_number_sign_contained_in_nonexistent(page, params[:referer])
else else
{ href: url_for(:new_page_path, title: page, parent: params[:referer]), class: 'new_wiki_link' } { href: url_for(:new_page_path, title: page, parent: params[:referer]), class: 'new_wiki_link' }
end end
@ -34,6 +34,24 @@ module FoodsoftWiki
private private
def link_attributes_if_number_sign_contained_in_nonexistent(page, referer)
# Interpret the part after the last number sign as anchor.
arr = page.split('#', -1)# `-1` preserves empty anchor
page = arr[0...-1].join('#')
anchor = arr[-1]
return { href: '#' + anchor } if page.empty?
permalink = Page.permalink(page)
if Page.exists?(:permalink => permalink)
{ href: url_for(:wiki_page_path, permalink: permalink, anchor: anchor) }
else
# Do not suggest to use number signs in the title.
good_page_title = arr[0]
{ href: url_for(:new_page_path, title: good_page_title, parent: referer), class: 'new_wiki_link' }
end
end
def url_for(path_name, options={}) def url_for(path_name, options={})
Rails.application.routes.url_helpers.send path_name, options.merge({foodcoop: FoodsoftConfig.scope}) Rails.application.routes.url_helpers.send path_name, options.merge({foodcoop: FoodsoftConfig.scope})
end end