Skip to content

Commit bf8b680

Browse files
author
- -
committed
add highlight&theme
1 parent 86abbf0 commit bf8b680

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

markdown-editor.vue

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default {
1212
name: 'markdown-editor',
1313
props: {
1414
value: String,
15+
previewClass: String,
1516
configs: {
1617
type: Object
1718
}
@@ -25,14 +26,44 @@ export default {
2526
configs.element = configs.element || this.$el.firstChild
2627
configs.initialValue = configs.initialValue || this.value
2728
29+
// 开启代码高亮
30+
if (configs.renderingConfig.codeSyntaxHighlighting) {
31+
require.ensure([], () => {
32+
var theme = configs.renderingConfig.highlightingTheme || 'default'
33+
window.hljs = require('highlight.js')
34+
require('highlight.js/styles/' + theme + '.css')
35+
}, 'highlight')
36+
}
37+
2838
// 实例化编辑器
2939
this.simplemde = new SimpleMDE(configs)
3040
41+
// 添加自定义 previewClass
42+
this.addPreviewClass()
43+
3144
// 绑定输入事件
3245
this.simplemde.codemirror.on('change', () => {
3346
this.$emit('input', this.simplemde.value())
3447
})
48+
},
49+
addPreviewClass () {
50+
var _class = this.configs.renderingConfig.codeSyntaxHighlighting ? this.previewClass + ' hljs' : this.previewClass
51+
var wrapper = this.simplemde.codemirror.getWrapperElement()
52+
var preview = document.createElement('div')
53+
this.$el.lastChild.className += ' ' + _class
54+
preview.className = 'editor-preview ' + _class
55+
wrapper.appendChild(preview)
3556
}
3657
}
3758
}
3859
</script>
60+
61+
<style>
62+
.markdown-editor .hljs {
63+
display: none
64+
}
65+
66+
.markdown-editor .editor-preview-active, .markdown-editor .editor-preview-active-side {
67+
display: block;
68+
}
69+
</style>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-simplemde",
3-
"version": "0.2.0",
3+
"version": "0.2.4",
44
"description": "SimpleMDE - Markdown Editor component for Vue.js",
55
"main": "index.js",
66
"scripts": {
@@ -17,6 +17,7 @@
1717
},
1818
"license": "MIT",
1919
"dependencies": {
20+
"highlight.js": "^9.9.0",
2021
"simplemde": "^1.11.2"
2122
},
2223
"repository": {

0 commit comments

Comments
 (0)