From ab200d53dff066cf20ef8fa16b3e986c50c9d657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Estev=C3=A3o=20Soares=20dos=20Santos?= Date: Sun, 24 Apr 2022 07:47:28 +0100 Subject: [PATCH] add underline --- src/subParsers/makehtml/underline.js | 71 ++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/src/subParsers/makehtml/underline.js b/src/subParsers/makehtml/underline.js index a3d4414..06ea727 100644 --- a/src/subParsers/makehtml/underline.js +++ b/src/subParsers/makehtml/underline.js @@ -5,21 +5,41 @@ showdown.subParser('makehtml.underline', function (text, options, globals) { return text; } - text = globals.converter._dispatch('makehtml.underline.before', text, options, globals).getText(); + let startEvent = new showdown.Event('makehtml.underline.onStart', text); + startEvent + .setOutput(text) + ._setGlobals(globals) + ._setOptions(options); + startEvent = globals.converter.dispatch(startEvent); + text = startEvent.output; if (options.literalMidWordUnderscores) { - text = text.replace(/\b___(\S[\s\S]*?)___\b/g, function (wm, txt) { - return '' + txt + ''; + + const rgx1 = /\b___(\S[\s\S]*?)___\b/g; + text = text.replace(rgx1, function (wm, txt) { + return parse(rgx1, wm, txt); }); - text = text.replace(/\b__(\S[\s\S]*?)__\b/g, function (wm, txt) { - return '' + txt + ''; + + const rgx2 = /\b__(\S[\s\S]*?)__\b/g; + text = text.replace(rgx2, function (wm, txt) { + return parse(rgx2, wm, txt); }); } else { - text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) { - return (/\S$/.test(m)) ? '' + m + '' : wm; + + const rgx3 = /___(\S[\s\S]*?)___/g; + text = text.replace(rgx3, function (wm, txt) { + if (!(/\S$/.test(txt))) { + return wm; + } + return parse(rgx3, wm, txt); }); - text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) { - return (/\S$/.test(m)) ? '' + m + '' : wm; + + const rgx4 = /__(\S[\s\S]*?)__/g; + text = text.replace(rgx4, function (wm, txt) { + if (!(/\S$/.test(txt))) { + return wm; + } + return parse(rgx4, wm, txt); }); } @@ -29,4 +49,37 @@ showdown.subParser('makehtml.underline', function (text, options, globals) { text = globals.converter._dispatch('makehtml.underline.after', text, options, globals).getText(); return text; + + + function parse (pattern, wholeMatch, txt) { + let otp; + let captureStartEvent = new showdown.Event('makehtml.underline.onCapture', txt); + captureStartEvent + .setOutput(null) + ._setGlobals(globals) + ._setOptions(options) + .setRegexp(pattern) + .setMatches({ + _wholeMatch: wholeMatch, + strikethrough: txt + }) + .setAttributes({}); + captureStartEvent = globals.converter.dispatch(captureStartEvent); + // if something was passed as output, it takes precedence + // and will be used as output + if (captureStartEvent.output && captureStartEvent.output !== '') { + otp = captureStartEvent.output; + } else { + otp = '' + txt + ''; + } + let beforeHashEvent = new showdown.Event('makehtml.underline.onHash', otp); + beforeHashEvent + .setOutput(otp) + ._setGlobals(globals) + ._setOptions(options); + beforeHashEvent = globals.converter.dispatch(beforeHashEvent); + return beforeHashEvent.output; + } + + });