Skip to content

Commit 1ceef62

Browse files
committed
feat: auto format markdown text(Ctrl-F)
按 Ctrl-F 自动格式化 Markdown 文章
1 parent c42ac06 commit 1ceef62

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"juice": "^6.0.0",
1919
"markdown": "^0.5.0",
2020
"marked": "^0.8.0",
21+
"prettier": "^2.0.5",
2122
"prettify": "^0.1.7",
2223
"vue": "^2.6.10",
2324
"vue-router": "^3.1.3",

src/store/index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Vuex from 'vuex'
33
import config from '../scripts/config';
44
import WxRenderer from '../scripts/renderers/wx-renderer'
55
import marked from 'marked'
6+
import prettier from 'prettier/standalone'
7+
import prettierMarkdown from 'prettier/parser-markdown'
68
import CodeMirror from 'codemirror/lib/codemirror'
79
import DEFAULT_CONTENT from '../scripts/default-content'
810
import DEFAULT_CSS_CONTENT from '../scripts/themes/default-theme-css'
@@ -70,15 +72,25 @@ const mutations = {
7072
},
7173
initEditorEntity(state) {
7274
state.editor = CodeMirror.fromTextArea(
73-
document.getElementById('editor'),
75+
document.getElementById('editor'),
7476
{
7577
value: '',
7678
mode: 'text/x-markdown',
7779
theme: 'xq-light',
7880
lineNumbers: false,
7981
lineWrapping: true,
8082
styleActiveLine: true,
81-
autoCloseBrackets: true
83+
autoCloseBrackets: true,
84+
extraKeys: {
85+
'Ctrl-F': function autoFormat(editor) {
86+
const doc = prettier.format(editor.getValue(0), {
87+
parser: 'markdown',
88+
plugins: [prettierMarkdown]
89+
})
90+
localStorage.setItem('__editor_content', doc)
91+
editor.setValue(doc)
92+
}
93+
}
8294
}
8395
)
8496
// 如果有编辑器内容被保存则读取,否则加载默认内容
@@ -90,7 +102,8 @@ const mutations = {
90102
},
91103
initCssEditorEntity(state) {
92104
state.cssEditor = CodeMirror.fromTextArea(
93-
document.getElementById('cssEditor'), {
105+
document.getElementById('cssEditor'),
106+
{
94107
value: '',
95108
mode: 'css',
96109
theme: 'style-mirror',
@@ -99,10 +112,10 @@ const mutations = {
99112
matchBrackets: true,
100113
autofocus: true,
101114
extraKeys: {
102-
'Ctrl-F': function autoFormat(editor) {
103-
const totalLines = editor.lineCount()
115+
'Ctrl-F': function autoFormat(editor) {
116+
const totalLines = editor.lineCount()
104117

105-
editor.autoFormatRange({
118+
editor.autoFormatRange({
106119
line: 0,
107120
ch: 0
108121
}, {

0 commit comments

Comments
 (0)