我一直在使用 Node.JS 的Express Web 框架,并且一直在使用 markdown 解析器来将 markdown解析为 HTML。我还能够使用 Express 呈现这个降价。我通常在我的 Express 项目中使用 EJS 模板,我希望能够将 EJS 与 markdown 一起使用。chjj/marked
理想情况下,我希望能够使用 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
,README 解释了我做得很好。该模块使用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 %>
![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>
<nop>
标记被删除markedejs
并包含在降价模板中,因此不会在和<p>
的内容周围添加标记。header.html
footer.html
但是,这还不是我最初想要的,我希望能够include
在其他 HTML 模板和其他 markdown 模板中标记模板。目前我只能在我的降价模板中包含 HTML 模板。仍然希望任何人都可以更好地了解我如何使 EJS 包含与降价文件一起使用?