Skip to content

Commit

Permalink
Grab label_for helper from Pals
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyf committed Dec 15, 2023
1 parent 373e794 commit 8d35b5d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# frozen_string_literal: true

module ApplicationHelper
# Yep, we're ignoring the advice; because the translations are safe as is the markdown converter.
# rubocop:disable Rails/OutputSafety
include ::HyraxHelper
include Hyrax::OverrideHelperBehavior
include GroupNavigationHelper
include SharedSearchHelper

def label_for(term:, record_class: nil)
locale_for(type: 'labels', term: term, record_class: record_class)
end

def hint_for(term:, record_class: nil)
hint = locale_for(type: 'hints', term: term, record_class: record_class)

Expand All @@ -16,8 +22,8 @@ def locale_for(type:, term:, record_class:)
@term = term.to_s
@record_class = record_class.to_s.downcase
work_or_collection = @record_class == 'collection' ? 'collection' : 'defaults'
default_locale = t("simple_form.#{type}.#{work_or_collection}.#{@term}")
locale = t("hyrax.#{@record_class}.#{type}.#{@term}")
default_locale = t("simple_form.#{type}.#{work_or_collection}.#{@term}").html_safe
locale = t("hyrax.#{@record_class}.#{type}.#{@term}").html_safe

return default_locale if missing_translation(locale)

Expand All @@ -27,4 +33,15 @@ def locale_for(type:, term:, record_class:)
def missing_translation(value)
value.include?('translation missing')
end

def markdown(text)
options = %i[
hard_wrap autolink no_intra_emphasis tables fenced_code_blocks
disable_indented_code_blocks strikethrough lax_spacing space_after_headers
quote footnotes highlight underline
]
text ||= ""
Markdown.new(text, *options).to_html.html_safe
end
# rubocop:enable Rails/OutputSafety
end
13 changes: 13 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

RSpec.describe ApplicationHelper do
describe "#markdown" do
let(:header) { '# header' }
let(:bold) { '*bold*' }

it 'renders markdown into html' do
expect(helper.markdown(header)).to eq("<h1>header</h1>\n")
expect(helper.markdown(bold)).to eq("<p><em>bold</em></p>\n")
end
end
end

0 comments on commit 8d35b5d

Please sign in to comment.