Skip to content

Instantly share code, notes, and snippets.

@tiberiucorbu
Last active September 15, 2021 15:24
Show Gist options
  • Save tiberiucorbu/67df6c13d61c0fb3a34a to your computer and use it in GitHub Desktop.
Save tiberiucorbu/67df6c13d61c0fb3a34a to your computer and use it in GitHub Desktop.
Middleman - Markdown from text or url
require 'open-uri'
###
# Compass
###
# Change Compass configuration
# compass_config do |config|
# config.output_style = :compact
# end
###
# Page options, layouts, aliases and proxies
###
# Per-page layout changes:
#
# With no layout
# page "/path/to/file.html", :layout => false
#
# With alternative layout
# page "/path/to/file.html", :layout => :otherlayout
#
# A path which all have the same layout
# with_layout :admin do
# page "/admin/*"
# end
# Proxy pages (http://middlemanapp.com/basics/dynamic-pages/)
# proxy "/this-page-has-no-template.html", "/template-file.html", :locals => {
# :which_fake_page => "Rendering a fake page with a local variable" }
###
# Helpers
###
# Automatic image dimensions on image_tag helper
# activate :automatic_image_sizes
# Reload the browser automatically whenever files change
# activate :livereload
# Methods defined in the helpers block are available in templates
# helpers do
# def some_helper
# "Helping"
# end
# end
# Methods defined in the helpers block are available in templates
helpers do
def markdown(text)
renderer = Redcarpet::Render::XHTML.new
markdown = Redcarpet::Markdown.new(renderer)
return markdown.render(text)
end
def markdownFromUrl(url)
file = open(url)
contents = file.read
return markdown(contents);
end
end
set :css_dir, 'stylesheets'
set :js_dir, 'javascripts'
set :images_dir, 'images'
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
# activate :minify_css
# Minify Javascript on build
# activate :minify_javascript
# Enable cache buster
# activate :asset_hash
# Use relative URLs
# activate :relative_assets
# Or use a different image path
# set :http_prefix, "/Content/images/"
end
# If you have OpenSSL installed, we recommend updating
# the following line to use "https"
source 'http://rubygems.org'
gem "middleman", "~>3.2.2"
gem 'redcarpet'
# Live-reloading plugin
gem "middleman-livereload", "~> 3.1.0"
# For faster file watcher updates on Windows:
gem "wdm", "~> 0.1.0", :platforms => [:mswin, :mingw]
---
title: Middleman - Markdown from text or url
---
<div class="container">
<article class="markdown-body">
<%= markdownFromUrl("https://gist.githubusercontent.com/tiberiucorbu/67df6c13d61c0fb3a34a/raw/middleman-markdown_from_text.md") %>
</article><!-- .welcome -->
</div>

Middleman - Markdown from text or URL

I was wondering if one can make a static wiki deployed on a private site, using middleman and the private gists from github (Muhahaha) and after a bit of investigation I manage to do it using two helpers in config.rb file with minimum effort.

Breakdown

I added into the gemfile redcapets gem:

gem "middleman", "~>3.2.2"

Ran install on the project folder with command :

$ bundle install

Uncommented helpers in config.rb and added markdown method:

# Methods defined in the helpers block are available in templates
helpers do
  # Initialize a redcarpet XHTML renderer and renders the input text
  def markdown(text)
    renderer = Redcarpet::Render::XHTML.new
    markdown = Redcarpet::Markdown.new(renderer)
    return markdown.render(text)
  end
end

markdown method can be called in a erb template with <%= markdown("#This is an H1") %>.

In addition to this method in the helpers I added another method that will get the text from a particular URL provided in the input, the content is passed further on to the markdown helper.

require 'open-uri'
# ...
# Methods defined in the helpers block are available in templates
helpers do
  # Initialize a redcarpet XHTML renderer and renders the input text
  # ...

  def markdownFromUrl(url)
    file = open(url)
    contents = file.read
    return markdown(contents);
  end
end

markdownFromUrl method can be called in a erb template with <%= markdownFromUrl("https://gist.githubusercontent.com/tiberiucorbu/67df6c13d61c0fb3a34a/raw/middleman-markdown_from_text.md") %>.

As for the gist markdown files there is a little trick to get a particular or the latest revision.

To get a particular revision construct the URLs like this :

 https://gist.githubusercontent.com/<username>/<gist_id>/raw/<revision>/<filename>

Otherwise if you want the latest revision omit the <revision> part :

 https://gist.githubusercontent.com/<username>/<gist_id>/raw/<filename>

Follow-ups (self-reviewed):

Here is a list of things considered but not implemented:

  • Check memory leaks : Shame on me - I have no idea if opening an url requires a close as well in Ruby like in Java;
  • Optimize to use minimum resources : Just by reviewing the code here markdown method can save a little initializations of redcarpet renderer instances with a lazy load pattern.
  • These methods are not "monkey proofed" and they lack any validation or error handling whatsoever, just by expecting that they are not going to be exposed to "monkeys" - Is that something that you can really relay on ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment