diff --git a/site/app/helpers/application_helper.rb b/site/app/helpers/application_helper.rb index 248353381..de8860882 100644 --- a/site/app/helpers/application_helper.rb +++ b/site/app/helpers/application_helper.rb @@ -2,8 +2,8 @@ require 'kramdown-parser-gfm' module ApplicationHelper - def auto_link(text, options = {}) - Anchored::Linker.auto_link(text, options) + def auto_link(text, options = {}, &) + Anchored::Linker.auto_link(text, options, &) end def status_to_color(status) @@ -36,6 +36,32 @@ def boolean_i18n(value) I18n.t(i18n_key) end + def external_link_to(name = nil, href = nil, html_options = {}, &) + if block_given? + html_options = href if href.is_a?(Hash) + href = name + end + + html_options = html_options.merge(target: '_blank', rel: 'noopener noreferrer') + sr_label = content_tag(:span, '(nouvelle fenêtre)', class: 'fr-sr-only') + + if block_given? + link_to(href, html_options) do + concat(capture(&)) + concat(sr_label) + end + else + link_to(safe_join([name, sr_label]), href, html_options) + end + end + + def full_page_title + namespace = controller_path.split('/').first + site_name = t("layouts.#{namespace}.application.title") + title = content_for?(:page_title) ? content_for(:page_title) : static_page_title + title.present? ? "#{title} — #{site_name}" : site_name + end + def icon(kind) "".html_safe end @@ -47,4 +73,13 @@ def api_name def support_email t("#{namespace}.support_email") end + + private + + def static_page_title + key = "#{controller_path.tr('/', '.')}.#{action_name}.page_title" + return t(key).presence if I18n.exists?(key) + + raise "Missing page title for '#{key}'. Use content_for(:page_title) or add the i18n key." if Rails.env.local? + end end diff --git a/site/app/models/abstract_blog_post.rb b/site/app/models/abstract_blog_post.rb index 21eae25c8..d136e9fc0 100644 --- a/site/app/models/abstract_blog_post.rb +++ b/site/app/models/abstract_blog_post.rb @@ -4,6 +4,10 @@ class AbstractBlogPost attr_accessor :id, :content + def title + content.lines.find { |l| l.start_with?('# ') }&.delete_prefix('# ')&.strip + end + def self.find(id) file = Rails.root.join('config/blog_posts', api, "#{id}.md") diff --git a/site/app/views/api_entreprise/cas_usages/index.html.erb b/site/app/views/api_entreprise/cas_usages/index.html.erb index 8e967399e..a2c177ab1 100644 --- a/site/app/views/api_entreprise/cas_usages/index.html.erb +++ b/site/app/views/api_entreprise/cas_usages/index.html.erb @@ -1,3 +1,4 @@ +<% content_for :title, t('.title') %>