Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I render markdown with EJS using Express / Node.JS?
    primarykey
    data
    text
    <p>I have been using the <a href="http://expressjs.com" rel="nofollow"><strong>Express web framework</strong></a> for <a href="http://nodejs.org" rel="nofollow"><strong>Node.JS</strong></a>, and have been using the markdown parser <a href="https://github.com/chjj/marked" rel="nofollow"><strong><code>chjj/marked</code></strong></a> in order to parse markdown into HTML. I have also been able to render this markdown using Express. I use EJS templates in my Express projects typically and what I would like to be able to do is use EJS with markdown.</p> <p>Ideally I would like to be able to use the compile-time includes that are normally used in EJS, an example of which is shown here:</p> <pre><code>&lt;% include header.html %&gt; &lt;h3&gt;User List -- Located in users.html&lt;/h3&gt; &lt;ul id="users"&gt; &lt;% users.forEach(function(user){ %&gt; &lt;li&gt;&lt;%= user.name %&gt; -- &lt;%= user.email %&gt;&lt;/li&gt; &lt;% }) %&gt; &lt;/ul&gt; &lt;% include footer.html %&gt; </code></pre> <p>It would be nice if I could include markdown files as well within my EJS templates with something like the following:</p> <pre><code>&lt;% include markdown-file.md %&gt; </code></pre> <p>It would also be nice to be able to use the EJS syntax within markdown or provide some way of accessing the variables within the markdown. Is anything like this possible? If not, what would be the easiest way for me to use markdown for my content in an EJS template?</p> <p><strong>EDIT 5/19/13:</strong> I have really wanted to do something like this for use in my own projects so I had to give it a go at combining markdown with EJS. Take a look at my module on GitHub called <a href="https://github.com/CoryG89/markedejs" rel="nofollow"><strong><code>markedejs</code></strong></a> if you are interested in this as well, the README explains what I've done pretty well. This module uses the <a href="https://github.com/CoryG89/markedejs" rel="nofollow"><strong><code>marked</code></strong></a> in order to parse markdown into HTML, unescapes it, and passes the HTML template to EJS for final rendering. All the EJS syntax works within the markdown, and including an HTML templates within markdown templates is working as well. You can make markdown templates looking like the following:</p> <pre><code>&lt;nop&gt;&lt;% include header.html %&gt;&lt;/nop&gt; &lt;%= site.title %&gt; ======================= &lt;%= site.description %&gt; This project was created by &lt;%= author.name %&gt;. My website is located at the url [&lt;%= author.url %&gt;](). ## &lt;%= header %&gt; ![Markdown Logo](img/mdlogo.png) Hey &lt;%= user.name %&gt;! This is a test template for the `markedejs` module. We can use markdown and EJS together for some pretty awesome results. ### The Classic EJS Supplies List &lt;ul&gt; &lt;% for (var i = 0; i &lt; supplies.length; i++) { %&gt; &lt;li&gt;&lt;%= supplies[i] %&gt;&lt;/li&gt; &lt;% } %&gt; &lt;/ul&gt; ### Your User Data I like using markdown lists a whole lot better when I can. - **Username:** &lt;%= user.username %&gt; - **Name:** &lt;%= user.name %&gt; - **Stars:** &lt;%= user.stars %&gt; We can do some conditionals as well. You will only see the footer below this paragraph if you pass in `true` for the `showFooter` flag. &lt;% if (showFooter !== undefined &amp;&amp; showFooter === true) { %&gt; &lt;%= footer %&gt; &lt;% } %&gt; &lt;nop&gt;&lt;% include footer.html %&gt;&lt;/nop&gt; </code></pre> <ul> <li>The <code>&lt;nop&gt;</code> tags are removed by <a href="https://github.com/CoryG89/markedejs" rel="nofollow"><strong><code>markedejs</code></strong></a> and are included in the markdown template so that <code>&lt;p&gt;</code> tags are not added around the content of <code>header.html</code> and <code>footer.html</code>.</li> </ul> <p>However, this is not quite doing what I originally wanted yet, I would like to be able to <code>include</code> markdown templates within both other HTML templates and other markdown templates. Currently I can only include HTML templates within my markdown templates. Still hoping anyone might have a better idea about how I can make EJS includes work with markdown files?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload