Skip to content

Commit b03b9d5

Browse files
authored
Merge pull request #1887 from o-3/master
Add configuration to render newlines as <br> or not
2 parents c9cb31b + 83a9e54 commit b03b9d5

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
@@ -56,6 +56,7 @@ export const DEFAULT_CONFIG = {
5656
plantUMLServerAddress: 'http://www.plantuml.com/plantuml',
5757
scrollPastEnd: false,
5858
smartQuotes: true,
59+
breaks: true,
5960
sanitize: 'STRICT' // 'STRICT', 'ALLOW_STYLES', 'NONE'
6061
},
6162
blog: {

browser/main/modals/PreferencesModal/UiTab.js

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

480491
<div styleName='group-section'>
481492
<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
@@ -4,6 +4,20 @@ The actual snapshot is saved in `markdown-test.js.snap`.
44

55
Generated by [AVA](https://ava.li).
66

7+
## Markdown.render() should render line breaks correctly
8+
9+
> Snapshot 1
10+
11+
`<p data-line="0">This is the first line.<br />␊
12+
This is the second line.</p>␊
13+
`
14+
15+
> Snapshot 2
16+
17+
`<p data-line="0">This is the first line.␊
18+
This is the second line.</p>␊
19+
`
20+
721
## Markdown.render() should renders KaTeX correctly
822

923
> Snapshot 1
59 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)