Skip to content

Commit dfe326e

Browse files
committed
Add support for not linking w/ buildUrl returning false
Closes GH-30.
1 parent a7a09a6 commit dfe326e

File tree

3 files changed

+114
-117
lines changed

3 files changed

+114
-117
lines changed

index.js

Lines changed: 61 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88
* @callback BuildUrl
99
* @param {BuildUrlValues} values
1010
* @param {DefaultBuildUrl} defaultBuildUrl
11-
* @returns {string}
11+
* @returns {string|false}
1212
*
1313
* @typedef {BuildUrlCommitValues|BuildUrlCompareValues|BuildUrlIssueValues|BuildUrlMentionValues} BuildUrlValues
1414
*
1515
* @typedef BuildUrlCommitValues
1616
* Arguments for buildUrl functions for commit hash
1717
* @property {'commit'} type The type of special object
18-
* @property {string} hash The commit hash value
19-
* @property {string} project The project of the repo
2018
* @property {string} user The owner of the repo
19+
* @property {string} project The project of the repo
20+
* @property {string} hash The commit hash value
2121
*
2222
* @typedef BuildUrlCompareValues
2323
* Arguments for buildUrl functions for commit hash ranges
2424
* @property {'compare'} type The type of special object
25+
* @property {string} user The owner of the repo
26+
* @property {string} project The project of the repo
2527
* @property {string} base The SHA of the range start
2628
* @property {string} compare The SHA of the range end
27-
* @property {string} project The project of the repo
28-
* @property {string} user The owner of the repo
2929
*
3030
* @typedef BuildUrlIssueValues
3131
* Arguments for buildUrl functions for issues
3232
* @property {'issue'} type The type of special object
33-
* @property {string} no The parsed issue number
34-
* @property {string} project The project of the repo
3533
* @property {string} user The owner of the repo
34+
* @property {string} project The project of the repo
35+
* @property {string} no The parsed issue number
3636
*
3737
* @typedef BuildUrlMentionValues
3838
* Arguments for buildUrl functions for mentions
@@ -41,8 +41,8 @@
4141
*
4242
* @typedef RepositoryInfo
4343
* The owner and project of the repo
44-
* @property {string} project The project/repo name
4544
* @property {string} user The user/organization name
45+
* @property {string} project The project/repo name
4646
*
4747
* @typedef Options
4848
* Configuration.
@@ -164,7 +164,7 @@ export default function remarkGithub(options = {}) {
164164

165165
/**
166166
* @param {BuildUrlValues} values
167-
* @returns {string}
167+
* @returns {string|false}
168168
*/
169169
function buildUrl(values) {
170170
if (options.buildUrl) return options.buildUrl(values, defaultBuildUrl)
@@ -257,19 +257,18 @@ export default function remarkGithub(options = {}) {
257257
return false
258258
}
259259

260+
const url = buildUrl({type: 'mention', user: username})
261+
262+
if (!url) return false
263+
260264
/** @type {StaticPhrasingContent} */
261265
let node = {type: 'text', value}
262266

263267
if (options.mentionStrong !== false) {
264268
node = {type: 'strong', children: [node]}
265269
}
266270

267-
return {
268-
type: 'link',
269-
title: null,
270-
url: buildUrl({type: 'mention', user: username}),
271-
children: [node]
272-
}
271+
return {type: 'link', title: null, url, children: [node]}
273272
}
274273

275274
/**
@@ -286,17 +285,11 @@ export default function remarkGithub(options = {}) {
286285
return false
287286
}
288287

289-
return {
290-
type: 'link',
291-
title: null,
292-
url: buildUrl({
293-
type: 'issue',
294-
no,
295-
project: repositoryInfo.project,
296-
user: repositoryInfo.user
297-
}),
298-
children: [{type: 'text', value}]
299-
}
288+
const url = buildUrl({type: 'issue', ...repositoryInfo, no})
289+
290+
return url
291+
? {type: 'link', title: null, url, children: [{type: 'text', value}]}
292+
: false
300293
}
301294

302295
/**
@@ -315,18 +308,21 @@ export default function remarkGithub(options = {}) {
315308
return false
316309
}
317310

318-
return {
319-
type: 'link',
320-
title: null,
321-
url: buildUrl({
322-
type: 'compare',
323-
base: a,
324-
compare: b,
325-
project: repositoryInfo.project,
326-
user: repositoryInfo.user
327-
}),
328-
children: [{type: 'inlineCode', value: abbr(a) + '...' + abbr(b)}]
329-
}
311+
const url = buildUrl({
312+
type: 'compare',
313+
...repositoryInfo,
314+
base: a,
315+
compare: b
316+
})
317+
318+
return url
319+
? {
320+
type: 'link',
321+
title: null,
322+
url,
323+
children: [{type: 'inlineCode', value: abbr(a) + '...' + abbr(b)}]
324+
}
325+
: false
330326
}
331327

332328
/**
@@ -346,77 +342,63 @@ export default function remarkGithub(options = {}) {
346342
return false
347343
}
348344

349-
return {
350-
type: 'link',
351-
title: null,
352-
url: buildUrl({
353-
type: 'commit',
354-
project: repositoryInfo.project,
355-
user: repositoryInfo.user,
356-
hash: value
357-
}),
358-
children: [{type: 'inlineCode', value: abbr(value)}]
359-
}
345+
const url = buildUrl({type: 'commit', ...repositoryInfo, hash: value})
346+
347+
return url
348+
? {
349+
type: 'link',
350+
title: null,
351+
url,
352+
children: [{type: 'inlineCode', value: abbr(value)}]
353+
}
354+
: false
360355
}
361356

362357
/**
363358
* @type {ReplaceFunction}
364359
* @param {string} $0
365360
* @param {string} user
366-
* @param {string} project
361+
* @param {string} specificProject
367362
* @param {string} no
368-
* @param {string} sha
363+
* @param {string} hash
369364
* @param {Match} match
370365
*/
371366
// eslint-disable-next-line max-params
372-
function replaceReference($0, user, project, no, sha, match) {
373-
let value = ''
374-
367+
function replaceReference($0, user, specificProject, no, hash, match) {
375368
if (
376369
/[^\t\n\r (@[{]/.test(match.input.charAt(match.index - 1)) ||
377370
/\w/.test(match.input.charAt(match.index + $0.length))
378371
) {
379372
return false
380373
}
381374

375+
const project = specificProject || repositoryInfo.project
376+
const url = no
377+
? buildUrl({type: 'issue', user, project, no})
378+
: buildUrl({type: 'commit', user, project, hash})
379+
380+
if (!url) return false
381+
382382
/** @type {StaticPhrasingContent[]} */
383383
const nodes = []
384+
let value = ''
384385

385-
if (user !== repositoryInfo.user) {
386+
if (project !== repositoryInfo.project) {
387+
value += user + '/' + project
388+
} else if (user !== repositoryInfo.user) {
386389
value += user
387390
}
388391

389-
if (project && project !== repositoryInfo.project) {
390-
value = user + '/' + project
391-
}
392-
393392
if (no) {
394393
value += '#' + no
395394
} else {
396395
value += '@'
397-
nodes.push({type: 'inlineCode', value: abbr(sha)})
396+
nodes.push({type: 'inlineCode', value: abbr(hash)})
398397
}
399398

400399
nodes.unshift({type: 'text', value})
401400

402-
return {
403-
type: 'link',
404-
title: null,
405-
url: no
406-
? buildUrl({
407-
type: 'issue',
408-
no,
409-
project: project || repositoryInfo.project,
410-
user
411-
})
412-
: buildUrl({
413-
type: 'commit',
414-
hash: sha,
415-
project: project || repositoryInfo.project,
416-
user
417-
}),
418-
children: nodes
419-
}
401+
return {type: 'link', title: null, url, children: nodes}
420402
}
421403
}
422404

@@ -448,7 +430,7 @@ function defaultBuildUrl(values) {
448430
if (values.type === 'issue')
449431
return [base, user, project, 'issues', values.no].join('/')
450432

451-
// This should only run if values.type is 'compare'
433+
// `values.type` is `'compare'`
452434
return [
453435
base,
454436
user,

readme.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,30 +134,6 @@ in GitHub issues, PRs, and comments (see
134134
* At-mentions:
135135
`@wooorm`[**@wooorm**][mention]
136136

137-
##### Custom URLs
138-
139-
By default we build URLs to public GitHub.
140-
You can overwrite them to point to GitHub Enterprise or other places by passing
141-
a `buildUrl`.
142-
That function is given an object with different values and the default
143-
`buildUrl`.
144-
145-
```js
146-
remark()
147-
.use(remarkGithub, {
148-
// The fields in `values` depends on the kind reference:
149-
// {type: 'commit', hash, project, user}
150-
// {type: 'hashrange', base, compare, project, user}
151-
// {type: 'issue', no, project, user}
152-
// {type: 'mention', user}
153-
buildUrl: (values, defaultBuildUrl) => {
154-
return values.type === 'mention'
155-
? `https://yourwebsite.com/${values.user}/`
156-
: defaultBuildUrl(values)
157-
}
158-
})
159-
```
160-
161137
###### Repository
162138

163139
These links are generated relative to a project.
@@ -176,6 +152,32 @@ However, this creates different HTML markup, as the GitHub site applies these
176152
styles using CSS.
177153
Pass `mentionStrong: false` to turn off this behavior.
178154

155+
##### Custom URLs
156+
157+
By default we build URLs to public GitHub.
158+
You can overwrite them to point to GitHub Enterprise or other places by passing
159+
a `buildUrl`.
160+
That function is given an object with different values and the default
161+
`buildUrl`.
162+
If `buildUrl` returns `false`, the value is not linked.
163+
164+
```js
165+
remark()
166+
.use(remarkGithub, {
167+
// The fields in `values` depends on the kind reference:
168+
// {type: 'commit', user, project, hash}
169+
// {type: 'compare', user, project, base, compare}
170+
// {type: 'issue', user, project, no}
171+
// {type: 'mention', user}
172+
// You can return a URL, which will be used, or `false`, to not link.
173+
buildUrl(values, defaultBuildUrl) {
174+
return values.type === 'mention'
175+
? `https://yourwebsite.com/${values.user}/`
176+
: defaultBuildUrl(values)
177+
}
178+
})
179+
```
180+
179181
## Security
180182

181183
Use of `remark-github` does not involve [**rehype**][rehype] ([**hast**][hast]).

0 commit comments

Comments
 (0)