Skip to content

Commit 28c6d9a

Browse files
author
Sethen
committed
Fixes for converting strings to links
1 parent baf8e78 commit 28c6d9a

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* [Make a table of contents](#make-a-table-of-contents)
44
* [How To Install](#how-to-install)
55
* [How To Use](#how-to-use)
6-
* [markdown.json](#markdown.json)
6+
* [markdown.json](#markdownjson)
77
* [How It Works](#how-it-works)
88

99

@@ -41,7 +41,7 @@ Something in another markdown file!
4141

4242
## Make a table of contents
4343

44-
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.
44+
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.
4545

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

docs/markdown_include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Something in another markdown file!
3232

3333
## Make a table of contents !heading
3434

35-
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.
35+
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.
3636

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

markdown-include.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,33 @@
1010
var fs = require('fs');
1111
var includePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md"/gm;
1212
var ignorePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md" !ignore/gm;
13-
var headingPattern = /#+\s.+ !heading/gm;
13+
var headingPattern = /^#+\s.+ !heading/gm;
1414
var build = {};
1515
var tableOfContents = '';
1616

17+
/**
18+
* Builds links for table of contents
19+
* @param {String} str String to test and transform
20+
* @return {String} String for link
21+
*/
22+
function buildLinkString(str) {
23+
var linkPatterns = {
24+
dot: /\./g,
25+
stick: /\|/g
26+
};
27+
var key;
28+
29+
for (key in linkPatterns) {
30+
var pattern = linkPatterns[key];
31+
32+
if (pattern.test(str)) {
33+
str = str.replace(pattern, '');
34+
}
35+
}
36+
37+
return str.trim().split(' ').join('-').toLowerCase();
38+
}
39+
1740
/**
1841
* Build content item for navigation
1942
* @param {Object} obj Object containing count and headingTag
@@ -24,7 +47,7 @@
2447
var count = obj.count;
2548
var item = headingTag.substring(count + 1);
2649
var index = headingTag.indexOf(item);
27-
var headingTrimmed = headingTag.substring(index).trim().split(' ').join('-').toLowerCase();
50+
var headingTrimmed = buildLinkString(headingTag.substring(index));
2851
var navItem;
2952

3053
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-include",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Include markdown files into other markdown files with C style syntax.",
55
"main": "markdown-include.js",
66
"repository": {

0 commit comments

Comments
 (0)