Skip to content

Commit

Permalink
feat: add options.tocPattern, customizable pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
hikerpig committed Jul 27, 2019
1 parent e751260 commit 35abebc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ markdownIt({

(default: `true`)

Allows you to enable/disable the toc transformation of `@[toc]`
Allows you to enable/disable the toc transformation of toc

#### `tocPattern`

(default: `/\[@toc\]/`)

The pattern to detect. You can specify it to meet other conventions, such as `\[TOC\]` for Typora.

#### `tocClassName`

Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import clone from "clone";
import uslug from "uslug";
import Token from "markdown-it/lib/token";

const TOC = "@[toc]";
const TOC_RE = /^@\[toc\]/im;
const DEFAULT_TOC_PATTERN = /@\[toc\]/;
const TOC_MARKUP = 'TOC';

let markdownItSecondInstance = () => {};
let headingIds = {};
Expand Down Expand Up @@ -145,6 +145,7 @@ export default function(md, options) {
options = {
toc: true,
tocClassName: "markdownIt-TOC",
tocPattern: DEFAULT_TOC_PATTERN,
tocFirstLevel: 1,
tocLastLevel: 6,
tocCallback: null,
Expand Down Expand Up @@ -264,15 +265,15 @@ export default function(md, options) {
}

// Detect TOC markdown
match = TOC_RE.exec(state.src);
match = options.tocPattern.exec(state.src);
match = !match ? [] : match.filter(m => m);
if (match.length < 1) {
return false;
}

// Build content
token = state.push("toc_open", "toc", 1);
token.markup = TOC;
token.markup = TOC_MARKUP;
token = state.push("toc_body", "", 0);
token = state.push("toc_close", "toc", -1);

Expand Down

0 comments on commit 35abebc

Please sign in to comment.