Skip to content

Commit b9dd651

Browse files
authored
Merge pull request #3093 from nathan-castlehow/feat-run-prettier-on-markdown
Feat run prettier on markdown
2 parents 25ef456 + 2d3c69d commit b9dd651

File tree

8 files changed

+88
-5
lines changed

8 files changed

+88
-5
lines changed

browser/components/CodeEditor.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-gene
2828
import markdownlint from 'markdownlint'
2929
import Jsonlint from 'jsonlint-mod'
3030
import { DEFAULT_CONFIG } from '../main/lib/ConfigManager'
31+
import prettier from 'prettier'
3132

3233
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
3334

@@ -216,6 +217,28 @@ export default class CodeEditor extends React.Component {
216217
}
217218
return CodeMirror.Pass
218219
},
220+
[translateHotkey(hotkey.prettifyMarkdown)]: cm => {
221+
// Default / User configured prettier options
222+
const currentConfig = JSON.parse(self.props.prettierConfig)
223+
224+
// Parser type will always need to be markdown so we override the option before use
225+
currentConfig.parser = 'markdown'
226+
227+
// Get current cursor position
228+
const cursorPos = cm.getCursor()
229+
currentConfig.cursorOffset = cm.doc.indexFromPos(cursorPos)
230+
231+
// Prettify contents of editor
232+
const formattedTextDetails = prettier.formatWithCursor(cm.doc.getValue(), currentConfig)
233+
234+
const formattedText = formattedTextDetails.formatted
235+
const formattedCursorPos = formattedTextDetails.cursorOffset
236+
cm.doc.setValue(formattedText)
237+
238+
// Reset Cursor position to be at the same markdown as was before prettifying
239+
const newCursorPos = cm.doc.posFromIndex(formattedCursorPos)
240+
cm.doc.setCursor(newCursorPos)
241+
},
219242
[translateHotkey(hotkey.pasteSmartly)]: cm => {
220243
this.handlePaste(cm, true)
221244
}
@@ -269,7 +292,8 @@ export default class CodeEditor extends React.Component {
269292
explode: this.props.explodingPairs,
270293
override: true
271294
},
272-
extraKeys: this.defaultKeyMap
295+
extraKeys: this.defaultKeyMap,
296+
prettierConfig: this.props.prettierConfig
273297
})
274298

275299
document.querySelector('.CodeMirror-lint-markers').style.display = enableMarkdownLint ? 'inline-block' : 'none'
@@ -1194,5 +1218,6 @@ CodeEditor.defaultProps = {
11941218
autoDetect: false,
11951219
spellCheck: false,
11961220
enableMarkdownLint: DEFAULT_CONFIG.editor.enableMarkdownLint,
1197-
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig
1221+
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig,
1222+
prettierConfig: DEFAULT_CONFIG.editor.prettierConfig
11981223
}

