Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Light weight react component for creating a table of contents from the given markdown text⚡⚡

License

NotificationsYou must be signed in to change notification settings

K-Sato1995/react-toc

Repository files navigation

react-toc

NPMnpm bundle sizenpmMIT

Overview

  • The idea is that you can automatically create a customizable table of contents from your markdown text.
  • It's regex based. Thus, managed to keep the bundle size pretty tiny.(Check it out onBUNDLEPHOBIA)

Image from Gyazo

Demo

Check out the demo page.

Installation

npm install --save react-toc

or

yarn add react-toc

Usage

Import Toc from the package and pass props to it. As for now,markdownText is the only required prop.

importReactfrom"react";importTocfrom"react-toc";constExample=()=>{constyourMarkdownText="# test \n your markdown Content # test2\n";return<TocmarkdownText={yourMarkdownText}/>;};exportdefaultExample;

Props

NameTypeDescription
markdownTextstringRequired The markdown text you want to creat a TOC from.
titleLimitnumberThe maximum length of each title in the TOC.
highestHeadingLevelnumberThe highest level of headings you want to extract from the given markdownText.
lowestHeadingLevelnumberThe lowest level of headings you want to extract from the given markdownText.
classNamestrigYour custom className.
type"deafult" or"raw"The type of a TOC you want to use.
customMatchers{ [key: string]: string }The matchers you want to use to replace the letters with.

CustomDesign

Add a custom className

PassclassName like the code below.

importReactfrom"react";importTocfrom"react-toc";constExample=()=>{constyourMarkdownText="# test \n your markdown Content # test2\n";return<TocmarkdownText={yourMarkdownText}className={"customClassName"}/>;};exportdefaultExample;

Style the custom class

Now you can style your custom class just like the code below.

.customClassName {border: solid1px;}.customClassName>li {padding-bottom:10px;}

Custom Matchers

You can use thecustomMatchers prop to replace letters in your toc.For instance, if you want to replace? or! with- in your list, you can simply do this.

importReactfrom"react";importTocfrom"react-toc";constExample=()=>{constyourMarkdownText="# test \n your markdown Content # test2\n";constmatchers={"[?!]":"-"}return<TocmarkdownText={yourMarkdownText}className={"customClassName"}customMatchers={matchers}/>;};exportdefaultExample;

You can also give more options to thecustomMatchers prop like the code below.

importReactfrom"react";importTocfrom"react-toc";constExample=()=>{constyourMarkdownText="# test \n your markdown Content # test2\n";constmatchers={"[?!]":"-","\\*":""}return<TocmarkdownText={yourMarkdownText}className={"customClassName"}customMatchers={matchers}/>;};exportdefaultExample;

Development

  • Install dev dependencies.
$ yarn install
  • Test
$ yarn test
  • Lint
$ yarn lint
  • Run demo locally
$ cd demo && yarn && yarn start

License

MIT

About

Light weight react component for creating a table of contents from the given markdown text⚡⚡

Topics

Resources

License

Stars

Watchers

Forks

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp