From 368ffbd535568020b6b4f0870a4acd0175ca9633 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 20 Feb 2020 13:22:50 -0800 Subject: [PATCH] Add --noGitHubIssueLinking to stop issue link expansion in package step --- src/main.ts | 3 ++- src/package.ts | 29 ++++++++++++++++------------- src/test/package.test.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main.ts b/src/main.ts index dcaf77c..7554eee 100644 --- a/src/main.ts +++ b/src/main.ts @@ -74,7 +74,8 @@ module.exports = function (argv: string[]): void { .option('--baseImagesUrl [url]', 'Prepend all relative image links in README.md with this url.') .option('--yarn', 'Use yarn instead of npm') .option('--ignoreFile [path]', 'Indicate alternative .vscodeignore') - .action(({ out, baseContentUrl, baseImagesUrl, yarn, ignoreFile }) => main(packageCommand({ packagePath: out, baseContentUrl, baseImagesUrl, useYarn: yarn, ignoreFile }))); + .option('--noGitHubIssueLinking', 'Prevent automatic expansion of GitHub-style issue syntax into links') + .action(({ out, baseContentUrl, baseImagesUrl, yarn, ignoreFile, noGitHubIssueLinking }) => main(packageCommand({ packagePath: out, baseContentUrl, baseImagesUrl, useYarn: yarn, ignoreFile, expandGitHubIssueLinks: noGitHubIssueLinking }))); program .command('publish []') diff --git a/src/package.ts b/src/package.ts index f84d78a..95660bb 100644 --- a/src/package.ts +++ b/src/package.ts @@ -65,6 +65,7 @@ export interface IPackageOptions { useYarn?: boolean; dependencyEntryPoints?: string[]; ignoreFile?: string; + expandGitHubIssueLinks?: boolean; } export interface IProcessor { @@ -356,6 +357,7 @@ export class MarkdownProcessor extends BaseProcessor { private baseImagesUrl: string; private isGitHub: boolean; private repositoryUrl: string; + private expandGitHubIssueLinks: boolean; constructor(manifest: Manifest, private name: string, private regexp: RegExp, private assetType: string, options: IPackageOptions = {}) { super(manifest); @@ -366,6 +368,7 @@ export class MarkdownProcessor extends BaseProcessor { this.baseImagesUrl = options.baseImagesUrl || options.baseContentUrl || (guess && guess.images); this.repositoryUrl = (guess && guess.repository); this.isGitHub = isGitHubRepository(this.repositoryUrl); + this.expandGitHubIssueLinks = typeof options.expandGitHubIssueLinks === 'boolean' ? options.expandGitHubIssueLinks : true; } async onFile(file: IFile): Promise { @@ -424,17 +427,17 @@ export class MarkdownProcessor extends BaseProcessor { return all.replace(link, urljoin(prefix, link)); }); - const markdownIssueRegex = /(\s|\n)([\w\d_-]+\/[\w\d_-]+)?#(\d+)\b/g - const issueReplace = (all: string, prefix: string, ownerAndRepositoryName: string, issueNumber: string): string => { - let result = all; - let owner: string; - let repositoryName: string; + if (this.isGitHub && this.expandGitHubIssueLinks) { + const markdownIssueRegex = /(\s|\n)([\w\d_-]+\/[\w\d_-]+)?#(\d+)\b/g + const issueReplace = (all: string, prefix: string, ownerAndRepositoryName: string, issueNumber: string): string => { + let result = all; + let owner: string; + let repositoryName: string; - if (ownerAndRepositoryName) { - [owner, repositoryName] = ownerAndRepositoryName.split('/', 2); - } + if (ownerAndRepositoryName) { + [owner, repositoryName] = ownerAndRepositoryName.split('/', 2); + } - if (this.isGitHub) { if (owner && repositoryName && issueNumber) { // Issue in external repository const issueUrl = urljoin('https://github.com', owner, repositoryName, 'issues', issueNumber); @@ -444,12 +447,12 @@ export class MarkdownProcessor extends BaseProcessor { // Issue in own repository result = prefix + `[#${issueNumber}](${urljoin(this.repositoryUrl, 'issues', issueNumber)})`; } - } - return result; + return result; + } + // Replace Markdown issue references with urls + contents = contents.replace(markdownIssueRegex, issueReplace); } - // Replace Markdown issue references with urls - contents = contents.replace(markdownIssueRegex, issueReplace); const html = markdownit({ html: true }).render(contents); const $ = cheerio.load(html); diff --git a/src/test/package.test.ts b/src/test/package.test.ts index 08b7ed5..4194ec9 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -1551,6 +1551,33 @@ describe('MarkdownProcessor', () => { }); }); + it('should not replace issue links with urls if its a github repo but issue link expansion is disabled.', () => { + const manifest = { + name: 'test', + publisher: 'mocha', + version: '0.0.1', + description: 'test extension', + engines: Object.create(null), + repository: 'https://github.com/username/repository.git' + }; + + const root = fixture('readme'); + const processor = new ReadmeProcessor(manifest, { expandGitHubIssueLinks: false }); + const readme = { + path: 'extension/readme.md', + localPath: path.join(root, 'readme.github.md') + }; + + return processor.onFile(readme) + .then(file => read(file)) + .then(actual => { + return readFile(path.join(root, 'readme.github.md'), 'utf8') + .then(expected => { + assert.equal(actual, expected); + }); + }); + }); + it('should not replace issue links with urls if its not a github repo.', () => { const manifest = { name: 'test',