Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat run prettier on markdown #3093

Merged
merged 24 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
090b5c3
feat: Added Context Menu for markdown preview mode and copy url when …
nathan-castlehow Jun 9, 2019
ef18093
feat(prettierOnMarkdown): Added Reference To Prettier
nathan-castlehow Jun 11, 2019
25bdaf9
feat(prettierOnMarkdown): Added Reference to prettier in Code Editor…
nathan-castlehow Jun 11, 2019
f0380ef
feat(prettierOnMarkdown): Added support for prettyifing markdown as w…
nathan-castlehow Jun 15, 2019
ae0837e
feat(prettierOnMarkdown):Added prettier config default to config manager
nathan-castlehow Jun 16, 2019
020bc11
feat(prettierOnMarkdown):Tweaked spacing on default Prettier Config V…
nathan-castlehow Jun 16, 2019
fbb9afe
feat(prettierOnMarkdown):Fixed incorrect options passed to code mirro…
nathan-castlehow Jun 23, 2019
ed4a670
feat(prettierOnMarkdown): Changed default hotkey value
nathan-castlehow Jun 23, 2019
bde357f
feat(prettierOnMarkdown): Changed Prettier require to use import
nathan-castlehow Jul 3, 2019
1d59d89
feat(prettierOnMarkdown): Forced prettier options to always have pars…
nathan-castlehow Jul 3, 2019
a39e9c2
feat(prettierOnMarkdown): Added Reference To Prettier
nathan-castlehow Jun 11, 2019
7e3c662
feat(prettierOnMarkdown): Added Reference to prettier in Code Editor…
nathan-castlehow Jun 11, 2019
33161e4
feat(prettierOnMarkdown): Added support for prettyifing markdown as w…
nathan-castlehow Jun 15, 2019
de0af15
feat(prettierOnMarkdown):Added prettier config default to config manager
nathan-castlehow Jun 16, 2019
70892ca
feat(prettierOnMarkdown):Tweaked spacing on default Prettier Config V…
nathan-castlehow Jun 16, 2019
89ae2a9
feat(prettierOnMarkdown):Fixed incorrect options passed to code mirro…
nathan-castlehow Jun 23, 2019
0ad3da5
feat(prettierOnMarkdown): Changed default hotkey value
nathan-castlehow Jun 23, 2019
911fd9a
feat(prettierOnMarkdown): Changed Prettier require to use import
nathan-castlehow Jul 3, 2019
1173631
feat(prettierOnMarkdown): Forced prettier options to always have pars…
nathan-castlehow Jul 3, 2019
86370ed
Merge branch 'feat-run-prettier-on-markdown' of https://github.com/na…
nathan-castlehow Aug 1, 2019
918a862
Merge upstream into master
nathan-castlehow Aug 1, 2019
eeca031
Merge upstream into master
nathan-castlehow Aug 1, 2019
b837653
Merged Master into feature branch and fixed conflicts
nathan-castlehow Aug 1, 2019
2d3c69d
Fixed eslint issue
nathan-castlehow Aug 1, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions browser/components/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-gene
import markdownlint from 'markdownlint'
import Jsonlint from 'jsonlint-mod'
import { DEFAULT_CONFIG } from '../main/lib/ConfigManager'
import prettier from 'prettier'

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

Expand Down Expand Up @@ -216,6 +217,28 @@ export default class CodeEditor extends React.Component {
}
return CodeMirror.Pass
},
[translateHotkey(hotkey.prettifyMarkdown)]: cm => {
// Default / User configured prettier options
const currentConfig = JSON.parse(self.props.prettierConfig)

// Parser type will always need to be markdown so we override the option before use
currentConfig.parser = 'markdown'

// Get current cursor position
const cursorPos = cm.getCursor()
currentConfig.cursorOffset = cm.doc.indexFromPos(cursorPos)

// Prettify contents of editor
const formattedTextDetails = prettier.formatWithCursor(cm.doc.getValue(), currentConfig)

const formattedText = formattedTextDetails.formatted
const formattedCursorPos = formattedTextDetails.cursorOffset
cm.doc.setValue(formattedText)

// Reset Cursor position to be at the same markdown as was before prettifying
const newCursorPos = cm.doc.posFromIndex(formattedCursorPos)
cm.doc.setCursor(newCursorPos)
},
[translateHotkey(hotkey.pasteSmartly)]: cm => {
this.handlePaste(cm, true)
}
Expand Down Expand Up @@ -269,7 +292,8 @@ export default class CodeEditor extends React.Component {
explode: this.props.explodingPairs,
override: true
},
extraKeys: this.defaultKeyMap
extraKeys: this.defaultKeyMap,
prettierConfig: this.props.prettierConfig
})

