mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
hard line break implementation
This commit is contained in:
parent
6e6af3cc03
commit
cd293fb61a
|
@ -55,6 +55,8 @@ showdown.subParser('makehtml.codeSpan', function (text, options, globals) {
|
|||
|
||||
c = c.replace(/^([ \t]*)/g, ''); // leading whitespace
|
||||
c = c.replace(/[ \t]*$/g, ''); // trailing whitespace
|
||||
// remove newlines
|
||||
c = c.replace(/\n/, ' ');
|
||||
|
||||
let captureStartEvent = new showdown.Event('makehtml.codeSpan.onCapture', c);
|
||||
captureStartEvent
|
||||
|
|
|
@ -85,18 +85,23 @@ showdown.subParser('makehtml.emphasisAndStrong', function (text, options, global
|
|||
if (showdown.helper.isUndefined(attributes.strong)) {
|
||||
attributes.strong = {};
|
||||
}
|
||||
|
||||
switch (tags) {
|
||||
case '<em>':
|
||||
otp = '<em' + showdown.helper._populateAttributes(attributes.em) + '>' + txt + '</em>';
|
||||
otp = '<em' + showdown.helper._populateAttributes(attributes.em) + '>' +
|
||||
showdown.subParser('makehtml.hardLineBreaks')(txt, options, globals) +
|
||||
'</em>';
|
||||
break;
|
||||
case '<strong>':
|
||||
otp = '<strong' + showdown.helper._populateAttributes(attributes.strong) + '>' + txt + '</strong>';
|
||||
otp = '<strong' + showdown.helper._populateAttributes(attributes.strong) + '>' +
|
||||
showdown.subParser('makehtml.hardLineBreaks')(txt, options, globals) +
|
||||
'</strong>';
|
||||
break;
|
||||
case '<strong><em>':
|
||||
otp = '<strong' + showdown.helper._populateAttributes(attributes.strong) + '>' +
|
||||
'<em' + showdown.helper._populateAttributes(attributes.em) + '>' +
|
||||
txt +
|
||||
'</em>' +
|
||||
'<em' + showdown.helper._populateAttributes(attributes.em) + '>' +
|
||||
showdown.subParser('makehtml.hardLineBreaks')(txt, options, globals) +
|
||||
'</em>' +
|
||||
'</strong>';
|
||||
break;
|
||||
}
|
||||
|
@ -109,10 +114,11 @@ showdown.subParser('makehtml.emphasisAndStrong', function (text, options, global
|
|||
._setOptions(options);
|
||||
beforeHashEvent = globals.converter.dispatch(beforeHashEvent);
|
||||
otp = beforeHashEvent.output;
|
||||
otp = showdown.subParser('makehtml.hashHTMLSpans')(otp, options, globals);
|
||||
return otp;
|
||||
}
|
||||
|
||||
// it's faster to have 3 separate regexes for each case than have just one
|
||||
// it's faster to have separate regexes for each case than have just one
|
||||
// because of backtracking, in some cases, it could lead to an exponential effect
|
||||
// called "catastrophic backtrace". Ominous!
|
||||
const lmwuStrongEmRegex = /\b___(\S[\s\S]*?)___\b/g,
|
||||
|
|
|
@ -42,7 +42,6 @@ showdown.subParser('makehtml.githubCodeBlock', function (text, options, globals)
|
|||
});
|
||||
|
||||
text = text.replace(emptyBlockRegex, function (wholeMatch, delim, language) {
|
||||
console.log('|', wholeMatch, '|>');
|
||||
return parse(emptyBlockRegex, wholeMatch, delim, language, '');
|
||||
});
|
||||
|
||||
|
|
32
src/subParsers/makehtml/hardLineBreaks.js
Normal file
32
src/subParsers/makehtml/hardLineBreaks.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
////
|
||||
// makehtml/emphasisAndStrong.js
|
||||
// Copyright (c) 2022 ShowdownJS
|
||||
//
|
||||
// Transforms MD emphasis and strong into `<em>` and `<strong>` html entities
|
||||
//
|
||||
// Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.
|
||||
// Text wrapped with one * or _ will be wrapped with an HTML <em> tag;
|
||||
// double *’s or _’s will be wrapped with an HTML <strong> tag
|
||||
//
|
||||
// ***Author:***
|
||||
// - Estêvão Soares dos Santos (Tivie) <https://github.com/tivie>
|
||||
////
|
||||
|
||||
showdown.subParser('makehtml.hardLineBreaks', function (text, options) {
|
||||
|
||||
// Do hard breaks
|
||||
if (options.simpleLineBreaks) {
|
||||
// GFM style hard breaks
|
||||
// only add line breaks if the text does not contain a block (special case for lists)
|
||||
if (!/\n\n¨K/.test(text)) {
|
||||
text = text.replace(/\n+/gm, '<br />\n');
|
||||
}
|
||||
} else {
|
||||
// Vanilla hard breaks
|
||||
text = text.replace(/ +\n/g, '<br />\n');
|
||||
}
|
||||
text = text.replace(/\\\n/g, '<br />\n');
|
||||
|
||||
return text;
|
||||
|
||||
});
|
|
@ -168,7 +168,6 @@ showdown.subParser('makehtml.heading', function (text, options, globals) {
|
|||
}
|
||||
|
||||
nPrepend = showdown.subParser('makehtml.blockGamut')(multilineText, options, globals, 'makehtml.heading');
|
||||
//console.log(nPrepend);
|
||||
if (nPrepend !== multilineText) {
|
||||
// we found a block, so it should take precedence
|
||||
prepend += nPrepend;
|
||||
|
@ -192,7 +191,7 @@ showdown.subParser('makehtml.heading', function (text, options, globals) {
|
|||
|
||||
function parseHeader (pattern, wholeMatch, headingText, headingLevel, headingId) {
|
||||
let captureStartEvent = new showdown.Event('makehtml.heading.onCapture', headingText),
|
||||
otp;
|
||||
otp;
|
||||
|
||||
captureStartEvent
|
||||
.setOutput(null)
|
||||
|
@ -215,7 +214,7 @@ showdown.subParser('makehtml.heading', function (text, options, globals) {
|
|||
} else {
|
||||
headingText = captureStartEvent.matches.heading;
|
||||
let spanGamut = showdown.subParser('makehtml.spanGamut')(headingText, options, globals),
|
||||
attributes = captureStartEvent.attributes;
|
||||
attributes = captureStartEvent.attributes;
|
||||
otp = '<h' + headingLevel + showdown.helper._populateAttributes(attributes) + '>' + spanGamut + '</h' + headingLevel + '>';
|
||||
}
|
||||
|
||||
|
|
|
@ -34,17 +34,7 @@ showdown.subParser('makehtml.spanGamut', function (text, options, globals) {
|
|||
// now we encode amps and angles
|
||||
text = showdown.subParser('makehtml.encodeAmpsAndAngles')(text, options, globals);
|
||||
|
||||
// Do hard breaks
|
||||
if (options.simpleLineBreaks) {
|
||||
// GFM style hard breaks
|
||||
// only add line breaks if the text does not contain a block (special case for lists)
|
||||
if (!/\n\n¨K/.test(text)) {
|
||||
text = text.replace(/\n+/g, '<br />\n');
|
||||
}
|
||||
} else {
|
||||
// Vanilla hard breaks
|
||||
text = text.replace(/ +\n/g, '<br />\n');
|
||||
}
|
||||
text = showdown.subParser('makehtml.hardLineBreaks')(text, options, globals);
|
||||
|
||||
let afterEvent = new showdown.Event('makehtml.spanGamut.onEnd', text);
|
||||
afterEvent
|
||||
|
|
|
@ -33,7 +33,9 @@ showdown.subParser('makehtml.strikethrough', function (text, options, globals) {
|
|||
if (captureStartEvent.output && captureStartEvent.output !== '') {
|
||||
otp = captureStartEvent.output;
|
||||
} else {
|
||||
otp = '<del' + showdown.helper._populateAttributes(captureStartEvent.attributes) + '>' + txt + '</del>';
|
||||
otp = '<del' + showdown.helper._populateAttributes(captureStartEvent.attributes) + '>' +
|
||||
showdown.subParser('makehtml.hardLineBreaks')(txt, options, globals) +
|
||||
'</del>';
|
||||
}
|
||||
|
||||
let beforeHashEvent = new showdown.Event('makehtml.strikethrough.onHash', otp);
|
||||
|
|
|
@ -74,7 +74,9 @@ showdown.subParser('makehtml.underline', function (text, options, globals) {
|
|||
if (captureStartEvent.output && captureStartEvent.output !== '') {
|
||||
otp = captureStartEvent.output;
|
||||
} else {
|
||||
otp = '<u' + showdown.helper._populateAttributes(captureStartEvent.attributes) + '>' + txt + '</u>';
|
||||
otp = '<u' + showdown.helper._populateAttributes(captureStartEvent.attributes) + '>' +
|
||||
showdown.subParser('makehtml.hardLineBreaks')(txt, options, globals) +
|
||||
'</u>';
|
||||
}
|
||||
let beforeHashEvent = new showdown.Event('makehtml.underline.onHash', otp);
|
||||
beforeHashEvent
|
||||
|
|
|
@ -26,6 +26,7 @@ describe('makeHtml() commonmark testsuite', function () {
|
|||
case 'Thematic breaks_43': // malformed input of test case
|
||||
case 'Thematic breaks_61': // hr inside lists does not make sense
|
||||
case 'Fenced code blocks_146': // as of date, github doesn't support this so we don't either
|
||||
//case 'Raw HTML_619': // breaks prettifier so the test fails
|
||||
continue;
|
||||
|
||||
case 'Setext headings_91': //it's failing because the testcase converts " to " even though it's not supposed to
|
||||
|
@ -40,6 +41,10 @@ describe('makeHtml() commonmark testsuite', function () {
|
|||
case 'Fenced code blocks_144': // we use different classes to mark languages in fenced code blocks
|
||||
testsuite[section][i].expected = testsuite[section][i].expected.replace('language-;', '; language-;');
|
||||
break;
|
||||
|
||||
case 'Hard line breaks_638':
|
||||
console.log(testsuite[section][i].input);
|
||||
|
||||
}
|
||||
it(name, assertion(testsuite[section][i], converter, true));
|
||||
}
|
||||
|
|
|
@ -112,8 +112,14 @@
|
|||
|
||||
//prettify
|
||||
if (prettify) {
|
||||
testCase.expected = htmlPrettify(testCase.expected);
|
||||
testCase.actual = htmlPrettify(testCase.actual);
|
||||
try {
|
||||
testCase.expected = htmlPrettify(testCase.expected);
|
||||
testCase.actual = htmlPrettify(testCase.actual);
|
||||
} catch (e) {
|
||||
// some weird html in testcase breaks prettifier so we skip it and do some manual stuff
|
||||
testCase.expected = testCase.expected.trim().replace('\n', '');
|
||||
testCase.actual = testCase.actual.trim().replace('\n', '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user