我正在通过nunjucks-markdown使用marked.js在节点/快速应用中渲染markdown。
markdown内容呈现得很好,但是,我想在ul元素上设置默认类。
默认情况下为:
<ul>
但是我想全局覆盖它,所以它呈现为:
<ul class='toolkit-list'>
在我的app.js文件中,我尝试过:
const nunjucksMarkdown = require('nunjucks-markdown');
const marked = require('marked');
let markedRender = new marked.Renderer()
let renderListitem = markedRender.listitem.bind(markedRender)
markedRender.listitem = function(text, task) {
var html = renderListitem(text, task)
if (task) {
html = html.replace('<ul>', "<ul class='toolkit-list'>")
}
return html
}
nunjucksMarkdown.register(nunjucksAppEnv, marked)
答案 0 :(得分:1)
这可能需要进一步调整,但应该可以帮助您开始:
const marked = require('marked');
var md_list = `
- First
- Second
`;
const renderer = new marked.Renderer();
renderer.list = function(body, ordered, start) {
var temp = "<ul class='toolkit-list'>${body}</ul>";
return temp;
}
console.log(marked(md_list, { renderer: renderer }));
产生的输出:
<ul class='toolkit-list'>
<li>First</li>
<li>Second</li>
</ul>
答案 1 :(得分:0)
<html lang="en">
<head>
<title> All assignments with dropdown</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#inputEditor{
float:left;
width:45%;
height:80%;
background-color: rgb(235, 246, 250);
border:1px solid black;
margin:10px;
padding: 10px;
}
#preView{
float:right;
width:45%;
height:80%;
background-color: rgb(235, 246, 250);
border:1px solid black;
margin:10px;
padding: 10px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/1.1.1/marked.min.js"></script>
<script>
function renderTextFormat(){
var enteredText=document.getElementById('marked').value.trim();
document.getElementById('output').innerHTML=marked(enteredText);
}
</script>
</head>
<body>
<div id="previewer">
<h3>Assignment 3</h3>
<div id="inputEditor">
<div id="header"></div>
# Hints </br>
## This is some markdown </br>
#### Make List </br>
- Item 1 </br>
- Item 2 </br>
- Item 3 </br>
#### Make it **bold** or make it *italic* </br>
<strong>Enter your text : </strong> <input type="text" id="marked" onkeyup="renderTextFormat()" />
</div>
<div id="preView">
<div id="hints"></div>
<div id="output"></div>
</div>
<script>
document.getElementById('header').innerHTML=marked('MarkDown Input\n<hr>');
document.getElementById('hints').innerHTML=marked('MarkDown Output\n <hr> \n # Hints \n ## This is some markdown \n #### Make List \n - Item 1 \n - Item 2 \n - Item 3 \n #### Make it **bold** or make it *italic*\n');
</script>
</div>
</body>
</html>