type2docfx
Version:
A tool to convert json format output from TypeDoc to schema driven reference model for DocFx to consume.
85 lines • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertLinkToGfm = exports.getTextAndLink = void 0;
var dfmRegex = [
/\[(?:([^\]]+))\]{(@link|@link|@linkcode|@linkplain) +(?:module:)?([^}| ]+)}/g,
/\{(@link|@linkcode|@linkplain) +(?:module:)?([^}| ]+)(?:(?:\|| +)([^}]+))?\}/g
];
// http://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url#answer-3809435
var urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/;
function getTextAndLink(text) {
var matches = dfmRegex[0].exec(text);
if (matches[1] && matches[3]) {
return [matches[1], matches[3]];
}
matches = dfmRegex[1].exec(text);
if (matches[3] && matches[2]) {
return [matches[3], matches[2]];
}
return [];
}
exports.getTextAndLink = getTextAndLink;
function convertLinkToGfm(text, uidPrefix) {
if (uidPrefix === void 0) { uidPrefix = null; }
if (!text)
return '';
var dfmLinkRules = [
{
// [link text]{@link namepathOrURL}
regexp: dfmRegex[0],
callback: function (match, p1, p2, p3) {
return generateDfmLink(p2, p3, p1);
}
},
{
// {@link namepathOrURL}
// {@link namepathOrURL|link text}
// {@link namepathOrURL link text (after the first space)}
regexp: dfmRegex[1],
callback: function (match, p1, p2, p3) {
return generateDfmLink(p1, p2, p3);
}
}
];
var result = text;
dfmLinkRules.forEach(function (r) {
result = result.replace(r.regexp, r.callback);
});
return result;
function generateDfmLink(tag, target, text) {
var result = '';
if (text) {
result = text;
if (tag === '@linkcode') {
result = '<code>' + result + '</code>';
}
result = '[' + result + '](';
if (!urlRegex.test(target)) {
// if target isn't a url, it must be a namepath(uid)
result += 'xref:';
target = convertNamepathToUid(target);
}
result += target + ')';
}
else {
if (urlRegex.test(target)) {
return target;
}
// if link text is undefined and target isn't a url, it must link to namepath(uid)
result = '<xref:' + convertNamepathToUid(target) + '>';
if (tag === '@linkcode') {
result = '<code>' + result + '</code>';
}
}
return result;
function convertNamepathToUid(namepath) {
var uid = encodeURIComponent(namepath);
if (uidPrefix) {
uid = uidPrefix + uid;
}
return uid;
}
}
}
exports.convertLinkToGfm = convertLinkToGfm;
//# sourceMappingURL=linkConvertHelper.js.map