Skip to content

Commit

Permalink
feat(smart-indent-fix): fix for es6 identation problems
Browse files Browse the repository at this point in the history
Closes #259
  • Loading branch information
tivie committed Jun 7, 2016
1 parent e586201 commit 261f127
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ var defaultOptions = showdown.getDefaultOptions();
- [ ] This is still pending
```
* **smoothLivePreview**: (boolean) [default false] Prevents weird effects in live previews due to incomplete input
* **smartIndentationFix**: (boolean) [default false] Tries to smartly fix indentation problems related to es6 template strings in the midst of indented code.
## CLI Tool
Expand Down
19 changes: 17 additions & 2 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.

10 changes: 10 additions & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ showdown.Converter = function (converterOptions) {
listeners[name].push(callback);
}

function rTrimInputText(text) {
var rsp = text.match(/^\s*/)[0].length,
rgx = new RegExp('^\\s{0,' + rsp + '}', 'gm');
return text.replace(rgx, '');
}

/**
* Dispatch an event
* @private
Expand Down Expand Up @@ -262,6 +268,10 @@ showdown.Converter = function (converterOptions) {
text = text.replace(/\r\n/g, '\n'); // DOS to Unix
text = text.replace(/\r/g, '\n'); // Mac to Unix

if (options.smartIndentationFix) {
text = rTrimInputText(text);
}

// Make sure text begins and ends with a couple of newlines:
text = '\n\n' + text + '\n\n';

Expand Down
5 changes: 5 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ function getDefaultOpts(simple) {
default: false,
describe: 'Prevents weird effects in live previews due to incomplete input',
type: 'boolean'
},
smartIndentationFix: {
default: false,
description: 'Tries to smartly fix identation in es6 strings',
type: 'boolean'
}
};
if (simple === false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h2 id="markdowndoc">markdown doc</h2>

<p>you can use markdown for card documentation</p>

<ul>
<li>foo</li>

<li>bar</li>
</ul>
5 changes: 5 additions & 0 deletions test/features/#259.es6-template-strings-indentation-issues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## markdown doc
you can use markdown for card documentation
- foo
- bar
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({literalMidWordUnderscores: true, simplifiedAutoLink: true});
} else if (testsuite[i].name === '#198.literalMidWordUnderscores-changes-behavior-of-asterisk') {
converter = new showdown.Converter({literalMidWordUnderscores: true});
} else if (testsuite[i].name === '#259.es6-template-strings-indentation-issues') {
converter = new showdown.Converter({smartIndentationFix: true});
} else {
converter = new showdown.Converter();
}
Expand Down

1 comment on commit 261f127

@jdpigeon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature rocks! Thank you!

Please sign in to comment.