rehype-toc
Advanced tools
Comparing version
@@ -5,3 +5,4 @@ import { Node } from "unist"; | ||
* Returns the `<main>` node, or the `<body>` node if there is no `<main>`. | ||
* The second node returned is the parent of the first node. | ||
*/ | ||
export declare function findMainNode(root: Node): HtmlElementNode; | ||
export declare function findMainNode(root: Node): [HtmlElementNode, HtmlElementNode]; |
@@ -6,7 +6,16 @@ "use strict"; | ||
* Returns the `<main>` node, or the `<body>` node if there is no `<main>`. | ||
* The second node returned is the parent of the first node. | ||
*/ | ||
function findMainNode(root) { | ||
let body = findTagName(root, "body"); | ||
let main = findTagName(body || root, "main"); | ||
return main || body || root; | ||
let [body, bodyParent] = findTagName(root, "body"); | ||
let [main, mainParent] = findTagName(body || root, "main"); | ||
if (main) { | ||
return [main, mainParent || body || root]; | ||
} | ||
else { | ||
return [ | ||
body || root, | ||
bodyParent || root | ||
]; | ||
} | ||
} | ||
@@ -19,3 +28,3 @@ exports.findMainNode = findMainNode; | ||
if (type_guards_1.isHtmlElementNode(node) && node.tagName === tagName) { | ||
return node; | ||
return [node, undefined]; | ||
} | ||
@@ -25,9 +34,10 @@ if (node.children) { | ||
for (let child of parent.children) { | ||
let found = findTagName(child, tagName); | ||
let [found] = findTagName(child, tagName); | ||
if (found) { | ||
return found; | ||
return [found, parent]; | ||
} | ||
} | ||
} | ||
return [undefined, undefined]; | ||
} | ||
//# sourceMappingURL=find-main-node.js.map |
import { toc } from "./rehype-toc"; | ||
export { Options, PartialOptions } from "./options"; | ||
export { InsertPosition, Options, PartialOptions } from "./options"; | ||
export * from "./types"; | ||
export { toc }; | ||
export default toc; |
import { Node } from "unist"; | ||
import { HeadingTagName, HtmlElementNode } from "./types"; | ||
/** | ||
* The different positions at which the table of contents can be inserted, | ||
* relative to the `<main>` element. | ||
*/ | ||
export declare type InsertPosition = "beforebegin" | "afterbegin" | "beforeend" | "afterend"; | ||
/** | ||
* Options for the Rehype TOC plugin | ||
@@ -8,2 +13,17 @@ */ | ||
/** | ||
* Determines whether the table of contents is wrapped in a `<nav>` element. | ||
* | ||
* Defaults to `true`. | ||
*/ | ||
nav: boolean; | ||
/** | ||
* The position at which the table of contents should be inserted, relative to the `<main>` | ||
* element. | ||
* | ||
* Defaults to "afterbegin"; | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement | ||
*/ | ||
position: InsertPosition; | ||
/** | ||
* HTML heading elements to include in the table of contents. | ||
@@ -15,8 +35,2 @@ * | ||
/** | ||
* Determines whether the table of contents is wrapped in a `<nav>` element. | ||
* | ||
* Defaults to `true`. | ||
*/ | ||
nav: boolean; | ||
/** | ||
* CSS class names for various parts of the table of contents. | ||
@@ -23,0 +37,0 @@ */ |
@@ -9,2 +9,4 @@ "use strict"; | ||
return { | ||
nav: config.nav === undefined ? true : Boolean(config.nav), | ||
position: config.position || "afterbegin", | ||
headings: config.headings || ["h1", "h2", "h3", "h4", "h5", "h6"], | ||
@@ -17,3 +19,2 @@ cssClasses: { | ||
}, | ||
nav: config.nav === undefined ? true : Boolean(config.nav), | ||
customizeTOC: config.customizeTOC || ((toc) => toc), | ||
@@ -20,0 +21,0 @@ }; |
@@ -7,2 +7,2 @@ import { Processor, Transformer } from "unified"; | ||
*/ | ||
export declare function toc(this: Processor, config?: PartialOptions): Transformer; | ||
export declare function toc(this: Processor, opts?: PartialOptions): Transformer; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const find_main_node_1 = require("./find-main-node"); | ||
const insert_toc_1 = require("./insert-toc"); | ||
const options_1 = require("./options"); | ||
@@ -13,7 +14,7 @@ /** | ||
*/ | ||
function toc(config) { | ||
let options = options_1.applyDefaults(config); | ||
function toc(opts) { | ||
let options = options_1.applyDefaults(opts); | ||
return function transformer(root) { | ||
// Find the <main> or <body> element | ||
let mainNode = find_main_node_1.findMainNode(root); | ||
let [mainNode, mainParent] = find_main_node_1.findMainNode(root); | ||
// Find all heading elements | ||
@@ -27,3 +28,3 @@ let headings = fiind_headings_1.findHeadings(mainNode, options); | ||
// Add the table of contents to the <main> element | ||
mainNode.children.unshift(node); | ||
insert_toc_1.insertTOC(node, mainNode, mainParent, options); | ||
} | ||
@@ -30,0 +31,0 @@ return root; |
{ | ||
"name": "rehype-toc", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A rehype plugin that adds a table of contents (TOC) to the page", | ||
@@ -65,4 +65,4 @@ "keywords": [ | ||
"typescript": "^3.5.3", | ||
"typescript-tslint-plugin": "^0.5.3", | ||
"unified": "^8.0.1", | ||
"typescript-tslint-plugin": "^0.5.4", | ||
"unified": "^8.1.0", | ||
"version-bump-prompt": "^5.0.4" | ||
@@ -69,0 +69,0 @@ }, |
@@ -227,4 +227,5 @@ # Table of Contents plugin for rehype | ||
|:---------------------|:-------------------|:----------------------|:----------------------------------------- | ||
|`nav` |boolean |true |Determines whether the table of contents is wrapped in a `<nav>` element. | ||
|`position` |string |"afterbegin" |The position at which the table of contents should be inserted, relative to the `<main>` or `<body>` element. Can be "beforebegin", "afterbegin", "beforeend", or "afterend". See [the `insertAdjacentElement()` docs](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement) for an explanation of each value. | ||
|`headings` |array of strings |h1, h2, h3, h4, h5, h6 |The HTML heading tags to include in the table of contents | ||
|`nav` |boolean |true |Determines whether the table of contents is wrapped in a `<nav>` element. | ||
|`cssClasses.toc` |string |toc |The CSS class name for the top-level `<nav>` or `<ol>` element that contains the whole table of contents. | ||
@@ -231,0 +232,0 @@ |`cssClasses.list` |string |toc-level |The CSS class name for all `<ol>` elements in the table of contents, including the top-level one. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43000
13.69%37
12.12%555
14.2%272
0.37%