remark-github
Advanced tools
Comparing version 11.2.3 to 11.2.4
@@ -1,2 +0,2 @@ | ||
export default remarkGithub | ||
export {default} from './lib/index.js' | ||
export type Options = import('./lib/index.js').Options | ||
@@ -12,2 +12,1 @@ export type BuildUrl = import('./lib/index.js').BuildUrl | ||
import('./lib/index.js').BuildUrlMentionValues | ||
import remarkGithub from './lib/index.js' |
/** | ||
* Get the repository from `package.json`. | ||
* | ||
* @param {string} cwd | ||
* @returns {string|undefined} | ||
*/ | ||
export function getRepoFromPackage(): string | undefined | ||
export function getRepoFromPackage(cwd: string): string | undefined | ||
export type PackageJson = import('type-fest').PackageJson |
@@ -6,3 +6,2 @@ /** | ||
import fs from 'fs' | ||
import process from 'process' | ||
import path from 'path' | ||
@@ -13,5 +12,6 @@ | ||
* | ||
* @param {string} cwd | ||
* @returns {string|undefined} | ||
*/ | ||
export function getRepoFromPackage() { | ||
export function getRepoFromPackage(cwd) { | ||
/** @type {PackageJson|undefined} */ | ||
@@ -21,5 +21,3 @@ let pkg | ||
try { | ||
pkg = JSON.parse( | ||
String(fs.readFileSync(path.join(process.cwd(), 'package.json'))) | ||
) | ||
pkg = JSON.parse(String(fs.readFileSync(path.join(cwd, 'package.json')))) | ||
} catch {} | ||
@@ -26,0 +24,0 @@ |
301
lib/index.js
@@ -142,15 +142,18 @@ /** | ||
export default function remarkGithub(options = {}) { | ||
const repository = options.repository || getRepoFromPackage() | ||
return (tree, vfile) => { | ||
const repository = options.repository || getRepoFromPackage(vfile.cwd) | ||
// Parse the URL: See the tests for all possible kinds. | ||
const repositoryMatch = repoRegex.exec(repository || '') | ||
// Parse the URL: See the tests for all possible kinds. | ||
const repositoryMatch = repoRegex.exec(repository || '') | ||
if (!repositoryMatch) { | ||
throw new Error('Missing or invalid `repository` field in `options`') | ||
} | ||
if (!repositoryMatch) { | ||
throw new Error('Missing or invalid `repository` field in `options`') | ||
} | ||
/** @type {RepositoryInfo} */ | ||
const repositoryInfo = {user: repositoryMatch[1], project: repositoryMatch[2]} | ||
/** @type {RepositoryInfo} */ | ||
const repositoryInfo = { | ||
user: repositoryMatch[1], | ||
project: repositoryMatch[2] | ||
} | ||
return (tree) => { | ||
findAndReplace( | ||
@@ -214,170 +217,170 @@ tree, | ||
}) | ||
} | ||
/** | ||
* @param {BuildUrlValues} values | ||
* @returns {string|false} | ||
*/ | ||
function buildUrl(values) { | ||
if (options.buildUrl) return options.buildUrl(values, defaultBuildUrl) | ||
return defaultBuildUrl(values) | ||
} | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {string} username | ||
* @param {Match} match | ||
*/ | ||
function replaceMention(value, username, match) { | ||
if ( | ||
/[\w`]/.test(match.input.charAt(match.index - 1)) || | ||
/[/\w`]/.test(match.input.charAt(match.index + value.length)) || | ||
denyMention.has(username) | ||
) { | ||
return false | ||
/** | ||
* @param {BuildUrlValues} values | ||
* @returns {string|false} | ||
*/ | ||
function buildUrl(values) { | ||
if (options.buildUrl) return options.buildUrl(values, defaultBuildUrl) | ||
return defaultBuildUrl(values) | ||
} | ||
const url = buildUrl({type: 'mention', user: username}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {string} username | ||
* @param {Match} match | ||
*/ | ||
function replaceMention(value, username, match) { | ||
if ( | ||
/[\w`]/.test(match.input.charAt(match.index - 1)) || | ||
/[/\w`]/.test(match.input.charAt(match.index + value.length)) || | ||
denyMention.has(username) | ||
) { | ||
return false | ||
} | ||
if (!url) return false | ||
const url = buildUrl({type: 'mention', user: username}) | ||
/** @type {StaticPhrasingContent} */ | ||
let node = {type: 'text', value} | ||
if (!url) return false | ||
if (options.mentionStrong !== false) { | ||
node = {type: 'strong', children: [node]} | ||
} | ||
/** @type {StaticPhrasingContent} */ | ||
let node = {type: 'text', value} | ||
return {type: 'link', title: null, url, children: [node]} | ||
} | ||
if (options.mentionStrong !== false) { | ||
node = {type: 'strong', children: [node]} | ||
} | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {string} no | ||
* @param {Match} match | ||
*/ | ||
function replaceIssue(value, no, match) { | ||
if ( | ||
/\w/.test(match.input.charAt(match.index - 1)) || | ||
/\w/.test(match.input.charAt(match.index + value.length)) | ||
) { | ||
return false | ||
return {type: 'link', title: null, url, children: [node]} | ||
} | ||
const url = buildUrl({type: 'issue', ...repositoryInfo, no}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {string} no | ||
* @param {Match} match | ||
*/ | ||
function replaceIssue(value, no, match) { | ||
if ( | ||
/\w/.test(match.input.charAt(match.index - 1)) || | ||
/\w/.test(match.input.charAt(match.index + value.length)) | ||
) { | ||
return false | ||
} | ||
return url | ||
? {type: 'link', title: null, url, children: [{type: 'text', value}]} | ||
: false | ||
} | ||
const url = buildUrl({type: 'issue', ...repositoryInfo, no}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {string} a | ||
* @param {string} b | ||
* @param {Match} match | ||
*/ | ||
function replaceHashRange(value, a, b, match) { | ||
if ( | ||
/[^\t\n\r (@[{]/.test(match.input.charAt(match.index - 1)) || | ||
/\w/.test(match.input.charAt(match.index + value.length)) || | ||
denyHash.has(value) | ||
) { | ||
return false | ||
return url | ||
? {type: 'link', title: null, url, children: [{type: 'text', value}]} | ||
: false | ||
} | ||
const url = buildUrl({ | ||
type: 'compare', | ||
...repositoryInfo, | ||
base: a, | ||
compare: b | ||
}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {string} a | ||
* @param {string} b | ||
* @param {Match} match | ||
*/ | ||
function replaceHashRange(value, a, b, match) { | ||
if ( | ||
/[^\t\n\r (@[{]/.test(match.input.charAt(match.index - 1)) || | ||
/\w/.test(match.input.charAt(match.index + value.length)) || | ||
denyHash.has(value) | ||
) { | ||
return false | ||
} | ||
return url | ||
? { | ||
type: 'link', | ||
title: null, | ||
url, | ||
children: [{type: 'inlineCode', value: abbr(a) + '...' + abbr(b)}] | ||
} | ||
: false | ||
} | ||
const url = buildUrl({ | ||
type: 'compare', | ||
...repositoryInfo, | ||
base: a, | ||
compare: b | ||
}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {Match} match | ||
*/ | ||
function replaceHash(value, match) { | ||
if ( | ||
/[^\t\n\r (@[{.]/.test(match.input.charAt(match.index - 1)) || | ||
// For some weird reason GH does link two dots, but not one π€·ββοΈ | ||
(match.input.charAt(match.index - 1) === '.' && | ||
match.input.charAt(match.index - 2) !== '.') || | ||
/\w/.test(match.input.charAt(match.index + value.length)) || | ||
denyHash.has(value) | ||
) { | ||
return false | ||
return url | ||
? { | ||
type: 'link', | ||
title: null, | ||
url, | ||
children: [{type: 'inlineCode', value: abbr(a) + '...' + abbr(b)}] | ||
} | ||
: false | ||
} | ||
const url = buildUrl({type: 'commit', ...repositoryInfo, hash: value}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} value | ||
* @param {Match} match | ||
*/ | ||
function replaceHash(value, match) { | ||
if ( | ||
/[^\t\n\r (@[{.]/.test(match.input.charAt(match.index - 1)) || | ||
// For some weird reason GH does link two dots, but not one π€·ββοΈ | ||
(match.input.charAt(match.index - 1) === '.' && | ||
match.input.charAt(match.index - 2) !== '.') || | ||
/\w/.test(match.input.charAt(match.index + value.length)) || | ||
denyHash.has(value) | ||
) { | ||
return false | ||
} | ||
return url | ||
? { | ||
type: 'link', | ||
title: null, | ||
url, | ||
children: [{type: 'inlineCode', value: abbr(value)}] | ||
} | ||
: false | ||
} | ||
const url = buildUrl({type: 'commit', ...repositoryInfo, hash: value}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} $0 | ||
* @param {string} user | ||
* @param {string} specificProject | ||
* @param {string} no | ||
* @param {string} hash | ||
* @param {Match} match | ||
*/ | ||
// eslint-disable-next-line max-params | ||
function replaceReference($0, user, specificProject, no, hash, match) { | ||
if ( | ||
/[^\t\n\r (@[{]/.test(match.input.charAt(match.index - 1)) || | ||
/\w/.test(match.input.charAt(match.index + $0.length)) | ||
) { | ||
return false | ||
return url | ||
? { | ||
type: 'link', | ||
title: null, | ||
url, | ||
children: [{type: 'inlineCode', value: abbr(value)}] | ||
} | ||
: false | ||
} | ||
const project = specificProject || repositoryInfo.project | ||
const url = no | ||
? buildUrl({type: 'issue', user, project, no}) | ||
: buildUrl({type: 'commit', user, project, hash}) | ||
/** | ||
* @type {ReplaceFunction} | ||
* @param {string} $0 | ||
* @param {string} user | ||
* @param {string} specificProject | ||
* @param {string} no | ||
* @param {string} hash | ||
* @param {Match} match | ||
*/ | ||
// eslint-disable-next-line max-params | ||
function replaceReference($0, user, specificProject, no, hash, match) { | ||
if ( | ||
/[^\t\n\r (@[{]/.test(match.input.charAt(match.index - 1)) || | ||
/\w/.test(match.input.charAt(match.index + $0.length)) | ||
) { | ||
return false | ||
} | ||
if (!url) return false | ||
const project = specificProject || repositoryInfo.project | ||
const url = no | ||
? buildUrl({type: 'issue', user, project, no}) | ||
: buildUrl({type: 'commit', user, project, hash}) | ||
/** @type {StaticPhrasingContent[]} */ | ||
const nodes = [] | ||
let value = '' | ||
if (!url) return false | ||
if (project !== repositoryInfo.project) { | ||
value += user + '/' + project | ||
} else if (user !== repositoryInfo.user) { | ||
value += user | ||
} | ||
/** @type {StaticPhrasingContent[]} */ | ||
const nodes = [] | ||
let value = '' | ||
if (no) { | ||
value += '#' + no | ||
} else { | ||
value += '@' | ||
nodes.push({type: 'inlineCode', value: abbr(hash)}) | ||
} | ||
if (project !== repositoryInfo.project) { | ||
value += user + '/' + project | ||
} else if (user !== repositoryInfo.user) { | ||
value += user | ||
} | ||
nodes.unshift({type: 'text', value}) | ||
if (no) { | ||
value += '#' + no | ||
} else { | ||
value += '@' | ||
nodes.push({type: 'inlineCode', value: abbr(hash)}) | ||
} | ||
return {type: 'link', title: null, url, children: nodes} | ||
nodes.unshift({type: 'text', value}) | ||
return {type: 'link', title: null, url, children: nodes} | ||
} | ||
} | ||
@@ -384,0 +387,0 @@ } |
{ | ||
"name": "remark-github", | ||
"version": "11.2.3", | ||
"version": "11.2.4", | ||
"description": "remark plugin to autolink references like in GitHub issues, PRs, and comments", | ||
@@ -63,3 +63,3 @@ "license": "MIT", | ||
"remark": "^14.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-gfm": "^3.0.0", | ||
@@ -72,3 +72,4 @@ "remark-preset-wooorm": "^9.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.48.0" | ||
"vfile": "^5.0.0", | ||
"xo": "^0.50.0" | ||
}, | ||
@@ -75,0 +76,0 @@ "scripts": { |
@@ -123,12 +123,8 @@ # remark-github | ||
main() | ||
const file = await remark() | ||
.use(remarkGfm) | ||
.use(remarkGithub) | ||
.process(await read('example.md')) | ||
async function main() { | ||
const file = await remark() | ||
.use(remarkGfm) | ||
.use(remarkGithub) | ||
.process(await read('example.md')) | ||
console.log(String(file)) | ||
} | ||
console.log(String(file)) | ||
``` | ||
@@ -135,0 +131,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
34823
614
14
397