Class: AdminSuite::DocsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/admin_suite/docs_controller.rb

Constant Summary collapse

DocNotFound =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#indexObject

GET /docs GET /docs?path=relative/path.md



11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/admin_suite/docs_controller.rb', line 11

def index
  @files = grouped_markdown_files
  @selected_path = params[:path].presence

  if @selected_path.present?
    load_doc_content(@selected_path)
  elsif @files.values.flatten.any?
    @selected_path = @files.values.flatten.first
    load_doc_content(@selected_path)
  end
end

#showObject

GET /docs/*path



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/admin_suite/docs_controller.rb', line 24

def show
  relative_path = params[:path].to_s
  if params[:format].present? && !relative_path.end_with?(".#{params[:format]}")
    relative_path = "#{relative_path}.#{params[:format]}"
  end

  # Even when the doc path ends with `.md`, we always render HTML.
  request.format = :html
  file_path = resolve_doc_path!(relative_path)

  @files = grouped_markdown_files
  @selected_path = relative_path
  @title = File.basename(file_path, ".md").tr("_", " ").tr("-", " ").titleize
  @raw_markdown = File.read(file_path)

  rendered = markdown_renderer.new(@raw_markdown).render
  @content_html = rendered[:html]
  @toc = rendered[:toc]
  @reading_time = rendered[:reading_time_minutes]

  render :index, formats: [ :html ]
rescue DocNotFound
  redirect_to docs_path, alert: "Doc not found."
end