[Web] 簡易Markdown可預覽編輯器 —— Codemirror+Marked+Prism
阿新 • • 發佈:2019-02-19
程式碼很簡單,都是在用別人寫好的東西,覺得這個還是有點用,所以簡單記錄一下。
外掛
效果預覽
程式碼
除了外掛那些的js
和css
檔案,就只有三個自己要寫的。主頁index.html
,樣式style.css
,還有一個初始化 codemirror
和 marked
配置的 main.js
。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markdown</title>
<link rel="stylesheet" href="codemirror/lib/codemirror.css">
<link rel="stylesheet" href="codemirror/theme/material.css">
<link rel="stylesheet" href="codemirror/addon/dialog/dialog.css">
<link rel="stylesheet" href="stylesheets/style.css">
<link rel="stylesheet" href="stylesheets/github-markdown.css" >
<link rel="stylesheet" href="stylesheets/prism.css">
</head>
<body>
<div class="editor-wrap">
<textarea name="editor" id="editor" cols="30" rows="10"></textarea>
</div>
<div class="preview-wrap">
<div class="markdown-body"></div>
</div>
<script src="javascripts/jquery.min.js"></script>
<script src="javascripts/marked.min.js"></script>
<script src="codemirror/lib/codemirror.js"></script>
<script src="codemirror/addon/edit/continuelist.js"></script>
<script src="codemirror/addon/edit/matchbrackets.js"></script>
<script src="codemirror/addon/edit/closebrackets.js"></script>
<script src="codemirror/mode/markdown/markdown.js"></script>
<script src="codemirror/keymap/sublime.js"></script>
<script src="codemirror/addon/search/search.js"></script>
<script src="codemirror/addon/search/searchcursor.js"></script>
<script src="codemirror/addon/dialog/dialog.js"></script>
<script src="codemirror/addon/comment/comment.js"></script>
<script src="codemirror/addon/wrap/hardwrap.js"></script>
<script src="javascripts/prism.js"></script>
<script src="javascripts/main.js"></script>
</body>
</html>
main.js
(function() {
$(function() {
// marked
var markedRender = new marked.Renderer();
marked.setOptions({
renderer: markedRender,
gfm: true,
tables: true,
breaks: true, // '>' 換行,回車換成 <br>
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false
});
// codemirror editor
var editor = CodeMirror.fromTextArea($('#editor').get(0), {
mode: 'markdown',
lineNumbers: true,
autoCloseBrackets: true,
matchBrackets: true,
showCursorWhenSelecting: true,
lineWrapping: true, // 長句子折行
theme: "material",
keyMap: 'sublime',
extraKeys: {"Enter": "newlineAndIndentContinueMarkdownList"}
});
editor.on('change', editorOnHandler);
});
function editorOnHandler(cm, co) {
$('.markdown-body').html(marked(cm.getValue()));
$('.markdown-body pre code').each(function(i, block) {
Prism.highlightElement(block);
});
}
})();
style.css
html,
body {
margin: 0px;
padding: 0px;
height: 100%;
}
.editor-wrap {
float: left;
width: 50%;
height: 100%;
overflow: auto;
}
.editor-wrap .CodeMirror {
font-size: 18px;
height: 100%;
}
.preview-wrap {
float: left;
width: 50%;
height: 100%;
overflow: auto;
}
.preview-wrap .markdown-body {
box-sizing: border-box;
width: 100%;
max-width: 980px;
padding: 45px;
font-size: 18px;
}
.preview-wrap .markdown-body ul > li {
list-style-type: disc;
}
.preview-wrap .markdown-body ol > li {
list-style-type: lower-roman;
}