diff --git a/README.md b/README.md index 74c0e35..2b7827b 100644 --- a/README.md +++ b/README.md @@ -71,13 +71,13 @@ node path/to/markdown-include.js path/to/markdown.json `markdown.json` can be populated with the following options: -| Option | Type | Description | -|:-------------------------:|:-------------:|:--------------------------------------------------------------------------:| -| `build` | String | File path of where everything should be compiled, like `README.md` | -| `files` | Array | Array of files to to compile | -| `tableOfContents` | Object | Object to hold options for table of contents generation | -| `tableOfContents.heading` | String | Heading for table of contents, added to the `tableOfContents` object | - +| Option | Type | Description | +|:-------------------------:|:-------:|:--------------------------------------------------------------------------:| +| `build` | String | File path of where everything should be compiled, like `README.md`. | +| `files` | Array | Array of files to to compile. | +| `tableOfContents` | Object | Object to hold options for table of contents generation. | +| `tableOfContents.heading` | String | Heading for table of contents (use markdown syntax if desired). | +| `tableOfContents.lead` | String | What navigation items in table of contents lead with. If value is `number` will add numbers before each item and subitem. If not, will add asterisks. Refer to markdown syntax to understand the difference. | # How It Works markdown-include works by recursively going through files based on the tags that are found. For instance, consider the following in a `_README.md` file: diff --git a/docs/how_to_use.md b/docs/how_to_use.md index afaea0c..4400e32 100644 --- a/docs/how_to_use.md +++ b/docs/how_to_use.md @@ -10,9 +10,10 @@ node path/to/markdown-include.js path/to/markdown.json `markdown.json` can be populated with the following options: -| Option | Type | Description | -|:-------------------------:|:-------------:|:--------------------------------------------------------------------------:| -| `build` | String | File path of where everything should be compiled, like `README.md` | -| `files` | Array | Array of files to to compile | -| `tableOfContents` | Object | Object to hold options for table of contents generation | -| `tableOfContents.heading` | String | Heading for table of contents (use markdown syntax if desired) | \ No newline at end of file +| Option | Type | Description | +|:-------------------------:|:-------:|:--------------------------------------------------------------------------:| +| `build` | String | File path of where everything should be compiled, like `README.md`. | +| `files` | Array | Array of files to to compile. | +| `tableOfContents` | Object | Object to hold options for table of contents generation. | +| `tableOfContents.heading` | String | Heading for table of contents (use markdown syntax if desired). | +| `tableOfContents.lead` | String | What navigation items in table of contents lead with. If value is `number` will add numbers before each item and subitem. If not, will add asterisks. Refer to markdown syntax to understand the difference. | \ No newline at end of file diff --git a/markdown-include.js b/markdown-include.js index 19b14cb..1fc3b94 100644 --- a/markdown-include.js +++ b/markdown-include.js @@ -8,11 +8,12 @@ var exec = require('child_process').exec; var fs = require('fs'); + var build = {}; var includePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md"/gm; var ignorePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md" !ignore/gm; var headingPattern = /^#+\s.+ !heading/gm; - var build = {}; var tableOfContents = ''; + var options; /** * Builds links for table of contents @@ -48,6 +49,7 @@ var item = headingTag.substring(count + 1); var index = headingTag.indexOf(item); var headingTrimmed = buildLinkString(headingTag.substring(index)); + var lead = options.tableOfContents.lead && options.tableOfContents.lead === 'number' ? '1.' : '*'; var navItem; /** @@ -61,22 +63,22 @@ switch (obj.count) { case 1: - navItem = '* ' + buildNavItem(headingTrimmed); + navItem = lead + ' ' + buildNavItem(headingTrimmed); break; case 2: - navItem = ' * ' + buildNavItem(headingTrimmed); + navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed); break; case 3: - navItem = ' * ' + buildNavItem(headingTrimmed); + navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed); break; case 4: - navItem = ' * ' + buildNavItem(headingTrimmed); + navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed); break; case 5: - navItem = ' * ' + buildNavItem(headingTrimmed); + navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed); break; case 6: - navItem = ' * ' + buildNavItem(headingTrimmed); + navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed); break; } @@ -93,7 +95,7 @@ throw err; } - var options = JSON.parse(data.toString()); + options = JSON.parse(data.toString()); var files = options.files; var i; @@ -118,7 +120,7 @@ } } - writeFile(options, build[file].parsedData); + writeFile(build[file].parsedData); } }); } @@ -324,16 +326,11 @@ var currentPatternTagLength = patterns[i].length; var replacedTag = currentPattern.substring(0, currentPatternTagLength - stringLength); - if (obj.replace) { - console.log('do something else'); + if (replacedData) { + replacedData = replacedData.replace(currentPattern, replacedTag); } else { - if (replacedData) { - replacedData = replacedData.replace(currentPattern, replacedTag); - } - else { - replacedData = obj.data.replace(currentPattern, replacedTag); - } + replacedData = obj.data.replace(currentPattern, replacedTag); } } @@ -346,7 +343,7 @@ * @param {String} path Path to build new file * @param {String} data Data to write into file */ - function writeFile(options, parsedData) { + function writeFile(parsedData) { fs.writeFile(options.build, parsedData, function (err) { if (err) { throw err;