Code Monkey home page Code Monkey logo

remark-reference-links's Introduction

remark-reference-links

Build Coverage Downloads Size Sponsors Backers Chat

remark plugin to change links and images to references with separate definitions.

Contents

What is this?

This package is a unified (remark) plugin to turn links ([text](url)) and images (![alt](url)) into references ([text][id], ![alt][id]) and definitions ([id]: url).

When should I use this?

This project is useful when you want to transform markdown and prefer that it uses references and definitions. Long URLs in source code can make reading markdown difficult. References and definitions improve that by moving those URLs into definitions, outside of paragraphs.

This plugin is very similar to the alternative remark-defsplit. The difference is that that plugin generates identifiers based on hostnames of URLs and places definitions at the end of each section, whereas this plugin generates numeric identifiers at the end of the document.

A different plugin, remark-inline-links, does the inverse: turn references and definitions into links and images.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install remark-reference-links

In Deno with esm.sh:

import remarkReferenceLinks from 'https://esm.sh/remark-reference-links@7'

In browsers with esm.sh:

<script type="module">
  import remarkReferenceLinks from 'https://esm.sh/remark-reference-links@7?bundle'
</script>

Use

Say we have the following file example.md:

# Pluto

[![Build](https://github.com/solar-system/pluto/workflows/main/badge.svg)](https://github.com/solar-system/pluto/actions)

## History

In the 1840s,
[Urbain Le Verrier](https://wikipedia.org/wiki/Urbain_Le_Verrier) used
Newtonian mechanics to predict the position of the then-undiscovered planet
[Neptune](https://wikipedia.org/wiki/Neptune) after analyzing perturbations
in the orbit of [Uranus](https://wikipedia.org/wiki/Uranus).

And our module example.js looks as follows:

import {remark} from 'remark'
import remarkReferenceLinks from 'remark-reference-links'
import {read} from 'to-vfile'

const file = await remark()
  .use(remarkReferenceLinks)
  .process(await read('example.md'))

console.log(String(file))

…then running node example.js yields:

# Pluto

[![Build][2]][1]

## History

In the 1840s,
[Urbain Le Verrier][3] used
Newtonian mechanics to predict the position of the then-undiscovered planet
[Neptune][4] after analyzing perturbations
in the orbit of [Uranus][5].

[1]: https://github.com/solar-system/pluto/actions

[2]: https://github.com/solar-system/pluto/workflows/main/badge.svg

[3]: https://wikipedia.org/wiki/Urbain_Le_Verrier

[4]: https://wikipedia.org/wiki/Neptune

[5]: https://wikipedia.org/wiki/Uranus

👉 Note: observe that definitions are added at the end of the document and that IDs are numeric identifiers.

API

This package exports no identifiers. The default export is remarkReferenceLinks.

unified().use(remarkReferenceLinks)

Change links and images to references with separate definitions.

Parameters

There are no parameters.

Returns

Transform (Transformer).

Types

This package is fully typed with TypeScript. It exports no additional options.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, remark-reference-links@^7, compatible with Node.js 16.

This plugin works with unified version 3+ and remark version 4+.

Security

Use of remark-reference-links does not involve rehype (hast) or user content so there are no openings for cross-site scripting (XSS) attacks.

Related

  • remark-defsplit — transform links and images into references and definitions with numeric IDs
  • remark-inline-links — transform references and definitions into normal links and images

Contribute

See contributing.md in remarkjs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer

remark-reference-links's People

Contributors

anko avatar greenkeeperio-bot avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

remark-reference-links's Issues

Removes duplicate links with different alt text

package.json:

{
  "name": "reference-links-test-case",
  "private": true,
  "scripts": {
    "test": "remark input.md -o output.md"
  },
  "dependencies": {
    "remark-cli": "^3.0.0",
    "remark-reference-links": "^3.0.1"
  },
  "remarkConfig": {
    "plugins": {
      "remark-reference-links": {}
    }
  }
}

input.md:

This is a link to [github](https://github.com "GitHub")

This is another link to [github](https://github.com "But it has different alt text")

Creates the following output.md:

This is a link to [github][1]

This is another link to [github][1]

[1]: https://github.com "GitHub"

It would be nice if the second alt text was preserved; using remark with https://github.com/jfmengels/all-contributors-cli.

Superfluous newlines added to Markdown references.

Superfluous newlines added to Markdown references.

Thanks for the great work on this plugin! Perhaps this is a bug or just correct behavior I wasn't expecting. When remark-reference-links is used to convert inline links to references, it adds extra newline character after every reference.

Your environment

  • OS: OSX 10.15.4
  • Packages: {"remark": "12.0.0", "remark-inline-links": "4.0.0", "remark-parse": "8.0.2", "unified": "9.0.0"}
  • Env: node v14.1.0

Steps to reproduce

Here's a minimal working example:

const remark = require('remark')
const refLinks = require('remark-reference-links')

const mdInline = "The quick brown [fox](http://en.wikipedia.org/wiki/Fox) jumped over the lazy [dog](http://en.wikipedia.org/wiki/Dog)."

remark()
  .use(refLinks)
  .process(mdInline, (err, d) => { console.log(d.contents) })

The output of the code above looks like this:

'The quick brown [fox][1] jumped over the lazy [dog][2].\n' +
'\n' +
'[1]: http://en.wikipedia.org/wiki/Fox\n' +
'\n' +
'[2]: http://en.wikipedia.org/wiki/Dog\n'

Expected behaviour

The expected behaviour is no newline characters after the final reference and no newline characters between reference lines. Here's what I would have expected:

'The quick brown [fox][1] jumped over the lazy [dog][2].\n' +
'\n' +
'[1]: http://en.wikipedia.org/wiki/Fox\n' +
'[2]: http://en.wikipedia.org/wiki/Dog'

Actual behaviour

'The quick brown [fox][1] jumped over the lazy [dog][2].\n' +
'\n' +
'[1]: http://en.wikipedia.org/wiki/Fox\n' +
'\n' +
'[2]: http://en.wikipedia.org/wiki/Dog\n'

Automatically create collapsed/implicit/shortcut reference links

Subject of the feature

If it's possible, I'd like to automatically create link references in the following style:

See [spot] run.
<!-- And/or ... -->
See [dog][] jump.

[spot]: https...
[dog]: https...

In the CommonMark spec I see this syntax referred to as shortcut reference link — if there is no [] — and collapsed reference link — if there is a []. In the canonical Markdown description, I don't see any equivalent of the shortcut reference link, but the collapsed reference link is called "implicit name link shortcut."

I don't think I can do this with either remark-reference-links or remark-def-split. So this is a feature idea.

I am worried this can't be implemented, though, because the link label (spot and dog above) can include any static phrasing content but I don't see a way for the mdast Definition node to store and render a complex label (or identifier). Here's an example I'm failing to figure out how to handle:

See [your **favorite** dog] run.

[your **favorite** dog]: https...

Do you think it's possible with the existing AST to perform a full transformation of all links to shortcuts or collapsed reference links? Or are we limited by the flexibility of label content, and could only transform links with simple text labels?

Broken on multiple pass throughs.

Hi!

It seems when I use this plugin in multiple passes then it breaks any new link additions. I've attached a small test case:

package.json:

{
  "name": "reference-links-test-case",
  "private": true,
  "scripts": {
    "test": "remark input.md -o output.md"
  },
  "dependencies": {
    "remark": "^7.0.0",
    "remark-cli": "^3.0.0",
    "remark-reference-links": "^3.0.1"
  },
  "remarkConfig": {
    "plugins": {
      "remark-reference-links": {}
    }
  }
}

input.md:

# Hello!

This is a [link][1] to [github](https://github.com/ben-eb).

[1]: https://github.com

When it is run, the following output.md is created:

# Hello!

This is a [link][1] to [github][1].

[1]: https://github.com

[1]: https://github.com/ben-eb

So I think this module needs some functionality to detect existing references. 😄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.