From 6e8eeee1d238515189b60ab92992770442bba7bc Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 23 Sep 2023 12:50:50 +0200 Subject: [PATCH] Change to export `defaultBuildUrl` --- index.js | 3 +- lib/index.js | 101 +++++++++++++++++++++++--------------------------- readme.md | 21 ++++++++--- test/index.js | 19 +++++----- 4 files changed, 72 insertions(+), 72 deletions(-) diff --git a/index.js b/index.js index aa2909d..a55a210 100644 --- a/index.js +++ b/index.js @@ -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' diff --git a/lib/index.js b/lib/index.js index 7d5fa10..42759a4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,8 +11,6 @@ * Create a URL. * @param {Readonly} 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. * @@ -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} values - * Info on the link to build. - * @returns {string} - * URL to link to. - * * @typedef Options * Configuration. * @property {BuildUrl | null | undefined} [buildUrl] @@ -174,6 +165,42 @@ const mentionRegex = new RegExp( 'gi' ) +/** + * Create a URL to GH. + * + * @satisfies {BuildUrl} + * @param {Readonly} 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. @@ -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 @@ -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}]} @@ -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 ? { @@ -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 ? { @@ -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 @@ -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. * diff --git a/readme.md b/readme.md index ded805a..56ee12c 100644 --- a/readme.md +++ b/readme.md @@ -19,6 +19,7 @@ on GitHub][github-writing]). * [Install](#install) * [Use](#use) * [API](#api) + * [`defaultBuildUrl(values)`](#defaultbuildurlvalues) * [`unified().use(remarkGithub[, options])`](#unifieduseremarkgithub-options) * [`BuildUrl`](#buildurl) * [`BuildUrlCommitValues`](#buildurlcommitvalues) @@ -26,7 +27,6 @@ on GitHub][github-writing]). * [`BuildUrlIssueValues`](#buildurlissuevalues) * [`BuildUrlMentionValues`](#buildurlmentionvalues) * [`BuildUrlValues`](#buildurlvalues) - * [`DefaultBuildUrl`](#defaultbuildurl) * [`Options`](#options) * [Examples](#examples) * [Example: `buildUrl`](#example-buildurl) @@ -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 @@ -257,10 +270,6 @@ type BuildUrlValues = | BuildUrlMentionValues ``` -### `DefaultBuildUrl` - -To do. - ### `Options` Configuration (TypeScript type). diff --git a/test/index.js b/test/index.js index b2b7887..ea292fc 100644 --- a/test/index.js +++ b/test/index.js @@ -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' ]) }) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)