Class: Markdowndocs::DocsController

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

Constant Summary collapse

SAFE_SLUG_PATTERN =
/\A[a-zA-Z0-9_-]+\z/

Instance Method Summary collapse

Instance Method Details

#indexObject



11
12
13
14
# File 'app/controllers/markdowndocs/docs_controller.rb', line 11

def index
  @docs_by_category = Documentation.grouped_by_category
  @search_enabled = Markdowndocs.config.search_enabled
end

#search_indexObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/markdowndocs/docs_controller.rb', line 16

def search_index
  unless Markdowndocs.config.search_enabled
    render_not_found
    return
  end

  cache_key = "markdowndocs:search_index:#{Documentation.all.map(&:cache_key).join(",")}"
  json = Rails.cache.fetch(cache_key, expires_in: Markdowndocs.config.cache_expiry) do
    Documentation.all.map do |doc|
      {
        id: doc.slug,
        title: doc.title,
        description: doc.description,
        content: doc.plain_text_content,
        keywords: doc.keywords.join(" "),
        code: doc.code_content
      }
    end.to_json
  end

  response.headers["Cache-Control"] = "public, max-age=#{Markdowndocs.config.cache_expiry.to_i}"
  render json: json
end

#showObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/markdowndocs/docs_controller.rb', line 40

def show
  @doc = Documentation.find_by_slug(params[:slug])

  if @doc.nil?
    render_not_found
    return
  end

  rendered_html = MarkdownRenderer.render(
    @doc.content,
    cache_key: @doc.cache_key,
    mode: @docs_mode
  )
  @rendered_content = helpers.add_heading_anchors(rendered_html)
  @related_docs = Documentation.by_category(@doc.category).reject { |d| d.slug == @doc.slug }
  @available_modes = @doc.available_modes
  @toc_items = helpers.generate_table_of_contents(@rendered_content)
end