LoginSignup
15
15

More than 5 years have passed since last update.

Haml と markdown を組み合わせるときの注意点

Last updated at Posted at 2013-04-22

以下のような Sinatra プロジェクトを想定.

Gemfile
source 'https://rubygems.org'

gem 'sinatra', '1.4.2'
gem 'haml', '4.0.2'
gem 'redcarpet', '2.2.2'
config.ru
require 'sinatra'
run Sinatra::Application
get '/' do
  haml :index
end
views/index.haml
!!! 5
%html
  %head
    %meta{'http-equiv' => 'content-type', 'content' => 'text/html', 'charset' => 'utf-8'}
    %meta{'name' => 'robots', 'content' => 'nofollow, noindex'}
    %title My Title
  %body
    ~ markdown(:index, layout: false, fenced_code_blocks: true)
views/index.md
# header

here is markdown area

\```
cd
ls
\```

(※ ``` の中で ``` を書く方法が分からなかったので \``` となっています)

index.haml の中で = markdown ではなく ~ markdown と書いているところがポイント.

こうしないと,``` の中身が <pre><code> として変換されたときに,インデントを保ってしまうので

...
  <body>
    ...
    <pre><code>cd
    ls
    </code></pre>
    ...
  </body>
...

となってしまい,表示が

cd
    ls

と崩れてしまいます.しかし ~ markdown としておけば,``` 内だけの改行を &#x000A; にしてくれるので崩れなくなります.&#x000A; ってのは \n です.

サンプルとソースコード

Heroku にサンプルをアップしました.http://ka-haml-md.herokuapp.com

GitHub にソースコードをアップしました.https://github.com/kaosf/haml-md

参考

15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15