Skip to content

Commit 05402ff

Browse files
committed
feat(): add TikTok mentions and hashtags
1 parent 620f51c commit 05402ff

File tree

9 files changed

+39
-5
lines changed

9 files changed

+39
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ So, this utility attempts to handle everything. It:
1414
- Will properly handle URLs with query parameters or a named anchor (i.e. hash)
1515
- Will autolink email addresses.
1616
- Will autolink phone numbers.
17-
- Will autolink mentions (Twitter, Instagram, Soundcloud).
17+
- Will autolink mentions (Twitter, Instagram, Soundcloud, TikTok).
1818
- Will autolink hashtags.
1919
- Will properly handle HTML input. The utility will not change the `href`
2020
attribute inside anchor (<a>) tags (or any other tag/attribute),
@@ -161,12 +161,12 @@ These include:
161161

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

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

172172
- [stripPrefix](http://gregjacobs.github.io/Autolinker.js/api/#!/api/Autolinker-cfg-stripPrefix) : boolean<br />

docs/api/source/hashtag-match.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
return &#39;https://www.facebook.com/hashtag/&#39; + hashtag;
9797
case &#39;instagram&#39;:
9898
return &#39;https://instagram.com/explore/tags/&#39; + hashtag;
99+
case &#39;tiktok&#39;:
100+
return &#39;https://www.tiktok.com/tag/&#39; + hashtag;
99101
default: // Shouldn&#39;t happen because Autolinker&#39;s constructor should block any invalid values, but just in case.
100102
throw new Error(&#39;Unknown service name to point hashtag to: &#39; + serviceName);
101103
}

docs/api/source/mention-match.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
return &#39;https://instagram.com/&#39; + this.mention;
9494
case &#39;soundcloud&#39;:
9595
return &#39;https://soundcloud.com/&#39; + this.mention;
96+
case &#39;instagram&#39;:
97+
return &#39;https://www.tiktok.com/@&#39; + this.mention;
9698
default: // Shouldn&#39;t happen because Autolinker&#39;s constructor should block any invalid values, but just in case.
9799
throw new Error(&#39;Unknown service name to point mention to: &#39; + this.serviceName);
98100
}

src/autolinker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,10 @@ export interface TruncateConfigObj {
10281028
}
10291029

10301030
export type HashtagConfig = false | HashtagServices;
1031-
export type HashtagServices = 'twitter' | 'facebook' | 'instagram';
1031+
export type HashtagServices = 'twitter' | 'facebook' | 'instagram' | 'tiktok';
10321032

10331033
export type MentionConfig = false | MentionServices;
1034-
export type MentionServices = 'twitter' | 'instagram' | 'soundcloud';
1034+
export type MentionServices = 'twitter' | 'instagram' | 'soundcloud' | 'tiktok';
10351035

10361036
export type ReplaceFn = ( match: Match ) => ReplaceFnReturn;
10371037
export type ReplaceFnReturn = boolean | string | HtmlTag | null | undefined | void;

src/match/hashtag-match.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export class HashtagMatch extends Match {
8989
return 'https://www.facebook.com/hashtag/' + hashtag;
9090
case 'instagram' :
9191
return 'https://instagram.com/explore/tags/' + hashtag;
92+
case 'tiktok' :
93+
return 'https://www.tiktok.com/tag/' + hashtag;
9294

9395
default : // Shouldn't happen because Autolinker's constructor should block any invalid values, but just in case.
9496
throw new Error( 'Unknown service name to point hashtag to: ' + serviceName );

src/match/mention-match.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export class MentionMatch extends Match {
8585
return 'https://instagram.com/' + this.mention;
8686
case 'soundcloud' :
8787
return 'https://soundcloud.com/' + this.mention;
88+
case 'tiktok' :
89+
return 'https://www.tiktok.com/@' + this.mention;
8890

8991
default : // Shouldn't happen because Autolinker's constructor should block any invalid values, but just in case.
9092
throw new Error( 'Unknown service name to point mention to: ' + this.serviceName );

tests/autolinker-hashtag.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ describe( `Autolinker Hashtag Matching -`, () => {
66
const twitterAutolinker = new Autolinker( { hashtag: 'twitter', newWindow: false } );
77
const facebookAutolinker = new Autolinker( { hashtag: 'facebook', newWindow: false } );
88
const instagramAutolinker = new Autolinker( { hashtag: 'instagram', newWindow: false } );
9+
const tiktokAutolinker = new Autolinker( { hashtag: 'tiktok', newWindow: false } );
910

1011
const services = [
1112
{ serviceName: 'twitter', urlPrefix: 'https://twitter.com/hashtag', autolinker: twitterAutolinker },
1213
{ serviceName: 'instagram', urlPrefix: 'https://instagram.com/explore/tags', autolinker: instagramAutolinker },
1314
{ serviceName: 'facebook', urlPrefix: 'https://www.facebook.com/hashtag', autolinker: facebookAutolinker },
15+
{ serviceName: 'tiktok', urlPrefix: 'https://www.tiktok.com/tag', autolinker: tiktokAutolinker },
1416
];
1517

1618

tests/autolinker-mention.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ describe( "Autolinker Mention Matching -", () => {
55
const twitterAutolinker = new Autolinker( { mention: 'twitter', newWindow: false } )
66
const instagramAutolinker = new Autolinker( { mention: 'instagram', newWindow: false } );
77
const soundcloudAutolinker = new Autolinker( { mention: 'soundcloud', newWindow: false } );
8+
const tiktokAutolinker = new Autolinker( { mention: 'soundcloud', newWindow: false } );
89

910
const services = [
1011
{ serviceName: 'twitter', urlPrefix: 'https://twitter.com', autolinker: twitterAutolinker },
1112
{ serviceName: 'instagram', urlPrefix: 'https://instagram.com', autolinker: instagramAutolinker },
1213
{ serviceName: 'soundcloud', urlPrefix: 'https://soundcloud.com', autolinker: soundcloudAutolinker },
14+
{ serviceName: 'tiktok', urlPrefix: 'https://www.tiktok.com/@', autolinker: tiktokAutolinker },
1315
];
1416

1517
it( `should not autolink mentions by default`, () => {
@@ -215,6 +217,25 @@ describe( "Autolinker Mention Matching -", () => {
215217

216218
} );
217219

220+
describe( 'tiktok-specific tests', () => {
221+
222+
it( 'should link a tiktok mention that is up to 24 characters long', () => {
223+
const aUsername = _.repeat( 'a', 24 );
224+
const bUsername = _.repeat( 'b', 25 ); // too long - don't link
225+
226+
const result = tiktokAutolinker.link( `@${aUsername} and @${bUsername}` );
227+
expect( result ).toBe( `<a href="https://www.tiktok.com/@${aUsername}">@${aUsername}</a> and @${bUsername}` );
228+
} );
229+
230+
231+
it( `should link a tiktok mention that has a period in it`, () => {
232+
const result = tiktokAutolinker.link( `Hello @asdf.defg` );
233+
234+
expect( result ).toBe( `Hello <a href="https://www.tiktok.com/@asdf.defg">@asdf.defg</a>` );
235+
} );
236+
237+
} );
238+
218239

219240
it( `should NOT automatically link a username that is actually part of an
220241
email address when email address linking is turned on

tests/autolinker.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,9 @@ describe( "Autolinker", function() {
917917

918918
result = Autolinker.link( "hi from @iggypopschest", { newWindow: false, mention: 'instagram', className: 'myLink' } );
919919
expect( result ).toBe( 'hi from <a href="https://instagram.com/iggypopschest" class="myLink myLink-mention myLink-instagram">@iggypopschest</a>' );
920+
921+
result = Autolinker.link( "hi from @iggypopschest", { newWindow: false, mention: 'tiktok', className: 'myLink' } );
922+
expect( result ).toBe( 'hi from <a href="https://www.tiktok.com/@iggypopschest" class="myLink myLink-mention myLink-instagram">@iggypopschest</a>' );
920923
} );
921924

922925
} );

0 commit comments

Comments
 (0)