document.querySelector('.CodeMirror-lint-markers').style.display = enableMarkdownLint ? 'inline-block' : 'none'
Expand Down Expand Up @@ -1183,5 +1207,6 @@ CodeEditor.defaultProps = {
autoDetect: false,
spellCheck: false,
enableMarkdownLint: DEFAULT_CONFIG.editor.enableMarkdownLint,
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig,
prettierConfig: DEFAULT_CONFIG.editor.prettierConfig
}
1 change: 1 addition & 0 deletions browser/components/MarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class MarkdownEditor extends React.Component {
switchPreview={config.editor.switchPreview}
enableMarkdownLint={config.editor.enableMarkdownLint}
customMarkdownLintConfig={config.editor.customMarkdownLintConfig}
prettierConfig={config.editor.prettierConfig}
/>
<MarkdownPreview styleName={this.state.status === 'PREVIEW'
? 'preview'
Expand Down
10 changes: 9 additions & 1 deletion browser/main/lib/ConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const DEFAULT_CONFIG = {
toggleMode: OSX ? 'Command + Alt + M' : 'Ctrl + M',
deleteNote: OSX ? 'Command + Shift + Backspace' : 'Ctrl + Shift + Backspace',
pasteSmartly: OSX ? 'Command + Shift + V' : 'Ctrl + Shift + V',
prettifyMarkdown: 'Shift + F',
insertDate: OSX ? 'Command + /' : 'Ctrl + /',
insertDateTime: OSX ? 'Command + Alt + /' : 'Ctrl + Shift + /',
toggleMenuBar: 'Alt'
Expand Down Expand Up @@ -68,7 +69,14 @@ export const DEFAULT_CONFIG = {
spellcheck: false,
enableSmartPaste: false,
enableMarkdownLint: false,
customMarkdownLintConfig: DEFAULT_MARKDOWN_LINT_CONFIG
customMarkdownLintConfig: DEFAULT_MARKDOWN_LINT_CONFIG,
prettierConfig: ` {
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}`

},
preview: {
fontSize: '14',
Expand Down
11 changes: 11 additions & 0 deletions browser/main/modals/PreferencesModal/HotkeyTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class HotkeyTab extends React.Component {
toggleMode: this.refs.toggleMode.value,
deleteNote: this.refs.deleteNote.value,
pasteSmartly: this.refs.pasteSmartly.value,
prettifyMarkdown: this.refs.prettifyMarkdown.value,
toggleMenuBar: this.refs.toggleMenuBar.value
}
this.setState({
Expand Down Expand Up @@ -173,6 +174,16 @@ class HotkeyTab extends React.Component {
/>
</div>
</div>
<div styleName='group-section'>
<div styleName='group-section-label'>{i18n.__('Prettify Markdown')}</div>
<div styleName='group-section-control'>
<input styleName='group-section-control-input'
onChange={(e) => this.handleHotkeyChange(e)}
ref='prettifyMarkdown'
value={config.hotkey.prettifyMarkdown}
type='text' />
</div>
</div>
<div styleName='group-section'>
<div styleName='group-section-label'>{i18n.__('Insert Current Date')}</div>
<div styleName='group-section-control'>
Expand Down
30 changes: 28 additions & 2 deletions browser/main/modals/PreferencesModal/UiTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ class UiTab extends React.Component {
CodeMirror.autoLoadMode(this.codeMirrorInstance.getCodeMirror(), 'javascript')
CodeMirror.autoLoadMode(this.customCSSCM.getCodeMirror(), 'css')
CodeMirror.autoLoadMode(this.customMarkdownLintConfigCM.getCodeMirror(), 'javascript')
CodeMirror.autoLoadMode(this.prettierConfigCM.getCodeMirror(), 'javascript')
// Set CM editor Sizes
this.customCSSCM.getCodeMirror().setSize('400px', '400px')
this.prettierConfigCM.getCodeMirror().setSize('400px', '400px')
this.customMarkdownLintConfigCM.getCodeMirror().setSize('400px', '200px')

this.handleSettingDone = () => {
this.setState({UiAlert: {
type: 'success',
Expand Down Expand Up @@ -107,7 +111,9 @@ class UiTab extends React.Component {
spellcheck: this.refs.spellcheck.checked,
enableSmartPaste: this.refs.enableSmartPaste.checked,
enableMarkdownLint: this.refs.enableMarkdownLint.checked,
customMarkdownLintConfig: this.customMarkdownLintConfigCM.getCodeMirror().getValue()
customMarkdownLintConfig: this.customMarkdownLintConfigCM.getCodeMirror().getValue(),
prettierConfig: this.prettierConfigCM.getCodeMirror().getValue()

},
preview: {
fontSize: this.refs.previewFontSize.value,
Expand Down Expand Up @@ -915,7 +921,27 @@ class UiTab extends React.Component {
</div>
</div>
</div>

<div styleName='group-section'>
<div styleName='group-section-label'>
{i18n.__('Prettier Config')}
</div>
<div styleName='group-section-control'>
<div style={{fontFamily}}>
<ReactCodeMirror
width='400px'
height='400px'
onChange={e => this.handleUIChange(e)}
ref={e => (this.prettierConfigCM = e)}
value={config.editor.prettierConfig}
options={{
lineNumbers: true,
mode: 'application/json',
lint: true,
theme: codemirrorTheme
}} />
</div>
</div>
</div>
<div styleName='group-control'>
<button styleName='group-control-rightButton'
onClick={(e) => this.handleSaveUIClick(e)}>{i18n.__('Save')}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"mousetrap": "^1.6.2",
"mousetrap-global-bind": "^1.1.0",
"node-ipc": "^8.1.0",
"prettier": "^1.18.2",
"prop-types": "^15.7.2",
"query-string": "^6.5.0",
"raphael": "^2.2.7",
Expand Down
6 changes: 6 additions & 0 deletions prettier.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7482,6 +7482,11 @@ preserve@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"

prettier@^1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==

pretty-bytes@^1.0.2:
version "1.0.4"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-1.0.4.tgz#0a22e8210609ad35542f8c8d5d2159aff0751c84"
Expand Down