Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

216 righe
7.1 KiB

  1. // Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE file in the project root for full license information.
  2. exports.path = {};
  3. exports.path.getFileNameWithoutExtension = getFileNameWithoutExtension;
  4. exports.path.getDirectoryName = getDirectoryName;
  5. exports.getHtmlId = getHtmlId;
  6. exports.getViewSourceHref = getViewSourceHref;
  7. exports.getImproveTheDocHref = getImproveTheDocHref;
  8. exports.processSeeAlso = processSeeAlso;
  9. exports.isAbsolutePath = isAbsolutePath;
  10. exports.isRelativePath = isRelativePath;
  11. function getFileNameWithoutExtension(path) {
  12. if (!path || path[path.length - 1] === '/' || path[path.length - 1] === '\\') return '';
  13. var fileName = path.split('\\').pop().split('/').pop();
  14. return fileName.slice(0, fileName.lastIndexOf('.'));
  15. }
  16. function getDirectoryName(path) {
  17. if (!path) return '';
  18. var index = path.lastIndexOf('/');
  19. return path.slice(0, index + 1);
  20. }
  21. function getHtmlId(input) {
  22. return input.replace(/\W/g, '_');
  23. }
  24. // Note: the parameter `gitContribute` won't be used in this function
  25. function getViewSourceHref(item, gitContribute, gitUrlPattern) {
  26. if (!item || !item.source || !item.source.remote) return '';
  27. return getRemoteUrl(item.source.remote, item.source.startLine - '0' + 1, null, gitUrlPattern);
  28. }
  29. function getImproveTheDocHref(item, gitContribute, gitUrlPattern) {
  30. if (!item) return '';
  31. if (!item.documentation || !item.documentation.remote) {
  32. return getNewFileUrl(item, gitContribute, gitUrlPattern);
  33. } else {
  34. return getRemoteUrl(item.documentation.remote, item.documentation.startLine + 1, gitContribute, gitUrlPattern);
  35. }
  36. }
  37. function processSeeAlso(item) {
  38. if (item.seealso) {
  39. for (var key in item.seealso) {
  40. addIsCref(item.seealso[key]);
  41. }
  42. }
  43. item.seealso = item.seealso || null;
  44. }
  45. function isAbsolutePath(path) {
  46. return /^(\w+:)?\/\//g.test(path);
  47. }
  48. function isRelativePath(path) {
  49. if (!path) return false;
  50. return !exports.isAbsolutePath(path);
  51. }
  52. var gitUrlPatternItems = {
  53. 'github': {
  54. // HTTPS form: https://github.com/{org}/{repo}.git
  55. // SSH form: git@github.com:{org}/{repo}.git
  56. // generate URL: https://github.com/{org}/{repo}/blob/{branch}/{path}
  57. 'testRegex': /^(https?:\/\/)?(\S+\@)?(\S+\.)?github\.com(\/|:).*/i,
  58. 'generateUrl': function (gitInfo) {
  59. var url = normalizeGitUrlToHttps(gitInfo.repo);
  60. url += '/blob' + '/' + gitInfo.branch + '/' + gitInfo.path;
  61. if (gitInfo.startLine && gitInfo.startLine > 0) {
  62. url += '/#L' + gitInfo.startLine;
  63. }
  64. return url;
  65. },
  66. 'generateNewFileUrl': function (gitInfo, uid) {
  67. var url = normalizeGitUrlToHttps(gitInfo.repo);
  68. url += '/new';
  69. url += '/' + gitInfo.branch;
  70. url += '/' + getOverrideFolder(gitInfo.apiSpecFolder);
  71. url += '/new?filename=' + getHtmlId(uid) + '.md';
  72. url += '&value=' + encodeURIComponent(getOverrideTemplate(uid));
  73. return url;
  74. }
  75. },
  76. 'vso': {
  77. // HTTPS form: https://{user}.visualstudio.com/{org}/_git/{repo}
  78. // SSH form: ssh://{user}@{user}.visualstudio.com:22/{org}/_git/{repo}
  79. // generated URL: https://{user}.visualstudio.com/{org}/_git/{repo}?path={path}&version=GB{branch}
  80. 'testRegex': /^(https?:\/\/)?(ssh:\/\/\S+\@)?(\S+\.)?visualstudio\.com(\/|:).*/i,
  81. 'generateUrl': function (gitInfo) {
  82. var url = normalizeGitUrlToHttps(gitInfo.repo);
  83. url += '?path=' + gitInfo.path + '&version=GB' + gitInfo.branch;
  84. if (gitInfo.startLine && gitInfo.startLine > 0) {
  85. url += '&line=' + gitInfo.startLine;
  86. }
  87. return url;
  88. },
  89. 'generateNewFileUrl': function (gitInfo, uid) {
  90. return '';
  91. }
  92. }
  93. }
  94. function normalizeGitUrlToHttps(repo) {
  95. var pos = repo.indexOf('@');
  96. if (pos == -1) return repo;
  97. return 'https://' + repo.substr(pos + 1).replace(/:[0-9]+/g, '').replace(/:/g, '/');
  98. }
  99. function getNewFileUrl(item, gitContribute, gitUrlPattern) {
  100. // do not support VSO for now
  101. if (!item.source) {
  102. return '';
  103. }
  104. var gitInfo = getGitInfo(gitContribute, item.source.remote);
  105. if (!gitInfo.repo || !gitInfo.branch || !gitInfo.path) {
  106. return '';
  107. }
  108. if (gitInfo.repo.substr(-4) === '.git') {
  109. gitInfo.repo = gitInfo.repo.substr(0, gitInfo.repo.length - 4);
  110. }
  111. var patternName = getPatternName(gitInfo.repo, gitUrlPattern);
  112. if (!patternName) return patternName;
  113. return gitUrlPatternItems[patternName].generateNewFileUrl(gitInfo, item.uid);
  114. }
  115. function getRemoteUrl(remote, startLine, gitContribute, gitUrlPattern) {
  116. var gitInfo = getGitInfo(gitContribute, remote);
  117. if (!gitInfo.repo || !gitInfo.branch || !gitInfo.path) {
  118. return '';
  119. }
  120. if (gitInfo.repo.substr(-4) === '.git') {
  121. gitInfo.repo = gitInfo.repo.substr(0, gitInfo.repo.length - 4);
  122. }
  123. var patternName = getPatternName(gitInfo.repo, gitUrlPattern);
  124. if (!patternName) return '';
  125. gitInfo.startLine = startLine;
  126. return gitUrlPatternItems[patternName].generateUrl(gitInfo);
  127. }
  128. function getGitInfo(gitContribute, gitRemote) {
  129. // apiSpecFolder defines the folder contains overwrite files for MRef, the default value is apiSpec
  130. var defaultApiSpecFolder = "apiSpec";
  131. if (!gitContribute && !gitRemote) {
  132. return {
  133. apiSpecFolder: defaultApiSpecFolder
  134. }
  135. }
  136. if (!gitContribute) {
  137. return {
  138. apiSpecFolder: defaultApiSpecFolder,
  139. repo: gitRemote.repo,
  140. branch: gitRemote.branch,
  141. // path defines the relative path from current git repo.
  142. path: gitRemote.path
  143. }
  144. }
  145. return {
  146. apiSpecFolder: gitContribute.apiSpecFolder || defaultApiSpecFolder,
  147. repo: gitContribute.repo || gitRemote.repo,
  148. branch: gitContribute.branch || gitRemote.branch,
  149. path: gitRemote.path
  150. }
  151. }
  152. function getPatternName(repo, gitUrlPattern) {
  153. if (gitUrlPattern && gitUrlPattern.toLowerCase() in gitUrlPatternItems) {
  154. return gitUrlPattern.toLowerCase();
  155. } else {
  156. for (var p in gitUrlPatternItems) {
  157. if (gitUrlPatternItems[p].testRegex.test(repo)) {
  158. return p;
  159. }
  160. }
  161. }
  162. return '';
  163. }
  164. function getOverrideFolder(path) {
  165. if (!path) return "";
  166. path = path.replace('\\', '/');
  167. if (path.charAt(path.length - 1) == '/') path = path.substring(0, path.length - 1);
  168. return path;
  169. }
  170. function getOverrideTemplate(uid) {
  171. if (!uid) return "";
  172. var content = "";
  173. content += "---\n";
  174. content += "uid: " + uid + "\n";
  175. content += "summary: '*You can override summary for the API here using *MARKDOWN* syntax'\n";
  176. content += "---\n";
  177. content += "\n";
  178. content += "*Please type below more information about this API:*\n";
  179. content += "\n";
  180. return content;
  181. }
  182. function addIsCref(seealso) {
  183. if (!seealso.linkType || seealso.linkType.toLowerCase() == "cref") {
  184. seealso.isCref = true;
  185. }
  186. }