Skip to content

Commit 62ba373

Browse files
committed
feat(smoothLivePreview): fix weird effects due to parsing incomplete input
1 parent 7ee2017 commit 62ba373

6 files changed

Lines changed: 25 additions & 33 deletions

File tree

dist/showdown.js

Lines changed: 11 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/options.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ function getDefaultOpts(simple) {
6565
default: false,
6666
describe: 'Turn on/off GFM tasklist support',
6767
type: 'boolean'
68+
},
69+
smoothLivePreview: {
70+
default: false,
71+
describe: 'Prevents weird effects in live previews due to incomplete input',
72+
type: 'boolean'
6873
}
6974
};
7075
if (simple === false) {

src/subParsers/headers.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ showdown.subParser('headers', function (text, options, globals) {
22
'use strict';
33

44
var prefixHeader = options.prefixHeaderId,
5-
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart);
5+
headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
66

77
// Set text-style headers:
88
// Header 1
@@ -11,7 +11,10 @@ showdown.subParser('headers', function (text, options, globals) {
1111
// Header 2
1212
// --------
1313
//
14-
text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function (wholeMatch, m1) {
14+
setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
15+
setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;
16+
17+
text = text.replace(setextRegexH1, function (wholeMatch, m1) {
1518

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

23-
text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
26+
text = text.replace(setextRegexH2, function (matchFound, m1) {
2427
var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
2528
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
2629
hLevel = headerLevelStart + 1,
@@ -35,18 +38,6 @@ showdown.subParser('headers', function (text, options, globals) {
3538
// ...
3639
// ###### Header 6
3740
//
38-
39-
/*
40-
text = text.replace(/
41-
^(\#{1,6}) // $1 = string of #'s
42-
[ \t]*
43-
(.+?) // $2 = Header text
44-
[ \t]*
45-
\#* // optional closing #'s (not counted)
46-
\n+
47-
/gm, function() {...});
48-
*/
49-
5041
text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
5142
var span = showdown.subParser('spanGamut')(m2, options, globals),
5243
hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',

0 commit comments

Comments
 (0)