Question

how to parsing markdown to cut off or move some chunks of code from *.md to html with my html template?

I have markdown file like this: carrot_soup.md

Very nice carrot soup
============

I'd like soup like this:

### Ingredients ###

      * carrots
      * celery
      * lentils

### Coocking ###

Boil some water. And eat it with all Ingredients

I need to parse it to something like this:

<div class="head"">
<h1>Very nice carrot soup</h1>
<p>I'd like soup like this:</p>
</div>
<!-- Something else -->

<div class="Ingredients">
<ul>
<li>carrots</li>
<li>celery</li>
<li>lentils</li>
</ul>
</div>

<div class="Coocking">
<p>Boil some water. And eat it with all Ingredients</p>
</div>

I need to move some markdown data from one markdown file to different parts in my html template

I have:

1) html template 2) static build engine 3) file with markdown code

In my html template I have some <div> parts and I need to combine markdown data with html template not as is. I need to cut some markdown code and put this code to different html <div> parts. How to do it?

Was it helpful?

Solution

Answering the question

As your markdown reads now it is hard to do because there is nothing in the markdown that makes it possible to extract parts of it. If you don't want to change your markdown format (or use something more suited for this) then you can do it client side with a bit of javascript (or maybe css). Put the "something else" part in the end of your generated code with an id that you find with javascript. The move it to the place where you want it. If you always have a header named Ingredients you should be able to query for that element and insert you other content before it. If you can stand wrapping you markdown parts with divs then it may be a bit more failsafe.

Better alternative

I don't know about jekyll but nanoc that I am using has a header that you put in your source file that you can extract information from in your layout. I would put title and description in there and then pull them out in my layout. The nanoc markdown file would look like this:

---
title: Very nice carrot soup
kind: recipe
created_at: 2013-10-03 1:59:00
author: fredrik
description: This is some really good carrot soup.....
---
### Ingredients ###

  * carrots
  * celery
  * lentils

### Cooking ###

Boil some water. And eat it with all Ingredients

and the layout would extract the title and the author. Snippet:

<div class="head">
    <h1><%= @item[:title] %></h1>
    <p><%= @item[:description] %></p>
</div>
<%= add_some_more_stuff %>
<%= yield %>

You can probably do something similar with jekyll.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top