feat(makeMarkdown.ghMentions): add support for ghMentions in makeMarkdown

Related to #910
This commit is contained in:
Estevão Soares dos Santos 2022-04-16 20:06:05 +01:00
parent a5f3add2b6
commit 3a616c5bf6
21 changed files with 93 additions and 64 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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'),

View File

@ -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 += '*';
}

View File

@ -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;

View File

@ -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;
});

View File

@ -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:
// <a class="user-mention" href="https://github.com/user">@user</a>
// 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;
});

View File

@ -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;
}

View File

@ -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)) {

View File

@ -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:

View File

@ -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);
}
}

View File

@ -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');

View File

@ -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 += '~~';
}

View File

@ -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 += '**';
}

View File

@ -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);
}

View File

@ -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();
});

View File

@ -0,0 +1,2 @@
<p><a class="bazinga user-mention foo bar baz" href="https://github.com/tivie">@tivie</a></p>
<p><a class="not-user-mention" href="https://www.google.com">@something</a></p>

View File

@ -0,0 +1,3 @@
@tivie
[@something](<https://www.google.com>)

View File

@ -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));
}
});
});