mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
improve compliance in fenced blocks
This commit is contained in:
parent
9235131045
commit
6e6af3cc03
|
@ -32,9 +32,37 @@ showdown.subParser('makehtml.githubCodeBlock', function (text, options, globals)
|
||||||
startEvent = globals.converter.dispatch(startEvent);
|
startEvent = globals.converter.dispatch(startEvent);
|
||||||
text = startEvent.output + '¨0';
|
text = startEvent.output + '¨0';
|
||||||
|
|
||||||
let pattern = /(?:^|\n) {0,3}(```+|~~~+) *([^\n\t`~]*)\n([\s\S]*?)\n {0,3}\1/g;
|
//const accentRegex = /(?:^|\n) {0,3}(```+|~~~+) *([^\n\t`~]*)\n([\s\S]*?)(?:(\n {0,3}\1[`~]*)|¨0)/g;
|
||||||
|
const closedBlockRegex = /^ {0,3}(```+|~~~+) *([^\n\t`~]*)\n([\s\S]*?)\n {0,3}\1[`~]*/gm;
|
||||||
|
const unclosedBlockRegex = /^ {0,3}(```+|~~~+) *([^\n\t`~]*)\n([\s\S]*?)¨0/gm;
|
||||||
|
const emptyBlockRegex = /^ {0,3}(```+|~~~+) *([^\n\t`~]*)\n {0,3}\1[`~]*/gm;
|
||||||
|
|
||||||
text = text.replace(pattern, function (wholeMatch, delim, language, codeblock) {
|
text = text.replace(closedBlockRegex, function (wholeMatch, delim, language, codeblock) {
|
||||||
|
return parse(closedBlockRegex, wholeMatch, delim, language, codeblock);
|
||||||
|
});
|
||||||
|
|
||||||
|
text = text.replace(emptyBlockRegex, function (wholeMatch, delim, language) {
|
||||||
|
console.log('|', wholeMatch, '|>');
|
||||||
|
return parse(emptyBlockRegex, wholeMatch, delim, language, '');
|
||||||
|
});
|
||||||
|
|
||||||
|
text = text.replace(unclosedBlockRegex, function (wholeMatch, delim, language, codeblock) {
|
||||||
|
return parse(unclosedBlockRegex, wholeMatch, delim, language, codeblock);
|
||||||
|
});
|
||||||
|
|
||||||
|
// attacklab: strip sentinel
|
||||||
|
text = text.replace(/¨0/, '');
|
||||||
|
|
||||||
|
let afterEvent = new showdown.Event('makehtml.githubCodeBlock.onEnd', text);
|
||||||
|
afterEvent
|
||||||
|
.setOutput(text)
|
||||||
|
._setGlobals(globals)
|
||||||
|
._setOptions(options);
|
||||||
|
afterEvent = globals.converter.dispatch(afterEvent);
|
||||||
|
return afterEvent.output;
|
||||||
|
|
||||||
|
|
||||||
|
function parse (pattern, wholeMatch, delim, language, codeblock) {
|
||||||
let end = (options.omitExtraWLInCodeBlocks) ? '' : '\n',
|
let end = (options.omitExtraWLInCodeBlocks) ? '' : '\n',
|
||||||
otp,
|
otp,
|
||||||
attributes = {
|
attributes = {
|
||||||
|
@ -119,16 +147,7 @@ showdown.subParser('makehtml.githubCodeBlock', function (text, options, globals)
|
||||||
// store the primitive text and the parsed text in a global var,
|
// store the primitive text and the parsed text in a global var,
|
||||||
// and then return a token
|
// and then return a token
|
||||||
return '\n\n¨G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: otp}) - 1) + 'G\n\n';
|
return '\n\n¨G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: otp}) - 1) + 'G\n\n';
|
||||||
});
|
}
|
||||||
|
|
||||||
// attacklab: strip sentinel
|
|
||||||
text = text.replace(/¨0/, '');
|
|
||||||
|
|
||||||
let afterEvent = new showdown.Event('makehtml.githubCodeBlock.onEnd', text);
|
|
||||||
afterEvent
|
|
||||||
.setOutput(text)
|
|
||||||
._setGlobals(globals)
|
|
||||||
._setOptions(options);
|
|
||||||
afterEvent = globals.converter.dispatch(afterEvent);
|
|
||||||
return afterEvent.output;
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,6 +10,7 @@ let bootstrap = require('./makehtml.bootstrap.js'),
|
||||||
}),
|
}),
|
||||||
assertion = bootstrap.assertion,
|
assertion = bootstrap.assertion,
|
||||||
testsuite = bootstrap.getJsonTestSuite('test/functional/makehtml/cases/commonmark.testsuite.json');
|
testsuite = bootstrap.getJsonTestSuite('test/functional/makehtml/cases/commonmark.testsuite.json');
|
||||||
|
const {tests} = require('commonmark-spec');
|
||||||
|
|
||||||
describe('makeHtml() commonmark testsuite', function () {
|
describe('makeHtml() commonmark testsuite', function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
@ -24,10 +25,21 @@ describe('makeHtml() commonmark testsuite', function () {
|
||||||
case 'Setext headings_93': // spec says it cannot be lazy continuation but then proceeds to make it a lazy continuation.
|
case 'Setext headings_93': // spec says it cannot be lazy continuation but then proceeds to make it a lazy continuation.
|
||||||
case 'Thematic breaks_43': // malformed input of test case
|
case 'Thematic breaks_43': // malformed input of test case
|
||||||
case 'Thematic breaks_61': // hr inside lists does not make sense
|
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
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 'Setext headings_91': //it's failing because the testcase converts " to " even though it's not supposed to
|
case 'Setext headings_91': //it's failing because the testcase converts " to " even though it's not supposed to
|
||||||
testsuite[section][i].expected = testsuite[section][i].expected.replace(/"/g, '"')
|
testsuite[section][i].expected = testsuite[section][i].expected.replace(/"/g, '"')
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'Fenced code blocks_142': // we use different classes to mark languages in fenced code blocks
|
||||||
|
case 'Fenced code blocks_143': // we use different classes to mark languages in fenced code blocks
|
||||||
|
testsuite[section][i].expected = testsuite[section][i].expected.replace('language-ruby', 'ruby language-ruby');
|
||||||
|
break;
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
it(name, assertion(testsuite[section][i], converter, true));
|
it(name, assertion(testsuite[section][i], converter, true));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user