Skip to content

feat(config): add file property for notes #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ This puts the top level API documentation for the `Map`, `LngLat`, and `LngLatBo
items in the given order, and inserts a narrative item titled `Geography`
after the section on maps. The `description` property of that narrative item
is interpreted as Markdown.
If you would like reuse your existing markdown files or just keep the content separate from the configuration you can use the `file` property. It is a filename that will be resolved against `process.cwd()`.

So with a `documentation.yml` file like this

```yml
toc:
- Map
- name: Geography
file: geo.md
- LngLat
- LngLatBounds
```

and a file `geo.md`

```markdown
These are Mapbox GL JS's ways of representing locations
and areas on the sphere.
```

it would produce the same output as the previous example.
11 changes: 11 additions & 0 deletions lib/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var parseMarkdown = require('./parse_markdown');
var chalk = require('chalk');
var path = require('path');
var fs = require('fs');

/**
* Sort two documentation objects, given an optional order object. Returns
Expand Down Expand Up @@ -36,6 +38,15 @@ module.exports = function sortDocs(comments, options) {
var fixed = options.toc.filter(function (val) {
return typeof val === 'object' && val.name;
}).map(function (val) {
if (typeof val.file === 'string') {
var filename = path.join(process.cwd(), val.file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this more, in a case like

documentation build --config=some/directory/documentation.yml index.js

The paths in documentation.yml should be relative to some/directory rather than to ./. In that case, we might want to add the config path when we go through configParser (docs for that on yargs), so that sort can use it as a relative path if it's present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it so that

  • if loading through the config, the file is resolved by that path.
  • sort.js checks for absolute paths and uses them if found, otherwise it resolves against process.cwd()

try {
val.description = fs.readFileSync(filename).toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently sync because this pipeline is currently all sync. I can also move it to the async version if that's preferred but that would mean changing more in this file and the interface to sort.

delete val.file;
} catch (err) {
process.stderr.write(chalk.red('Failed to read file ' + filename));
}
}
if (typeof val.description === 'string') {
val.description = parseMarkdown(val.description);
}
Expand Down
1 change: 1 addition & 0 deletions test/fixture/snowflake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# The Snowflake
50 changes: 50 additions & 0 deletions test/lib/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,53 @@ test('sort an already-sorted stream containing a section/description', function
t.deepEqual(sortTwice, [carrot, sectionMarkdown, bananas, apples]);
t.end();
});

test('sort toc with files', function (t) {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };

var snowflake = {
name: 'snowflake',
file: 'test/fixture/snowflake.md'
};

var processedSnowflake = {
name: 'snowflake',
kind: 'note',
description: {
children: [{
children: [{
position: {
end: {column: 16, line: 1, offset: 15},
indent: [],
start: {column: 3, line: 1, offset: 2}
},
type: 'text',
value: 'The Snowflake'
}],
depth: 1,
position: {
end: {column: 16, line: 1, offset: 15},
indent: [],
start: {column: 1, line: 1, offset: 0}
},
type: 'heading'
}],
position: {
end: {column: 1, line: 2, offset: 16},
start: {column: 1, line: 1, offset: 0}
},
type: 'root'
}
};
t.deepEqual(sort([
apples, carrot, bananas
], {
toc: [snowflake]
}), [
processedSnowflake, apples, carrot, bananas
], 'with configuration');

t.end();
});