1.2.0 • Published 1 year ago

@sak1sham/react-toc-highlight v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-toc

NPM npm bundle size npm MIT

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 on BUNDLEPHOBIA)

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.

import React from "react";
import Toc from "react-toc";

const Example = () => {
  const yourMarkdownText = "# test \n your markdown Content # test2\n";
  return <Toc markdownText={yourMarkdownText} />;
};

export default Example;

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

Pass className like the code below.

import React from "react";
import Toc from "react-toc";

const Example = () => {
  const yourMarkdownText = "# test \n your markdown Content # test2\n";
  return <Toc markdownText={yourMarkdownText} className={"customClassName"} />;
};

export default Example;

Style the custom class

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

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

Custom Matchers

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

import React from "react";
import Toc from "react-toc";

const Example = () => {
  const yourMarkdownText = "# test \n your markdown Content # test2\n";
  const matchers = { "[?!]": "-" }

  return <Toc markdownText={yourMarkdownText} className={"customClassName"} customMatchers={matchers}/>;
};

export default Example;

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

import React from "react";
import Toc from "react-toc";

const Example = () => {
  const yourMarkdownText = "# test \n your markdown Content # test2\n";
  const matchers = { "[?!]": "-", "\\*": "" }

  return <Toc markdownText={yourMarkdownText} className={"customClassName"} customMatchers={matchers}/>;
};

export default Example;

Development

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

License

MIT

