From 3a616c5bf6f4be2fef26cbdb3e46e441b74a9dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Estev=C3=A3o=20Soares=20dos=20Santos?= Date: Sat, 16 Apr 2022 20:06:05 +0100 Subject: [PATCH] feat(makeMarkdown.ghMentions): add support for ghMentions in makeMarkdown Related to #910 --- src/converter.js | 2 +- src/subParsers/makemarkdown/blockquote.js | 4 +- src/subParsers/makemarkdown/codeBlock.js | 2 +- src/subParsers/makemarkdown/emphasis.js | 4 +- src/subParsers/makemarkdown/header.js | 4 +- src/subParsers/makemarkdown/input.js | 4 +- src/subParsers/makemarkdown/links.js | 36 ++++++++++---- src/subParsers/makemarkdown/list.js | 4 +- src/subParsers/makemarkdown/listItem.js | 4 +- src/subParsers/makemarkdown/node.js | 48 +++++++++---------- src/subParsers/makemarkdown/paragraph.js | 4 +- src/subParsers/makemarkdown/pre.js | 2 +- src/subParsers/makemarkdown/strikethrough.js | 4 +- src/subParsers/makemarkdown/strong.js | 4 +- src/subParsers/makemarkdown/table.js | 6 +-- src/subParsers/makemarkdown/tableCell.js | 4 +- .../cases/features/ghMentions/github.html | 2 + .../cases/features/ghMentions/github.md | 3 ++ .../features/{ => issues}/tasklists.html | 0 .../cases/features/{ => issues}/tasklists.md | 0 .../makemarkdown/testsuite.features.js | 16 +++++-- 21 files changed, 93 insertions(+), 64 deletions(-) create mode 100644 test/functional/makemarkdown/cases/features/ghMentions/github.html create mode 100644 test/functional/makemarkdown/cases/features/ghMentions/github.md rename test/functional/makemarkdown/cases/features/{ => issues}/tasklists.html (100%) rename test/functional/makemarkdown/cases/features/{ => issues}/tasklists.md (100%) diff --git a/src/converter.js b/src/converter.js index 80ec044..6e7b29d 100644 --- a/src/converter.js +++ b/src/converter.js @@ -386,7 +386,7 @@ showdown.Converter = function (converterOptions) { mdDoc = ''; for (var i = 0; i < nodes.length; i++) { - mdDoc += showdown.subParser('makeMarkdown.node')(nodes[i], globals); + mdDoc += showdown.subParser('makeMarkdown.node')(nodes[i], options, globals); } function clean (node) { diff --git a/src/subParsers/makemarkdown/blockquote.js b/src/subParsers/makemarkdown/blockquote.js index 143e5a8..5a5daa1 100644 --- a/src/subParsers/makemarkdown/blockquote.js +++ b/src/subParsers/makemarkdown/blockquote.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.blockquote', function (node, globals) { +showdown.subParser('makeMarkdown.blockquote', function (node, options, globals) { 'use strict'; var txt = ''; @@ -7,7 +7,7 @@ showdown.subParser('makeMarkdown.blockquote', function (node, globals) { childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - var innerTxt = showdown.subParser('makeMarkdown.node')(children[i], globals); + var innerTxt = showdown.subParser('makeMarkdown.node')(children[i], options, globals); if (innerTxt === '') { continue; diff --git a/src/subParsers/makemarkdown/codeBlock.js b/src/subParsers/makemarkdown/codeBlock.js index bf8cf17..0510553 100644 --- a/src/subParsers/makemarkdown/codeBlock.js +++ b/src/subParsers/makemarkdown/codeBlock.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.codeBlock', function (node, globals) { +showdown.subParser('makeMarkdown.codeBlock', function (node, options, globals) { 'use strict'; var lang = node.getAttribute('language'), diff --git a/src/subParsers/makemarkdown/emphasis.js b/src/subParsers/makemarkdown/emphasis.js index 283cd02..e7e1310 100644 --- a/src/subParsers/makemarkdown/emphasis.js +++ b/src/subParsers/makemarkdown/emphasis.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.emphasis', function (node, globals) { +showdown.subParser('makeMarkdown.emphasis', function (node, options, globals) { 'use strict'; var txt = ''; @@ -7,7 +7,7 @@ showdown.subParser('makeMarkdown.emphasis', function (node, globals) { var children = node.childNodes, childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } txt += '*'; } diff --git a/src/subParsers/makemarkdown/header.js b/src/subParsers/makemarkdown/header.js index 9a1b0c5..e88614b 100644 --- a/src/subParsers/makemarkdown/header.js +++ b/src/subParsers/makemarkdown/header.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.header', function (node, globals, headerLevel) { +showdown.subParser('makeMarkdown.header', function (node, options, globals, headerLevel) { 'use strict'; var headerMark = new Array(headerLevel + 1).join('#'), @@ -10,7 +10,7 @@ showdown.subParser('makeMarkdown.header', function (node, globals, headerLevel) childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } } return txt; diff --git a/src/subParsers/makemarkdown/input.js b/src/subParsers/makemarkdown/input.js index 0b13f04..694be7f 100644 --- a/src/subParsers/makemarkdown/input.js +++ b/src/subParsers/makemarkdown/input.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.input', function (node, globals) { +showdown.subParser('makeMarkdown.input', function (node, options, globals) { 'use strict'; var txt = ''; @@ -10,7 +10,7 @@ showdown.subParser('makeMarkdown.input', function (node, globals) { var children = node.childNodes, childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } return txt; }); diff --git a/src/subParsers/makemarkdown/links.js b/src/subParsers/makemarkdown/links.js index 6b23255..b7c1a4d 100644 --- a/src/subParsers/makemarkdown/links.js +++ b/src/subParsers/makemarkdown/links.js @@ -1,20 +1,36 @@ -showdown.subParser('makeMarkdown.links', function (node, globals) { +showdown.subParser('makeMarkdown.links', function (node, options, globals) { 'use strict'; var txt = ''; if (node.hasChildNodes() && node.hasAttribute('href')) { var children = node.childNodes, childrenLength = children.length; - txt = '['; - for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + + // special case for mentions + // to simplify (and not make stuff really complicated) mentions will only work in this circumstance: + // @user + // that is, if there's a "user-mention" class and option ghMentions is true + // otherwise is ignored + var classes = node.getAttribute('class'); + if (options.ghMentions && /(?:^| )user-mention\b/.test(classes)) { + for (var ii = 0; ii < childrenLength; ++ii) { + txt += showdown.subParser('makeMarkdown.node')(children[ii], options, globals); + } + + } else { + txt = '['; + for (var i = 0; i < childrenLength; ++i) { + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); + } + txt += ']('; + txt += '<' + node.getAttribute('href') + '>'; + if (node.hasAttribute('title')) { + txt += ' "' + node.getAttribute('title') + '"'; + } + txt += ')'; } - txt += ']('; - txt += '<' + node.getAttribute('href') + '>'; - if (node.hasAttribute('title')) { - txt += ' "' + node.getAttribute('title') + '"'; - } - txt += ')'; + + } return txt; }); diff --git a/src/subParsers/makemarkdown/list.js b/src/subParsers/makemarkdown/list.js index f838618..77712df 100644 --- a/src/subParsers/makemarkdown/list.js +++ b/src/subParsers/makemarkdown/list.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.list', function (node, globals, type) { +showdown.subParser('makeMarkdown.list', function (node, options, globals, type) { 'use strict'; var txt = ''; @@ -23,7 +23,7 @@ showdown.subParser('makeMarkdown.list', function (node, globals, type) { } // parse list item - txt += bullet + showdown.subParser('makeMarkdown.listItem')(listItems[i], globals); + txt += bullet + showdown.subParser('makeMarkdown.listItem')(listItems[i], options, globals); ++listNum; } diff --git a/src/subParsers/makemarkdown/listItem.js b/src/subParsers/makemarkdown/listItem.js index bccd2a0..1634077 100644 --- a/src/subParsers/makemarkdown/listItem.js +++ b/src/subParsers/makemarkdown/listItem.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.listItem', function (node, globals) { +showdown.subParser('makeMarkdown.listItem', function (node, options, globals) { 'use strict'; var listItemTxt = ''; @@ -7,7 +7,7 @@ showdown.subParser('makeMarkdown.listItem', function (node, globals) { childrenLenght = children.length; for (var i = 0; i < childrenLenght; ++i) { - listItemTxt += showdown.subParser('makeMarkdown.node')(children[i], globals); + listItemTxt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } // if it's only one liner, we need to add a newline at the end if (!/\n$/.test(listItemTxt)) { diff --git a/src/subParsers/makemarkdown/node.js b/src/subParsers/makemarkdown/node.js index d4e1a33..d8913ea 100644 --- a/src/subParsers/makemarkdown/node.js +++ b/src/subParsers/makemarkdown/node.js @@ -1,6 +1,6 @@ -showdown.subParser('makeMarkdown.node', function (node, globals, spansOnly) { +showdown.subParser('makeMarkdown.node', function (node, options, globals, spansOnly) { 'use strict'; spansOnly = spansOnly || false; @@ -9,7 +9,7 @@ showdown.subParser('makeMarkdown.node', function (node, globals, spansOnly) { // edge case of text without wrapper paragraph if (node.nodeType === 3) { - return showdown.subParser('makeMarkdown.txt')(node, globals); + return showdown.subParser('makeMarkdown.txt')(node, options, globals); } // HTML comment @@ -30,91 +30,91 @@ showdown.subParser('makeMarkdown.node', function (node, globals, spansOnly) { // BLOCKS // case 'h1': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 1) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, options, globals, 1) + '\n\n'; } break; case 'h2': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 2) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, options, globals, 2) + '\n\n'; } break; case 'h3': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 3) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, options, globals, 3) + '\n\n'; } break; case 'h4': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 4) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, options, globals, 4) + '\n\n'; } break; case 'h5': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 5) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, options, globals, 5) + '\n\n'; } break; case 'h6': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, globals, 6) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.header')(node, options, globals, 6) + '\n\n'; } break; case 'p': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.paragraph')(node, globals) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.paragraph')(node, options, globals) + '\n\n'; } break; case 'blockquote': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.blockquote')(node, globals) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.blockquote')(node, options, globals) + '\n\n'; } break; case 'hr': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.hr')(node, globals) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.hr')(node, options, globals) + '\n\n'; } break; case 'ol': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.list')(node, globals, 'ol') + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.list')(node, options, globals, 'ol') + '\n\n'; } break; case 'ul': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.list')(node, globals, 'ul') + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.list')(node, options, globals, 'ul') + '\n\n'; } break; case 'precode': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.codeBlock')(node, globals) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.codeBlock')(node, options, globals) + '\n\n'; } break; case 'pre': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.pre')(node, globals) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.pre')(node, options, globals) + '\n\n'; } break; case 'table': - if (!spansOnly) { txt = showdown.subParser('makeMarkdown.table')(node, globals) + '\n\n'; } + if (!spansOnly) { txt = showdown.subParser('makeMarkdown.table')(node, options, globals) + '\n\n'; } break; // // SPANS // case 'code': - txt = showdown.subParser('makeMarkdown.codeSpan')(node, globals); + txt = showdown.subParser('makeMarkdown.codeSpan')(node, options, globals); break; case 'em': case 'i': - txt = showdown.subParser('makeMarkdown.emphasis')(node, globals); + txt = showdown.subParser('makeMarkdown.emphasis')(node, options, globals); break; case 'strong': case 'b': - txt = showdown.subParser('makeMarkdown.strong')(node, globals); + txt = showdown.subParser('makeMarkdown.strong')(node, options, globals); break; case 'del': - txt = showdown.subParser('makeMarkdown.strikethrough')(node, globals); + txt = showdown.subParser('makeMarkdown.strikethrough')(node, options, globals); break; case 'a': - txt = showdown.subParser('makeMarkdown.links')(node, globals); + txt = showdown.subParser('makeMarkdown.links')(node, options, globals); break; case 'img': - txt = showdown.subParser('makeMarkdown.image')(node, globals); + txt = showdown.subParser('makeMarkdown.image')(node, options, globals); break; case 'br': - txt = showdown.subParser('makeMarkdown.break')(node, globals); + txt = showdown.subParser('makeMarkdown.break')(node, options, globals); break; case 'input': - txt = showdown.subParser('makeMarkdown.input')(node, globals); + txt = showdown.subParser('makeMarkdown.input')(node, options, globals); break; default: diff --git a/src/subParsers/makemarkdown/paragraph.js b/src/subParsers/makemarkdown/paragraph.js index f899d59..8619511 100644 --- a/src/subParsers/makemarkdown/paragraph.js +++ b/src/subParsers/makemarkdown/paragraph.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.paragraph', function (node, globals) { +showdown.subParser('makeMarkdown.paragraph', function (node, options, globals) { 'use strict'; var txt = ''; @@ -6,7 +6,7 @@ showdown.subParser('makeMarkdown.paragraph', function (node, globals) { var children = node.childNodes, childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } } diff --git a/src/subParsers/makemarkdown/pre.js b/src/subParsers/makemarkdown/pre.js index 621701a..57a0298 100644 --- a/src/subParsers/makemarkdown/pre.js +++ b/src/subParsers/makemarkdown/pre.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.pre', function (node, globals) { +showdown.subParser('makeMarkdown.pre', function (node, options, globals) { 'use strict'; var num = node.getAttribute('prenum'); diff --git a/src/subParsers/makemarkdown/strikethrough.js b/src/subParsers/makemarkdown/strikethrough.js index 0676b86..a6de1a5 100644 --- a/src/subParsers/makemarkdown/strikethrough.js +++ b/src/subParsers/makemarkdown/strikethrough.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.strikethrough', function (node, globals) { +showdown.subParser('makeMarkdown.strikethrough', function (node, options, globals) { 'use strict'; var txt = ''; @@ -7,7 +7,7 @@ showdown.subParser('makeMarkdown.strikethrough', function (node, globals) { var children = node.childNodes, childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } txt += '~~'; } diff --git a/src/subParsers/makemarkdown/strong.js b/src/subParsers/makemarkdown/strong.js index 05ae9bc..bf28afe 100644 --- a/src/subParsers/makemarkdown/strong.js +++ b/src/subParsers/makemarkdown/strong.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.strong', function (node, globals) { +showdown.subParser('makeMarkdown.strong', function (node, options, globals) { 'use strict'; var txt = ''; @@ -7,7 +7,7 @@ showdown.subParser('makeMarkdown.strong', function (node, globals) { var children = node.childNodes, childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals); } txt += '**'; } diff --git a/src/subParsers/makemarkdown/table.js b/src/subParsers/makemarkdown/table.js index d84e64b..9b06003 100644 --- a/src/subParsers/makemarkdown/table.js +++ b/src/subParsers/makemarkdown/table.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.table', function (node, globals) { +showdown.subParser('makeMarkdown.table', function (node, options, globals) { 'use strict'; var txt = '', @@ -7,7 +7,7 @@ showdown.subParser('makeMarkdown.table', function (node, globals) { rows = node.querySelectorAll('tbody>tr'), i, ii; for (i = 0; i < headings.length; ++i) { - var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], globals), + var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], options, globals), allign = '---'; if (headings[i].hasAttribute('style')) { @@ -35,7 +35,7 @@ showdown.subParser('makeMarkdown.table', function (node, globals) { for (ii = 0; ii < headings.length; ++ii) { var cellContent = ' '; if (typeof cols[ii] !== 'undefined') { - cellContent = showdown.subParser('makeMarkdown.tableCell')(cols[ii], globals); + cellContent = showdown.subParser('makeMarkdown.tableCell')(cols[ii], options, globals); } tableArray[r].push(cellContent); } diff --git a/src/subParsers/makemarkdown/tableCell.js b/src/subParsers/makemarkdown/tableCell.js index b318ff1..f58c8c4 100644 --- a/src/subParsers/makemarkdown/tableCell.js +++ b/src/subParsers/makemarkdown/tableCell.js @@ -1,4 +1,4 @@ -showdown.subParser('makeMarkdown.tableCell', function (node, globals) { +showdown.subParser('makeMarkdown.tableCell', function (node, options, globals) { 'use strict'; var txt = ''; @@ -9,7 +9,7 @@ showdown.subParser('makeMarkdown.tableCell', function (node, globals) { childrenLength = children.length; for (var i = 0; i < childrenLength; ++i) { - txt += showdown.subParser('makeMarkdown.node')(children[i], globals, true); + txt += showdown.subParser('makeMarkdown.node')(children[i], options, globals, true); } return txt.trim(); }); diff --git a/test/functional/makemarkdown/cases/features/ghMentions/github.html b/test/functional/makemarkdown/cases/features/ghMentions/github.html new file mode 100644 index 0000000..acb0e8f --- /dev/null +++ b/test/functional/makemarkdown/cases/features/ghMentions/github.html @@ -0,0 +1,2 @@ +

