【发布时间】:2013-05-04 02:27:39
【问题描述】:
我一直在使用Express web framework 来代替Node.JS,并且一直在使用降价解析器chjj/marked 来将降价解析为HTML。我还能够使用 Express 呈现这个降价。我通常在我的 Express 项目中使用 EJS 模板,我希望能够将 EJS 与 markdown 一起使用。
理想情况下,我希望能够使用 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 %>
如果我可以在我的 EJS 模板中也包含 markdown 文件,那就太好了:
<% include markdown-file.md %>
如果能够在 markdown 中使用 EJS 语法或提供某种方式来访问 markdown 中的变量,那就太好了。这样的事情可能吗?如果没有,我在 EJS 模板中对我的内容使用 markdown 的最简单方法是什么?
编辑 5/19/13:我真的很想在我自己的项目中使用这样的东西,所以我不得不尝试将 markdown 与 EJS 结合起来。如果您也对此感兴趣,请查看我在 GitHub 上名为 markedejs 的模块,自述文件解释了我做得很好。该模块使用marked 将markdown 解析为HTML,将其转义,并将HTML 模板传递给EJS 进行最终渲染。所有 EJS 语法都可以在 markdown 中使用,并且在 markdown 模板中包含 HTML 模板也可以。您可以制作如下所示的降价模板:
<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 %>

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>
-
<nop>标签被markedejs移除并包含在markdown 模板中,因此<p>标签不会添加到header.html和footer.html的内容周围。
但是,这还不是我最初想要的,我希望能够在其他 HTML 模板和其他 markdown 模板中使用 include markdown 模板。目前我只能在我的降价模板中包含 HTML 模板。仍然希望有人对我如何使 EJS 包含对降价文件的工作有更好的了解?
【问题讨论】:
-
hexo 是这样做的,我会去那里看看他们是怎么做的。
标签: node.js express markdown ejs