Skip to content

Commit 48bec8f

Browse files
committed
Add youtube mentions
1 parent 90d908c commit 48bec8f

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

README.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Autolinker.js
22

33
Because I had so much trouble finding a good auto-linking implementation out in
4-
the wild, I decided to roll my own. It seemed that everything I found out there
4+
the wild, I decided to roll my own. It seemed that everything I found out there
55
was either an implementation that didn't cover every case, or was just limited
66
in one way or another.
77

@@ -27,27 +27,34 @@ Full API Docs: [http://gregjacobs.github.io/Autolinker.js/api/](http://gregjacob
2727
Live Example: [http://gregjacobs.github.io/Autolinker.js/examples/live-example/](http://gregjacobs.github.io/Autolinker.js/examples/live-example/)
2828

2929

30-
## v4.0 released September 2022
30+
## Upgrading from a previous major version of Autolinker?
3131

32-
See [Upgrading from v3.x -> v4.x (Breaking Changes)](#upgrading-from-v3x---v4x-breaking-changes) at the bottom of this readme.
32+
See [Breaking Changes](#upgrading-from-v3x---v4x-breaking-changes) at the bottom of this readme.
3333

3434
## Installation
3535

36-
#### Installing with the [npm](https://www.npmjs.org/) package manager:
36+
#### Installing with [npm](https://www.npmjs.org/):
3737

3838
```shell
3939
npm install autolinker --save
4040
```
4141

4242

43-
#### Installing with the [Yarn](https://yarnpkg.com/) package manager:
43+
#### Installing with [Yarn](https://yarnpkg.com/):
4444

4545
```shell
4646
yarn add autolinker
4747
```
4848

4949

50-
#### Installing with the [Bower](http://bower.io) package manager:
50+
#### Installing with [pnpm](https://pnpm.io/):
51+
52+
```shell
53+
pnpm add autolinker
54+
```
55+
56+
57+
#### Installing with [Bower](http://bower.io):
5158

5259
```shell
5360
bower install Autolinker.js --save
@@ -57,18 +64,18 @@ bower install Autolinker.js --save
5764
#### Direct download
5865

5966
Simply clone this repository or download a zip of the project, and link to
60-
either `dist/Autolinker.js` or `dist/Autolinker.min.js` with a script tag.
67+
either `dist/Autolinker.js` or `dist/Autolinker.min.js` with a `<script>` tag.
6168

6269

6370
## Importing Autolinker
6471

65-
#### ES6/TypeScript/Webpack:
72+
#### ES6+/TypeScript/Webpack/Node.js ESM:
6673

6774
```ts
6875
import Autolinker from 'autolinker';
6976
```
7077

71-
#### Node.js:
78+
#### Node.js CommonJS:
7279

7380
```javascript
7481
const Autolinker = require('autolinker');
@@ -159,14 +166,10 @@ These include:
159166
phone numbers. Defaults to `true`.
160167

161168
- [mention](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-mention) : string<br />
162-
A string for the service name to have mentions (@username) auto-linked to. Supported
163-
values at this time are 'twitter', 'soundcloud', 'instagram' and 'tiktok'. Pass `false` to skip
164-
auto-linking of mentions. Defaults to `false`.
169+
A string for the service name to have mentions (@username) auto-linked to. Supported values at this time are 'twitter', 'soundcloud', 'instagram', 'tiktok', and 'youtube'. Pass `false` to skip auto-linking of mentions. Defaults to `false`.
165170

166171
- [hashtag](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-hashtag) : boolean/string<br />
167-
A string for the service name to have hashtags auto-linked to. Supported
168-
values at this time are 'twitter', 'facebook', 'instagram' and 'tiktok'. Pass `false` to skip
169-
auto-linking of hashtags. Defaults to `false`.
172+
A string for the service name to have hashtags auto-linked to. Supported values at this time are 'twitter', 'facebook', 'instagram', 'tiktok', and 'youtube'. Pass `false` to skip auto-linking of hashtags. Defaults to `false`.
170173

171174
- [stripPrefix](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-stripPrefix) : boolean<br />
172175
`true` to have the `'http://'` (or `'https://'`) and/or the `'www.'`

live-example/src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $(document).ready(function () {
6060
mentionOption = new RadioOption({
6161
name: 'mention',
6262
description: 'Mentions',
63-
options: [false, 'twitter', 'instagram', 'soundcloud', 'tiktok'],
63+
options: [false, 'twitter', 'instagram', 'soundcloud', 'tiktok', 'youtube'],
6464
defaultValue: false,
6565
}).onChange(autolink);
6666

src/autolinker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ export default class Autolinker {
282282
* - 'instagram'
283283
* - 'soundcloud'
284284
* - 'tiktok'
285+
* - 'youtube'
285286
*
286287
* Defaults to `false` to skip auto-linking of mentions.
287288
*/

src/match/mention-match.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MentionService } from '../parser/mention-utils';
2+
import { assertNever } from '../utils';
23
import { AbstractMatch, AbstractMatchConfig } from './abstract-match';
34

45
/**
@@ -91,9 +92,12 @@ export class MentionMatch extends AbstractMatch {
9192
return 'https://soundcloud.com/' + this.mention;
9293
case 'tiktok':
9394
return 'https://www.tiktok.com/@' + this.mention;
95+
case 'youtube':
96+
return 'https://youtube.com/@' + this.mention;
9497

9598
default:
9699
// Shouldn't happen because Autolinker's constructor should block any invalid values, but just in case.
100+
assertNever(this.serviceName);
97101
throw new Error('Unknown service name to point mention to: ' + this.serviceName);
98102
}
99103
}

src/parser/mention-utils.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const mentionRegexes: { [serviceName in MentionService]: RegExp } = {
66
// TikTok usernames are 1-24 characters containing letters, numbers, underscores
77
// and periods, but cannot end in a period: https://support.tiktok.com/en/getting-started/setting-up-your-profile/changing-your-username
88
tiktok: /^@[.\w]{1,23}[\w]$/,
9+
10+
// Youtube usernames are 3-30 characters containing letters, numbers, underscores,
11+
// dashes, or latin middle dots ('·').
12+
// https://support.google.com/youtube/answer/11585688?hl=en&co=GENIE.Platform%3DAndroid#tns
13+
youtube: /^@[-.·\w]{3,30}$/,
914
};
1015

1116
// Regex that allows for all possible mention characters for any service. We'll
@@ -29,5 +34,11 @@ export function isValidMention(mention: string, serviceName: MentionService): bo
2934
return re.test(mention);
3035
}
3136

32-
export type MentionService = 'twitter' | 'instagram' | 'soundcloud' | 'tiktok';
33-
export const mentionServices: MentionService[] = ['twitter', 'instagram', 'soundcloud', 'tiktok'];
37+
export type MentionService = 'twitter' | 'instagram' | 'soundcloud' | 'tiktok' | 'youtube';
38+
export const mentionServices: MentionService[] = [
39+
'twitter',
40+
'instagram',
41+
'soundcloud',
42+
'tiktok',
43+
'youtube',
44+
];

tests/autolinker-mention.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ describe('Autolinker Mention Matching >', () => {
2020
mention: 'tiktok',
2121
newWindow: false,
2222
});
23+
const youtubeAutolinker = new Autolinker({
24+
mention: 'youtube',
25+
newWindow: false,
26+
});
2327

2428
const services: MentionTestService[] = [
2529
{
@@ -42,6 +46,11 @@ describe('Autolinker Mention Matching >', () => {
4246
urlPrefix: 'https://www.tiktok.com/@',
4347
autolinker: tiktokAutolinker,
4448
},
49+
{
50+
serviceName: 'youtube',
51+
urlPrefix: 'https://youtube.com/@',
52+
autolinker: youtubeAutolinker,
53+
},
4554
];
4655

4756
interface MentionTestService {

0 commit comments

Comments
 (0)