如何使用 Express / Node.JS 使用 EJS 渲染 Markdown?

Cor*_*oss 1 markdown ejs node.js express

我一直在使用快速Web框架Node.js的,并使用降价解析器一直chjj/marked以解析降价到HTML中。我还能够使用 Express 呈现此降价。我通常在 Express 项目中使用 EJS 模板,我希望能够做的是使用带有 Markdown 的 EJS。

理想情况下,我希望能够使用 EJS 中通常使用的编译时包含,此处显示了一个示例:

<% include header.html %>

<h3>User List -- Located in users.html</h3>
<ul id="users">
  <% users.forEach(function(user){ %>
    <li><%= user.name %> -- <%= user.email %></li>
  <% }) %>
</ul>

<% include footer.html %>
Run Code Online (Sandbox Code Playgroud)

如果我可以在我的 EJS 模板中包含如下所示的 Markdown 文件,那就太好了:

<% include markdown-file.md %>
Run Code Online (Sandbox Code Playgroud)

能够在 Markdown 中使用 EJS 语法或提供某种访问 Markdown 中变量的方法也很好。这样的事情有可能吗?如果不是,对我来说,在 EJS 模板中对我的内容使用 Markdown 的最简单方法是什么?

2013 年 5 月 19 日编辑:我真的很想做这样的事情以用于我自己的项目,所以我不得不尝试将 Markdown 与 EJS 结合起来。markedejs如果您也对此感兴趣,请查看我在 GitHub 上的模块,自述文件解释了我做得很好的内容。该模块使用 将markedMarkdown 解析为 HTML,对其进行转义,并将 HTML 模板传递给 EJS 以进行最终渲染。所有 EJS 语法都在 markdown 中起作用,并且在 markdown 模板中包含一个 HTML 模板也起作用。您可以制作如下所示的 Markdown 模板:

<nop><% include header.html %></nop>

<%= site.title %>
=======================

<%= site.description %>

This project was created by <%= author.name %>. My website is
located at the url [<%= author.url %>]().

## <%= header %>

![Markdown Logo](img/mdlogo.png)

Hey <%= user.name %>! 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
<ul>
<% for (var i = 0; i < supplies.length; i++) { %>
  <li><%= supplies[i] %></li>
<% } %>
</ul>

### Your User Data

I like using markdown lists a whole lot better when I can.

 - **Username:** <%= user.username %>
 - **Name:** <%= user.name %>
 - **Stars:** <%= user.stars %>

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.

<% if (showFooter !== undefined && showFooter === true) { %>
  <%= footer %>
<% } %>

<nop><% include footer.html %></nop>
Run Code Online (Sandbox Code Playgroud)
  • <nop>标签被取下markedejs,并列入降价模板,以便<p>标签不在身边的含量增加header.htmlfooter.html

但是,这并没有完全按照我最初想要的方式进行,我希望能够include在其他 HTML 模板和其他 Markdown 模板中使用 Markdown 模板。目前我只能在 Markdown 模板中包含 HTML 模板。仍然希望有人能更好地了解我如何使 EJS 包含与 Markdown 文件一起工作?

Lon*_*eve 5

如果你想让它处理它的降价文件,你可能需要修改 EJS

<% include filename.ext %>
Run Code Online (Sandbox Code Playgroud)

指示。它们处理包含的方式简单且递归,但它需要明确了解降价文件,并在将其添加到模板之前调用降价转换器来生成 html。

我已经使用标记创建了一个传递给我的 EJS 模板的函数,它可以被调用以包含一个 markdown 文件,它运行得非常好:

app.get ('/docs', function (req, res) {

   // Allow the docs.html template to 'include' markdown files
   var marked = require ('marked');

   var md = function (filename) {
      var path = __dirname +"/views/docs/" + filename;
      var include = fs.readFileSync (path, 'utf8');
      var html = marked (include);

      return html;
   };

   res.render ('docs', {"md": md});
});
Run Code Online (Sandbox Code Playgroud)

在模板中,执行以下操作:

<%- md("index.md") %>
Run Code Online (Sandbox Code Playgroud)

但它不允许我在降价文件中使用 EJS 模板函数,降价是内联的纯 HTML。