feat(faq): variabilize application name, host, contact_email

This commit is contained in:
Colin Darie 2024-04-16 12:24:39 +02:00
parent 472a9ae2ef
commit d05aee0c67
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
26 changed files with 87 additions and 70 deletions

View file

@ -4,8 +4,12 @@ class FAQsLoaderService
PATH = Rails.root.join('doc', 'faqs').freeze
ORDER = ['usager', 'instructeur', 'administrateur'].freeze
def initialize
@faqs_by_path ||= Rails.cache.fetch("faqs_data", expires_in: 1.day) do
attr_reader :substitutions
def initialize(substitutions)
@substitutions = substitutions
@faqs_by_path ||= Rails.cache.fetch(["faqs_data", substitutions], expires_in: 1.day) do
load_faqs
end
end
@ -13,7 +17,7 @@ class FAQsLoaderService
def find(path)
file_path = @faqs_by_path.fetch(path).fetch(:file_path)
FrontMatterParser::Parser.parse_file(file_path)
parse_with_substitutions(file_path)
end
def faqs_for_category(category)
@ -36,7 +40,7 @@ class FAQsLoaderService
def load_faqs
Dir.glob("#{PATH}/**/*.md").each_with_object({}) do |file_path, faqs_by_path|
parsed = FrontMatterParser::Parser.parse_file(file_path)
parsed = parse_with_substitutions(file_path)
front_matter = parsed.front_matter.symbolize_keys
faq_data = front_matter.slice(:slug, :title, :category, :subcategory, :locale, :keywords).merge(file_path: file_path)
@ -45,4 +49,13 @@ class FAQsLoaderService
faqs_by_path[path] = faq_data
end
end
# Substitute all string before front matter parser so metadata are also substituted.
# using standard ruby formatting, ie => `%{my_var} % { my_var: 'value' }`
# We have to escape % chars not used for substitutions, ie. not preceeded by {
def parse_with_substitutions(file_path)
substituted_content = File.read(file_path).gsub(/%(?!{)/, '%%') % substitutions
FrontMatterParser::Parser.new(:md).call(substituted_content)
end
end