Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default Example;
| -------------------- | ----------------- | ----------------------------------------------------------------------------- |
| `markdownText` | string | **Required** The markdown text you want to creat a TOC from. |
| `titleLimit` | number | The maximum length of each title in the TOC. |
| `highestHeadingLevel` | number | The highest level of headings you want to extract from the given markdownText. |
| `lowestHeadingLevel` | number | The lowest level of headings you want to extract from the given markdownText. |
| `className` | strig | Your custom className. |
| `type` | "deafult" or"raw" | The type of a TOC you want to use. |
Expand Down
17 changes: 15 additions & 2 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,30 @@ describe("extractHeadingsFromMd", () => {
`;

it("It extracts headings from the given markdownText.", () => {
expect(extractHeadingsFromMd(markdownText, 3)).toEqual([
expect(extractHeadingsFromMd(markdownText, 1, 3)).toEqual([
"# Heading1\n",
"## Heading2\n",
"### Heading3\n"
]);
});

it("It extracts headings from the given markdownText.", () => {
expect(extractHeadingsFromMd(markdownText, 2)).toEqual([
expect(extractHeadingsFromMd(markdownText, 1, 2)).toEqual([
"# Heading1\n",
"## Heading2\n"
]);
});

it("It extracts headings from the given markdownText.", () => {
expect(extractHeadingsFromMd(markdownText, 2, 3)).toEqual([
"## Heading2\n",
"### Heading3\n"
]);
});

it("It extracts headings from the given markdownText.", () => {
expect(extractHeadingsFromMd(markdownText, 2, 2)).toEqual([
"## Heading2\n"
]);
});
});
10 changes: 8 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ interface Props {
The maximum length of each title in the TOC.
*/
titleLimit?: number;
/*
The highest level of headings you want to extract from the given markdownText.
*/
highestHeadingLevel?: number;
/*
The lowest level of headings you want to extract from the given markdownText.
*/
Expand Down Expand Up @@ -39,6 +43,7 @@ interface Props {
const Toc = ({
markdownText,
titleLimit,
highestHeadingLevel,
lowestHeadingLevel,
className,
type
Expand All @@ -47,15 +52,16 @@ const Toc = ({
const limit = titleLimit ? titleLimit : 200;
const defaultClass = type === "raw" ? "" : "react-toc";
const customClass = className || defaultClass;
const headingLevel: number = lowestHeadingLevel || 6;
const headingLevels: number[] = [highestHeadingLevel || 1, lowestHeadingLevel || 6];

// Style settings
const style: string | undefined = styles[customClass] || className;

// Mutate headings
const matchedHeadings: RegExpMatchArray | null = extractHeadingsFromMd(
markdownText,
headingLevel
headingLevels[0],
headingLevels[1]
);
const headingObjects = matchedHeadings?.map(heading =>
newHeading(heading, limit)
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ const createTitle = (string: string, stringLimit: number) => {
// It extracts headings from the given markdownText.
const extractHeadingsFromMd = (
markdownText: string,
numberOftargetHeadings: number
highestTargetHeadings: number,
lowestTargetHeadings: number
): RegExpMatchArray | null => {
const headingRegex = new RegExp(
`^#{1,${numberOftargetHeadings}}\\s.+\\n`,
`^#{${highestTargetHeadings},${lowestTargetHeadings}}\\s.+\\n`,
"gm"
);
return markdownText.match(headingRegex);
Expand Down