npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

my-autolink

v0.2.0

Published

My customable autolinker for me.

Downloads

19

Readme

my-autolink

my-autolink is my autolinker for me.

Install

npm install my-autolink

TypeScript support

TypeScript declaration file (.d.ts) is provided. TypeScript will automatically find typing information for this module. TypeScript 3.0 or later is required.

Sample

const autolink = require('my-autolink').autolink;

console.log(autolink('Go to http://example.net/ now!'));
// -> "Go to <a href='http://example.net/'>http://example.net/</a> now!"

The result is escaped by escape-html.

API

autolink(text[, transforms][,options])

Returns text with a tags added.

  • transforms: an array of a string or a Transform Object explained below. Defaults to ["url"], where "url" means the built-in transform that turns a url into a link
  • options: options passed to built-in transforms.
    • url: passed to "url" transform.
      • requireSchemes: If set to false, urls that start with hostname is converted to link. (example: example.net/foo/bar to <a href='http://example.net/foo/bar'>example.net/foo/bar</a>) Defaults to true.
      • schemes: Array of schemes allowed. Default value is ["http","https"]. Set to the string "*" to accept any scheme.
      • attributes: Object specifying the attributes of generated a elements.
      • text: Function which takes a URL and returns a text for link.

compile(transforms, options)

Returns a reusable precompiled option object which can be passed to the autolink function. RegExp object is cached in it for speedup.

const { autolink, compile } = require('my-autolink');

const options = compile(['url'], {
  url: {
    attributes: {
      target: '_blank',
    },
  },
});

console.log(autolink('Go to http://example.net/ now!', options));
// -> "Go to <a target='_blank' href='http://example.net/'>http://example.net/</a> now!"

Transform Object

Custom autolinks can be defined by Transform Object, which consist of two methods:

  • pattern: receives the object options and returns RegExp.
  • transform: receives options and subsequent arguments provided by RegExp#exec and returns object that represents attributes of the a element. This object may have the extra field text to represent the text of link.

Note that RegExps returned by pattern must have its global flag set.

Examples

The simplest example:

{
  pattern: function(){ return /article\/(\d+)/g; },
  transform: function(_, text, num){
    return {
      href: "/path/to/article/"+num
    }
  }
}

This Transform Object converts the text article/1234 into <a href='/path/to/article/1234'>article/1234</a>.

Changelog

  • 0.2.0:
    • Add the compile function.
    • Refined type definition.
      • Breaking change: TypeScript 3.0 or later is now required.
    • Updated ES version.
      • Breaking change: support for Symbol and Object.assign is now required.
  • 0.1.0: Add text option to url transform.
  • 0.0.3: Update dependencies. Reform internal structure.
  • 0.0.2: Add attributes option to url transform.
  • 0.0.1

License

MIT