Skip to content

Commit ab038b1

Browse files
committed
Add configuration to render line breaks as <br> or not
1 parent 372933f commit ab038b1

File tree

10 files changed

+50
-6
lines changed

10 files changed

+50
-6
lines changed

browser/components/MarkdownEditor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ class MarkdownEditor extends React.Component {
283283
indentSize={editorIndentSize}
284284
scrollPastEnd={config.preview.scrollPastEnd}
285285
smartQuotes={config.preview.smartQuotes}
286+
breaks={config.preview.breaks}
286287
sanitize={config.preview.sanitize}
287288
ref='preview'
288289
onContextMenu={(e) => this.handleContextMenu(e)}

browser/components/MarkdownPreview.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,11 @@ export default class MarkdownPreview extends React.Component {
145145
}
146146

147147
initMarkdown () {
148-
const { smartQuotes, sanitize } = this.props
148+
const { smartQuotes, sanitize, breaks } = this.props
149149
this.markdown = new Markdown({
150150
typographer: smartQuotes,
151-
sanitize
151+
sanitize,
152+
breaks
152153
})
153154
}
154155

@@ -340,7 +341,9 @@ export default class MarkdownPreview extends React.Component {
340341

341342
componentDidUpdate (prevProps) {
342343
if (prevProps.value !== this.props.value) this.rewriteIframe()
343-
if (prevProps.smartQuotes !== this.props.smartQuotes || prevProps.sanitize !== this.props.sanitize) {
344+
if (prevProps.smartQuotes !== this.props.smartQuotes ||
345+
prevProps.sanitize !== this.props.sanitize ||
346+
prevProps.breaks !== this.props.breaks) {
344347
this.initMarkdown()
345348
this.rewriteIframe()
346349
}
@@ -599,5 +602,6 @@ MarkdownPreview.propTypes = {
599602
value: PropTypes.string,
600603
showCopyNotification: PropTypes.bool,
601604
storagePath: PropTypes.string,
602-
smartQuotes: PropTypes.bool
605+
smartQuotes: PropTypes.bool,
606+
breaks: PropTypes.bool
603607
}

browser/components/MarkdownSplitEditor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class MarkdownSplitEditor extends React.Component {
131131
lineNumber={config.preview.lineNumber}
132132
scrollPastEnd={config.preview.scrollPastEnd}
133133
smartQuotes={config.preview.smartQuotes}
134+
breaks={config.preview.breaks}
134135
sanitize={config.preview.sanitize}
135136
ref='preview'
136137
tabInde='0'

browser/lib/markdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Markdown {
2525
linkify: true,
2626
html: true,
2727
xhtmlOut: true,
28-
breaks: true,
28+
breaks: config.preview.breaks,
2929
highlight: function (str, lang) {
3030
const delimiter = ':'
3131
const langInfo = lang.split(delimiter)

browser/main/lib/ConfigManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const DEFAULT_CONFIG = {
5555
latexBlockClose: '$$',
5656
scrollPastEnd: false,
5757
smartQuotes: true,
58+
breaks: true,
5859
sanitize: 'STRICT' // 'STRICT', 'ALLOW_STYLES', 'NONE'
5960
},
6061
blog: {

browser/main/modals/PreferencesModal/UiTab.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class UiTab extends React.Component {
9696
latexBlockClose: this.refs.previewLatexBlockClose.value,
9797
scrollPastEnd: this.refs.previewScrollPastEnd.checked,
9898
smartQuotes: this.refs.previewSmartQuotes.checked,
99+
breaks: this.refs.previewBreaks.checked,
99100
sanitize: this.refs.previewSanitize.value
100101
}
101102
}
@@ -475,6 +476,16 @@ class UiTab extends React.Component {
475476
Enable smart quotes
476477
</label>
477478
</div>
479+
<div styleName='group-checkBoxSection'>
480+
<label>
481+
<input onChange={(e) => this.handleUIChange(e)}
482+
checked={this.state.config.preview.breaks}
483+
ref='previewBreaks'
484+
type='checkbox'
485+
/>&nbsp;
486+
Render newlines in Markdown paragraphs as &lt;br&gt;
487+
</label>
488+
</div>
478489

479490
<div styleName='group-section'>
480491
<div styleName='group-section-label'>

tests/fixtures/markdowns.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ const checkboxes = `
4848

4949
const smartQuotes = 'This is a "QUOTE".'
5050

51+
const breaks = 'This is the first line.\nThis is the second line.'
52+
5153
export default {
5254
basic,
5355
codeblock,
5456
katex,
5557
checkboxes,
56-
smartQuotes
58+
smartQuotes,
59+
breaks
5760
}

tests/lib/markdown-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ test('Markdown.render() should text with quotes correctly', t => {
3434
const renderedNonSmartQuotes = newmd.render(markdownFixtures.smartQuotes)
3535
t.snapshot(renderedNonSmartQuotes)
3636
})
37+
38+
test('Markdown.render() should render line breaks correctly', t => {
39+
const renderedBreaks = md.render(markdownFixtures.breaks)
40+
t.snapshot(renderedBreaks)
41+
42+
const newmd = new Markdown({ breaks: false })
43+
const renderedNonBreaks = newmd.render(markdownFixtures.breaks)
44+
t.snapshot(renderedNonBreaks)
45+
})

tests/lib/snapshots/markdown-test.js.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,18 @@ Generated by [AVA](https://ava.li).
7373
> Snapshot 2
7474
7575
`<p data-line="0">This is a &quot;QUOTE&quot;.</p>␊
76+
77+
78+
## Markdown.render() should render line breaks correctly
79+
80+
> Snapshot 1
81+
82+
`<p data-line="0">This is the first line.<br />␊
83+
This is the second line.</p>␊
7684
`
85+
86+
> Snapshot 2
87+
88+
`<p data-line="0">This is the first line.␊
89+
This is the second line.</p>␊
90+
`
1.36 KB
Binary file not shown.

0 commit comments

Comments
 (0)