@tivie

+

@something

diff --git a/test/functional/makemarkdown/cases/features/ghMentions/github.md b/test/functional/makemarkdown/cases/features/ghMentions/github.md new file mode 100644 index 0000000..2b2acda --- /dev/null +++ b/test/functional/makemarkdown/cases/features/ghMentions/github.md @@ -0,0 +1,3 @@ +@tivie + +[@something]() diff --git a/test/functional/makemarkdown/cases/features/tasklists.html b/test/functional/makemarkdown/cases/features/issues/tasklists.html similarity index 100% rename from test/functional/makemarkdown/cases/features/tasklists.html rename to test/functional/makemarkdown/cases/features/issues/tasklists.html diff --git a/test/functional/makemarkdown/cases/features/tasklists.md b/test/functional/makemarkdown/cases/features/issues/tasklists.md similarity index 100% rename from test/functional/makemarkdown/cases/features/tasklists.md rename to test/functional/makemarkdown/cases/features/issues/tasklists.md diff --git a/test/functional/makemarkdown/testsuite.features.js b/test/functional/makemarkdown/testsuite.features.js index 6dc5ca8..2786c71 100644 --- a/test/functional/makemarkdown/testsuite.features.js +++ b/test/functional/makemarkdown/testsuite.features.js @@ -4,20 +4,28 @@ var bootstrap = require('./makemarkdown.bootstrap.js'), showdown = bootstrap.showdown, assertion = bootstrap.assertion, - testsuite = bootstrap.getTestSuite('test/functional/makemarkdown/cases/features/'); + issues = bootstrap.getTestSuite('test/functional/makemarkdown/cases/features/issues/'), + ghMentions = bootstrap.getTestSuite('test/functional/makemarkdown/cases/features/ghMentions/'); describe('makeMarkdown() features testsuite', function () { 'use strict'; describe('issues', function () { - for (var i = 0; i < testsuite.length; ++i) { + for (var i = 0; i < issues.length; ++i) { var converter; - if (testsuite[i].name === '#164.4.tasklists') { + if (issues[i].name === '#164.4.tasklists') { converter = new showdown.Converter({tasklists: true}); } else { converter = new showdown.Converter(); } - it(testsuite[i].name.replace(/-/g, ' '), assertion(testsuite[i], converter)); + it(issues[i].name.replace(/-/g, ' '), assertion(issues[i], converter)); + } + }); + + describe('ghMentions', function () { + var converter = new showdown.Converter({ ghMentions: true }); + for (var i = 0; i < ghMentions.length; ++i) { + it(ghMentions[i].name.replace(/-/g, ' '), assertion(ghMentions[i], converter)); } }); });