-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuildBarProps.mjs
More file actions
151 lines (132 loc) · 4.17 KB
/
buildBarProps.mjs
File metadata and controls
151 lines (132 loc) · 4.17 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import readingTime from 'reading-time';
import { visit } from 'unist-util-visit';
import { getFullName } from './buildSignature.mjs';
import { DOC_API_BLOB_EDIT_BASE_URL } from '../../../constants.mjs';
import {
getCompatibleVersions,
getVersionFromSemVer,
getVersionURL,
} from '../../../utils/generators.mjs';
import { TOC_MAX_HEADING_DEPTH } from '../constants.mjs';
/**
* Generate a combined plain text string from all MDAST entries for estimating reading time.
*
* @param {Array<ApiDocMetadataEntry>} entries - API documentation entries
*/
export const extractTextContent = entries => {
return entries.reduce((acc, entry) => {
visit(entry.content, ['text', 'code'], node => {
acc += node.value || '';
});
return acc;
}, '');
};
/**
* Determines if an entry should be included in the Table of Contents.
* @param {ApiDocMetadataEntry} entry
*/
const shouldIncludeEntryInToC = ({ heading }) =>
// Only include headings with text,
heading?.data?.text.length &&
// and whose depth <= the maximum allowed.
heading?.data?.depth <= TOC_MAX_HEADING_DEPTH;
/**
* Extracts and formats heading information from an API documentation entry.
* @param {ApiDocMetadataEntry} entry
*/
const extractHeading = entry => {
const data = entry.heading.data;
const cliFlagOrEnv = [...data.text.matchAll(/`(-[\w-]+|[A-Z0-9_]+=)/g)];
let heading;
if (cliFlagOrEnv.length > 0) {
heading = cliFlagOrEnv.at(-1)[1];
} else {
const rawName = data.name
// Remove any containing code blocks
.replace(/`/g, '')
// Remove any prefixes (i.e. 'Class:')
.replace(/^[^:]+:/, '')
// Trim the remaining whitespace
.trim();
heading = getFullName(data, rawName);
}
if (data.type === 'ctor') {
heading += ' Constructor';
}
return {
depth: entry.heading.depth,
value: heading,
stability: parseInt(entry.stability?.children[0]?.data.index ?? 2),
slug: data.slug,
data: { id: data.slug, type: data.type },
};
};
/**
* Build the list of heading metadata for sidebar navigation.
*
* @param {Array<ApiDocMetadataEntry>} entries - All API metadata entries
*/
export const extractHeadings = entries =>
entries.filter(shouldIncludeEntryInToC).map(extractHeading);
/**
* Builds metadata for the meta bar (right panel).
*
* @param {ApiDocMetadataEntry} head - Main API metadata entry (used as reference point)
* @param {Array<ApiDocMetadataEntry>} entries - All documentation entries for a given API item
*/
export const buildMetaBarProps = (head, entries) => {
return {
headings: extractHeadings(entries),
addedIn: head.introduced_in || head.added_in || '',
readingTime: readingTime(extractTextContent(entries)).text,
viewAs: [
['JSON', `${head.api}.json`],
['MD', `${head.api}.md`],
],
editThisPage: `${DOC_API_BLOB_EDIT_BASE_URL}${head.api}.md`,
};
};
/**
* Converts a compatible version entry into a version label and link.
*
* @param {Array<ApiDocReleaseEntry>} compatibleVersions - Compatible versions
* @param {string} api - API identifier (used in link)
*/
export const formatVersionOptions = (compatibleVersions, api) => {
return compatibleVersions.map(({ version, isLts, isCurrent }) => {
const parsed = getVersionFromSemVer(version);
const value = getVersionURL(parsed, api);
let label = `v${parsed}`;
if (isLts) {
label += ' (LTS)';
}
if (isCurrent) {
label += ' (Current)';
}
return {
value,
label,
};
});
};
/**
* Builds metadata for the sidebar (left panel).
*
* @param {ApiDocMetadataEntry} entry - Current documentation entry
* @param {Array<ApiDocReleaseEntry>} releases - Available API releases
* @param {import('semver').SemVer} version - Current SemVer version
* @param {Array<[string, string]>} docPages - Available doc pages for sidebar navigation
*/
export const buildSideBarProps = (entry, releases, version, docPages) => {
const compatibleVersions = getCompatibleVersions(
entry.introduced_in,
releases,
true
);
return {
versions: formatVersionOptions(compatibleVersions, entry.api),
currentVersion: `v${version.version}`,
pathname: `${entry.api}.html`,
docPages,
};
};