-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add types #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add types #62
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
15c3395
Add types
BarryThePenguin d87c628
export mdastUtilToc namespace
BarryThePenguin 12c0abe
Update types/index.d.ts
BarryThePenguin 4af1f9a
Update types/tslint.json
BarryThePenguin 267dd25
Update types/index.d.ts
BarryThePenguin 7b01411
Update types/index.d.ts
BarryThePenguin 0cd5c12
Add mdast typings
BarryThePenguin d8a75ee
Change config feedback
BarryThePenguin 45e0e4c
Use mdast typings
BarryThePenguin 2f48939
Add docs to TOCOptions
BarryThePenguin 541eee2
Enable lint rules
BarryThePenguin 51b6a54
Fix padding for dslint
BarryThePenguin b6ce9ef
Add default hints
BarryThePenguin e077eae
Disable no redundant jsdoc
BarryThePenguin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// TypeScript Version: 3.0 | ||
|
||
import {Node} from 'unist' | ||
import {Parent, Heading, Link, Paragraph, List, ListItem} from 'mdast' | ||
import {Test} from 'unist-util-is' | ||
|
||
declare namespace mdastUtilToc { | ||
interface TOCOptions { | ||
/** | ||
* Heading to look for, wrapped in `new RegExp('^(' + value + ')$', 'i')`. | ||
*/ | ||
heading?: string | ||
|
||
/** | ||
* Maximum heading depth to include in the table of contents, | ||
* This is inclusive: when set to `3`, | ||
* level three headings are included (those with three hashes, `###`). | ||
* | ||
* @default 6 | ||
*/ | ||
maxDepth?: Heading['depth'] | ||
|
||
/** | ||
* Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`. | ||
* Any heading matching this expression will not be present in the table of contents. | ||
*/ | ||
skip?: string | ||
|
||
/** | ||
* Whether to compile list-items tightly. | ||
* | ||
* @default false | ||
*/ | ||
tight?: boolean | ||
|
||
/** | ||
* Add a prefix to links to headings in the table of contents. | ||
* Useful for example when later going from mdast to hast and sanitizing with `hast-util-sanitize`. | ||
* | ||
* @default null | ||
*/ | ||
prefix?: string | ||
|
||
/** | ||
* Allows headings to be children of certain node types | ||
* Internally, uses `unist-util-is` to check, so `parents` can be any `is`-compatible test. | ||
* | ||
* For example, this would allow headings under either `root` or `blockquote` to be used: | ||
* | ||
* ```ts | ||
* toc(tree, {parents: ['root', 'blockquote']}) | ||
* ``` | ||
* | ||
* @default the to `toc` given `tree`, to only allow top-level headings | ||
ChristianMurphy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
parents?: Test<Node> | Array<Test<Node>> | ||
} | ||
|
||
interface TOCResult { | ||
index: number | null | ||
endIndex: number | null | ||
map: List | null | ||
} | ||
} | ||
|
||
/** | ||
* Generate a Table of Contents from a tree. | ||
* | ||
* @param node searched for headings | ||
* @param options configuration and settings | ||
*/ | ||
declare function mdastUtilToc( | ||
BarryThePenguin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node: Node, | ||
options?: mdastUtilToc.TOCOptions | ||
): mdastUtilToc.TOCResult | ||
|
||
export = mdastUtilToc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {Link, Paragraph, List, ListItem} from 'mdast' | ||
|
||
import unified = require('unified') | ||
import u = require('unist-builder') | ||
import is = require('unist-util-is') | ||
import toc = require('mdast-util-toc') | ||
|
||
const tree = u('root', [ | ||
u('heading', {depth: 1}, [u('text', 'Alpha')]), | ||
u('heading', {depth: 2}, [u('text', 'Bravo')]), | ||
u('heading', {depth: 3}, [u('text', 'Charlie')]), | ||
u('heading', {depth: 2}, [u('text', 'Delta')]) | ||
]) | ||
|
||
const {map} = toc(tree) | ||
|
||
if (is<List>(map, 'list')) { | ||
const [firstListItem] = map.children | ||
|
||
if (is<ListItem>(firstListItem, 'listItem')) { | ||
const [firstParagraph] = firstListItem.children | ||
|
||
if (is<Paragraph>(firstParagraph, 'paragraph')) { | ||
const [firstLink] = firstParagraph.children | ||
|
||
is<Link>(firstLink, 'link') | ||
ChristianMurphy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
toc(tree, { | ||
heading: 'Table Of Contents' | ||
}) | ||
|
||
toc(tree, { | ||
maxDepth: 2 | ||
}) | ||
|
||
toc(tree, { | ||
skip: 'skip heading' | ||
}) | ||
|
||
toc(tree, { | ||
tight: true | ||
}) | ||
|
||
toc(tree, { | ||
prefix: '/prefix' | ||
}) | ||
|
||
toc(tree, { | ||
parents: ['root', 'blockquote'] | ||
}) | ||
|
||
/*=== usable in unified transform ===*/ | ||
unified().use(() => tree => { | ||
const table = toc(tree) | ||
|
||
if (is<List>(table.map, 'list')) { | ||
// do something | ||
} | ||
|
||
return tree | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2015"], | ||
"strict": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"mdast-util-toc": ["index.d.ts"] | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"no-redundant-jsdoc": false, | ||
"semicolon": false, | ||
"unified-signatures": true, | ||
"whitespace": false, | ||
"strict-export-declare-modifiers": false | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.