Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you create a blog or docs with high_voltage? #309

Open
collimarco opened this issue Jul 28, 2023 · 2 comments
Open

Can you create a blog or docs with high_voltage? #309

collimarco opened this issue Jul 28, 2023 · 2 comments

Comments

@collimarco
Copy link

collimarco commented Jul 28, 2023

I am looking for something like Jekyll, but directly embedded inside the Rails app.

The idea is to have a folder blog with all the blog posts. Each post would also have a front matter.

Is there any way to iterate on the blog posts?

Ideally you would read each file like it was a row in a database, so that you have a author, category, date, etc. from the front matter for each post, then you can display the index of posts or a category or a specific post.

Can you do that with this gem? Do you already have a similar approach for that (blog, docs, etc.) or I need to build from scratch?

@MatheusRich
Copy link
Contributor

I think you could get that working, but you need a way to make rails render markdown first. Here's one approach.

@richjdsmith
Copy link

I think you could get that working, but you need a way to make rails render markdown first. Here's one approach.

I had something like this implemented and it worked perfectly. I can dump markdown files (with erb) in my pages folder without issues. Here are my steps:

  1. Install the RedCarpet Markdown Gem ($ gem install redcarpet)
  2. Add a file to your app/config/initializers, mine is called markdown_template_handler.rb,
  3. Write a ruby file that calls the redcarpet gem on markdown files using the register_template_handler function. Here is a copy of mine, but please note that I have it wrap generated markdown in the tailwindcss prose class for styling.

Also DANGER, I am the only author, so I have erb available on all markdown files. You may want create a second file format like mderb so that you can limit when markdown can have erb:

# app/config/initializers/markdown_template_handler.rb
require 'redcarpet'

class CustomMarkdownRenderer < Redcarpet::Render::HTML
  def postprocess(full_document)
    "<div class=\"prose prose-xl prose-img:rounded-xl prose-headings:underline prose-a:text-blue-600 \">#{full_document}</div>"
  end
end

class MarkdownTemplateHandler
  def erb
    @erb ||= ActionView::Template.registered_template_handler(:erb)
  end

  def call(template, source)
    compiled_source = erb.call(template, source)
    "Redcarpet::Markdown.new(CustomMarkdownRenderer.new).render(begin;#{compiled_source};end.to_s).html_safe"
  end
end

ActionView::Template.register_template_handler(:md, MarkdownTemplateHandler.new)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants