How to build Help based on Markdown in Ruby on Rails 5? -


i willing create manual pages using markdown files within rails application. using markdown limited elements in pages, want create dynamic pages using md files. searching around, found tips, no complete howto.

my main issue generating route various pages based on requested markdown file. propose solution below, fits documenting application, , allows contributors enrich or translate documentation.

writing question, found solution. here did:

1 - created hierarchy of folders in public supporting several languages. each folder contains help_index.md file, contains links other md files:

  • public

    • help-en
      help-index.md
      logging.md
      user-ceation.md

    • help-fr
      help-index.md
      logging.md
      etc.

2 - created route (routes.rb) reach these pages based on page name:

#  '/help', 'help/*page_name', :controller => 'help', :action => 'help', :as => :help 

3 - created link application header (application.html.erb):

<%= link_to t('help'), help_path("help_index") %> 

4 - created template pages:

<% provide(:title, t('help')) %> <h1>help pages</h1> <p> <%= displayhelp %> </p> 

5 - created controller (help_controller.rb) displaying page:

class helpcontroller < applicationcontroller    def   end end 

6 - created helper (application_helper.rb) interpreting markdown based on redcarpet gem:

module applicationhelper   def markdown         markdown = redcarpet::markdown.new(redcarpet::render::html, :autolink => true, :space_after_headers => true)   end    def displayhelp     filename = file.join(rails.root, 'public', "help-#{i18n.locale.to_s}", "#{params[:page_name]}.md")     begin       file = file.open(filename, "rb")       markdown.render(file.read).html_safe     rescue errno::enoent       render :file => "public/404.html", :status => 404     end   end end 

and works fine!


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -