-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathindex.mjs
More file actions
317 lines (280 loc) · 10.3 KB
/
index.mjs
File metadata and controls
317 lines (280 loc) · 10.3 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict';
import { u as createTree } from 'unist-builder';
import { SKIP } from 'unist-util-visit';
import {
DOC_API_STABILITY_SECTION_REF_URL,
VALID_JAVASCRIPT_PROPERTY,
} from './constants.mjs';
import {
extractYamlContent,
parseHeadingIntoMetadata,
parseYAMLIntoMetadata,
transformTypeToReferenceLink,
transformUnixManualToLink,
} from '../parser/index.mjs';
import { getRemark } from '../remark.mjs';
import { transformNodesToString } from '../unist.mjs';
/**
* Creates an instance of the Query Manager, which allows to do multiple sort
* of metadata and content metadata manipulation within an API Doc
*/
const createQueries = () => {
const remark = getRemark();
/**
* Sanitizes the YAML source by returning the inner YAML content
* and then parsing it into an API Metadata object and updating the current Metadata
*
* @param {import('@types/mdast').Html} node A HTML node containing the YAML content
* @param {ReturnType<import('../../metadata.mjs').default>} apiEntryMetadata The API entry Metadata
*/
const addYAMLMetadata = (node, apiEntryMetadata) => {
const yamlContent = extractYamlContent(node);
apiEntryMetadata.updateProperties(parseYAMLIntoMetadata(yamlContent));
return [SKIP];
};
/**
* Parse a Heading node into metadata and updates the current metadata
*
* @param {import('@types/mdast').Heading} node A Markdown heading node
* @param {ReturnType<import('../../metadata.mjs').default>} apiEntryMetadata The API entry Metadata
*/
const setHeadingMetadata = (node, apiEntryMetadata) => {
const stringifiedHeading = transformNodesToString(node.children);
// Append the heading metadata to the node's `data` property
node.data = parseHeadingIntoMetadata(stringifiedHeading, node.depth);
apiEntryMetadata.setHeading(node);
};
/**
* Updates a Markdown link into a HTML link for API docs
*
* @param {import('@types/mdast').Link} node A Markdown link node
*/
const updateMarkdownLink = node => {
node.url = node.url.replace(
createQueries.QUERIES.markdownUrl,
(_, filename, hash = '') => `${filename}.html${hash}`
);
return [SKIP];
};
/**
* Updates a reference
*
* @param {import('@types/mdast').Text} node The current node
* @param {import('@types/mdast').Parent} parent The parent node
* @param {string|RegExp} query The search query
* @param {Function} transformer The function to transform the reference
*
*/
const updateReferences = (query, transformer, node, parent) => {
const replacedTypes = node.value
.replace(query, transformer)
// Remark doesn't handle leading / trailing spaces, so replace them with
// HTML entities.
.replace(/^\s/, ' ')
.replace(/\s$/, ' ');
// This changes the type into a link by splitting it up into several nodes,
// and adding those nodes to the parent.
const {
children: [newNode],
} = remark.parse(replacedTypes);
// Find the index of the original node in the parent
const index = parent.children.indexOf(node);
// Replace the original node with the new node(s)
parent.children.splice(index, 1, ...newNode.children);
return [SKIP];
};
/**
* Updates a Markdown Link Reference into an actual Link to the Definition
*
* @param {import('@types/mdast').LinkReference} node A link reference node
* @param {Array<import('@types/mdast').Definition>} definitions The Definitions of the API Doc
*/
const updateLinkReference = (node, definitions) => {
const definition = definitions.find(
({ identifier }) => identifier === node.identifier
);
node.type = 'link';
node.url = definition.url;
return [SKIP];
};
/**
* Parses a Stability Index Entry and updates the current Metadata
*
* @param {import('@types/mdast').Blockquote} node Thead Link Reference Node
* @param {ReturnType<import('../../metadata.mjs').default>} apiEntryMetadata The API entry Metadata
*/
const addStabilityMetadata = (node, apiEntryMetadata) => {
// `node` is a `blockquote` node, and the first child will always be
// a `paragraph` node, so we can safely access the children of the first child
// which we use as the prefix and description of the Stability Index
const stabilityPrefix = transformNodesToString(node.children[0].children);
// Attempts to grab the Stability index and description from the prefix
const matches = createQueries.QUERIES.stabilityIndex.exec(stabilityPrefix);
// Ensures that the matches are valid and that we have at least 3 entries
if (matches && matches.length === 3) {
// Updates the `data` property of the Stability Index node
// so that the original node data can also be inferred
node.data = {
// The 2nd match should be the group that matches the Stability Index
index: matches[1],
// The 3rd match should be the group containing all the remaining text
// which is used as a description (we trim it to an one liner)
description: matches[2].replace(/\n/g, ' ').trim(),
};
// Creates a new Tree node containing the Stability Index metadata
const stabilityIndexNode = createTree(
'root',
{ data: node.data },
node.children
);
// Adds the Stability Index metadata to the current Metadata entry
apiEntryMetadata.addStability(stabilityIndexNode);
}
return [SKIP];
};
/**
* Updates the Stability Index Prefixes to be Markdown Links
* to the API documentation
*
* @param {import('vfile').VFile} vfile The source Markdown file before any modifications
*/
const updateStabilityPrefixToLink = vfile => {
// The `vfile` value is a String (check `loaders.mjs`)
vfile.value = String(vfile.value).replace(
createQueries.QUERIES.stabilityIndexPrefix,
match => `[${match}](${DOC_API_STABILITY_SECTION_REF_URL})`
);
};
return {
addYAMLMetadata,
setHeadingMetadata,
updateMarkdownLink,
/** @param {Array<import('@types/mdast').Node>} args */
updateTypeReference: (...args) =>
updateReferences(
createQueries.QUERIES.normalizeTypes,
transformTypeToReferenceLink,
...args
),
/** @param {Array<import('@types/mdast').Node>} args */
updateUnixManualReference: (...args) =>
updateReferences(
createQueries.QUERIES.unixManualPage,
transformUnixManualToLink,
...args
),
updateLinkReference,
addStabilityMetadata,
updateStabilityPrefixToLink,
};
};
// This defines the actual REGEX Queries
createQueries.QUERIES = {
// Fixes the references to Markdown pages into the API documentation
markdownUrl: /^(?![+a-zA-Z]+:)([^#?]+)\.md(#.+)?$/,
// ReGeX to match the {Type}<Type> (API type references)
normalizeTypes: /(\{|<)(?! )[^<({})>]+(?! )(\}|>)/g,
// ReGex to match the type API type references that got already parsed
// so that they can be transformed into HTML links
linksWithTypes: /\[`<[^<({})>]+>`\]\((\S+)\)/g,
// ReGeX for handling Stability Indexes Metadata
stabilityIndex: /^Stability: ([0-5](?:\.[0-3])?)(?:\s*-\s*)?(.*)$/s,
// ReGeX for handling the Stability Index Prefix
stabilityIndexPrefix: /Stability: ([0-5](?:\.[0-3])?)/,
// ReGeX for retrieving the inner content from a YAML block
yamlInnerContent: /^<!--[ ]?(?:YAML([\s\S]*?)|([ \S]*?))?[ ]?-->/,
// ReGeX for finding references to Unix manuals
unixManualPage: /\b([a-z.]+)\((\d)([a-z]?)\)/g,
// ReGeX for determing a typed list's non-property names
typedListStarters: /^(Returns|Extends|Type):?\s*/,
};
createQueries.UNIST = {
/**
* @param {import('@types/mdast').Blockquote} blockquote
* @returns {boolean}
*/
isStabilityNode: ({ type, children }) =>
type === 'blockquote' &&
createQueries.QUERIES.stabilityIndex.test(transformNodesToString(children)),
/**
* @param {import('@types/mdast').Html} html
* @returns {boolean}
*/
isYamlNode: ({ type, value }) =>
type === 'html' && createQueries.QUERIES.yamlInnerContent.test(value),
/**
* @param {import('@types/mdast').Text} text
* @returns {boolean}
*/
isTextWithType: ({ type, value }) =>
type === 'text' && createQueries.QUERIES.normalizeTypes.test(value),
/**
* @param {import('@types/mdast').Text} text
* @returns {boolean}
*/
isTextWithUnixManual: ({ type, value }) =>
type === 'text' && createQueries.QUERIES.unixManualPage.test(value),
/**
* @param {import('@types/mdast').Html} html
* @returns {boolean}
*/
isHtmlWithType: ({ type, value }) =>
type === 'html' && createQueries.QUERIES.linksWithTypes.test(value),
/**
* @param {import('@types/mdast').Link} link
* @returns {boolean}
*/
isMarkdownUrl: ({ type, url }) =>
type === 'link' && createQueries.QUERIES.markdownUrl.test(url),
/**
* @param {import('@types/mdast').Heading} heading
* @returns {boolean}
*/
isHeading: ({ type, depth }) =>
type === 'heading' && depth >= 1 && depth <= 5,
/**
* @param {import('@types/mdast').LinkReference} linkReference
* @returns {boolean}
*/
isLinkReference: ({ type, identifier }) =>
type === 'linkReference' && !!identifier,
/**
* @param {import('@types/mdast').List} list
* @returns {boolean}
*/
isTypedList: list => {
// Exit early if not a list node
if (list.type !== 'list') {
return false;
}
// Get the content nodes of the first list item's paragraph
const [node, ...contentNodes] =
list?.children?.[0]?.children?.[0]?.children ?? [];
const possibleProperty = node?.value?.trimStart();
// Exit if no content in node (or if no node exists)
if (!possibleProperty) {
return false;
}
// Check for other starters
if (possibleProperty.match(createQueries.QUERIES.typedListStarters)) {
return true;
}
// Check for direct type link pattern (starts with '<')
if (node.type === 'link' && node.children?.[0]?.value?.[0] === '<') {
return true;
}
// Check for inline code + space + type link pattern
if (
node.type === 'inlineCode' &&
possibleProperty.match(VALID_JAVASCRIPT_PROPERTY) &&
contentNodes[0]?.value?.trim() === '' &&
contentNodes[1]?.type === 'link' &&
contentNodes[1]?.children?.[0]?.value?.[0] === '<'
) {
return true;
}
// Not a typed list
return false;
},
};
export default createQueries;