Module: Trifle::Docs::Helper::MarkdownLayout

Defined in:
lib/trifle/docs/helper/markdown_layout.rb

Class Method Summary collapse

Class Method Details

.derive_title_from_url(url) ⇒ Object



43
44
45
46
47
# File 'lib/trifle/docs/helper/markdown_layout.rb', line 43

def derive_title_from_url(url)
  return 'Untitled' if url.nil? || url.empty?

  url.split('/').last.to_s.gsub(/[-_]/, ' ').split.map(&:capitalize).join(' ')
end

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trifle/docs/helper/markdown_layout.rb', line 25

def navigation_toc(sitemap, depth: 0) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return '' unless sitemap.is_a?(Hash)

  sitemap.keys.reject { |k| k == '_meta' }.sort.map do |key|
    node = sitemap[key]
    meta = node['_meta'] || {}
    title = meta['title'] || derive_title_from_url(meta['url'] || key)
    url = meta['url'] || "/#{key}"
    indent = '  ' * depth
    children = node.reject { |child_key, _| child_key == '_meta' }

    [
      "#{indent}- [#{title}](#{url})",
      navigation_toc(children, depth: depth + 1)
    ].reject(&:empty?).join("\n")
  end.join("\n")
end

.render(meta:, raw_content:, sitemap:) ⇒ Object

rubocop:disable Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/trifle/docs/helper/markdown_layout.rb', line 9

def render(meta:, raw_content:, sitemap:) # rubocop:disable Metrics/MethodLength
  lines = []
  title = meta['title'] || derive_title_from_url(meta['url'])

  lines << "# #{title}"
  lines << ''
  lines << '## Navigation'
  lines << navigation_toc(sitemap)
  lines << ''
  lines << '## Content'
  lines << raw_content.to_s.strip
  lines << ''

  lines.join("\n")
end