Skip to content

Commit 9f37f34

Browse files
committed
Export defaultContentParser
1 parent 738dd68 commit 9f37f34

File tree

8 files changed

+67
-51
lines changed

8 files changed

+67
-51
lines changed

src/components/ContentParser.js

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { MarkdownParser } from './plugin/MarkdownParser'
2+
import md5 from 'md5'
3+
4+
function contentParserFactory (parsers) {
5+
return content => {
6+
let newContent = content
7+
const stringMap = []
8+
9+
parsers.forEach(function (parserConfig) {
10+
let matches = newContent.match(parserConfig.reg)
11+
12+
while (matches !== null) {
13+
const mapItem = {
14+
hash: md5(matches[0]),
15+
segment: matches[0],
16+
content: parserConfig.parser(matches)
17+
}
18+
newContent = newContent.replace(mapItem.segment, mapItem.hash)
19+
stringMap.push(mapItem)
20+
21+
matches = newContent.match(parserConfig.reg)
22+
}
23+
})
24+
25+
newContent = MarkdownParser(newContent, stringMap)
26+
27+
stringMap.forEach(function (mapItem) {
28+
newContent = newContent.replace(mapItem.hash, mapItem.content)
29+
})
30+
return newContent
31+
}
32+
}
33+
34+
export { contentParserFactory }

src/components/DefaultConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function set (obj, config) {
1515
export const defaultConfig = {
1616
height: '500px',
1717
previewDisplay: 'normal',
18-
fullscreen: false,
18+
fullScreen: false,
1919
parsers: [
2020
KatexParser
2121
],

src/components/MarkdownPalettes.vue

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div id="mp-luogu-markdown-editor" class="mp-editor-container" :class="{'mp-fullscreen': this.config.fullscreen}">
2+
<div id="mp-luogu-markdown-editor" class="mp-editor-container" :class="{'mp-full-screen': this.config.fullScreen}">
33
<div id="mp-editor-toolbar" class="mp-editor-toolbar">
44
<toolbar @change="insert" @click="clickToolbar" @input="handleToolbarOperation"
55
:toolbarConfig="editorConfig.toolbarConfig" ref="toolbar"></toolbar>
@@ -21,7 +21,7 @@
2121
'mp-editor-area': this.config.previewDisplay === 'normal',
2222
'mp-editor-area-hide': this.config.previewDisplay === 'hide'
2323
}">
24-
<preview-area v-model="code" :parsers="editorConfig.parsers" ref="previewArea"></preview-area>
24+
<preview-area v-model="code" :parser="contentParser" ref="previewArea"></preview-area>
2525
</div>
2626
</div>
2727
<div id="mp-editor-dialog">
@@ -58,7 +58,7 @@
5858
padding-bottom: 2px;
5959
}
6060
61-
.mp-fullscreen {
61+
.mp-full-screen {
6262
position: fixed;
6363
z-index: 9997;
6464
top: 0;
@@ -74,7 +74,8 @@ import PreviewArea from './PreviewArea.vue'
7474
import Toolbar from './Toolbar.vue'
7575
import EditorDialog from './Dialog.vue'
7676
77-
import {defaultConfig, getConfig} from './DefaultConfig'
77+
import { defaultConfig, getConfig } from './DefaultConfig'
78+
import { contentParserFactory } from './ContentParserFactory'
7879
7980
export default {
8081
name: 'markdown-palettes',
@@ -97,7 +98,8 @@ export default {
9798
dialogRequest: {},
9899
insertCode: null,
99100
editorConfig: config,
100-
editorHeight: config.height
101+
editorHeight: config.height,
102+
contentParser: contentParserFactory(config.parsers)
101103
}
102104
},
103105
mounted () {
@@ -136,12 +138,12 @@ export default {
136138
if (operation === 'hide') {
137139
if (this.config.previewDisplay === 'normal') { this.config.previewDisplay = 'hide' } else { this.config.previewDisplay = 'normal' }
138140
}
139-
if (operation === 'fullscreen') {
140-
if (!this.config.fullscreen) {
141-
this.config.fullscreen = true
141+
if (operation === 'fullScreen') {
142+
if (!this.config.fullScreen) {
143+
this.config.fullScreen = true
142144
this.editorHeight = (window.innerHeight - this.$refs.toolbar.$el.clientHeight).toString() + 'px'
143145
} else {
144-
this.config.fullscreen = false
146+
this.config.fullScreen = false
145147
this.editorHeight = this.editorConfig.height
146148
}
147149
}

src/components/PreviewArea.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,15 @@
7979
</style>
8080

8181
<script>
82-
import { ContentParser } from './ContentParser'
83-
8482
export default {
8583
name: 'preview-area',
8684
props: {
8785
value: {
8886
type: String,
8987
default: ''
9088
},
91-
parsers: {
92-
type: Array
89+
parser: {
90+
type: Function
9391
}
9492
},
9593
data () {
@@ -102,7 +100,7 @@ export default {
102100
},
103101
methods: {
104102
updateContent (newContent) {
105-
this.content = ContentParser(newContent, this.parsers)
103+
this.content = this.parser(newContent)
106104
}
107105
},
108106
watch: {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export default {
2-
name: 'fullscreen',
2+
name: 'fullScreen',
33
icon: 'fa-arrows-alt',
44
title: '全屏',
55
action: {
6-
event: 'fullscreen'
6+
event: 'fullScreen'
77
}
88
}

src/main.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import Vue from 'vue'
22
import Editor from './components/MarkdownPalettes.vue'
3+
import { getConfig } from './components/DefaultConfig'
4+
import { contentParserFactory } from './components/ContentParserFactory'
35

46
Vue.config.productionTip = false
57

68
class MarkdownPalettes {
7-
constructor (el) {
9+
constructor (el, config = {}) {
10+
this.config = config
811
this.editor = new Vue({
912
el: el,
1013
data () {
@@ -16,7 +19,8 @@ class MarkdownPalettes {
1619
const vm = this
1720
return createElement(Editor, {
1821
props: {
19-
value: vm.code
22+
value: vm.code,
23+
config: this.config
2024
},
2125
on: {
2226
input (event) {
@@ -35,6 +39,9 @@ class MarkdownPalettes {
3539
}
3640
})
3741
}
42+
getContentParaser () {
43+
return contentParserFactory(getConfig(this.config))
44+
}
3845
}
3946

4047
window.MarkdownPalettes = MarkdownPalettes

src/module.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import Editor from './components/MarkdownPalettes.vue'
22

3+
import { contentParserFactory } from './components/ContentParserFactory'
4+
import { defaultConfig } from './components/DefaultConfig'
5+
36
export default Editor
7+
8+
const defaultContentParser = contentParserFactory(defaultConfig.parsers)
9+
10+
export { Editor, defaultContentParser }

0 commit comments

Comments
 (0)