Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 17, 2021
1 parent 288d97d commit 640f2d8
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 111 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 36 additions & 40 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
/// <reference lib="dom"/>
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 <a href="https://github.com/sindresorhus/dofle/issue/143">#143</a>'`.
`'string'` will return it as a flat string like `'See <a href="https://github.com/sindresorhus/dofle/issue/143">#143</a>'`.
`'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',
Expand All @@ -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';
21 changes: 9 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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();
Expand All @@ -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)) {
Expand All @@ -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`');
}
20 changes: 10 additions & 10 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {expectType} from 'tsd';
import linkifyIssues = require('.');
import linkifyIssues from './index.js';

expectType<string>(
linkifyIssues('Fixes #143 and avajs/ava#1023', {
user: 'sindresorhus',
repository: 'dofle'
})
repository: 'dofle',
}),
);
expectType<string>(
linkifyIssues('Fixes #143 and avajs/ava#1023', {
Expand All @@ -16,23 +16,23 @@ expectType<string>(
multiple: ['a', 'b'],
number: 1,
exclude: false,
include: true
}
})
include: true,
},
}),
);
expectType<string>(
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<DocumentFragment>(fragment);
document.body.appendChild(fragment);
document.body.append(fragment);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (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:

Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]",
"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"
Expand All @@ -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": [
Expand Down
31 changes: 12 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -39,29 +37,30 @@ 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

Type: `object`

##### user

**Required**\
Type: `string`

GitHub user.

##### repository

**Required**\
Type: `string`

GitHub repository.
Expand All @@ -74,30 +73,24 @@ HTML attributes to add to the link.

##### baseUrl

Type: `string`<br>
Type: `string`\
Default: `'https://github.com'`

Base URL.
The base URL.

##### type

Type: `string`<br>
Values: `'string'` `'dom'`<br>
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 <a href="https://github.com/sindresorhus/dofle/issue/143">#143</a>'`.

`'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)
Loading

0 comments on commit 640f2d8

Please sign in to comment.