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.path_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 66 67 68 69 |
# File 'app/controllers/markdowndocs/docs_controller.rb', line 43 def show # `params[:mode]` here is the URL path segment (e.g. /docs/technical/foo → "technical") # if the request matched the scoped route. Otherwise, fall back to the resolved # current preference (@docs_mode) so root-mounted docs honor audience-frontmatter # filtering. lookup_mode = params[:mode].presence || @docs_mode @doc = Documentation.find_by_slug(params[:slug], mode: lookup_mode) if @doc.nil? render_not_found return end # `mode:` here controls in-doc <!-- mode: X --> block stripping, # which should reflect the user's PREFERENCE (@docs_mode), not the # URL-path mode (lookup_mode). A /technical/foo URL does not force # guide-only blocks invisible. 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.path_slug == @doc.path_slug } @available_modes = @doc.available_modes @toc_items = helpers.generate_table_of_contents(@rendered_content) end |