Skip to content

Commit

Permalink
Fixes for converting strings to links
Browse files Browse the repository at this point in the history
  • Loading branch information
Sethen committed Mar 4, 2015
1 parent baf8e78 commit 28c6d9a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* [Make a table of contents](#make-a-table-of-contents)
* [How To Install](#how-to-install)
* [How To Use](#how-to-use)
* [markdown.json](#markdown.json)
* [markdown.json](#markdownjson)
* [How It Works](#how-it-works)


Expand Down Expand Up @@ -41,7 +41,7 @@ Something in another markdown file!

## Make a table of contents

Aside from compiling your markdown files, markdown-include can also build your table of contents. This works by evaluating the heading tags inside of your files. Since markdown works on using `#` for headings, this makes it easy to assemble table of contents from them. The more `#` you have in front of your headings (up to 6) will decide how the table of contents is built. Use one `#` and it's a top level navigation item... Use two `#` and it would be underneath the previous navigation item.
Aside from compiling your markdown files, markdown-include can also build your table of contents. This works by evaluating the heading tags inside of your files. Since markdown works on using `#` for headings, this makes it easy to assemble table of contents from them. The more `#` you have in front of your headings (up to 6) will decide how the table of contents is built. Use one `#` and it's a top level navigation item... Use two `#` and it would be underneath the previous navigation item and so on.

For each heading that you would like to be included in a table of contents just add ` !heading` to the end of it.

Expand Down
2 changes: 1 addition & 1 deletion docs/markdown_include.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Something in another markdown file!

## Make a table of contents !heading

Aside from compiling your markdown files, markdown-include can also build your table of contents. This works by evaluating the heading tags inside of your files. Since markdown works on using `#` for headings, this makes it easy to assemble table of contents from them. The more `#` you have in front of your headings (up to 6) will decide how the table of contents is built. Use one `#` and it's a top level navigation item... Use two `#` and it would be underneath the previous navigation item.
Aside from compiling your markdown files, markdown-include can also build your table of contents. This works by evaluating the heading tags inside of your files. Since markdown works on using `#` for headings, this makes it easy to assemble table of contents from them. The more `#` you have in front of your headings (up to 6) will decide how the table of contents is built. Use one `#` and it's a top level navigation item... Use two `#` and it would be underneath the previous navigation item and so on.

For each heading that you would like to be included in a table of contents just add ` !heading` to the end of it.

27 changes: 25 additions & 2 deletions markdown-include.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,33 @@
var fs = require('fs');
var includePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md"/gm;
var ignorePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md" !ignore/gm;
var headingPattern = /#+\s.+ !heading/gm;
var headingPattern = /^#+\s.+ !heading/gm;
var build = {};
var tableOfContents = '';

/**
* Builds links for table of contents
* @param {String} str String to test and transform
* @return {String} String for link
*/
function buildLinkString(str) {
var linkPatterns = {
dot: /\./g,
stick: /\|/g
};
var key;

for (key in linkPatterns) {
var pattern = linkPatterns[key];

if (pattern.test(str)) {
str = str.replace(pattern, '');
}
}

return str.trim().split(' ').join('-').toLowerCase();
}

/**
* Build content item for navigation
* @param {Object} obj Object containing count and headingTag
Expand All @@ -24,7 +47,7 @@
var count = obj.count;
var item = headingTag.substring(count + 1);
var index = headingTag.indexOf(item);
var headingTrimmed = headingTag.substring(index).trim().split(' ').join('-').toLowerCase();
var headingTrimmed = buildLinkString(headingTag.substring(index));
var navItem;

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-include",
"version": "0.2.0",
"version": "0.2.1",
"description": "Include markdown files into other markdown files with C style syntax.",
"main": "markdown-include.js",
"repository": {
Expand Down

0 comments on commit 28c6d9a

Please sign in to comment.