Skip to content

Commit

Permalink
feat(makeMarkdown.ghMentions): add support for ghMentions in makeMark…
Browse files Browse the repository at this point in the history
…down

Related to #910
  • Loading branch information
tivie committed Apr 16, 2022
1 parent a5f3add commit 3a616c5
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/blockquote.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.blockquote', function (node, globals) {
showdown.subParser('makeMarkdown.blockquote', function (node, options, globals) {
'use strict';

var txt = '';
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/subParsers/makemarkdown/codeBlock.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/emphasis.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.emphasis', function (node, globals) {
showdown.subParser('makeMarkdown.emphasis', function (node, options, globals) {
'use strict';

var txt = '';
Expand All @@ -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 += '*';
}
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/header.js
Original file line number Diff line number Diff line change
@@ -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('#'),
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/input.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.input', function (node, globals) {
showdown.subParser('makeMarkdown.input', function (node, options, globals) {
'use strict';

var txt = '';
Expand All @@ -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;
});
36 changes: 26 additions & 10 deletions src/subParsers/makemarkdown/links.js
Original file line number Diff line number Diff line change
@@ -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);
}
txt += '](';
txt += '<' + node.getAttribute('href') + '>';
if (node.hasAttribute('title')) {
txt += ' "' + node.getAttribute('title') + '"';

// 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 += ')';


}
return txt;
});
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/list.js
Original file line number Diff line number Diff line change
@@ -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 = '';
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/listItem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.listItem', function (node, globals) {
showdown.subParser('makeMarkdown.listItem', function (node, options, globals) {
'use strict';

var listItemTxt = '';
Expand All @@ -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)) {
Expand Down
48 changes: 24 additions & 24 deletions src/subParsers/makemarkdown/node.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/paragraph.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
showdown.subParser('makeMarkdown.paragraph', function (node, globals) {
showdown.subParser('makeMarkdown.paragraph', function (node, options, globals) {
'use strict';

var txt = '';
if (node.hasChildNodes()) {
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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/subParsers/makemarkdown/pre.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/strikethrough.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.strikethrough', function (node, globals) {
showdown.subParser('makeMarkdown.strikethrough', function (node, options, globals) {
'use strict';

var txt = '';
Expand All @@ -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 += '~~';
}
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/strong.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.strong', function (node, globals) {
showdown.subParser('makeMarkdown.strong', function (node, options, globals) {
'use strict';

var txt = '';
Expand All @@ -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 += '**';
}
Expand Down
6 changes: 3 additions & 3 deletions src/subParsers/makemarkdown/table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.table', function (node, globals) {
showdown.subParser('makeMarkdown.table', function (node, options, globals) {
'use strict';

var txt = '',
Expand All @@ -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')) {
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/subParsers/makemarkdown/tableCell.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
showdown.subParser('makeMarkdown.tableCell', function (node, globals) {
showdown.subParser('makeMarkdown.tableCell', function (node, options, globals) {
'use strict';

var txt = '';
Expand All @@ -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();
});
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tivie

[@something](<https://www.google.com>)
Loading

0 comments on commit 3a616c5

Please sign in to comment.