Skip to content

Commit

Permalink
Merge pull request #424 from rjmholt/stop-issue-expansion
Browse files Browse the repository at this point in the history
Add --noGitHubIssueLinking to stop issue link expansion in package step
  • Loading branch information
joaomoreno authored Feb 27, 2020
2 parents ee42cf4 + 368ffbd commit acd1c3c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<version>]')
Expand Down
29 changes: 16 additions & 13 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface IPackageOptions {
useYarn?: boolean;
dependencyEntryPoints?: string[];
ignoreFile?: string;
expandGitHubIssueLinks?: boolean;
}

export interface IProcessor {
Expand Down Expand Up @@ -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);
Expand All @@ -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<IFile> {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions src/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit acd1c3c

Please sign in to comment.