1 | (function () {
|
2 | function convertLinkToGfm(text, uidPrefix) {
|
3 | var linkRules = [
|
4 | {
|
5 |
|
6 | regexp: /\[(?:([^\]]+))\]{(@link|@linkcode|@linkplain) +([^}| ]+)}/g,
|
7 | callback: function (match, p1, p2, p3) {
|
8 | return generateDfmLink(p2, p3, p1);
|
9 | }
|
10 | },
|
11 | {
|
12 |
|
13 |
|
14 |
|
15 | regexp: /\{(@link|@linkcode|@linkplain) +([^}| ]+)(?:(?:\|| +)([^}]+))?\}/g,
|
16 | callback: function (match, p1, p2, p3) {
|
17 | return generateDfmLink(p1, p2, p3);
|
18 | }
|
19 | }
|
20 | ];
|
21 |
|
22 | if (!text) return '';
|
23 | var result = text;
|
24 | linkRules.forEach(function (r) {
|
25 | result = result.replace(r.regexp, r.callback);
|
26 | });
|
27 | return result;
|
28 |
|
29 | function generateDfmLink(tag, target, text) {
|
30 | var result = '';
|
31 | if (!text) {
|
32 |
|
33 | result = '<xref:' + convertNamepathToUid(target) + '>';
|
34 | if (tag === '@linkcode') {
|
35 | return '<code>' + result + '</code>';
|
36 | }
|
37 | } else {
|
38 | result = text;
|
39 | if (tag === '@linkcode') {
|
40 | result = '<code>' + result + '</code>';
|
41 | }
|
42 | result = '[' + result + '](';
|
43 |
|
44 | if (!/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(target)) {
|
45 |
|
46 | result += 'xref:';
|
47 | target = convertNamepathToUid(target);
|
48 | }
|
49 | result += target + ')';
|
50 | }
|
51 | return result;
|
52 |
|
53 | function convertNamepathToUid(namepath) {
|
54 | var uid = encodeURIComponent(namepath);
|
55 | if (uidPrefix) {
|
56 | uid = uidPrefix + uid;
|
57 | }
|
58 | return uid;
|
59 | }
|
60 | }
|
61 | }
|
62 |
|
63 | module.exports = {
|
64 | convertLinkToGfm: convertLinkToGfm
|
65 | };
|
66 | })(); |
\ | No newline at end of file |