Skip to content

Commit

Permalink
Change to export defaultBuildUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 23, 2023
1 parent 27da7c9 commit 6e8eeee
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 72 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* @typedef {import('./lib/index.js').BuildUrlIssueValues} BuildUrlIssueValues
* @typedef {import('./lib/index.js').BuildUrlMentionValues} BuildUrlMentionValues
* @typedef {import('./lib/index.js').BuildUrlValues} BuildUrlValues
* @typedef {import('./lib/index.js').DefaultBuildUrl} DefaultBuildUrl
* @typedef {import('./lib/index.js').Options} Options
*/

export {default} from './lib/index.js'
export {default, defaultBuildUrl} from './lib/index.js'
101 changes: 46 additions & 55 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
* Create a URL.
* @param {Readonly<BuildUrlValues>} values
* Info on the link to build.
* @param {DefaultBuildUrl} defaultBuildUrl
* Function that can be called to perform normal behavior.
* @returns {string | false}
* URL to use or `false` to not link.
*
Expand Down Expand Up @@ -61,13 +59,6 @@
* @typedef {BuildUrlCommitValues | BuildUrlCompareValues | BuildUrlIssueValues | BuildUrlMentionValues} BuildUrlValues
* Info.
*
* @callback DefaultBuildUrl
* Given a set of values based on the values type, returns link URL.
* @param {Readonly<BuildUrlValues>} values
* Info on the link to build.
* @returns {string}
* URL to link to.
*
* @typedef Options
* Configuration.
* @property {BuildUrl | null | undefined} [buildUrl]
Expand Down Expand Up @@ -174,6 +165,42 @@ const mentionRegex = new RegExp(
'gi'
)

/**
* Create a URL to GH.
*
* @satisfies {BuildUrl}
* @param {Readonly<BuildUrlValues>} values
* Info on the link to build.
* @returns {string}
* URL to use.
*/
export 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('/')
}

/**
* Link references to users, commits, and issues, in the same way that GitHub
* does in comments, issues, PRs, and releases.
Expand Down Expand Up @@ -296,7 +323,7 @@ export default function remarkGithub(options) {
return false
}

const url = buildUrl({type: 'mention', user: username}, defaultBuildUrl)
const url = buildUrl({type: 'mention', user: username})

if (!url) return false

Expand Down Expand Up @@ -324,10 +351,7 @@ export default function remarkGithub(options) {
return false
}

const url = buildUrl(
{no, type: 'issue', ...repositoryInfo},
defaultBuildUrl
)
const url = buildUrl({no, type: 'issue', ...repositoryInfo})

return url
? {type: 'link', title: null, url, children: [{type: 'text', value}]}
Expand All @@ -350,10 +374,12 @@ export default function remarkGithub(options) {
return false
}

const url = buildUrl(
{base: a, compare: b, type: 'compare', ...repositoryInfo},
defaultBuildUrl
)
const url = buildUrl({
base: a,
compare: b,
type: 'compare',
...repositoryInfo
})

return url
? {
Expand Down Expand Up @@ -382,10 +408,7 @@ export default function remarkGithub(options) {
return false
}

const url = buildUrl(
{hash: value, type: 'commit', ...repositoryInfo},
defaultBuildUrl
)
const url = buildUrl({hash: value, type: 'commit', ...repositoryInfo})

return url
? {
Expand Down Expand Up @@ -420,7 +443,7 @@ export default function remarkGithub(options) {
const values = no
? {no, project, type: 'issue', user}
: {hash, project, type: 'commit', user}
const url = buildUrl(values, defaultBuildUrl)
const url = buildUrl(values)

if (!url) return false

Expand Down Expand Up @@ -460,38 +483,6 @@ function abbr(sha) {
return sha.slice(0, minShaLength)
}

/**
* 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.
*
Expand Down
21 changes: 15 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ on GitHub][github-writing]).
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`defaultBuildUrl(values)`](#defaultbuildurlvalues)
* [`unified().use(remarkGithub[, options])`](#unifieduseremarkgithub-options)
* [`BuildUrl`](#buildurl)
* [`BuildUrlCommitValues`](#buildurlcommitvalues)
* [`BuildUrlCompareValues`](#buildurlcomparevalues)
* [`BuildUrlIssueValues`](#buildurlissuevalues)
* [`BuildUrlMentionValues`](#buildurlmentionvalues)
* [`BuildUrlValues`](#buildurlvalues)
* [`DefaultBuildUrl`](#defaultbuildurl)
* [`Options`](#options)
* [Examples](#examples)
* [Example: `buildUrl`](#example-buildurl)
Expand Down Expand Up @@ -155,9 +155,22 @@ Some links:

## API

This package exports no identifiers.
This package exports the identifier [`defaultBuildUrl`][api-default-build-url].
The default export is [`remarkGithub`][api-remark-github].

### `defaultBuildUrl(values)`

Create a URL to GH.

###### Parameters

* `values` ([`BuildUrlValues`][api-build-url-values])
— info on the link to build

###### Returns

URL to use (`string`).

### `unified().use(remarkGithub[, options])`

Link references to users, commits, and issues, in the same way that GitHub does
Expand Down Expand Up @@ -257,10 +270,6 @@ type BuildUrlValues =
| BuildUrlMentionValues
```
### `DefaultBuildUrl`
To do.
### `Options`
Configuration (TypeScript type).
Expand Down
19 changes: 10 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import test from 'node:test'
import {remark} from 'remark'
import remarkGfm from 'remark-gfm'
import {VFile} from 'vfile'
import remarkGithub from '../index.js'
import remarkGithub, {defaultBuildUrl} from '../index.js'

test('remarkGithub', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
'default'
'default',
'defaultBuildUrl'
])
})

Expand Down Expand Up @@ -121,7 +122,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'mention'
? `https://github.yourcompany.com/${values.user}/`
: defaultBuildUrl(values)
Expand All @@ -139,7 +140,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'issue'
? `https://github.yourcompany.com/${values.user}/${values.project}/issues/${values.no}`
: defaultBuildUrl(values)
Expand All @@ -157,7 +158,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'issue'
? `https://github.yourcompany.com/${values.user}/${values.project}/issues/${values.no}`
: defaultBuildUrl(values)
Expand All @@ -177,7 +178,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'compare'
? `https://github.yourcompany.com/${values.user}/${values.project}/compare/${values.base}...${values.compare}`
: defaultBuildUrl(values)
Expand All @@ -196,7 +197,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'commit'
? `https://github.yourcompany.com/${values.user}/${values.project}/commit/${values.hash}`
: defaultBuildUrl(values)
Expand All @@ -216,7 +217,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'issue'
? `https://github.yourcompany.com/${values.user}/${values.project}/issues/${values.no}`
: defaultBuildUrl(values)
Expand All @@ -237,7 +238,7 @@ test('remarkGithub', async function (t) {
const file = await remark()
.use(remarkGfm)
.use(remarkGithub, {
buildUrl(values, defaultBuildUrl) {
buildUrl(values) {
return values.type === 'commit'
? `https://github.yourcompany.com/${values.user}/${values.project}/commit/${values.hash}`
: defaultBuildUrl(values)
Expand Down

0 comments on commit 6e8eeee

Please sign in to comment.