Skip to content

Commit

Permalink
Fix style of generated TOCs
Browse files Browse the repository at this point in the history
This update adds extra new lines when a TOC entry has both
a title and contents, making for cleaner looking tables.
  • Loading branch information
wooorm committed May 25, 2015
1 parent e2a1a00 commit d0eaf12
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ UMD (globals/AMD/CommonJS) ([uncompressed](mdast-toc.js) and [compressed](mdast-
## Table of Contents

* [Usage](#usage)

* [API](#api)

* [mdast.use(toc, options)](#mdastusetoc-options)

* [License](#license)

## Usage
Expand Down Expand Up @@ -72,8 +75,11 @@ Yields:

```markdown
- [Usage](#usage)

- [API](#api)

- [mdast.use(toc, options)](#mdastusetoc-options)

- [License](#license)
```

Expand Down
29 changes: 26 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ function listItem() {
*/
function insert(node, parent, library) {
var children = parent.children;
var last = children[children.length - 1];
var index = -1;
var length = children.length;
var last = children[length - 1];
var isLoose = false;
var item;

if (node.depth === 1) {
Expand Down Expand Up @@ -247,8 +250,6 @@ function insert(node, parent, library) {
} else if (parent.type === LIST) {
item = listItem();

item.loose = true;

insert(node, item, library);

children.push(item);
Expand All @@ -260,6 +261,28 @@ function insert(node, parent, library) {

children.push(item);
}

/*
* Properly style list-items with new lines.
*/

if (parent.type === LIST_ITEM) {
parent.loose = children.length > 1;
} else {
while (++index < length) {
if (children[index].loose) {
isLoose = true;

break;
}
}

index = -1;

while (++index < length) {
children[index].loose = isLoose;
}
}
}

/**
Expand Down
29 changes: 26 additions & 3 deletions mdast-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ function listItem() {
*/
function insert(node, parent, library) {
var children = parent.children;
var last = children[children.length - 1];
var index = -1;
var length = children.length;
var last = children[length - 1];
var isLoose = false;
var item;

if (node.depth === 1) {
Expand Down Expand Up @@ -431,8 +434,6 @@ function insert(node, parent, library) {
} else if (parent.type === LIST) {
item = listItem();

item.loose = true;

insert(node, item, library);

children.push(item);
Expand All @@ -444,6 +445,28 @@ function insert(node, parent, library) {

children.push(item);
}

/*
* Properly style list-items with new lines.
*/

if (parent.type === LIST_ITEM) {
parent.loose = children.length > 1;
} else {
while (++index < length) {
if (children[index].loose) {
isLoose = true;

break;
}
}

index = -1;

while (++index < length) {
children[index].loose = isLoose;
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mdast-toc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/fixtures/custom-heading/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Fooooo

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

# Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/image-in-link-in-heading/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## [![](./some-table.png "Table of Contents")](some-link.com)

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

# Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/normal-dashes/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Table-of-contents

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

# Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/normal-nesting-inverted/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
- - [Something if](#something-if)

- [Something else](#something-else)

- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

## Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/normal-singular/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Table of Content

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

# Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/normal-toc/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## TOC

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

# Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/normal/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Table of Contents

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [Something iffi](#something-iffi)

# Something if
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/slug-for-images-and-links/Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Table of Contents

- [Something if](#something-if)

- [Something else](#something-else)
- [Something elsefi](#something-elsefi)

- [another title](#another-title)

# [Something if](http://a-link.com)
Expand Down

0 comments on commit d0eaf12

Please sign in to comment.