Skip to content

Commit

Permalink
docs(nxdev): consolidate documentats consumption (#10546)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcabanes committed Jun 1, 2022
1 parent 925a27b commit 210edd7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
8 changes: 4 additions & 4 deletions docs/map.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"name": "Nx",
"id": "default",
"name": "Nx docs",
"id": "nx-documentation",
"itemList": [
{
"name": "Getting Started",
Expand Down Expand Up @@ -692,8 +692,8 @@
]
},
{
"name": "Nx Cloud",
"id": "nx-cloud",
"name": "Nx Cloud docs",
"id": "nx-cloud-documentation",
"itemList": [
{
"name": "Intro",
Expand Down
18 changes: 15 additions & 3 deletions nx-dev/data-access-documents/src/lib/documents.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class DocumentsApi {
private readonly options: {
publicDocsRoot: string;
documentSources: DocumentMetadata[];
addAncestor: { id: string; name: string } | null;
}
) {
if (!options.publicDocsRoot) {
Expand All @@ -22,12 +23,23 @@ export class DocumentsApi {
if (!options.documentSources) {
throw new Error('public document sources cannot be undefined');
}

const itemList: DocumentMetadata[] = options.documentSources.flatMap(
(x) => x.itemList
) as DocumentMetadata[];

this.documents = {
id: 'documents',
name: 'documents',
itemList: options.documentSources.flatMap(
(x) => x.itemList
) as DocumentMetadata[],
itemList: !!this.options.addAncestor
? [
{
id: this.options.addAncestor.id,
name: this.options.addAncestor.name,
itemList,
},
]
: itemList,
};
}

Expand Down
19 changes: 10 additions & 9 deletions nx-dev/data-access-menu/src/lib/menu.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class MenuApi {

constructor(
private readonly documents: DocumentMetadata,
private readonly packageDocuments: DocumentMetadata[]
private readonly packageDocuments: DocumentMetadata[] = []
) {}

getMenu(): Menu {
Expand All @@ -23,13 +23,12 @@ export class MenuApi {
const items = createMenuItems(this.documents);
if (items) {
menu = {
sections: [
getBasicSection(items),
getDeepDiveSection(items),
// getApiSection(items),
this.getReferenceApiMenuSection(),
],
sections: [getBasicSection(items), getDeepDiveSection(items)],
};
if (!!this.packageDocuments.length)
menu.sections.push(
this.getReferenceApiMenuSection(this.packageDocuments)
);
} else {
throw new Error(`Cannot find any documents`);
}
Expand All @@ -38,11 +37,13 @@ export class MenuApi {
return menu;
}

getReferenceApiMenuSection(): MenuSection {
getReferenceApiMenuSection(
packageDocuments: DocumentMetadata[]
): MenuSection {
const documents: DocumentMetadata = {
id: 'packages',
name: 'Packages',
itemList: this.packageDocuments,
itemList: packageDocuments,
};

const items = createMenuItems(documents);
Expand Down
11 changes: 6 additions & 5 deletions nx-dev/nx-dev/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MenuApi } from '@nrwl/nx-dev/data-access-menu';
import { PackagesApi } from '@nrwl/nx-dev/data-access-packages/node-only';
import { DocumentMetadata } from '@nrwl/nx-dev/models-document';

// Imports JSON directly so they can be bundled into the app and functions.
// Imports JSON directly, so they can be bundled into the app and functions.
// Also provides some test safety.
import documents from '../public/documentation/map.json';
import packages from '../public/documentation/packages.json';
Expand All @@ -13,15 +13,16 @@ export const packagesApi = new PackagesApi({
packagesIndex: packages,
});

export const documentsApi = new DocumentsApi({
export const nxDocumentsApi = new DocumentsApi({
publicDocsRoot: 'nx-dev/nx-dev/public/documentation',
documentSources: [
documents.find((x) => x.id === 'default'),
documents.find((x) => x.id === 'nx-documentation'),
documents.find((x) => x.id === 'additional-api-references'),
].filter((x) => !!x) as DocumentMetadata[],
addAncestor: null,
});

export const menuApi = new MenuApi(
documentsApi.getDocuments(),
export const nxMenuApi = new MenuApi(
nxDocumentsApi.getDocuments(),
packagesApi.getPackageDocuments().itemList as DocumentMetadata[]
);
12 changes: 6 additions & 6 deletions nx-dev/nx-dev/pages/[...segments].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Footer, Header } from '@nrwl/nx-dev/ui-common';
import cx from 'classnames';
import Router from 'next/router';
import { useCallback, useEffect, useState } from 'react';
import { documentsApi, menuApi, packagesApi } from '../lib/api';
import { nxDocumentsApi, nxMenuApi, packagesApi } from '../lib/api';

interface DocumentationPageProps {
menu: Menu;
Expand Down Expand Up @@ -143,7 +143,7 @@ export async function getStaticProps({
}: {
params: { segments: string[] };
}) {
const menu = menuApi.getMenu();
const menu = nxMenuApi.getMenu();

if (params.segments[0] === 'packages') {
let pkg: PackageMetadata | null = null;
Expand All @@ -170,7 +170,7 @@ export async function getStaticProps({
return {
props: {
document: null,
menu: menuApi.getMenu(),
menu: nxMenuApi.getMenu(),
pkg,
schemaRequest: null,
},
Expand All @@ -180,7 +180,7 @@ export async function getStaticProps({
return {
props: {
document: null,
menu: menuApi.getMenu(),
menu: nxMenuApi.getMenu(),
pkg,
schemaRequest: {
type: params.segments[2],
Expand All @@ -192,7 +192,7 @@ export async function getStaticProps({

let document: DocumentData | undefined;
try {
document = documentsApi.getDocument(params.segments);
document = nxDocumentsApi.getDocument(params.segments);
} catch (e) {
// Do nothing
}
Expand Down Expand Up @@ -220,7 +220,7 @@ export async function getStaticPaths() {
return {
paths: [
...packagesApi.getStaticPackagePaths(),
...documentsApi.getStaticDocumentPaths(),
...nxDocumentsApi.getStaticDocumentPaths(),
],
fallback: 'blocking',
};
Expand Down
2 changes: 1 addition & 1 deletion scripts/documentation/open-graph/generate-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { resolve } from 'path';
const mapJson = readJSONSync('./docs/map.json', 'utf8');

const documents: any[] = [
...mapJson.find((x) => x.id === 'default')?.['itemList'],
...mapJson.find((x) => x.id === 'nx-documentation')?.['itemList'],
...mapJson.find((x) => x.id === 'additional-api-references')?.['itemList'],
].filter(Boolean);

Expand Down

1 comment on commit 210edd7

@vercel
Copy link

@vercel vercel bot commented on 210edd7 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.