Skip to content

Commit

Permalink
feat(smoothLivePreview): fix weird effects due to parsing incomplete …
Browse files Browse the repository at this point in the history
…input
  • Loading branch information
tivie committed Jul 14, 2015
1 parent 7ee2017 commit 62ba373
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
26 changes: 11 additions & 15 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.

2 changes: 1 addition & 1 deletion 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
Expand Up @@ -65,6 +65,11 @@ function getDefaultOpts(simple) {
default: false,
describe: 'Turn on/off GFM tasklist support',
type: 'boolean'
},
smoothLivePreview: {
default: false,
describe: 'Prevents weird effects in live previews due to incomplete input',
type: 'boolean'
}
};
if (simple === false) {
Expand Down
21 changes: 6 additions & 15 deletions src/subParsers/headers.js
Expand Up @@ -2,7 +2,7 @@ showdown.subParser('headers', function (text, options, globals) {
'use strict';

var prefixHeader = options.prefixHeaderId,
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart);
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),

// Set text-style headers:
// Header 1
Expand All @@ -11,7 +11,10 @@ showdown.subParser('headers', function (text, options, globals) {
// Header 2
// --------
//
text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function (wholeMatch, m1) {
setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;

text = text.replace(setextRegexH1, function (wholeMatch, m1) {

var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
Expand All @@ -20,7 +23,7 @@ showdown.subParser('headers', function (text, options, globals) {
return showdown.subParser('hashBlock')(hashBlock, options, globals);
});

text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
text = text.replace(setextRegexH2, function (matchFound, m1) {
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
hLevel = headerLevelStart + 1,
Expand All @@ -35,18 +38,6 @@ showdown.subParser('headers', function (text, options, globals) {
// ...
// ###### Header 6
//

/*
text = text.replace(/
^(\#{1,6}) // $1 = string of #'s
[ \t]*
(.+?) // $2 = Header text
[ \t]*
\#* // optional closing #'s (not counted)
\n+
/gm, function() {...});
*/

text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
var span = showdown.subParser('spanGamut')(m2, options, globals),
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
Expand Down

0 comments on commit 62ba373

Please sign in to comment.