forked from umijs/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseDoc.js
More file actions
38 lines (31 loc) · 832 Bytes
/
parseDoc.js
File metadata and controls
38 lines (31 loc) · 832 Bytes
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
module.exports = function (docStr) {
docStr = docStr.trim();
const hasYamlConfig = docStr.startsWith('---');
const docArr = docStr.split('\n');
let title = null;
let titleIndex = null;
let yamlEndIndex = null;
let i = hasYamlConfig ? 1 : 0;
while (i < docArr.length) {
const line = docArr[i];
// yaml end
if (line.startsWith('---')) {
yamlEndIndex = i;
}
// title
if (line.startsWith('# ') && titleIndex === null) {
title = line.replace('# ', '');
titleIndex = i;
}
i += 1;
}
if (titleIndex === null && yamlEndIndex !== null) {
titleIndex = yamlEndIndex;
}
return {
hasYamlConfig,
title,
yamlConfig: hasYamlConfig ? docArr.slice(1, yamlEndIndex) : [],
body: docArr.slice(titleIndex === null ? titleIndex : titleIndex + 1),
};
};