11import path from 'path' ;
2- import textile from 'textile-js' ;
32import { identity } from 'lodash' ;
43import { safeFileExists } from './safeFileExists' ;
5- import { flattenContentOrderedList , maybeRetrievePartial } from '../transform' ;
6- import { postParser } from '../transform/post-parser' ;
7- import { htmlParser } from '../html-parser' ;
84import { createLanguagePageVariants } from './createPageVariants' ;
95import { LATEST_ABLY_API_VERSION_STRING } from '../transform/constants' ;
106import { createContentMenuDataFromPage } from './createContentMenuDataFromPage' ;
@@ -20,59 +16,6 @@ const documentTemplate = path.resolve(`src/templates/document.tsx`);
2016const apiReferenceTemplate = path . resolve ( `src/templates/apiReference.tsx` ) ;
2117const examplesTemplate = path . resolve ( `src/templates/examples.tsx` ) ;
2218
23- interface Edge {
24- node : {
25- slug : string ;
26- version ?: string ;
27- contentOrderedList : Array < {
28- data : string ;
29- type : string ;
30- } > ;
31- meta ?: {
32- redirect_from ?: string [ ] ;
33- } ;
34- } ;
35- }
36-
37- interface DocumentQueryResult {
38- allError : {
39- nodes : {
40- message : string ;
41- } [ ] ;
42- } ;
43- allFileHtml : {
44- edges : {
45- node : {
46- slug : string ;
47- contentOrderedList : {
48- data : string ;
49- type : string ;
50- } [ ] ;
51- meta : {
52- redirect_from ?: string [ ] ;
53- } ;
54- } ;
55- } [ ] ;
56- } ;
57- }
58-
59- interface ApiReferenceQueryResult {
60- allFileHtml : {
61- edges : {
62- node : {
63- slug : string ;
64- contentOrderedList : {
65- data : string ;
66- type : string ;
67- } [ ] ;
68- meta : {
69- redirect_from ?: string [ ] ;
70- } ;
71- } ;
72- } [ ] ;
73- } ;
74- }
75-
7619interface ExampleQueryResult {
7720 allExampleFile : {
7821 nodes : {
@@ -102,82 +45,6 @@ interface MdxRedirectsQueryResult {
10245}
10346
10447export const createPages : GatsbyNode [ 'createPages' ] = async ( { graphql, actions : { createPage, createRedirect } } ) => {
105- /**
106- * It's not ideal to have:
107- * * the reusable function `documentCreator` defined inline like this
108- * * so much of `documentCreator` being imperative processing of data
109- * - but Gatsby throws horrible unrelated errors (from onCreateNode) if
110- * you try to extract any of this functionality into an independent composable
111- * and testable function.
112- */
113-
114- // DOCUMENT TEMPLATE
115- const documentResult = await graphql < DocumentQueryResult > ( `
116- query {
117- allError {
118- nodes {
119- message
120- }
121- }
122- allFileHtml(filter: { articleType: { eq: "document" } }) {
123- edges {
124- node {
125- slug
126- contentOrderedList {
127- data
128- type
129- }
130- meta {
131- redirect_from
132- }
133- }
134- }
135- }
136- }
137- ` ) ;
138-
139- /**
140- * We could log here, the reason we don't right now is that the error should already have been caught and logged.
141- * Because Gatsby spawns a bunch of async processes during the onCreateNode step, though, and errors don't prevent
142- * the onCreateNode processes from continuing, the workaround is to create Error nodes and then quit when we get
143- * to the next synchronous stage in the Gatsby pipeline if any Error nodes exist.
144- * It's an awkward solution and an alternative would be welcome, I'm not sure what we would do instead though.
145- * We could log only during createPages and never during onCreateNode, but then if there's an error that does manage
146- * to crash Gatsby somehow or the Error Node is broken, we wouldn't see the output.
147- *
148- * For context:
149- * The original ticket here was EDX-21. When I originally detected missing meta descriptions, I wanted to
150- * console.warn and continue just so editors could see the error. However it was decided that it should be a
151- * requirement to always have a meta description, so the app had to avoid building while still throwing & logging
152- * an error when a page didn't have a meta description.
153- */
154- if ( documentResult . data ?. allError . nodes && documentResult . data . allError . nodes . length > 0 ) {
155- process . exit ( 1 ) ;
156- }
157-
158- // API REFERENCES TEMPLATE
159- const apiReferenceResult = await graphql < ApiReferenceQueryResult > ( `
160- query {
161- allFileHtml(filter: { articleType: { eq: "apiReference" } }) {
162- edges {
163- node {
164- slug
165- contentOrderedList {
166- data
167- type
168- }
169- meta {
170- redirect_from
171- }
172- }
173- }
174- }
175- }
176- ` ) ;
177-
178- // Query partials used in textile files
179- const retrievePartialFromGraphQL = maybeRetrievePartial ( graphql ) ;
180-
18148 // Query our examples
18249 const examplesResult = await graphql < ExampleQueryResult > ( `
18350 query {
@@ -212,83 +79,13 @@ export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions:
21279 }
21380 ` ) ;
21481
215- const documentCreator =
216- ( documentTemplate : string ) =>
217- async ( edge : Edge ) : Promise < string > => {
218- const content = flattenContentOrderedList (
219- await Promise . all ( edge . node . contentOrderedList . map ( retrievePartialFromGraphQL ) ) ,
220- )
221- . map ( ( content : { data : unknown } ) => ( content . data ? content . data : '' ) )
222- . join ( '\n' ) ;
223-
224- const postParsedContent = postParser ( textile ( content ) ) ;
225- const contentOrderedList = htmlParser ( postParsedContent ) ;
226- const contentMenu = contentOrderedList . map ( ( item ) => createContentMenuDataFromPage ( item ) ) ;
227- const [ languages , contentMenuObject ] = createLanguagePageVariants ( identity , documentTemplate ) (
228- contentOrderedList ,
229- edge . node . slug ,
230- ) ;
231-
232- contentMenuObject [ DEFAULT_LANGUAGE ] = contentMenu ;
233-
234- const slug = edge . node . slug ;
235- const script = safeFileExists ( `static/scripts/${ slug } .js` ) ;
236- const pagePath = `/docs/${ slug } ` ;
237-
238- const redirectFromList = edge . node . meta ?. redirect_from ;
239-
240- if ( redirectFromList ) {
241- redirectFromList . forEach ( ( redirectFrom ) => {
242- const redirectFromUrl = new URL ( redirectFrom , siteMetadata . siteUrl ) ;
243-
244- if ( ! redirectFromUrl . hash ) {
245- // We need to be prefix aware just like Gatsby's internals so it works
246- // with nginx redirects
247- writeRedirect ( redirectFrom , pagePath ) ;
248- }
249-
250- createRedirect ( {
251- fromPath : redirectFrom ,
252- toPath : pagePath ,
253- isPermanent : true ,
254- force : true ,
255- redirectInBrowser : true ,
256- } ) ;
257- } ) ;
258- }
259-
260- createPage ( {
261- path : pagePath ,
262- component : documentTemplate ,
263- context : {
264- slug,
265- version : edge . node . version ?? LATEST_ABLY_API_VERSION_STRING ,
266- language : DEFAULT_LANGUAGE ,
267- languages,
268- contentOrderedList,
269- contentMenu : contentMenuObject ,
270- script,
271- layout : { leftSidebar : true , rightSidebar : true , searchBar : true , template : 'base' } ,
272- } ,
273- } ) ;
274- return slug ;
275- } ;
276-
27782 createRedirect ( {
27883 fromPath : '/' ,
27984 toPath : '/docs' ,
28085 isPermanent : true ,
28186 redirectInBrowser : true ,
28287 } ) ;
28388
284- if ( ! documentResult . data ) {
285- throw new Error ( 'Document result is undefined' ) ;
286- }
287-
288- if ( ! apiReferenceResult . data ) {
289- throw new Error ( 'API reference result is undefined' ) ;
290- }
291-
29289 if ( ! mdxRedirectsResult . data ) {
29390 throw new Error ( 'MDX redirects result is undefined' ) ;
29491 }
@@ -355,8 +152,6 @@ export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions:
355152 } ;
356153
357154 await Promise . all ( [
358- ...documentResult . data . allFileHtml . edges . map ( documentCreator ( documentTemplate ) ) ,
359- ...apiReferenceResult . data . allFileHtml . edges . map ( documentCreator ( apiReferenceTemplate ) ) ,
360155 ...examples . map ( exampleCreator ) ,
361156 ...mdxRedirectsResult . data . allMdx . nodes . map ( mdxRedirectCreator ) ,
362157 ] ) ;
0 commit comments