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

nonplain-md-link

v0.0.10

Published

Tools for markdown links in nonplain files

Downloads

12

Readme

nonplain-md-link

Tools for markdown links in nonplain files.

What this library does

Markdown files often contain markdown-style links, which look like this:

[link inner text](/path/to/some/file/or/site)

They can also sometimes contain wiki-style links when used for notetaking. Wiki-style links look like this:

[[/path/to/some/file/or/site]]

The concept: provide a tool for gathering and working with markdown and wiki-style links within nonplain files.

Once these links are gathered, they can be analyzed and selectively edited to accomplish various goals such as:

  • converting links to HTML
  • re-basing links to work on your personal website
  • listing all forward links in a file
  • calculating backlinks for a file

...and more.

API

Initialization

To parse a link, pass it as a string to the Link class upon initialization:

const Link = require("nonplain-md-link").default;

const markdownLink = new Link('[my markdown link](/path/to/file.md)');
const wikiLink = new Link('[[/path/to/file.md]]');

console.log(markdownLink);

// Output:
//
// {
//   "initialStyle": "markdown",
//   "innerText": "my markdown link",
//   "path": "/path/to/file.md",
// }

console.log(wikiLink);

// Output:
//
// {
//   "initialStyle": "wiki",
//   "innerText": "/path/to/file.md",
//   "path": "/path/to/file.md",
// }

Once a link is parsed, its contents can be used to construct new links.

Link.collectAllLinksFromContent()

Returns Link instances for all markdown and wiki-style links within a given string.

const Link = require("nonplain-md-link").default;
const { File } = require("nonplain");

const file = new File().load('/path/to/file.md');

const linksInFileBody = Link.collectAllLinksFromContent(file.body);

console.log(linksInFileBody);

// Output:
//
// [
//   {
//     "initialStyle": "markdown",
//     "innerText": "as discussed in class",
//     "path": "../notes/CS231n notes.md"
//   },
//   {
//     "initialStyle": "markdown",
//     "innerText": "descending the loss gradient",
//     "path": "https://en.wikipedia.org/wiki/Gradient_descent"
//   },
//   {
//     "initialStyle": "wiki",
//     "innerText": "How to backpropagate",
//     "path": "How to backpropagate"
//   }
// ]

Link.detectLinkStyle()

Returns the link style ('markdown' or 'wiki') of a provided string.

const markdownLink = new Link('[my markdown link](/path/to/file.md)');
const wikiLink = new Link('[[/path/to/file.md]]');

console.log(Link.detectLinkStyle(markdownLink));

// Output:
//
// 'markdown'

console.log(Link.detectLinkStyle(markdownLink));

// Output:
//
// 'wiki'

Link.prototype.composeHTML()

link.composeHTML([attributesStr])

Returns an HTML string based on the current Link instance. The optional argument attributesStr inserts custom attributes after the href attribute.

const link = new Link('[my markdown link](/path/to/file.md)');

console.log(link.composeHTML());

// Output:
//
// '<a href="/path/to/file.md">my markdown link</a>'

const externalLinkAttributes = 'target="_blank" rel="noreferrer"';
console.log(link.composeHTML(externalLinkAttributes));

// Output:
//
// '<a href="/path/to/file.md" target="_blank" rel="noreferrer">my markdown link</a>'

Link.prototype.composeMarkdown()

link.composeMarkdown()

Returns a markdown link based on the current Link instance.

const link = new Link('[my markdown link](/path/to/file.md)');

console.log(link.composeMarkdown());

// Output:
//
// '[my markdown link](/path/to/file.md)'

Link.prototype.composeWiki()

link.composeWiki()

Returns a wiki-style link based on the current Link instance.

const link = new Link('[my markdown link](/path/to/file.md)');

console.log(link.composeWiki());

// Output:
//
// '[[/path/to/file.md]]'

Regex

This library exports an object containing useful regex patterns for extracting markdown and wiki-style links from string content. To use these patterns, import regex from this package:

const { regex } = require("nonplain-md-link");

Related work

Contributing

These tools are in progress. If your use-case requires specific functionality from this library, please contact me and we'll see what we can do together. Thanks!