1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-26 19:49:26 +01:00
docs/gatsby-node.js

270 lines
9.1 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-console */
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const Swagger = require('swagger-client')
const { redirects } = require('./config')
2018-11-07 12:24:53 +01:00
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
2018-11-08 16:03:16 +01:00
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
2018-11-14 15:17:19 +01:00
let slug = createFilePath({ node, getNode, basePath: 'content' })
let section = parsedFilePath.dir
if (node.frontmatter.slug) {
;({ slug } = node.frontmatter)
}
if (node.frontmatter.section) {
;({ section } = node.frontmatter)
}
createNodeField({
node,
name: 'slug',
value: slug
})
createNodeField({
node,
name: 'section',
value: section
})
}
}
// https://github.com/swagger-api/swagger-js
const getSpec = async () => {
const spec = await Swagger(
'http://petstore.swagger.io/v2/swagger.json'
).then(client => {
return client.spec // The resolved spec
// client.originalSpec // In case you need it
// client.errors // Any resolver errors
// Tags interface
// client.apis.pet.addPet({id: 1, name: "bobby"}).then(...)
// TryItOut Executor, with the `spec` already provided
// client.execute({operationId: 'addPet', parameters: {id: 1, name: "bobby") }).then(...)
})
return spec
}
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
return new Promise((resolve, reject) => {
resolve(
graphql(
`
2018-11-27 13:54:00 +01:00
query {
2018-11-09 22:10:35 +01:00
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/" } }
) {
edges {
node {
fields {
slug
section
}
}
}
}
devOceanDocs: allMarkdownRemark(
2018-11-14 15:17:19 +01:00
filter: {
2018-11-15 14:46:20 +01:00
fileAbsolutePath: { regex: "/dev-ocean/doc/" }
2018-11-14 15:17:19 +01:00
}
) {
edges {
node {
fields {
slug
section
}
frontmatter {
slug
title
description
section
}
}
}
}
}
`
).then(async result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const docTemplate = path.resolve('./src/templates/Doc.jsx')
const posts = result.data.allMarkdownRemark.edges
//
// Create Doc pages
//
posts.forEach(post => {
createPage({
path: `${post.node.fields.slug}`,
component: docTemplate,
context: {
slug: post.node.fields.slug,
section: post.node.fields.section
}
})
})
//
// Create pages from dev-ocean contents
//
const postsDevOcean = result.data.devOceanDocs.edges
postsDevOcean
// only grab files with required frontmatter defined
.filter(
post =>
post.node.frontmatter &&
post.node.frontmatter.slug &&
post.node.frontmatter.title &&
post.node.frontmatter.description &&
post.node.frontmatter.section
)
.forEach(post => {
createPage({
path: `${post.node.fields.slug}`,
component: docTemplate,
context: {
slug: post.node.fields.slug,
section: post.node.fields.section
}
})
2018-11-14 15:17:19 +01:00
})
//
2018-11-27 12:01:44 +01:00
// Create pages from swagger json files
//
2018-11-27 12:01:44 +01:00
const apiSwaggerTemplate = path.resolve(
'./src/templates/Swagger/index.jsx'
2018-11-27 12:01:44 +01:00
)
2018-11-29 22:43:10 +01:00
const petStoreSlug = '/references/petstore/'
2018-11-27 12:01:44 +01:00
try {
const spec = await getSpec()
2018-11-27 13:54:00 +01:00
createPage({
path: petStoreSlug,
component: apiSwaggerTemplate,
context: {
slug: petStoreSlug,
api: spec
}
})
} catch (error) {
console.log(error)
}
2018-11-27 13:54:00 +01:00
const aquariusSpecs = require('./data/aquarius.json')
2018-11-29 22:43:10 +01:00
const aquariusSlug = '/references/aquarius/'
createPage({
path: aquariusSlug,
component: apiSwaggerTemplate,
context: {
slug: aquariusSlug,
api: aquariusSpecs
}
})
const brizoSpecs = require('./data/brizo.json')
2018-11-29 22:43:10 +01:00
const brizoSlug = '/references/brizo/'
createPage({
path: brizoSlug,
component: apiSwaggerTemplate,
context: {
slug: brizoSlug,
api: brizoSpecs
}
2018-11-29 12:53:43 +01:00
})
2019-01-18 17:35:04 +01:00
//
// Create pages from TypeDoc json files
//
2019-01-22 10:44:58 +01:00
const typeDocSpecs = ['./data/squid-js.json']
2019-01-18 17:35:04 +01:00
const typedocTemplate = path.resolve(
'./src/templates/Typedoc/index.jsx'
2019-01-18 17:35:04 +01:00
)
2019-01-22 10:44:58 +01:00
typeDocSpecs.forEach(spec => {
const typedoc = require(spec) // eslint-disable-line
2019-01-18 17:35:04 +01:00
2019-01-22 10:44:58 +01:00
const name = path
.basename(spec)
.split('.json')
.join('')
const slug = `/references/${name}/`
createPage({
path: slug,
component: typedocTemplate,
context: {
slug,
typedoc,
// TODO: defining these classes for inclusion
// needs to be handled somewhere else to keep
// it generic for all TypeDoc specs
classes: [
'ocean/Ocean',
2019-01-28 11:48:07 +01:00
'ocean/OceanAccounts',
'ocean/OceanAssets',
'ocean/OceanAgreements',
2019-01-22 10:44:58 +01:00
'ocean/Account',
2019-01-22 16:48:15 +01:00
'ocean/DID',
'ocean/ServiceAgreements/ServiceAgreement',
2019-01-22 10:44:58 +01:00
'ddo/DDO',
2019-01-22 16:48:15 +01:00
'ddo/Service',
'aquarius/AquariusProvider',
'aquarius/Aquarius',
'aquarius/query/SearchQuery',
'brizo/BrizoProvider',
'brizo/Brizo',
'keeper/Keeper',
'keeper/Web3Provider',
2019-01-29 11:39:15 +01:00
'secretstore/SecretStoreProvider',
'models/Config',
'models/Balance'
2019-01-22 10:44:58 +01:00
]
}
})
2019-01-18 17:35:04 +01:00
})
2018-11-29 10:53:35 +01:00
//
// create redirects
//
redirects.forEach(({ from, to }) => {
createRedirect({
fromPath: from,
redirectInBrowser: true,
toPath: to
})
console.log('Create redirect: ' + from + ' --> ' + to)
})
2018-11-27 12:01:44 +01:00
resolve()
})
)
})
}