diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..441975c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,13 +10,10 @@ jobs: fail-fast: false matrix: node-version: - - 14 - - 12 - - 10 - - 8 + - 16 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 0251969..ab37de1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,56 +1,52 @@ -/// +import {HTMLAttributes} from 'create-html-element'; -declare namespace linkifyIssues { - interface Options { - /** - GitHub user. - */ - user: string; +export interface Options { + /** + GitHub user. + */ + readonly user: string; - /** - GitHub repository. - */ - repository: string; + /** + GitHub repository. + */ + readonly repository: string; - /** - HTML attributes to add to the link. - */ - attributes?: { - [attributeName: string]: string | number | boolean | readonly string[]; - }; + /** + HTML attributes to add to the link. + */ + readonly attributes?: HTMLAttributes; - /** - Base URL. + /** + The base URL. - @default 'https://github.com' - */ - baseUrl?: string; + @default 'https://github.com' + */ + readonly baseUrl?: string; - /** - Format of the generated content. + /** + The format of the generated content. - `'string'` will return it as a flat string like `'See #143'`. + `'string'` will return it as a flat string like `'See #143'`. - `'dom'` will return it as a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('See '), HTMLAnchorElement('#143'))`. This type only works in the browser. + `'dom'` will return it as a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('See '), HTMLAnchorElement('#143'))`. This type only works in the browser. - @default 'string' - */ - type?: 'string' | 'dom'; - } + @default 'string' + */ + readonly type?: 'string' | 'dom'; +} - interface TypeDomOptions extends Options { - type: 'dom'; - } +export interface TypeDomOptions extends Options { + readonly type: 'dom'; } /** Linkify GitHub issue references. -@param string - String with issue references to linkify. +@param string - A string with issue references to linkify. @example ``` -import linkifyIssues = require('linkify-issues'); +import linkifyIssues from 'linkify-issues'; linkifyIssues('Fixes #143 and avajs/ava#1023', { user: 'sindresorhus', @@ -76,13 +72,13 @@ const fragment = linkifyUrls('See #143', { document.body.appendChild(fragment); ``` */ -declare function linkifyIssues( +export default function linkifyIssues( string: string, - options: linkifyIssues.TypeDomOptions + options: TypeDomOptions ): DocumentFragment; -declare function linkifyIssues( +export default function linkifyIssues( string: string, - options?: linkifyIssues.Options + options?: Options ): string; -export = linkifyIssues; +export {HTMLAttributes} from 'create-html-element'; diff --git a/index.js b/index.js index 11069bc..df31af5 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ -'use strict'; -const issueRegex = require('issue-regex'); -const createHtmlElement = require('create-html-element'); +import issueRegex from 'issue-regex'; +import createHtmlElement from 'create-html-element'; const groupedIssueRegex = new RegExp(`(${issueRegex().source})`, 'g'); @@ -16,18 +15,16 @@ const linkify = (match, options) => { attributes: { href: '', ...options.attributes, - href // eslint-disable-line no-dupe-keys + href, // eslint-disable-line no-dupe-keys }, - text: match + text: match, }); }; // Get DOM node from HTML const domify = html => document.createRange().createContextualFragment(html); -const getAsString = (string, options) => { - return string.replace(groupedIssueRegex, match => linkify(match, options)); -}; +const getAsString = (string, options) => string.replace(groupedIssueRegex, match => linkify(match, options)); const getAsDocumentFragment = (string, options) => { const fragment = document.createDocumentFragment(); @@ -42,12 +39,12 @@ const getAsDocumentFragment = (string, options) => { return fragment; }; -module.exports = (string, options) => { +export default function linkifyIssues(string, options) { options = { attributes: {}, baseUrl: 'https://github.com', type: 'string', - ...options + ...options, }; if (!(options.user && options.repository)) { @@ -62,5 +59,5 @@ module.exports = (string, options) => { return getAsDocumentFragment(string, options); } - throw new Error('The `type` option must be either `dom` or `string`'); -}; + throw new TypeError('The `type` option must be either `dom` or `string`'); +} diff --git a/index.test-d.ts b/index.test-d.ts index 35eedab..fe1d8b8 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,11 +1,11 @@ import {expectType} from 'tsd'; -import linkifyIssues = require('.'); +import linkifyIssues from './index.js'; expectType( linkifyIssues('Fixes #143 and avajs/ava#1023', { user: 'sindresorhus', - repository: 'dofle' - }) + repository: 'dofle', + }), ); expectType( linkifyIssues('Fixes #143 and avajs/ava#1023', { @@ -16,23 +16,23 @@ expectType( multiple: ['a', 'b'], number: 1, exclude: false, - include: true - } - }) + include: true, + }, + }), ); expectType( linkifyIssues('Fixes #143 and avajs/ava#1023', { user: 'sindresorhus', repository: 'dofle', - type: 'string' - }) + type: 'string', + }), ); const fragment = linkifyIssues('Fixes #143 and avajs/ava#1023', { user: 'sindresorhus', repository: 'dofle', - type: 'dom' + type: 'dom', }); expectType(fragment); -document.body.appendChild(fragment); +document.body.append(fragment); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 4ecae74..9e7d844 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Linkify GitHub issue references", "license": "MIT", "repository": "sindresorhus/linkify-issues", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "test": "xo && ava && tsd" @@ -34,14 +37,14 @@ "link" ], "dependencies": { - "create-html-element": "^2.1.0", - "issue-regex": "^3.0.0" + "create-html-element": "^4.0.1", + "issue-regex": "^4.0.0" }, "devDependencies": { - "ava": "^1.4.1", - "jsdom": "^14.0.0", - "tsd": "^0.7.2", - "xo": "^0.24.0" + "ava": "^3.15.0", + "jsdom": "^18.0.0", + "tsd": "^0.18.0", + "xo": "^0.45.0" }, "xo": { "envs": [ diff --git a/readme.md b/readme.md index afc2c7d..8533f5f 100644 --- a/readme.md +++ b/readme.md @@ -2,18 +2,16 @@ > Linkify GitHub issue references - ## Install -``` -$ npm install linkify-issues +```sh +npm install linkify-issues ``` - ## Usage ```js -const linkifyIssues = require('linkify-issues'); +import linkifyIssues from 'linkify-issues'; linkifyIssues('Fixes #143 and avajs/ava#1023', { user: 'sindresorhus', @@ -39,16 +37,15 @@ const fragment = linkifyUrls('See #143', { document.body.appendChild(fragment); ``` - ## API -### linkifyIssues(string, [options]) +### linkifyIssues(string, options) #### string Type: `string` -String with issue references to linkify. +A string with issue references to linkify. #### options @@ -56,12 +53,14 @@ Type: `object` ##### user +**Required**\ Type: `string` GitHub user. ##### repository +**Required**\ Type: `string` GitHub repository. @@ -74,30 +73,24 @@ HTML attributes to add to the link. ##### baseUrl -Type: `string`
+Type: `string`\ Default: `'https://github.com'` -Base URL. +The base URL. ##### type -Type: `string`
-Values: `'string'` `'dom'`
+Type: `string`\ +Values: `'string' | 'dom'`\ Default: `'string'` -Format of the generated content. +The format of the generated content. `'string'` will return it as a flat string like `'See #143'`. `'dom'` will return it as a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('See '), HTMLAnchorElement('#143'))`. This type only works in the browser. - ## Related - [issue-regex](https://github.com/sindresorhus/issue-regex) - Regular expression for matching issue references - [linkify-urls](https://github.com/sindresorhus/linkify-urls) - Linkify URLs in text - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index 6e48341..852a982 100644 --- a/test.js +++ b/test.js @@ -1,10 +1,10 @@ import test from 'ava'; import jsdom from 'jsdom'; -import linkifyIssues from '.'; +import linkifyIssues from './index.js'; const dom = new jsdom.JSDOM(); -global.window = dom.window; -global.document = dom.window.document; +globalThis.window = dom.window; +globalThis.document = dom.window.document; // Ponyfill until this is in: // https://github.com/tmpvar/jsdom/issues/317 @@ -13,7 +13,7 @@ document.createRange = () => ({ const element = document.createElement('template'); element.innerHTML = html; return element.content; - } + }, }); // Get DOM node from HTML @@ -29,7 +29,7 @@ const html = dom => { test('main', t => { t.is( linkifyIssues('Fixes #143 and avajs/ava#1023', {user: 'sindresorhus', repository: 'dofle'}), - 'Fixes #143 and avajs/ava#1023' + 'Fixes #143 and avajs/ava#1023', ); }); @@ -39,10 +39,10 @@ test('escapes user input', t => { user: '', repository: 'x', attributes: { - class: '' - } + class: '', + }, }), - 'See #1' + 'See #1', ); }); @@ -53,10 +53,10 @@ test('attributes option', t => { repository: 'x', attributes: { class: 'unicorn', - target: '_blank' - } + target: '_blank', + }, }), - 'See avajs/ava#1023' + 'See avajs/ava#1023', ); }); @@ -65,9 +65,9 @@ test('baseUrl option', t => { linkifyIssues('Fixes avajs/ava#1023', { user: 'x', repository: 'x', - baseUrl: '' + baseUrl: '', }), - 'Fixes avajs/ava#1023' + 'Fixes avajs/ava#1023', ); }); @@ -79,9 +79,9 @@ test('DocumentFragment support', t => { baseUrl: '', type: 'dom', attributes: { - class: '' - } + class: '', + }, })), - html(domify('See #1')) + html(domify('See #1')), ); });