2018-08-18 07:19:57 -04:00
# frozen_string_literal: true
2016-12-26 22:44:03 -05:00
module WikiHelper
2018-09-04 06:39:08 -04:00
include API :: Helpers :: RelatedResourcesHelpers
2020-07-02 11:09:08 -04:00
def wiki_page_title ( page , action = nil )
titles = [ _ ( 'Wiki' ) ]
if page . persisted?
titles << page . human_title
breadcrumb_title ( page . human_title )
2021-11-02 08:12:25 -04:00
wiki_breadcrumb_collapsed_links ( page . slug )
2020-07-02 11:09:08 -04:00
end
titles << action if action
page_title ( * titles . reverse )
add_to_breadcrumbs ( _ ( 'Wiki' ) , wiki_path ( page . wiki ) )
end
def link_to_wiki_page ( page , ** options )
link_to page . human_title , wiki_page_path ( page . wiki , page ) , ** options
end
def wiki_sidebar_toggle_button
2021-04-09 23:09:09 -04:00
content_tag :button , class : 'gl-button btn btn-default btn-icon sidebar-toggle js-sidebar-wiki-toggle' , role : 'button' , type : 'button' do
2020-07-02 11:09:08 -04:00
sprite_icon ( 'chevron-double-lg-left' )
end
end
2016-12-26 22:44:03 -05:00
# Produces a pure text breadcrumb for a given page.
#
# page_slug - The slug of a WikiPage object.
#
# Returns a String composed of the capitalized name of each directory and the
# capitalized name of the page itself.
def breadcrumb ( page_slug )
2017-06-21 09:48:12 -04:00
page_slug . split ( '/' )
. map { | dir_or_page | WikiPage . unhyphenize ( dir_or_page ) . capitalize }
. join ( ' / ' )
2016-12-26 22:44:03 -05:00
end
2017-08-16 08:13:34 -04:00
2021-11-02 08:12:25 -04:00
def wiki_breadcrumb_collapsed_links ( page_slug )
2017-08-16 08:13:34 -04:00
page_slug_split = page_slug . split ( '/' )
page_slug_split . pop ( 1 )
current_slug = " "
page_slug_split
. map do | dir_or_page |
2017-09-06 06:55:23 -04:00
current_slug = " #{ current_slug } #{ dir_or_page } / "
2021-11-02 08:12:25 -04:00
add_to_breadcrumb_collapsed_links link_to ( WikiPage . unhyphenize ( dir_or_page ) . capitalize , wiki_page_path ( @wiki , current_slug ) ) , location : :after
2017-08-16 08:13:34 -04:00
end
end
2018-02-05 12:17:21 -05:00
2018-09-04 06:39:08 -04:00
def wiki_attachment_upload_url
2020-09-14 17:09:27 -04:00
case @wiki . container
when Project
expose_url ( api_v4_projects_wikis_attachments_path ( id : @wiki . container . id ) )
else
raise TypeError , " Unsupported wiki container #{ @wiki . container . class } "
end
2018-09-04 06:39:08 -04:00
end
2019-04-04 12:28:56 -04:00
2020-06-11 11:08:36 -04:00
def wiki_sort_controls ( wiki , sort , direction )
sort || = Wiki :: TITLE_ORDER
2021-04-09 23:09:09 -04:00
link_class = 'gl-button btn btn-default btn-icon has-tooltip reverse-sort-btn qa-reverse-sort rspec-reverse-sort'
2019-10-31 17:06:28 -04:00
reversed_direction = direction == 'desc' ? 'asc' : 'desc'
icon_class = direction == 'desc' ? 'highest' : 'lowest'
2020-06-11 11:08:36 -04:00
link_to ( wiki_path ( wiki , action : :pages , sort : sort , direction : reversed_direction ) ,
2019-10-31 17:06:28 -04:00
type : 'button' , class : link_class , title : _ ( 'Sort direction' ) ) do
2020-08-10 14:09:54 -04:00
sprite_icon ( " sort- #{ icon_class } " )
2019-04-04 12:28:56 -04:00
end
end
def wiki_sort_title ( key )
2020-06-11 11:08:36 -04:00
if key == Wiki :: CREATED_AT_ORDER
2019-04-04 12:28:56 -04:00
s_ ( " Wiki|Created date " )
else
s_ ( " Wiki|Title " )
end
end
2020-06-11 11:08:36 -04:00
def wiki_empty_state_messages ( wiki )
case wiki . container
when Project
2020-07-14 14:09:55 -04:00
writable_body = s_ ( " WikiEmpty|A wiki is where you can store all the details about your project. This can include why you've created it, its principles, how to use it, and so on. " )
writable_body += s_ ( " WikiEmpty| Have a Confluence wiki already? Use that instead. " ) if show_enable_confluence_integration? ( wiki . container )
2020-06-11 11:08:36 -04:00
{
writable : {
title : s_ ( 'WikiEmpty|The wiki lets you write documentation for your project' ) ,
2020-07-14 14:09:55 -04:00
body : writable_body
2020-06-11 11:08:36 -04:00
} ,
issuable : {
title : s_ ( 'WikiEmpty|This project has no wiki pages' ) ,
body : s_ ( 'WikiEmptyIssueMessage|You must be a project member in order to add wiki pages. If you have suggestions for how to improve the wiki for this project, consider opening an issue in the %{issues_link}.' )
} ,
readonly : {
title : s_ ( 'WikiEmpty|This project has no wiki pages' ) ,
body : s_ ( 'WikiEmpty|You must be a project member in order to add wiki pages.' )
}
}
when Group
{
writable : {
title : s_ ( 'WikiEmpty|The wiki lets you write documentation for your group' ) ,
body : s_ ( " WikiEmpty|A wiki is where you can store all the details about your group. This can include why you've created it, its principles, how to use it, and so on. " )
} ,
issuable : {
title : s_ ( 'WikiEmpty|This group has no wiki pages' ) ,
body : s_ ( 'WikiEmptyIssueMessage|You must be a group member in order to add wiki pages. If you have suggestions for how to improve the wiki for this group, consider opening an issue in the %{issues_link}.' )
} ,
readonly : {
title : s_ ( 'WikiEmpty|This group has no wiki pages' ) ,
body : s_ ( 'WikiEmpty|You must be a group member in order to add wiki pages.' )
}
}
else
raise NotImplementedError , " Unknown wiki container type #{ wiki . container . class . name } "
end
end
2020-07-09 20:09:13 -04:00
def wiki_page_tracking_context ( page )
{
2022-08-01 23:09:33 -04:00
'wiki-format' = > page . format ,
'wiki-title-size' = > page . title . bytesize ,
'wiki-content-size' = > page . raw_content . bytesize ,
2020-10-06 08:08:38 -04:00
'wiki-directory-nest-level' = > page . path . scan ( '/' ) . count ,
2022-08-01 23:09:33 -04:00
'wiki-container-type' = > page . wiki . container . class . name
2020-07-09 20:09:13 -04:00
}
end
2020-07-14 14:09:55 -04:00
def show_enable_confluence_integration? ( container )
container . is_a? ( Project ) &&
current_user & . can? ( :admin_project , container ) &&
! container . has_confluence?
end
2022-03-24 02:08:31 -04:00
def wiki_page_render_api_endpoint ( page )
2022-04-13 17:09:57 -04:00
expose_path ( api_v4_projects_wikis_path ( wiki_page_render_api_endpoint_params ( page ) ) )
2022-03-24 02:08:31 -04:00
end
2022-04-08 08:08:48 -04:00
def wiki_markup_hash_by_name_id
Wiki :: VALID_USER_MARKUPS . map { | key , value | { value [ :name ] = > key } } . reduce ( { } , :merge )
end
2022-03-24 02:08:31 -04:00
private
def wiki_page_render_api_endpoint_params ( page )
{ id : page . container . id , slug : ERB :: Util . url_encode ( page . slug ) , params : { version : page . version . id } }
end
2016-12-26 22:44:03 -05:00
end
2020-09-14 17:09:27 -04:00
2021-05-11 17:10:21 -04:00
WikiHelper . prepend_mod_with ( 'WikiHelper' )