如何在Markdown文件中实现不同列宽元素并配置对应HTML模板
实现方法
要生成你需要的Markdown格式内容,核心是用模板引擎根据内容类型分别渲染文本和iframe区块,以下是具体实现方案:
1. 定义数据结构
先把需要渲染的内容整理成结构化数据,方便模板循环处理:
const contentBlocks = [ { type: 'text', content: 'text' }, { type: 'iframe', className: 'col-11' }, { type: 'text', content: 'more text' }, { type: 'iframe', className: 'col-11' } ];
2. 编写HTML模板(以EJS为例)
创建一个EJS模板文件(比如md-template.ejs),通过遍历数据数组,分别输出文本和转义后的HTML标签:
<% contentBlocks.forEach(block => { %> <% if (block.type === 'text') { %> <%= block.content %> <% } else if (block.type === 'iframe') { %> <div class="<%= block.className %>"><iframe></iframe></div> <% } %> <% }) %>
3. 生成Markdown文件
用模板引擎(比如EJS的Node.js工具)将模板和数据结合,生成最终的index.md文件,输出内容会完全符合你要求的格式。
如果是固定内容不需要动态生成,直接写静态模板即可:
text <div class="col-11"><iframe></iframe></div> more text <div class="col-11"><iframe></iframe></div>
内容的提问来源于stack exchange,提问作者functional_overflow
相关产品推荐
相关产品推荐

