Autolinker.js
Autolinker.js is a robust JavaScript utility designed to automatically detect and convert various patterns like URLs, email addresses, phone numbers, and social media mentions/hashtags (Twitter, Instagram, YouTube, TikTok, Facebook, Soundcloud) within plain text or HTML into clickable hyperlinks. The current stable version is `v4.1.5`. The library maintains an active release cadence, frequently publishing minor and patch updates, often focusing on performance enhancements and bug fixes. A key differentiator is its rewritten URL parser in `v4.0.0`, which utilizes a finite state machine for `O(n)` linear time performance, significantly improving speed and preventing 'catastrophic backtracking' issues common with regular expression-based parsers. Unlike many alternatives, Autolinker.js is meticulously designed to correctly handle HTML input, avoiding the creation of nested anchor tags or inadvertently modifying existing `href` attributes, making it particularly suitable for processing rich text content where correctness and performance are critical.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported
cause Attempting to `require()` Autolinker in a CommonJS environment while the package is treated as an ES Module, or vice versa, due to Node.js module resolution issues.fixFor CommonJS, use `const Autolinker = require('autolinker');`. For ESM, use `import Autolinker from 'autolinker';`. Ensure your project's `package.json` `"type"` field and `tsconfig.json` `module` and `moduleResolution` options are correctly configured for your environment (e.g., `"type": "module"` for ESM, `"type": "commonjs"` for CJS). -
TypeError: Autolinker is not a constructor
cause Trying to instantiate `Autolinker` as a class (`new Autolinker()`) but it was imported incorrectly, often due to using a named import `import { Autolinker } from 'autolinker';` instead of the default import.fixChange the import statement to `import Autolinker from 'autolinker';` for ESM/TypeScript environments. For CommonJS, ensure `const Autolinker = require('autolinker');` is used. -
Uncaught SyntaxError: Cannot use import statement outside a module
cause Using `import Autolinker from 'autolinker';` in a non-ES module browser environment or a Node.js script not configured as an ES module.fixFor browsers, include the `Autolinker.min.js` script directly via a `<script>` tag, which exposes `Autolinker` globally. For Node.js, ensure your `package.json` contains `"type": "module"` or use a `.mjs` file extension, or revert to CommonJS `require()` syntax.
Warnings
- breaking Autolinker v4.0.0 introduced significant breaking changes, primarily a complete rewrite of the URL parser from regular expressions to a finite state machine. This change, while greatly improving performance and preventing ReDoS attacks, removes the ability to override regular expressions in `Matcher` classes. The `urls.wwwMatches` config was also removed, and `Match.getType()` replaced `Match.type`.
- gotcha When migrating from older versions or other autolinking libraries, be aware that Autolinker.js explicitly handles HTML input to prevent common issues like double-nested anchor tags or clobbering existing `href` attributes. Directly manipulating innerHTML or using other string replacement methods alongside Autolinker may lead to unexpected results.
- deprecated The codebase was converted to TypeScript and now uses ES6 exports. Direct overriding of regular expressions by assigning to `Matcher.prototype` is no longer supported since v2, which may affect highly customized implementations.
- gotcha Autolinker v3.16.2 reverted an attempted fix for Right-to-Left Override (RTLO) characters splitting links, meaning the library may not fully handle edge cases involving these characters and could result in improperly split links.
Install
-
npm install autolinker -
yarn add autolinker -
pnpm add autolinker
Imports
- Autolinker
import { Autolinker } from 'autolinker';import Autolinker from 'autolinker';
- Autolinker (CommonJS)
const autolinker = require('autolinker');const Autolinker = require('autolinker'); - Match
import { Match } from 'autolinker';import Autolinker, { Match } from 'autolinker';
Quickstart
import Autolinker from 'autolinker';
const inputText: string = "Check out my website at example.com, email me at test@example.org, or call me at +1 (123) 456-7890. Also, find me on Twitter @myhandle and hashtag #myproject.";
const autolinker = new Autolinker({
urls: { scheme: true, www: true, ipV4Matches: true },
email: true,
phone: true,
mention: 'twitter',
hashtag: 'twitter',
newWindow: true,
stripPrefix: false,
// Custom replace function to add specific classes or attributes
replaceFn: (match) => {
const tag = match.buildTag();
if (match.getType() === 'url') {
tag.setAttr('class', 'my-url-link');
}
return tag;
}
});
const linkedText: string = autolinker.link(inputText);
console.log(linkedText);
// Expected output: Check out my website at <a href="http://example.com" class="my-url-link" target="_blank" rel="noopener noreferrer">example.com</a>, email me at <a href="mailto:test@example.org" target="_blank" rel="noopener noreferrer">test@example.org</a>, or call me at <a href="tel:+11234567890" target="_blank" rel="noopener noreferrer">+1 (123) 456-7890</a>. Also, find me on Twitter <a href="https://twitter.com/myhandle" target="_blank" rel="noopener noreferrer">@myhandle</a> and hashtag <a href="https://twitter.com/hashtag/myproject" target="_blank" rel="noopener noreferrer">#myproject</a>.