Skip to content

Commit 718988e

Browse files
author
Sethen
committed
Added lead option to markdown.json, minor fixes
1 parent 9bf262c commit 718988e

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ node path/to/markdown-include.js path/to/markdown.json
7171

7272
`markdown.json` can be populated with the following options:
7373

74-
| Option | Type | Description |
75-
|:-------------------------:|:-------------:|:--------------------------------------------------------------------------:|
76-
| `build` | String | File path of where everything should be compiled, like `README.md` |
77-
| `files` | Array | Array of files to to compile |
78-
| `tableOfContents` | Object | Object to hold options for table of contents generation |
79-
| `tableOfContents.heading` | String | Heading for table of contents, added to the `tableOfContents` object |
80-
74+
| Option | Type | Description |
75+
|:-------------------------:|:-------:|:--------------------------------------------------------------------------:|
76+
| `build` | String | File path of where everything should be compiled, like `README.md`. |
77+
| `files` | Array | Array of files to to compile. |
78+
| `tableOfContents` | Object | Object to hold options for table of contents generation. |
79+
| `tableOfContents.heading` | String | Heading for table of contents (use markdown syntax if desired). |
80+
| `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. |
8181
# How It Works
8282

8383
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:

docs/how_to_use.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ node path/to/markdown-include.js path/to/markdown.json
1010

1111
`markdown.json` can be populated with the following options:
1212

13-
| Option | Type | Description |
14-
|:-------------------------:|:-------------:|:--------------------------------------------------------------------------:|
15-
| `build` | String | File path of where everything should be compiled, like `README.md` |
16-
| `files` | Array | Array of files to to compile |
17-
| `tableOfContents` | Object | Object to hold options for table of contents generation |
18-
| `tableOfContents.heading` | String | Heading for table of contents (use markdown syntax if desired) |
13+
| Option | Type | Description |
14+
|:-------------------------:|:-------:|:--------------------------------------------------------------------------:|
15+
| `build` | String | File path of where everything should be compiled, like `README.md`. |
16+
| `files` | Array | Array of files to to compile. |
17+
| `tableOfContents` | Object | Object to hold options for table of contents generation. |
18+
| `tableOfContents.heading` | String | Heading for table of contents (use markdown syntax if desired). |
19+
| `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. |

markdown-include.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
var exec = require('child_process').exec;
1010
var fs = require('fs');
11+
var build = {};
1112
var includePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md"/gm;
1213
var ignorePattern = /^#include\s"(.+\/|\/|\w|-|\/)+.md" !ignore/gm;
1314
var headingPattern = /^#+\s.+ !heading/gm;
14-
var build = {};
1515
var tableOfContents = '';
16+
var options;
1617

1718
/**
1819
* Builds links for table of contents
@@ -48,6 +49,7 @@
4849
var item = headingTag.substring(count + 1);
4950
var index = headingTag.indexOf(item);
5051
var headingTrimmed = buildLinkString(headingTag.substring(index));
52+
var lead = options.tableOfContents.lead && options.tableOfContents.lead === 'number' ? '1.' : '*';
5153
var navItem;
5254

5355
/**
@@ -61,22 +63,22 @@
6163

6264
switch (obj.count) {
6365
case 1:
64-
navItem = '* ' + buildNavItem(headingTrimmed);
66+
navItem = lead + ' ' + buildNavItem(headingTrimmed);
6567
break;
6668
case 2:
67-
navItem = ' * ' + buildNavItem(headingTrimmed);
69+
navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed);
6870
break;
6971
case 3:
70-
navItem = ' * ' + buildNavItem(headingTrimmed);
72+
navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed);
7173
break;
7274
case 4:
73-
navItem = ' * ' + buildNavItem(headingTrimmed);
75+
navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed);
7476
break;
7577
case 5:
76-
navItem = ' * ' + buildNavItem(headingTrimmed);
78+
navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed);
7779
break;
7880
case 6:
79-
navItem = ' * ' + buildNavItem(headingTrimmed);
81+
navItem = ' ' + lead + ' ' + buildNavItem(headingTrimmed);
8082
break;
8183
}
8284

@@ -93,7 +95,7 @@
9395
throw err;
9496
}
9597

96-
var options = JSON.parse(data.toString());
98+
options = JSON.parse(data.toString());
9799
var files = options.files;
98100
var i;
99101

@@ -118,7 +120,7 @@
118120
}
119121
}
120122

121-
writeFile(options, build[file].parsedData);
123+
writeFile(build[file].parsedData);
122124
}
123125
});
124126
}
@@ -324,16 +326,11 @@
324326
var currentPatternTagLength = patterns[i].length;
325327
var replacedTag = currentPattern.substring(0, currentPatternTagLength - stringLength);
326328

327-
if (obj.replace) {
328-
console.log('do something else');
329+
if (replacedData) {
330+
replacedData = replacedData.replace(currentPattern, replacedTag);
329331
}
330332
else {
331-
if (replacedData) {
332-
replacedData = replacedData.replace(currentPattern, replacedTag);
333-
}
334-
else {
335-
replacedData = obj.data.replace(currentPattern, replacedTag);
336-
}
333+
replacedData = obj.data.replace(currentPattern, replacedTag);
337334
}
338335
}
339336

@@ -346,7 +343,7 @@
346343
* @param {String} path Path to build new file
347344
* @param {String} data Data to write into file
348345
*/
349-
function writeFile(options, parsedData) {
346+
function writeFile(parsedData) {
350347
fs.writeFile(options.build, parsedData, function (err) {
351348
if (err) {
352349
throw err;

0 commit comments

Comments
 (0)