Skip to content

Commit a2e3480

Browse files
committed
Enabled metadata via paths
Added option to parse metadata from folder/file names.
1 parent f112940 commit a2e3480

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

index.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function gulpMarkdownDocs(fileOpt, opt) {
1818
var DEFAULTS = {
1919
yamlMeta: true,
2020
stylesheetUrl: '',
21-
categorySort: 'alphabetical', // 'alphabetical' || 'rank'
21+
categorySort: 'alphabetical', // 'alphabetical' || 'rank'
2222
documentSort: 'alphabetical', // 'alphabetical' || 'rank'
2323
layoutStylesheetUrl: __dirname + '/resources/layout.css',
2424
templatePath: __dirname + '/resources/index.html',
@@ -36,16 +36,16 @@ function gulpMarkdownDocs(fileOpt, opt) {
3636
smartLists: true,
3737
smartypants: false
3838
}
39-
}
39+
};
4040
var _ORPHAN_SLUG = 'orphans';
41-
42-
// merge defaults and passed options
41+
42+
// merge defaults and passed options
4343
var options = _.extend({}, DEFAULTS, opt);
4444
var markdownOptions = options.markdown = _.extend({}, DEFAULTS.markdown, opt.markdown);
4545

4646
// apply options for markdown parsing
4747
marked.setOptions(markdownOptions);
48-
48+
4949
// gather needed resources
5050
var indexHtml = fs.readFileSync(options.templatePath);
5151
var highlightCss = fs.readFileSync(require.resolve('highlight.js/styles/'+options.highlightTheme+'.css'));
@@ -57,8 +57,8 @@ function gulpMarkdownDocs(fileOpt, opt) {
5757
$head.append('<style>'+highlightCss+'</style>');
5858
if (layoutCss) { $head.append('<style>'+layoutCss+'</style>'); }
5959
// add the custom style sheet last for sensible overrides
60-
if (options.stylesheetUrl) {
61-
$head.append('<link rel="stylesheet" type="text/css" href="'+options.stylesheetUrl+'">');
60+
if (options.stylesheetUrl) {
61+
$head.append('<link rel="stylesheet" type="text/css" href="'+options.stylesheetUrl+'">');
6262
}
6363

6464
if (typeof fileOpt !== 'string') {
@@ -80,9 +80,9 @@ function gulpMarkdownDocs(fileOpt, opt) {
8080
categories.forEach(function (category) {
8181
var $section = $('<section class="doc-section"></section>');
8282
var $navGroup = $('<ul class="doc-nav-group"></ul>');
83-
if (category.categoryLabel && category.categorySlug !== _ORPHAN_SLUG) {
83+
if (category.categoryLabel && category.categorySlug !== _ORPHAN_SLUG) {
8484
$navGroup.append('<li class="doc-nav-group-header"><a href="#'+category.categorySlug+'">'+category.categoryLabel+'</a></li>');
85-
$section.append('<h1 class="doc-section-header"><a class="doc-nav-anchor" name="'+category.categorySlug+'"></a>'+category.categoryLabel+'</h1>');
85+
$section.append('<h1 class="doc-section-header"><a class="doc-nav-anchor" name="'+category.categorySlug+'"></a>'+category.categoryLabel+'</h1>');
8686
}
8787
category.children.forEach(function (doc) {
8888
var anchor = '';
@@ -119,15 +119,15 @@ function gulpMarkdownDocs(fileOpt, opt) {
119119
categories[slug].categoryRank = (!!doc.meta.categoryRank) ? doc.meta.categoryRank : categories[slug].categoryRank;
120120
} else {
121121
categories[_ORPHAN_SLUG] = categories[_ORPHAN_SLUG] || { children: [] };
122-
categories[_ORPHAN_SLUG].children.push(doc);
122+
categories[_ORPHAN_SLUG].children.push(doc);
123123
// orphans go to the back
124124
categories[_ORPHAN_SLUG].categoryRank = 1000000;
125125
categories[_ORPHAN_SLUG].categoryLabel = 'zzzzzzz';
126126
}
127127
});
128128
// map categories into an array
129129
categories = _.map(categories, function (category, key) {
130-
return { categoryLabel: category.categoryLabel, children: category.children, categoryRank: category.categoryRank, categorySlug: key }
130+
return { categoryLabel: category.categoryLabel, children: category.children, categoryRank: category.categoryRank, categorySlug: key };
131131
});
132132
// sort categories
133133
categories = _.sortBy(categories, (options.categorySort === 'rank' ? 'categoryRank' : 'categoryLabel'));
@@ -143,7 +143,7 @@ function gulpMarkdownDocs(fileOpt, opt) {
143143
var firstFile = null;
144144
function bufferContents(file) {
145145
if (file.isNull()) return; // ignore
146-
if (file.isStream()) return this.emit('error', new PluginError('gulp-markdown-docs', 'Streaming not supported'));
146+
if (file.isStream()) return this.emit('error', new PluginError('gulp-markdown-docs', 'Streaming not supported'));
147147

148148
var markdown, meta, html;
149149
if (!firstFile) firstFile = file;
@@ -153,7 +153,28 @@ function gulpMarkdownDocs(fileOpt, opt) {
153153
delete parsedFile['__content'];
154154
var metadata = parsedFile;
155155

156-
if (options.yamlMeta) {
156+
if (options.yamlMeta === 'use-paths') {
157+
var relativePath = file.path.substring(file.base.length);
158+
var relativePathMinusExtension = relativePath.substring(0, relativePath.lastIndexOf('.'));
159+
160+
var pathSegments = relativePathMinusExtension.split('\\');
161+
162+
if (pathSegments.length > 1) {
163+
var fileParts = pathSegments[pathSegments.length - 1].split(' ');
164+
var name = fileParts[fileParts.length-1].substring(0, fileParts[fileParts.length-1].lastIndexOf('.'));
165+
var categoryParts = pathSegments[pathSegments.length - 2].split(' ');
166+
meta = {};
167+
meta.documentRank = fileParts.length > 1 ? parseInt(fileParts.shift()) : 0;
168+
meta.label = fileParts.join(' ');
169+
meta.categoryRank = categoryParts.length > 1 ? parseInt(categoryParts.shift()) : 0;
170+
meta.categorySlug = meta.categoryLabel = categoryParts.join(' ');
171+
meta.id = categoryParts.join('-').toLowerCase() + '_' + fileParts.join('-').toLowerCase();
172+
}
173+
collectedDocs.push({
174+
html: parseMarkdown(markdown),
175+
meta: meta
176+
});
177+
} else if (options.yamlMeta) {
157178
collectedDocs.push({
158179
meta: metadata,
159180
html: parseMarkdown(markdown)
@@ -172,12 +193,12 @@ function gulpMarkdownDocs(fileOpt, opt) {
172193
if (firstFile) {
173194
var joinedFile = firstFile;
174195
var sortedDocs = sortDocs(collectedDocs);
175-
196+
176197
appendToIndex(sortedDocs);
177-
198+
178199
if (typeof fileOpt === 'string') {
179200
joinedFile = firstFile.clone({contents: false});
180-
joinedFile.path = path.join(firstFile.base, fileOpt)
201+
joinedFile.path = path.join(firstFile.base, fileOpt);
181202
}
182203
joinedFile.contents = new Buffer($.html());
183204
this.emit('data', joinedFile);
@@ -186,7 +207,6 @@ function gulpMarkdownDocs(fileOpt, opt) {
186207
}
187208

188209
return through(bufferContents, endStream);
189-
};
210+
}
190211

191212
module.exports = gulpMarkdownDocs;
192-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-markdown-docs",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "",
55
"license": "MIT",
66
"repository": "sojournerc/gulp-markdown-docs",

readme.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,47 @@ gulp.task('default', function () {
2828

2929
#### Meta Information
3030

31-
yamlMeta: true
31+
yamlMeta: true // true || false || 'use-paths'
3232

33-
*Required* in order to construct the navigation menu. When set to true, each document given to gulp-markdown-docs should have a Front Matter YAML header providing needed information about the document.
33+
*Must be either truthy or 'use paths'* in order to construct the navigation menu.
34+
35+
When set to true, each document given to gulp-markdown-docs should have a Front Matter YAML header providing needed information about the document.
3436

3537
```md
3638
---
3739
label: Nav Label
3840
id: unique_slug
3941
categorySlug:
40-
categoryLabel:
41-
categoryRank:
42-
documentRank:
42+
categoryLabel:
43+
categoryRank:
44+
documentRank:
4345
---
4446

4547
# Your Content
4648
...
4749
```
4850

51+
When set to 'use paths', the folder structure will be used to determine the metadata. The first part of the markdown filename should start with a number to indicate the `documentRank`, followed by a space, and the rest of the filename will be used for the `label` (without the extension).
52+
53+
Each markdown file should be in a folder with a similar naming strategy: the first part of the folder name should be a number indicating the `categoryRank`, the rest of the folder name will be used as the `categorySlug` and `categoryLabel`. The `id` is created by appending the `categoryLabel` and `label` properties.
54+
55+
```
56+
docs\10 Intro\10 Definitions.md
57+
docs\10 Intro\20 Theory.md
58+
docs\20 Getting Started\01 Step 1.md
59+
...
60+
```
61+
62+
This has the advantage of keeping your file structure sorted in the same way as the output HTML.
63+
4964
#### Sorting (`yamlMeta` must be true)
5065
If you wanted a document or category to always be at the end you could set this to 10000, or beginning -10000.
5166

5267
All sorting is done on a last-in basis, so the last rank seen is the value used for the category
5368

5469
**NOTE** slugs/ids need to be unique between categories and documents TODO: !!
5570

56-
categorySort: 'alphabetical', // 'alphabetical' || 'rank'
71+
categorySort: 'alphabetical', // 'alphabetical' || 'rank'
5772
documentSort: 'alphabetical', // 'alphabetical' || 'rank'
5873

5974
#### External Stylesheet
@@ -72,15 +87,15 @@ gulp-markdown-docs includes a simple layout by default. `false` will prevent it
7287

7388
templatePath: __dirname + '/resources/index.html'
7489

75-
gulp-markdown-docs includes a simple HTML document by default. Passing a path to a different HTML file allows you to customize the resulting documentation page.
76-
**NOTE** This module looks for `<head>...</head>` to add stylesheets to, and `.doc-nav` and `.doc-content` to append the navigation items and documentation respectively.
90+
gulp-markdown-docs includes a simple HTML document by default. Passing a path to a different HTML file allows you to customize the resulting documentation page.
91+
**NOTE** This module looks for `<head>...</head>` to add stylesheets to, and `.doc-nav` and `.doc-content` to append the navigation items and documentation respectively.
92+
93+
#### Highlight theme
7794

78-
#### Highlight theme
79-
8095
highlightTheme: 'solarized_dark'
8196

8297
see [highlight.js](https://highlightjs.org/) for available themes
83-
98+
8499

85100
#### Markdown
86101

@@ -98,7 +113,7 @@ see [highlight.js](https://highlightjs.org/) for available themes
98113
smartypants: false
99114
}
100115

101-
Defaults shown. See documentation for [marked](https://www.npmjs.org/package/marked) for additional details.
116+
Defaults shown. See documentation for [marked](https://www.npmjs.org/package/marked) for additional details.
102117

103118

104119
## License

0 commit comments

Comments
 (0)