Class: Markdowndocs::DocsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- Markdowndocs::DocsController
- 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
#index ⇒ Object
11 12 13 14 15 16 17 |
# File 'app/controllers/markdowndocs/docs_controller.rb', line 11 def index # Pass the resolved mode so docs whose `audience:` frontmatter # excludes it are hidden from the index. Categories with no # surviving docs are dropped (see Documentation.grouped_by_category). @docs_by_category = Documentation.grouped_by_category(mode: @docs_mode) @search_enabled = Markdowndocs.config.search_enabled end |
#search_index ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/controllers/markdowndocs/docs_controller.rb', line 19 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 |
#show ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/controllers/markdowndocs/docs_controller.rb', line 43 def show # Audience filter: a doc with `audience: technical` is unreachable # via slug while the user is in guide mode — they get the 404 page, # not the doc's content. This matters because the index already # hides those docs; making the show route honor the same filter # keeps URL guessing / shared-link scenarios consistent. @doc = Documentation.find_by_slug(params[:slug], mode: @docs_mode) 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 |