-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuildBarProps.mjs
More file actions
151 lines (131 loc) · 4.28 KB
/
buildBarProps.mjs
File metadata and controls
151 lines (131 loc) · 4.28 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
'use strict';
import readingTime from 'reading-time';
import { visit } from 'unist-util-visit';
import getConfig from '../../../utils/configuration/index.mjs';
import {
GITHUB_EDIT_URL,
populate,
} from '../../../utils/configuration/templates.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<import('../../metadata/types').MetadataEntry>} 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 {import('../../metadata/types').MetadataEntry} entry
*/
const shouldIncludeEntryInToC = ({ heading }) =>
// Only include headings with text,
heading?.data?.text.length &&
// and whose depth <= the maximum allowed.
heading?.depth <= TOC_MAX_HEADING_DEPTH;
/**
* Extracts and formats heading information from an API documentation entry.
* @param {import('../../metadata/types').MetadataEntry} entry
*/
const extractHeading = entry => {
const data = entry.heading.data;
const cliFlagOrEnv = [...data.text.matchAll(/`(-[\w-]+|[A-Z0-9_]+=)/g)];
const heading =
cliFlagOrEnv.length > 0
? cliFlagOrEnv.at(-1)[1]
: data.text
// Remove any containing code blocks
.replace(/`/g, '')
// Remove any prefixes (i.e. 'Class:')
.replace(/^[^:]+:/, '')
// Trim the remaining whitespace
.trim();
return {
depth: entry.heading.depth,
value: heading,
stability: parseInt(entry.stability?.data.index ?? 2),
slug: data.slug,
data: { id: data.slug, type: data.type },
};
};
/**
* Build the list of heading metadata for sidebar navigation.
*
* @param {Array<import('../../metadata/types').MetadataEntry>} entries - All API metadata entries
*/
export const extractHeadings = entries =>
entries.filter(shouldIncludeEntryInToC).map(extractHeading);
/**
* Builds metadata for the meta bar (right panel).
*
* @param {import('../../metadata/types').MetadataEntry} head - Main API metadata entry (used as reference point)
* @param {Array<import('../../metadata/types').MetadataEntry>} entries - All documentation entries for a given API item
*/
export const buildMetaBarProps = (head, entries) => {
const config = getConfig('jsx-ast');
return {
headings: extractHeadings(entries),
addedIn: head.added || head.introduced_in || '',
readingTime: readingTime(extractTextContent(entries)).text,
viewAs: [
['JSON', `${head.basename}.json`],
['MD', `${head.basename}.md`],
],
editThisPage: `${populate(GITHUB_EDIT_URL, config)}${head.path}.md`,
};
};
/**
* Converts a compatible version entry into a version label and link.
*
* @param {Array<import('../../../parsers/types').ReleaseEntry>} compatibleVersions - Compatible versions
* @param {string} path - path for the version URL
*/
export const formatVersionOptions = (compatibleVersions, path) => {
const config = getConfig('jsx-ast');
return compatibleVersions.map(({ version, isLts, isCurrent }) => {
const parsed = getVersionFromSemVer(version);
const value = getVersionURL(parsed, path, config.baseURL);
let label = `v${parsed}`;
if (isLts) {
label += ' (LTS)';
}
if (isCurrent) {
label += ' (Current)';
}
return {
value,
label,
};
});
};
/**
* Builds metadata for the sidebar (left panel).
*
* @param {import('../../metadata/types').MetadataEntry} entry - Current documentation entry
* @param {Array<[string, string]>} docPages - Available doc pages for sidebar navigation
*/
export const buildSideBarProps = (entry, docPages) => {
const config = getConfig('jsx-ast');
const compatibleVersions = getCompatibleVersions(
entry.introduced_in,
config.changelog,
true
);
return {
versions: formatVersionOptions(compatibleVersions, entry.path),
currentVersion: `v${config.version.version}`,
pathname: `${entry.basename}.html`,
docPages,
};
};