GithubHelp home page GithubHelp logo

remark-link-rewrite's Introduction

Remark Link Rewrite Plugin

The Remark Link Rewrite Plugin is a utility for the Remark processor that enables you to customize link URLs dynamically. You can use it to change the domain of links, modify file extensions, or apply any other transformation required by your specific use case.

Requirements

This package uses ECMAScript modules (ESM) and requires Node.js versions 12.20+, 14.14+, or 16.0+.

Installation

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install remark-link-rewrite

Basic Usage

Here's a simple example of how to use the plugin to replace links that start with https://example.com to https://example.org.

import { remark } from 'remark';
import RemarkLinkRewrite from 'remark-link-rewrite';

remark()
  .use(RemarkLinkRewrite, {
    replacer: (url) => {
      if (url.startsWith('https://example.com')) {
        return url.replace('https://example.com', 'https://example.org')
      }
      return url
    }
  })
  .process('[Example](https://example.com)')
  .then(({ value }) => {
    console.log(String(value))
  });

Usage with Asynchronous Replacer

You can also use the plugin with an asynchronous replacer function. Here's an example where the replacer fetches a new URL from an API or a database.

import { remark } from 'remark';
import RemarkLinkRewrite from 'remark-link-rewrite';

// example async function to fetch a URL from an API or database
async function fetchFromAPI(url) {
  return 'https://example.org'

  // example:
  // const response = await fetch('/my-api', {
  //   method: 'POST',
  //   body: JSON.stringify({ url }),
  //   headers: {
  //     'Content-Type': 'application/json'
  //   }
  // })
  // return response.url
}

remark()
  .use(RemarkLinkRewrite, {
    replacer: async (url) => {
      if (url.startsWith('https://example.com')) {
        const resolvedURL = await fetchFromAPI(url);
        return url.replace('https://example.com', resolvedURL);
      }
      return url
    }
  })
  .process('[Example](https://example.com)')
  .then(({ value }) => {
    console.log(String(value))
  });

Options

  • replacer: A function that receives a URL and returns a new URL. It gets called for every link in the document. This function can be synchronous or asynchronous.

License

MIT License

remark-link-rewrite's People

Contributors

rjanjic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

robinboers

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.