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

@gerhobbelt/markdown-it-link-attributes

v3.0.0-1

Published

A markdown-it plugin to configure the attributes for links

Downloads

349

Readme

markdown-it-link-attributes

Build Status NPM version Coverage Status

Link attributes plugin for markdown-it markdown parser.

Forked version

This is a forked version of markdown-it-link-attributes that allows for custom attributes to be added to individual links.

[standard link](https://github.com)

[link with custom attributes](https://github.com|aria-describedBy=some-id|hidden)

Result:

<p><a href="https://github.com">standard link</a></p>

<p><a href="https://github.com" aria-describedBy="some-id" hidden="true">link with custom attributes</a></p>

These transformations are applied before any changes applied by the original plugin.

Install

node.js, browser:

npm install markdown-it-link-attributes --save
bower install markdown-it-link-attributes --save

Use

Basic Configuration

You can pass an object with an attrs property. Each link parsed with this config will have the passed attributes.

var md = require('markdown-it')()
var mila = require('markdown-it-link-attributes')

md.use(mila, {
  attrs: {
    target: '_blank',
    rel: 'noopener'
  }
})

var result = md.render('[Example](https://example.com')

result // <a href="https://example.com" target="_blank" rel="noopener">Example</a>

If the linkify option is set to true on markdown-it, then the attributes will be applied to plain links as well.

var md = require('markdown-it')({
  linkify: true
})

md.use(mila, {
  target: '_blank',
  rel: 'noopener'
})

var html = md.render('foo https://google.com bar')
html // <p>foo <a href="https://google.com" target="_blank" rel="noopener">https://google.com</a> bar</p>

Pattern

You can also specify a pattern property. The link's href property will be checked against the pattern RegExp provided and only apply the attributes if it matches the pattern.

md.use(mila, {
  pattern: /^https:/,
  attrs: {
    target: '_blank',
    rel: 'noopener'
  }
})

var matchingResult = md.render('[Matching Example](https://example.com')
var ignoredResult = md.render('[Not Matching Example](http://example.com')

matchingResult // <a href="https://example.com" target="_blank" rel="noopener">Matching Example</a>
ignoredResult // <a href="http://example.com">Not Matching Example</a>

Applying classes

You can either apply a class to a link by using a class or a className property. Either one will work, but use only one, not both.

md.use(mila, {
  attrs: {
    class: 'my-class'
  }
})

// or
md.use(mila, {
  attrs: {
    className: 'my-class'
  }
})

Multiple Configurations

Alternatively, you can pass an Array of configurations. The first pattern to match will be applied to the link.

md.use(mila, [{
  pattern: /^https?:\/\//,
  attrs: {
    class: 'external-link'
  }
}, {
  pattern: /^\//,
  attrs: {
    class: 'absolute-link'
  }
}, {
  pattern: /blue/,
  attrs: {
    class: 'link-that-contains-the-word-blue'
  }
}])

var externalResult = md.render('[external](https://example.com')
var absoluteResult = md.render('[absolute](/some-page')
var blueResult = md.render('[blue](relative/link/with/blue/in/the/name')

externalResult // <a href="https://example.com" class="external-link">external</a>
absoluteResult // <a href="/some-page" class="absolute-link">absolute</a>
blueResult // <a href="relative/link/with/blue/in/the/name" class="link-that-contains-the-word-blue">blue</a>

If multiple patterns match, the first configuration to match will be used.

// This matches both the "starts with http or https" rule and the "contains the word blue" rule.
// Since the http/https rule was defined first, that is the configuration that is used.
var result = md.render('[external](https://example.com/blue')

result // <a href="https://example.com/blue" class="external-link">external</a>

Usage in the browser

Differences in browser. If you load script directly into the page, without a package system, the module will add itself globally as window.markdownitLinkAttributes. You need to load dist/markdown-it-link-attributes.min.js, if you don't use a build system.

Testing

This plugin is tested against markdown-it @ 9, 10 and latest

License

MIT