acornacorn-jsxajvalphanum-sortansi-escapesansi-regexansi-stylesanymatchargparsearray-includesarray-unionarray-uniqarray.prototype.flatmaparray.prototype.reduceasyncbabel-jestbabel-plugin-dynamic-import-nodebabel-plugin-istanbulbabel-plugin-jest-hoistbabel-plugin-polyfill-corejs2babel-plugin-polyfill-corejs3babel-plugin-polyfill-regeneratorbabel-preset-current-node-syntaxbabel-preset-jestbalanced-matchbig.jsboolbasebrace-expansionbracesbrowserslistbs-loggerbserbuffer-frombuiltin-modulescall-bindcaller-callsitecaller-pathcallsitescamelcasecaniuse-apicaniuse-litechalkchar-regexci-infocjs-module-lexercliuicocoacollect-v8-coveragecolorcolor-convertcolor-namecolor-stringcommandercommondirconcat-mapconcat-with-sourcemapsconvert-source-mapcore-jscore-js-compatcosmiconfigcross-spawncss-color-namescss-declaration-sortercss-modules-loader-corecss-selectcss-select-base-adaptercss-selector-tokenizercss-treecss-whatcssesccssnanocssnano-preset-defaultcssnano-util-get-argumentscssnano-util-get-matchcssnano-util-raw-cachecssnano-util-same-parentcssocsstypedebugdedentdeep-isdeepmergedefine-propertiesdetect-newlinediff-sequencesdir-globdoctrinedom-serializerdomelementtypedomhandlerdomutilsdot-propelectron-to-chromiumemail-addressesemitteryemoji-regexemojis-listentitieserror-exes-abstractes-array-method-boxes-properlyes-shim-unscopableses-to-primitiveescaladeescape-string-regexpeslint-scopeeslint-utilseslint-visitor-keysespreeesprimaesqueryesrecurseestraverseestree-walkeresutilseventemitter3execaexitexpectfast-deep-equalfast-globfast-json-stable-stringifyfast-levenshteinfastparsefastqfb-watchmanfile-entry-cachefilename-reserved-regexfilenamifyfill-rangefind-cache-dirfind-upflat-cacheflattedfs-extrafs.realpathfunction-bindfunction.prototype.namefunctions-have-namesgeneric-namesgensyncget-caller-fileget-intrinsicget-package-typeget-streamget-symbol-descriptionglobglob-parentglobalsglobbygraceful-fsgrapheme-splitterhashas-ansihas-bigintshas-flaghas-property-descriptorshas-symbolshas-tostringtaghex-color-regexhsl-regexhsla-regexhtml-escaperhuman-signalsicss-replace-symbolsicss-utilsignoreimport-cwdimport-freshimport-fromimport-localimurmurhashindexes-ofinflightinheritsinternal-slotis-absolute-urlis-arrayishis-bigintis-boolean-objectis-callableis-color-stopis-core-moduleis-date-objectis-directoryis-extglobis-fullwidth-code-pointis-generator-fnis-globis-moduleis-negative-zerois-numberis-number-objectis-objis-referenceis-regexis-resolvableis-shared-array-bufferis-streamis-stringis-symbolis-weakrefisexeistanbul-lib-coverageistanbul-lib-instrumentistanbul-lib-reportistanbul-lib-source-mapsistanbul-reportsjest-changed-filesjest-circusjest-clijest-configjest-diffjest-docblockjest-eachjest-environment-nodejest-get-typejest-haste-mapjest-leak-detectorjest-matcher-utilsjest-message-utiljest-mockjest-pnp-resolverjest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-runtimejest-snapshotjest-utiljest-validatejest-watcherjest-workerjs-sdsljs-tokensjs-yamljsescjson-parse-better-errorsjson-parse-even-better-errorsjson-schema-traversejson-stable-stringify-without-jsonifyjson5jsonfilejsx-ast-utilskleurlevenlevnlines-and-columnsloader-utilslocate-pathlodashlodash.camelcaselodash.debouncelodash.memoizelodash.mergelodash.uniqloose-envifylru-cachemagic-stringmake-dirmake-errormakeerrormdn-datamerge-streammerge2micromatchmimemimic-fnminimatchminimistmkdirpmsnanoidnatural-comparenode-int64node-releasesnormalize-pathnormalize-urlnpm-run-pathnth-checkobject-assignobject-inspectobject-keysobject.assignobject.entriesobject.fromentriesobject.getownpropertydescriptorsobject.hasownobject.valuesonceonetimeoptionatorp-finallyp-limitp-locatep-queuep-timeoutp-tryparent-moduleparse-jsonpath-existspath-is-absolutepath-keypath-parsepath-typepicocolorspicomatchpifypinkiepinkie-promisepiratespkg-dirpostcsspostcss-calcpostcss-colorminpostcss-convert-valuespostcss-discard-commentspostcss-discard-duplicatespostcss-discard-emptypostcss-discard-overriddenpostcss-load-configpostcss-merge-longhandpostcss-merge-rulespostcss-minify-font-valuespostcss-minify-gradientspostcss-minify-paramspostcss-minify-selectorspostcss-modulespostcss-modules-extract-importspostcss-modules-local-by-defaultpostcss-modules-scopepostcss-modules-valuespostcss-normalize-charsetpostcss-normalize-display-valuespostcss-normalize-positionspostcss-normalize-repeat-stylepostcss-normalize-stringpostcss-normalize-timing-functionspostcss-normalize-unicodepostcss-normalize-urlpostcss-normalize-whitespacepostcss-ordered-valuespostcss-reduce-initialpostcss-reduce-transformspostcss-selector-parserpostcss-svgopostcss-unique-selectorspostcss-value-parserprelude-lspretty-formatpromise.seriespromptsprop-typespunycodeqqueue-microtaskreact-isreact-shallow-rendererregenerateregenerate-unicode-propertiesregenerator-runtimeregenerator-transformregexp.prototype.flagsregexppregexpu-coreregjsgenregjsparserrequire-directoryreserved-wordsresolveresolve-cwdresolve-fromresolve.exportsreusifyrgb-regexrgba-regexrimrafrollup-plugin-postcssrollup-pluginutilsrun-parallelsafe-identifiersafe-regex-testsaxschedulersemvershebang-commandshebang-regexside-channelsignal-exitsimple-swizzlesisteransislashsource-mapsource-map-jssource-map-supportsourcemap-codecsprintf-jsstablestack-utilsstring-hashstring-lengthstring-widthstring.prototype.matchallstring.prototype.trimendstring.prototype.trimstartstrip-ansistrip-bomstrip-final-newlinestrip-json-commentsstrip-outerstyle-injectstylehackssupports-colorsupports-hyperlinkssupports-preserve-symlinks-flagsvg-parsersvgoterminal-linktest-excludetext-tabletimsorttmplto-fast-propertiesto-regex-rangetrim-repeatedtslibtsutilstype-checktype-detecttype-festunbox-primitiveunicode-canonical-property-names-ecmascriptunicode-match-property-ecmascriptunicode-match-property-value-ecmascriptunicode-property-aliases-ecmascriptuniquniqsuniversalifyunquoteupdate-browserslist-dburi-jsutil-deprecateutil.promisifyv8-to-istanbulvendorswalkerwhichwhich-boxed-primitiveword-wrapwrap-ansiwrappywrite-file-atomicy18nyallistyamlyargsyargs-parseryocto-queue
1.2.0

1 year ago

1.0.0

1 year ago