-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceref.js
More file actions
64 lines (59 loc) · 1.55 KB
/
sourceref.js
File metadata and controls
64 lines (59 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var path = require("path");
var fs = require("fs");
var typeConverters = {
stache: 'html',
mustache: 'html',
component: 'html'
};
var convertType = function(type) {
return typeConverters[type] || type;
};
/**
* @parent bit-docs-tag-sourceref/tags
* @module {bit-docs-process-tags/types/tag} bit-docs-tag-sourceref/tags/sourceref @sourceref
*
* @description Loads a file and wraps it with markdown for code block.
*
* @signature `@sourceref PATH`
*
* Wraps file contents with triple \` so it shows up as a code block.
*
* @param {String} PATH The path to any kind of file.
*
* @body
*
* An example file might look like:
*
* ```js
* var foo = 'bar';
* ```
*
* That gets used like:
*
* ```js
* @@sourceref ./demos/something.js
* ```
*
* And renders like:
*
* @@sourceref ./demos/something.js
*/
module.exports = {
add: function(line, curData) {
var file;
if(this.src.path) {
file = path.join(path.dirname(this.src.path), line.replace("@sourceref", "").trim());
} else {
file = line.replace("@sourceref", "").trim()
}
var type = convertType(path.extname(file).substring(1));
var validCurData = (curData && curData.length !== 2);
var useCurData = validCurData && (typeof curData.description === "string") && !curData.body;
var markdown = '\n```' + type + '\n' + fs.readFileSync(file).toString() + '\n```\n';
if (useCurData) {
curData.description += markdown;
} else {
this.body += markdown;
}
}
};