Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Latest commit

 

History

History

remark-truncate-links

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

remark-truncate-links

shortens plain links in markdown you haven't wrapped in []()

Actually it'll shorten [auto] linked URLs where the URL and the text are equal as in [example.com](example.com)
So this plugin doesn't work if you want to show a full URL longer than length. Written in Typescript.

Examples

# input
https://github.com/GaiAma/Coding4GaiAma/tree/master/packages/remark-truncate-links
# smart
github.com/GaiAma/C…te-links
# middle
https://github…runcate-links
# end
https://github.com/GaiAma/C…

Install

yarn add -D remark-truncate-links
# or
npm i -D remark-truncate-links

Options

{
  style: `smart`, // one of [`smart`, `middle`, `end`]
  length: 30,
}
// showing default options

Usage

In gatsby-plugin-mdx

// default options
{
  resolve: `gatsby-plugin-mdx`,
  options: {
    remarkPlugins: [
      require('remark-truncate-links').remarkTruncateLinks,
    ],
  },
}

// providing options
{
  resolve: `gatsby-plugin-mdx`,
  options: {
    remarkPlugins: [
      [
        require('remark-truncate-links').remarkTruncateLinks,
        { style: 'middle' },
      ],
    ],
  },
}

Using Unified

import unified from 'unified'
import remarkParse from 'remark-parse'
import stringify from 'remark-stringify'
import { remarkTruncateLinks } from 'remark-truncate-links'

// with default options
unified()
  .use(remarkParse)
  .use(remarkTruncateLinks)

// providing options
unified()
  .use(remarkParse)
  .use(remarkTruncateLinks, { style: `middle`, length: 40 })

As MDX plugin

import mdx from '@mdx-js/mdx'
import { remarkTruncateLinks } from 'remark-truncate-links'
const options = { length: 60 }

mdx(`mdxString`, { remarkPlugins: [[remarkTruncateLinks, options]] })

Ideas

Used in

Credits

Mentioned in

License

MIT