remark-github
Advanced tools
Comparing version 11.1.1 to 11.2.0
104
index.d.ts
@@ -12,6 +12,110 @@ /** | ||
export type Root = import('mdast').Root | ||
export type DefaultBuildUrl = (values: BuildUrlValues) => string | ||
export type BuildUrl = ( | ||
values: BuildUrlValues, | ||
defaultBuildUrl: DefaultBuildUrl | ||
) => string | false | ||
export type BuildUrlValues = | ||
| BuildUrlCommitValues | ||
| BuildUrlCompareValues | ||
| BuildUrlIssueValues | ||
| BuildUrlMentionValues | ||
/** | ||
* Arguments for buildUrl functions for commit hash | ||
*/ | ||
export type BuildUrlCommitValues = { | ||
/** | ||
* The type of special object | ||
*/ | ||
type: 'commit' | ||
/** | ||
* The owner of the repo | ||
*/ | ||
user: string | ||
/** | ||
* The project of the repo | ||
*/ | ||
project: string | ||
/** | ||
* The commit hash value | ||
*/ | ||
hash: string | ||
} | ||
/** | ||
* Arguments for buildUrl functions for commit hash ranges | ||
*/ | ||
export type BuildUrlCompareValues = { | ||
/** | ||
* The type of special object | ||
*/ | ||
type: 'compare' | ||
/** | ||
* The owner of the repo | ||
*/ | ||
user: string | ||
/** | ||
* The project of the repo | ||
*/ | ||
project: string | ||
/** | ||
* The SHA of the range start | ||
*/ | ||
base: string | ||
/** | ||
* The SHA of the range end | ||
*/ | ||
compare: string | ||
} | ||
/** | ||
* Arguments for buildUrl functions for issues | ||
*/ | ||
export type BuildUrlIssueValues = { | ||
/** | ||
* The type of special object | ||
*/ | ||
type: 'issue' | ||
/** | ||
* The owner of the repo | ||
*/ | ||
user: string | ||
/** | ||
* The project of the repo | ||
*/ | ||
project: string | ||
/** | ||
* The parsed issue number | ||
*/ | ||
no: string | ||
} | ||
/** | ||
* Arguments for buildUrl functions for mentions | ||
*/ | ||
export type BuildUrlMentionValues = { | ||
/** | ||
* The type of special object | ||
*/ | ||
type: 'mention' | ||
/** | ||
* The parsed user name | ||
*/ | ||
user: string | ||
} | ||
/** | ||
* The owner and project of the repo | ||
*/ | ||
export type RepositoryInfo = { | ||
/** | ||
* The user/organization name | ||
*/ | ||
user: string | ||
/** | ||
* The project/repo name | ||
*/ | ||
project: string | ||
} | ||
/** | ||
* Configuration. | ||
*/ | ||
export type Options = { | ||
buildUrl?: BuildUrl | undefined | ||
/** | ||
@@ -18,0 +122,0 @@ * Wrap mentions in `<strong>`, true by default. |
200
index.js
/** | ||
* @typedef {import('mdast').Root} Root | ||
* | ||
* @callback DefaultBuildUrl | ||
* @param {BuildUrlValues} values | ||
* @returns {string} | ||
* | ||
* @callback BuildUrl | ||
* @param {BuildUrlValues} values | ||
* @param {DefaultBuildUrl} defaultBuildUrl | ||
* @returns {string|false} | ||
* | ||
* @typedef {BuildUrlCommitValues|BuildUrlCompareValues|BuildUrlIssueValues|BuildUrlMentionValues} BuildUrlValues | ||
* | ||
* @typedef BuildUrlCommitValues | ||
* Arguments for buildUrl functions for commit hash | ||
* @property {'commit'} type The type of special object | ||
* @property {string} user The owner of the repo | ||
* @property {string} project The project of the repo | ||
* @property {string} hash The commit hash value | ||
* | ||
* @typedef BuildUrlCompareValues | ||
* Arguments for buildUrl functions for commit hash ranges | ||
* @property {'compare'} type The type of special object | ||
* @property {string} user The owner of the repo | ||
* @property {string} project The project of the repo | ||
* @property {string} base The SHA of the range start | ||
* @property {string} compare The SHA of the range end | ||
* | ||
* @typedef BuildUrlIssueValues | ||
* Arguments for buildUrl functions for issues | ||
* @property {'issue'} type The type of special object | ||
* @property {string} user The owner of the repo | ||
* @property {string} project The project of the repo | ||
* @property {string} no The parsed issue number | ||
* | ||
* @typedef BuildUrlMentionValues | ||
* Arguments for buildUrl functions for mentions | ||
* @property {'mention'} type The type of special object | ||
* @property {string} user The parsed user name | ||
* | ||
* @typedef RepositoryInfo | ||
* The owner and project of the repo | ||
* @property {string} user The user/organization name | ||
* @property {string} project The project/repo name | ||
* | ||
* @typedef Options | ||
* Configuration. | ||
* @property {BuildUrl} [buildUrl] | ||
* @property {boolean} [mentionStrong=true] | ||
@@ -121,2 +165,11 @@ * Wrap mentions in `<strong>`, true by default. | ||
/** | ||
* @param {BuildUrlValues} values | ||
* @returns {string|false} | ||
*/ | ||
function buildUrl(values) { | ||
if (options.buildUrl) return options.buildUrl(values, defaultBuildUrl) | ||
return defaultBuildUrl(values) | ||
} | ||
// Parse the URL: See the tests for all possible kinds. | ||
@@ -129,2 +182,3 @@ const repositoryMatch = repoRegex.exec(repository || '') | ||
/** @type {RepositoryInfo} */ | ||
const repositoryInfo = {user: repositoryMatch[1], project: repositoryMatch[2]} | ||
@@ -208,2 +262,6 @@ | ||
const url = buildUrl({type: 'mention', user: username}) | ||
if (!url) return false | ||
/** @type {StaticPhrasingContent} */ | ||
@@ -216,8 +274,3 @@ let node = {type: 'text', value} | ||
return { | ||
type: 'link', | ||
title: null, | ||
url: 'https://github.com/' + username, | ||
children: [node] | ||
} | ||
return {type: 'link', title: null, url, children: [node]} | ||
} | ||
@@ -239,14 +292,7 @@ | ||
return { | ||
type: 'link', | ||
title: null, | ||
url: | ||
'https://github.com/' + | ||
repositoryInfo.user + | ||
'/' + | ||
repositoryInfo.project + | ||
'/issues/' + | ||
no, | ||
children: [{type: 'text', value}] | ||
} | ||
const url = buildUrl({type: 'issue', ...repositoryInfo, no}) | ||
return url | ||
? {type: 'link', title: null, url, children: [{type: 'text', value}]} | ||
: false | ||
} | ||
@@ -270,14 +316,17 @@ | ||
return { | ||
type: 'link', | ||
title: null, | ||
url: | ||
'https://github.com/' + | ||
repositoryInfo.user + | ||
'/' + | ||
repositoryInfo.project + | ||
'/compare/' + | ||
value, | ||
children: [{type: 'inlineCode', value: abbr(a) + '...' + abbr(b)}] | ||
} | ||
const url = buildUrl({ | ||
type: 'compare', | ||
...repositoryInfo, | ||
base: a, | ||
compare: b | ||
}) | ||
return url | ||
? { | ||
type: 'link', | ||
title: null, | ||
url, | ||
children: [{type: 'inlineCode', value: abbr(a) + '...' + abbr(b)}] | ||
} | ||
: false | ||
} | ||
@@ -302,14 +351,12 @@ | ||
return { | ||
type: 'link', | ||
title: null, | ||
url: | ||
'https://github.com/' + | ||
repositoryInfo.user + | ||
'/' + | ||
repositoryInfo.project + | ||
'/commit/' + | ||
value, | ||
children: [{type: 'inlineCode', value: abbr(value)}] | ||
} | ||
const url = buildUrl({type: 'commit', ...repositoryInfo, hash: value}) | ||
return url | ||
? { | ||
type: 'link', | ||
title: null, | ||
url, | ||
children: [{type: 'inlineCode', value: abbr(value)}] | ||
} | ||
: false | ||
} | ||
@@ -321,11 +368,9 @@ | ||
* @param {string} user | ||
* @param {string} project | ||
* @param {string} specificProject | ||
* @param {string} no | ||
* @param {string} sha | ||
* @param {string} hash | ||
* @param {Match} match | ||
*/ | ||
// eslint-disable-next-line max-params | ||
function replaceReference($0, user, project, no, sha, match) { | ||
let value = '' | ||
function replaceReference($0, user, specificProject, no, hash, match) { | ||
if ( | ||
@@ -338,13 +383,19 @@ /[^\t\n\r (@[{]/.test(match.input.charAt(match.index - 1)) || | ||
const project = specificProject || repositoryInfo.project | ||
const url = no | ||
? buildUrl({type: 'issue', user, project, no}) | ||
: buildUrl({type: 'commit', user, project, hash}) | ||
if (!url) return false | ||
/** @type {StaticPhrasingContent[]} */ | ||
const nodes = [] | ||
let value = '' | ||
if (user !== repositoryInfo.user) { | ||
if (project !== repositoryInfo.project) { | ||
value += user + '/' + project | ||
} else if (user !== repositoryInfo.user) { | ||
value += user | ||
} | ||
if (project && project !== repositoryInfo.project) { | ||
value = user + '/' + project | ||
} | ||
if (no) { | ||
@@ -354,3 +405,3 @@ value += '#' + no | ||
value += '@' | ||
nodes.push({type: 'inlineCode', value: abbr(sha)}) | ||
nodes.push({type: 'inlineCode', value: abbr(hash)}) | ||
} | ||
@@ -360,16 +411,3 @@ | ||
return { | ||
type: 'link', | ||
title: null, | ||
url: | ||
'https://github.com/' + | ||
user + | ||
'/' + | ||
(project || repositoryInfo.project) + | ||
'/' + | ||
(no ? 'issues' : 'commit') + | ||
'/' + | ||
(no || sha), | ||
children: nodes | ||
} | ||
return {type: 'link', title: null, url, children: nodes} | ||
} | ||
@@ -389,2 +427,30 @@ } | ||
/** | ||
* Given a set of values based on the values type, returns link URL. | ||
* | ||
* @type {DefaultBuildUrl} | ||
*/ | ||
function defaultBuildUrl(values) { | ||
const base = 'https://github.com' | ||
if (values.type === 'mention') return [base, values.user].join('/') | ||
const {project, user} = values | ||
if (values.type === 'commit') | ||
return [base, user, project, 'commit', values.hash].join('/') | ||
if (values.type === 'issue') | ||
return [base, user, project, 'issues', values.no].join('/') | ||
// `values.type` is `'compare'` | ||
return [ | ||
base, | ||
user, | ||
project, | ||
'compare', | ||
values.base + '...' + values.compare | ||
].join('/') | ||
} | ||
/** | ||
* Parse a link and determine whether it links to GitHub. | ||
@@ -391,0 +457,0 @@ * |
{ | ||
"name": "remark-github", | ||
"version": "11.1.1", | ||
"version": "11.2.0", | ||
"description": "remark plugin to autolink references like in GitHub issues, PRs, and comments", | ||
@@ -33,3 +33,4 @@ "license": "MIT", | ||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)", | ||
"Anthony Maki <4cm4k1@gmail.com>" | ||
"Anthony Maki <4cm4k1@gmail.com>", | ||
"Ev Haus <ev@haus.gg>" | ||
], | ||
@@ -36,0 +37,0 @@ "sideEffects": false, |
@@ -155,2 +155,28 @@ # remark-github | ||
##### Custom URLs | ||
By default we build URLs to public GitHub. | ||
You can overwrite them to point to GitHub Enterprise or other places by passing | ||
a `buildUrl`. | ||
That function is given an object with different values and the default | ||
`buildUrl`. | ||
If `buildUrl` returns `false`, the value is not linked. | ||
```js | ||
remark() | ||
.use(remarkGithub, { | ||
// The fields in `values` depends on the kind reference: | ||
// {type: 'commit', user, project, hash} | ||
// {type: 'compare', user, project, base, compare} | ||
// {type: 'issue', user, project, no} | ||
// {type: 'mention', user} | ||
// You can return a URL, which will be used, or `false`, to not link. | ||
buildUrl(values, defaultBuildUrl) { | ||
return values.type === 'mention' | ||
? `https://yourwebsite.com/${values.user}/` | ||
: defaultBuildUrl(values) | ||
} | ||
}) | ||
``` | ||
## Security | ||
@@ -157,0 +183,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27796
559
260