Skip to content

Commit

Permalink
feature(simpleLineBreaks): parse linebreaks as <br />
Browse files Browse the repository at this point in the history
This option enables linebreaks to always be treated as `<br />` tags
without needing to add spaces in front of the line, the same way GitHub does.

Closes #206
  • Loading branch information
tivie committed Nov 30, 2016
1 parent 7c05093 commit 0942b5e
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 13 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ var defaultOptions = showdown.getDefaultOptions();
* **disableForced4SpacesIndentedSublists**: (boolean) [default false] Disables the requirement of indenting sublists by 4 spaces for them to be nested,
effectively reverting to the old behavior where 2 or 3 spaces were enough. (since v1.5.0)
* **simpleLineBreaks**: (boolean) [default false] Parses line breaks as <br> like GitHub does, without needing 2 spaces at the end of the line (since v1.5.1)
```md
a line
wrapped in two
```
turns into:
```html
<p>a line<br>
wrapped in two</p>
```
## Flavors
Expand Down
20 changes: 16 additions & 4 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function getDefaultOpts(simple) {
defaultValue: false,
description: 'Disables the requirement of indenting nested sublists by 4 spaces',
type: 'boolean'
},
simpleLineBreaks: {
defaultValue: false,
description: 'Parses simple line breaks as <br> (GFM Style)',
type: 'boolean'
}
};
if (simple === false) {
Expand Down
3 changes: 2 additions & 1 deletion src/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var showdown = {},
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true,
disableForced4SpacesIndentedSublists: true
disableForced4SpacesIndentedSublists: true,
simpleLineBreaks: true
},
vanilla: getDefaultOpts(true)
};
Expand Down
10 changes: 8 additions & 2 deletions src/subParsers/spanGamut.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ showdown.subParser('spanGamut', function (text, options, globals) {
text = showdown.subParser('italicsAndBold')(text, options, globals);
text = showdown.subParser('strikethrough')(text, options, globals);

// Do hard breaks:
text = text.replace(/ +\n/g, ' <br />\n');
// Do hard breaks

// GFM style hard breaks
if (options.simpleLineBreaks) {
text = text.replace(/\n/g, '<br />\n');
} else {
text = text.replace(/ +\n/g, '<br />\n');
}

text = globals.converter._dispatch('spanGamut.after', text, options, globals);
return text;
Expand Down
2 changes: 2 additions & 0 deletions test/features/#206.treat-single-line-breaks-as-br.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>a simple<br />
wrapped line</p>
2 changes: 2 additions & 0 deletions test/features/#206.treat-single-line-breaks-as-br.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a simple
wrapped line
2 changes: 1 addition & 1 deletion test/karlcow/line-break-2-spaces.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<p>A first sentence <br />
<p>A first sentence<br />
and a line break.</p>
2 changes: 1 addition & 1 deletion test/karlcow/line-break-5-spaces.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<p>A first sentence <br />
<p>A first sentence<br />
and a line break.</p>
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({simplifiedAutoLink: true});
} else if (testsuite[i].name === 'disableForced4SpacesIndentedSublists') {
converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
} else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') {
converter = new showdown.Converter({simpleLineBreaks: true});
} else {
converter = new showdown.Converter();
}
Expand Down

0 comments on commit 0942b5e

Please sign in to comment.