browser/components/MarkdownEditor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ class MarkdownEditor extends React.Component {
323323
switchPreview={config.editor.switchPreview}
324324
enableMarkdownLint={config.editor.enableMarkdownLint}
325325
customMarkdownLintConfig={config.editor.customMarkdownLintConfig}
326+
prettierConfig={config.editor.prettierConfig}
326327
/>
327328
<MarkdownPreview styleName={this.state.status === 'PREVIEW'
328329
? 'preview'

browser/main/lib/ConfigManager.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const DEFAULT_CONFIG = {
3131
toggleMode: OSX ? 'Command + Alt + M' : 'Ctrl + M',
3232
deleteNote: OSX ? 'Command + Shift + Backspace' : 'Ctrl + Shift + Backspace',
3333
pasteSmartly: OSX ? 'Command + Shift + V' : 'Ctrl + Shift + V',
34+
prettifyMarkdown: 'Shift + F',
3435
insertDate: OSX ? 'Command + /' : 'Ctrl + /',
3536
insertDateTime: OSX ? 'Command + Alt + /' : 'Ctrl + Shift + /',
3637
toggleMenuBar: 'Alt'
@@ -68,7 +69,14 @@ export const DEFAULT_CONFIG = {
6869
spellcheck: false,
6970
enableSmartPaste: false,
7071
enableMarkdownLint: false,
71-
customMarkdownLintConfig: DEFAULT_MARKDOWN_LINT_CONFIG
72+
customMarkdownLintConfig: DEFAULT_MARKDOWN_LINT_CONFIG,
73+
prettierConfig: ` {
74+
"trailingComma": "es5",
75+
"tabWidth": 4,
76+
"semi": false,
77+
"singleQuote": true
78+
}`
79+
7280
},
7381
preview: {
7482
fontSize: '14',

browser/main/modals/PreferencesModal/HotkeyTab.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class HotkeyTab extends React.Component {
8181
toggleMode: this.refs.toggleMode.value,
8282
deleteNote: this.refs.deleteNote.value,
8383
pasteSmartly: this.refs.pasteSmartly.value,
84+
prettifyMarkdown: this.refs.prettifyMarkdown.value,
8485
toggleMenuBar: this.refs.toggleMenuBar.value
8586
}
8687
this.setState({
@@ -173,6 +174,16 @@ class HotkeyTab extends React.Component {
173174
/>
174175
</div>
175176
</div>
177+
<div styleName='group-section'>
178+
<div styleName='group-section-label'>{i18n.__('Prettify Markdown')}</div>
179+
<div styleName='group-section-control'>
180+
<input styleName='group-section-control-input'
181+
onChange={(e) => this.handleHotkeyChange(e)}
182+
ref='prettifyMarkdown'
183+
value={config.hotkey.prettifyMarkdown}
184+
type='text' />
185+
</div>
186+
</div>
176187
<div styleName='group-section'>
177188
<div styleName='group-section-label'>{i18n.__('Insert Current Date')}</div>
178189
<div styleName='group-section-control'>

browser/main/modals/PreferencesModal/UiTab.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ class UiTab extends React.Component {
3131
CodeMirror.autoLoadMode(this.codeMirrorInstance.getCodeMirror(), 'javascript')
3232
CodeMirror.autoLoadMode(this.customCSSCM.getCodeMirror(), 'css')
3333
CodeMirror.autoLoadMode(this.customMarkdownLintConfigCM.getCodeMirror(), 'javascript')
34+
CodeMirror.autoLoadMode(this.prettierConfigCM.getCodeMirror(), 'javascript')
35+
// Set CM editor Sizes
3436
this.customCSSCM.getCodeMirror().setSize('400px', '400px')
37+
this.prettierConfigCM.getCodeMirror().setSize('400px', '400px')
3538
this.customMarkdownLintConfigCM.getCodeMirror().setSize('400px', '200px')
39+
3640
this.handleSettingDone = () => {
3741
this.setState({UiAlert: {
3842
type: 'success',
@@ -106,7 +110,9 @@ class UiTab extends React.Component {
106110
spellcheck: this.refs.spellcheck.checked,
107111
enableSmartPaste: this.refs.enableSmartPaste.checked,
108112
enableMarkdownLint: this.refs.enableMarkdownLint.checked,
109-
customMarkdownLintConfig: this.customMarkdownLintConfigCM.getCodeMirror().getValue()
113+
customMarkdownLintConfig: this.customMarkdownLintConfigCM.getCodeMirror().getValue(),
114+
prettierConfig: this.prettierConfigCM.getCodeMirror().getValue()
115+
110116
},
111117
preview: {
112118
fontSize: this.refs.previewFontSize.value,
@@ -914,7 +920,27 @@ class UiTab extends React.Component {
914920
</div>
915921
</div>
916922
</div>
917-
923+
<div styleName='group-section'>
924+
<div styleName='group-section-label'>
925+
{i18n.__('Prettier Config')}
926+
</div>
927+
<div styleName='group-section-control'>
928+
<div style={{fontFamily}}>
929+
<ReactCodeMirror
930+
width='400px'
931+
height='400px'
932+
onChange={e => this.handleUIChange(e)}
933+
ref={e => (this.prettierConfigCM = e)}
934+
value={config.editor.prettierConfig}
935+
options={{
936+
lineNumbers: true,
937+
mode: 'application/json',
938+
lint: true,
939+
theme: codemirrorTheme
940+
}} />
941+
</div>
942+
</div>
943+
</div>
918944
<div styleName='group-control'>
919945
<button styleName='group-control-rightButton'
920946
onClick={(e) => this.handleSaveUIClick(e)}>{i18n.__('Save')}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"mousetrap": "^1.6.2",
9999
"mousetrap-global-bind": "^1.1.0",
100100
"node-ipc": "^8.1.0",
101+
"prettier": "^1.18.2",
101102
"prop-types": "^15.7.2",
102103
"query-string": "^6.5.0",
103104
"raphael": "^2.2.7",

prettier.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 4,
4+
"semi": false,
5+
"singleQuote": true
6+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7482,6 +7482,11 @@ preserve@^0.2.0:
74827482
version "0.2.0"
74837483
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
74847484

7485+
prettier@^1.18.2:
7486+
version "1.18.2"
7487+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
7488+
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
7489+
74857490
pretty-bytes@^1.0.2:
74867491
version "1.0.4"
74877492
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-1.0.4.tgz#0a22e8210609ad35542f8c8d5d2159aff0751c84"

0 commit comments

Comments
 (0)