From 6e6af3cc0356f86038649f327b2f98746374b4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Estev=C3=A3o=20Soares=20dos=20Santos?= Date: Mon, 9 May 2022 03:17:43 +0100 Subject: [PATCH] improve compliance in fenced blocks --- src/subParsers/makehtml/githubCodeBlock.js | 43 +++++++++++++------ .../makehtml/extra.testsuite.commonmark.js | 12 ++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/subParsers/makehtml/githubCodeBlock.js b/src/subParsers/makehtml/githubCodeBlock.js index 4833c11..ad09aa7 100644 --- a/src/subParsers/makehtml/githubCodeBlock.js +++ b/src/subParsers/makehtml/githubCodeBlock.js @@ -32,9 +32,37 @@ showdown.subParser('makehtml.githubCodeBlock', function (text, options, globals) startEvent = globals.converter.dispatch(startEvent); 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', otp, 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, // and then return a token 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; }); diff --git a/test/functional/makehtml/extra.testsuite.commonmark.js b/test/functional/makehtml/extra.testsuite.commonmark.js index 1194f63..16edca8 100644 --- a/test/functional/makehtml/extra.testsuite.commonmark.js +++ b/test/functional/makehtml/extra.testsuite.commonmark.js @@ -10,6 +10,7 @@ let bootstrap = require('./makehtml.bootstrap.js'), }), assertion = bootstrap.assertion, testsuite = bootstrap.getJsonTestSuite('test/functional/makehtml/cases/commonmark.testsuite.json'); +const {tests} = require('commonmark-spec'); describe('makeHtml() commonmark testsuite', function () { '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 '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 continue; + 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, '"') 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)); }