Class: RailsSiteEngine::Content::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_site_engine/content/store.rb

Constant Summary collapse

RESERVED_PATHS =
[ "/robots.txt", "/sitemap.xml" ].freeze
ALLOWED_TEMPLATES =
%w[landing standard contact].freeze

Instance Method Summary collapse

Constructor Details

#initialize(root_path: nil, host_config: RailsSiteEngine::SiteEngineConfig.new) ⇒ Store

Returns a new instance of Store.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_site_engine/content/store.rb', line 30

def initialize(
  root_path: nil,
  host_config: RailsSiteEngine::SiteEngineConfig.new
)
  @host_config = host_config

  if root_path
    root = Pathname.new(root_path)
    @override_site_config_path = root.join("site.yml")
    @override_pages_path = root.join("pages")
  else
    @override_site_config_path = @host_config.override_site_config_path
    @override_pages_path = @host_config.override_pages_path
  end
end

Instance Method Details

#contact_page_id ⇒ Object



60
61
62
# File 'lib/rails_site_engine/content/store.rb', line 60

def contact_page_id
  site_config.dig("contact", "page").to_s.strip.presence || "contact"
end

#page(page_id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rails_site_engine/content/store.rb', line 64

def page(page_id)
  page_id = page_id.to_s.strip
  return nil if page_id.empty?

  parsed = parsed_page_content(page_id)
  return nil unless parsed

  frontmatter = parsed[:frontmatter]
  body_markdown = parsed[:body_markdown]
  template = resolved_template(page_id, frontmatter)
  sections = normalized_sections(frontmatter, body_markdown, template)

  Page.new(
    id: page_id,
    slug: page_id,
    path: path_for_page(page_id),
    title: frontmatter["title"].to_s.strip.presence,
    description: frontmatter["description"].to_s.strip.presence,
    template: template,
    show_title: resolved_show_title(frontmatter),
    lead: frontmatter["lead"].to_s.strip.presence,
    og_image: frontmatter["og_image"].to_s.strip.presence,
    twitter_title: frontmatter["twitter_title"].to_s.strip.presence,
    twitter_description: frontmatter["twitter_description"].to_s.strip.presence,
    sections: sections,
    body_markdown: body_markdown,
    body_html: RailsSiteEngine::MarkdownRenderer.render(body_markdown)
  )
end

#page_for_path(request_path) ⇒ Object



94
95
96
97
98
99
# File 'lib/rails_site_engine/content/store.rb', line 94

def page_for_path(request_path)
  page_id = route_map[normalize_request_path(request_path)]
  return nil unless page_id

  page(page_id)
end

#page_ids ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/rails_site_engine/content/store.rb', line 108

def page_ids
  @page_ids ||= begin
    if @override_pages_path.directory?
      @override_pages_path.glob("*.md").map { |path| path.basename(".md").to_s }.sort
    else
      []
    end
  end
end

#page_paths ⇒ Object



120
121
122
# File 'lib/rails_site_engine/content/store.rb', line 120

def page_paths
  route_map.keys.sort_by { |path| path == "/" ? "" : path }
end

#page_slugs ⇒ Object



118
# File 'lib/rails_site_engine/content/store.rb', line 118

def page_slugs = page_ids

#path_for_page(page_id) ⇒ Object



101
102
103
104
105
106
# File 'lib/rails_site_engine/content/store.rb', line 101

def path_for_page(page_id)
  page_id = page_id.to_s.strip
  return nil if page_id.empty?

  page_path_map[page_id]
end

#site_config ⇒ Object



46
47
48
# File 'lib/rails_site_engine/content/store.rb', line 46

def site_config
  @site_config ||= load_yaml(@override_site_config_path)
end

#site_locale ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rails_site_engine/content/store.rb', line 50

def site_locale
  configured_locale = site_config.dig("site", "locale").to_s.strip
  return I18n.default_locale.to_s if configured_locale.blank?

  available_locales = I18n.available_locales.map(&:to_s)
  return configured_locale if available_locales.include?(configured_locale)

  I18n.default_locale.to_s
end