This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathdocumentation.js
510 lines (457 loc) · 15.9 KB
/
documentation.js
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import { graphql } from "gatsby";
import { groupBy } from "helpers/groupBy";
import { buildPathFromFile } from "helpers/routes";
import {
compareNestedEntries,
compareOrders,
compareName,
} from "helpers/sortReference";
export const DOCS_CONTENT_URL =
"https://github.com/stellar/new-docs/blob/master/content/";
export const query = graphql`
fragment ApiReferencePage on Mdx {
id
frontmatter {
title
order
}
body
parent {
... on File {
relativePath
relativeDirectory
fields {
metadata {
data {
order
title
sortMethod
}
}
}
}
}
}
`;
export const buildRelPath = (relativeDirectory, rootDir) =>
relativeDirectory.replace(rootDir, "") || "/";
/**
* findInitialOpenTopics builds an object of booleans signifying which nav items should be in an open state on page load.
* @param {object} data Raw data from graphQL query.
* @param {string} pagePath Path of the current page.
* @param {string} rootDir The root dir, as defined in CreateDocsPage.js.
* @returns {object} An object with the format of
* {
* [path]: boolean
* }
*/
export const findInitialOpenTopics = (data, pagePath, rootDir) => {
const initialTopicsState = {};
const findPath = (relPath, pgPath) => {
initialTopicsState[relPath] = relPath === pgPath;
const pathSegments = relPath.split("/");
const currentPathSegments = pgPath.split("/");
if (currentPathSegments.length > 2) {
findPath(
pathSegments.slice(0, pathSegments.length - 1).join("/"),
currentPathSegments.slice(0, currentPathSegments.length - 1).join("/"),
);
}
};
data.forEach((file) => {
const relPath = buildRelPath(file.fieldValue, rootDir);
findPath(relPath, pagePath);
});
return initialTopicsState;
};
/**
* findArticle recursively travels down file paths to find child articles
* @param {string} pagePath / delimited string representing filepath.
* @param {object} docsContents Passed object to traverse.
* @returns {object} An object with the format of
* {
* [articleName]: {
* body,
* headings,
* id,
* modifiedTime,
* nextUp,
* title,
* url
* }
* }
*/
export const findArticle = (pagePath, docsContents, isNested = false) => {
if (!pagePath) return docsContents.articles;
const levels = pagePath.split("/");
return findArticle(
levels[2] ? `/${levels[2]}` : null,
isNested
? docsContents.articles[`/${levels[1]}`]
: docsContents[`/${levels[1]}`],
true,
);
};
/**
* insertPageData inserts page data and articles into corresponding keys of object
* @param {string} pagePath / delimited string representing filepath.
* @param {object} contents Passed object that will update object in caller.
* @param {object} articles Object containing all articles scoped to path.
* @param {object} rootPageData Title, TopicPath, and Id associated with each path.
* @returns {object} An object with the format of
* {
* [folderName]: {
* articles: {
* { mdxNode }
* { nestedArticles}
* },
* id,
* topicPath,
* title
* }
* }
*/
const insertPageData = (pagePath, contents, articles, rootPageData) => {
const currentNode = contents;
const enterDir = (remainingPath, newNode, isNested) => {
if (!remainingPath) {
Object.assign(newNode, rootPageData);
Object.assign(newNode.articles, articles);
return contents;
}
const levels = remainingPath.split("/");
/* eslint-disable no-param-reassign */
if (!levels[1]) {
newNode["/"] = { articles: {} };
} else if (!newNode || !newNode[`/${levels[1]}`]) {
if (isNested) {
newNode.articles[`/${levels[1]}`] = { articles: {} };
} else {
newNode[`/${levels[1]}`] = { articles: {} };
}
}
/* eslint-enable no-param-reassign */
return enterDir(
levels[2] ? `/${levels[2]}` : null,
isNested ? newNode.articles[`/${levels[1]}`] : newNode[`/${levels[1]}`],
true,
);
};
enterDir(pagePath, currentNode, false);
};
/**
* buildDocsContents creates an object from the data pulled from graphQL
* @param {object} data Raw data from GraphQL query.
* @param {string} rootDir The root dir, as defined in CreateDocsPage.js.
* @returns {object} An object with the format of
* {
* [folderName]: {
* articles: {
* { mdxNode }
* { nestedArticles}
* },
* id,
* topicPath,
* title
* }
* }
*/
export const buildDocsContents = (data, rootDir) => {
const contents = {};
[...data].forEach((topic) => {
const { fieldValue: topicPath } = topic;
const firstTopic = topic.nodes[0];
const relPath = buildRelPath(topicPath, rootDir);
const topicId = firstTopic.id;
const topicOrder = firstTopic.fields?.metadata.data.order;
const topicTitle = firstTopic.fields
? firstTopic.fields.metadata.data.title
: "MISSING METADATA.JSON";
const articles = {};
const isAlphabeticalOrder =
firstTopic.fields?.metadata.data.sortMethod === "alphabetical";
/* if the nodes' parent has a metadata.json with sortMethod === "alphabetical"
sort the nodes alphabetically */
if (isAlphabeticalOrder) {
topic.nodes.sort(compareName);
}
topic.nodes.forEach((node) => {
const { childMdx, modifiedTime, name, relativePath } = node;
const mdxLink = relativePath && DOCS_CONTENT_URL + relativePath;
const {
frontmatter: { title: articleTitle, description },
id: articleId,
} = childMdx;
articles[name] = {
id: articleId,
githubLink: mdxLink,
modifiedTime,
title: articleTitle || "{`title` Not Found}",
description,
url: buildPathFromFile(relativePath),
};
});
const rootPageData = {
id: topicId,
topicPath: relPath,
title: topicTitle,
order: topicOrder,
};
insertPageData(relPath, contents, articles, rootPageData);
});
/* After its nested page data are sorted and added in,
sort its parent by topicOrder which is metadata.data.order */
const sortedDocs = Object.entries(contents)
.sort(compareNestedEntries)
/* reverse it to object */
.reduce(
(acc, [k, v]) => ({
...acc,
[k]: v,
}),
{},
);
// Now that content is in proper order, travel through tree and add Next Up link to each article
const contentsList = Object.values(sortedDocs);
contentsList.forEach((topicData, topicIndex) => {
const topicArticles = Object.values(topicData.articles);
const nextTopic = contentsList[topicIndex + 1];
addNextUpToArticles(topicArticles, 0, nextTopic);
});
return sortedDocs;
};
/**
* addNextUpToArticles recursively travels through contents tree to add a Next Up property to each
* @param {array} articles List of articles of the current topic.
* @param {number} articleIndex The index of the article we're working with
* @param {number} nextTopic List of articles of the next topic
* @returns {void}
*/
const addNextUpToArticles = (articles, articleIndex, nextTopic) => {
if (articles.length === articleIndex) return;
const articleData = articles[articleIndex];
if (articleData?.articles) {
addNextUpToArticles(Object.values(articleData.articles), 0, nextTopic);
} else {
Object.assign(articleData, {
nextUp: nextUpLink(articles, articleIndex, nextTopic),
});
addNextUpToArticles(articles, articleIndex + 1, nextTopic);
}
};
/**
* nextUpLink returns the url and title of the next article, whether it's in the same topic or if it's in the next topic
* @param {array} topicArticles List of articles of the current topic.
* @param {number} articleIndex The index of the article we're working with
* @param {number} nextTopic List of articles of the next topic
* @returns {object} { title, url }
*/
export const nextUpLink = (topicArticles, articleIndex, nextTopic) => {
// // Go to next topic
if (topicArticles.length === articleIndex + 1) {
// We've reached the end
if (!nextTopic) {
return null;
}
const nextTopicFirstArticle = findNextFirstArticle(nextTopic);
return {
title: nextTopicFirstArticle.title,
url: nextTopicFirstArticle.url,
};
}
// Go to next article in topic
const nextChild = topicArticles[articleIndex + 1];
const nextNestedFirstArticle = findNextFirstArticle(nextChild);
return {
title: nextNestedFirstArticle.title,
url: nextNestedFirstArticle.url,
};
};
/**
* findNextFirstArticle recursively drills down nested articles to find the first article we can link to
* Scenario 1: Next article is a sibling, simply return that article
* Scenario 2: Next article is in another topic, drill down a level to get that topic's articles
* Scenario 3: Next article is nested multiple levels deep, keep drilling down
* @param {object} articleData List of articles of the current topic.
* @returns {object} { title, url }
*/
const findNextFirstArticle = (articleData) => {
if (!articleData.articles) return articleData;
const firstChild = Object.values(articleData.articles)[0];
if (firstChild.articles) {
return findNextFirstArticle(firstChild);
}
return firstChild;
};
// The `groupByCategory` function is used to build up the categories for the API
// Reference sidebar. We want these to share the same behavior as the rest of the
// page links, so we need to pass through the path somehow.
// This is a little hacky, but the rest of the sidebars assume they won't affect
// the route.
const useHrefAsId = (item) => {
if (!item.parent) {
return item;
}
return {
...item,
id: buildPathFromFile(item.parent.relativePath),
};
};
const createNestedItems = (totalCategories, currentCategoryItems) => ({
id: buildPathFromFile(currentCategoryItems[0].parent.relativePath),
title: currentCategoryItems[0].folder.title,
directory: currentCategoryItems[0].directory,
previousParent: totalCategories[totalCategories.length - 2],
currentDirectory: currentCategoryItems[0].currentDirectory,
order: currentCategoryItems[0].folder
? currentCategoryItems[0].folder.order
: currentCategoryItems[0].frontmatter.order,
items: currentCategoryItems.map(useHrefAsId),
});
export const groupByCategory = (referenceDocs) => {
const groupByParentCategory = groupBy(referenceDocs, "directory");
return Object.keys(groupByParentCategory).reduce((acc, category) => {
const splitCategories = category.split("/");
const numberOfCategories = splitCategories.length;
const categoryName = splitCategories[0];
if (!acc[categoryName]) {
acc[categoryName] = groupByParentCategory[category].map(useHrefAsId);
}
if (numberOfCategories > 1) {
const currentCategoryItems = groupByParentCategory[category];
const nestedItemsObj = createNestedItems(
splitCategories,
currentCategoryItems,
);
if (categoryName !== nestedItemsObj.previousParent) {
const newItems = acc[categoryName].find(
(el) => el.currentDirectory === nestedItemsObj.previousParent,
);
newItems.items.push(nestedItemsObj);
newItems.items.sort(compareOrders);
} else {
acc[categoryName].push(nestedItemsObj);
}
}
return acc;
}, {});
};
/**
* consolidateToSection is a reducer function to consolidate
* all of subsequent items after <h2/> into an array of section
* For <TrackedContent/> to surround its H2 and the content for /docs
* @returns {array} An array of objects with sections grouped
*/
export const consolidateToSection = () => {
let lastH2Index;
return function consolidateReducer(acc, ele, index) {
const arr = [];
if (ele.props.mdxType === "h2") {
if (!lastH2Index) {
lastH2Index = index;
} else {
lastH2Index += 1;
}
arr.push(ele);
acc.push(arr);
} else if (lastH2Index && index > lastH2Index) {
acc[lastH2Index].push(ele);
} else {
acc.push(ele);
}
return acc;
};
};
export const ensureArray = (maybeArray) =>
Array.isArray(maybeArray) ? maybeArray : [maybeArray];
const combineAdjacentStrings = (list) =>
list.reduce((accum, item) => {
const lastIndex = accum.length - 1;
if (typeof accum[lastIndex] === "string" && typeof item === "string") {
// eslint-disable-next-line no-param-reassign
accum[lastIndex] = `${accum[lastIndex]}${item}`;
} else {
accum.push(item);
}
return accum;
}, []);
/**
* buildAttributesList accepts React children and returns an attributes object.
* @param {array} mdxElements An array of React elements. It expects to find an
* unordered list parsed from MDX in the format of:
* - ATTRIBUTES
* - DATA TYPE
* - DESCRIPTION
* - SUB ATTRIBUTES
* - DATA TYPE
* - DESCRIPTION
* This can nest to an arbitrary depth. If a slot should be left empty, make sure
* to put some whitespace, or the list item gets dropped completely
* @returns {array} An array of objects with keys { name, type, description,
* childAttributes }. `childAttributes` recurses, and is also an array of objects
* with the same keys.
*/
export const buildAttributesList = (mdxElements) => {
const nodes = ensureArray(mdxElements);
if (process.env.NODE_ENV !== "production") {
console.assert(
nodes.length === 1,
"[AttributeTable] There must only be 1 markdown list within <AttributeTable>",
);
}
const list = nodes[0];
if (process.env.NODE_ENV !== "production") {
console.assert(
list.props.mdxType === "ul",
"[AttributeTable] The markdown within <AttributeTable> must be an unordered list",
);
}
const listItems = getListItems(list);
return listItems.map(getAttributes);
};
const getListItems = (listElement) => {
const listChildren = ensureArray(listElement?.props.children || []);
// Might need to recurse?
return listChildren.filter((c) => c.props.mdxType === "li");
};
const getAttributes = (listItemElement) => {
// Some text elements parse weirdly, like `\_links`, and produce multiple
// strings. Make sure any string values are merged into a single string.
const children = combineAdjacentStrings(
ensureArray(listItemElement.props.children),
);
if (process.env.NODE_ENV !== "production") {
console.assert(
children.length === 2,
`[AttributeTable] Expected attribute list item to have 2 children, a string and 2 list items. Found ${children.length} instead.`,
);
}
const [name, subList] = children;
const [typeElement, descriptionElement] = getListItems(subList);
if (process.env.NODE_ENV !== "production" && !descriptionElement) {
// If we have an empty list item (i.e. a `-` with no trailing space), that
// appears to get obliterated and cause rendering to blow up. Blow up with a
// descriptive message instead. This was a pain in the ass to figure out.
throw new Error(
"No description found. This can happen if the type field (the first list item below an attribute) is left blank and trailing whitespace gets removed. Make sure your editor isn't removing whitespace on save.",
);
}
const descChildren = ensureArray(descriptionElement.props.children);
const lastIndex = descChildren.length - 1;
// If there's a ul at the end, those are the child attributes. Everything else
// is description. Description will be an array of multiple children if there's
// formatting, for example.
const childAttributesList =
descChildren[lastIndex]?.props?.mdxType === "ul"
? descChildren[lastIndex]
: null;
const description = descChildren.filter((x) => x !== childAttributesList);
const childAttributes = getListItems(childAttributesList).map(getAttributes);
return {
name,
type:
typeElement.props.children === "skip" ? null : typeElement.props.children,
description: description.length === 1 ? description[0] : description,
childAttributes: childAttributes.length > 0 ? childAttributes : null,
};
};