Skip to content

Commit c3cfc3a

Browse files
committed
* Enable to replace GitHub issue references with absolute urls * Do not replace anything if it is no GitHub repository
1 parent 1ee7a2b commit c3cfc3a

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/package.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ function isHostTrusted(host: string): boolean {
177177
return TrustedSVGSources.indexOf(host.toLowerCase()) > -1;
178178
}
179179

180+
function isGitHubRepository(repository: string): boolean {
181+
return /^https:\/\/github\.com\/|^git@github\.com:/.test(repository || '');
182+
}
183+
180184
class ManifestProcessor extends BaseProcessor {
181185

182186
constructor(manifest: Manifest) {
@@ -189,7 +193,7 @@ class ManifestProcessor extends BaseProcessor {
189193
}
190194

191195
const repository = getRepositoryUrl(manifest.repository);
192-
const isGitHub = /^https:\/\/github\.com\/|^git@github\.com:/.test(repository || '');
196+
const isGitHub = isGitHubRepository(repository);
193197

194198
let enableMarketplaceQnA: boolean | undefined;
195199
let customerQnALink: string | undefined;
@@ -348,6 +352,8 @@ export class MarkdownProcessor extends BaseProcessor {
348352

349353
private baseContentUrl: string;
350354
private baseImagesUrl: string;
355+
private isGitHub: boolean;
356+
private repositoryUrl: string;
351357

352358
constructor(manifest: Manifest, private name: string, private regexp: RegExp, private assetType: string, options: IPackageOptions = {}) {
353359
super(manifest);
@@ -356,6 +362,8 @@ export class MarkdownProcessor extends BaseProcessor {
356362

357363
this.baseContentUrl = options.baseContentUrl || (guess && guess.content);
358364
this.baseImagesUrl = options.baseImagesUrl || options.baseContentUrl || (guess && guess.images);
365+
this.repositoryUrl = (guess && guess.repository);
366+
this.isGitHub = isGitHubRepository(this.repositoryUrl);
359367
}
360368

361369
async onFile(file: IFile): Promise<IFile> {
@@ -394,9 +402,36 @@ export class MarkdownProcessor extends BaseProcessor {
394402

395403
return `${isImage}[${title}](${urljoin(prefix, link)})`;
396404
};
397-
405+
// Replace Markdown links with urls
398406
contents = contents.replace(markdownPathRegex, urlReplace);
399407

408+
const markdownIssueRegex = /(\s|\n)([\w\d_-]+\/[\w\d_-]+)?#(\d+)\b/g
409+
const issueReplace = (all: string, prefix: string, ownerAndRepositoryName: string, issueNumber: string): string => {
410+
let result = all;
411+
let owner: string;
412+
let repositoryName: string;
413+
414+
if (ownerAndRepositoryName) {
415+
[owner, repositoryName] = ownerAndRepositoryName.split('/', 2);
416+
}
417+
418+
if (this.isGitHub){
419+
if (owner && repositoryName && issueNumber) {
420+
// Issue in external repository
421+
const issueUrl = urljoin('https://github.com', owner, repositoryName, 'issues', issueNumber);
422+
result = prefix + `[${owner}/${repositoryName}#${issueNumber}](${issueUrl})`;
423+
424+
} else if (!owner && !repositoryName && issueNumber) {
425+
// Issue in own repository
426+
result = prefix + `[#${issueNumber}](${urljoin(this.repositoryUrl, 'issues', issueNumber)})`;
427+
}
428+
}
429+
430+
return result;
431+
}
432+
// Replace Markdown issue references with urls
433+
contents = contents.replace(markdownIssueRegex, issueReplace);
434+
400435
const html = markdownit({ html: true }).render(contents);
401436
const $ = cheerio.load(html);
402437

@@ -428,7 +463,7 @@ export class MarkdownProcessor extends BaseProcessor {
428463
}
429464

430465
// GitHub heuristics
431-
private guessBaseUrls(): { content: string; images: string; } {
466+
private guessBaseUrls(): { content: string; images: string; repository: string} {
432467
let repository = null;
433468

434469
if (typeof this.manifest.repository === 'string') {
@@ -453,7 +488,8 @@ export class MarkdownProcessor extends BaseProcessor {
453488

454489
return {
455490
content: `https://github.com/${account}/${repositoryName}/blob/master`,
456-
images: `https://github.com/${account}/${repositoryName}/raw/master`
491+
images: `https://github.com/${account}/${repositoryName}/raw/master`,
492+
repository: `https://github.com/${account}/${repositoryName}`
457493
};
458494
}
459495
}

0 commit comments

Comments
 (0)