1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-02 08:20:22 +01:00
docs/gatsby-node.js

291 lines
7.5 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, swaggerComponents } = require('./config')
2018-11-07 12:24:53 +01:00
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
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
2018-11-14 15:17:19 +01:00
if (node.frontmatter.slug) {
;({ slug } = node.frontmatter)
}
2018-11-14 15:17:19 +01:00
if (node.frontmatter.section) {
;({ section } = node.frontmatter)
}
createNodeField({
node,
name: 'slug',
value: slug
})
createNodeField({
node,
name: 'section',
value: section
})
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions
return new Promise((resolve, reject) => {
resolve(
graphql(
`
query {
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/" } }
) {
edges {
node {
fields {
slug
section
}
}
}
}
2021-04-12 11:19:11 +02:00
allRepoMarkdown: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/markdowns/markdowns/" } }
2021-04-12 11:19:11 +02:00
) {
edges {
node {
id
html
htmlAst
tableOfContents
frontmatter {
2021-05-24 17:11:41 +02:00
slug
source
2021-04-12 11:19:11 +02:00
title
app
module
2021-06-02 01:23:41 +02:00
version
2021-04-10 17:08:14 +02:00
}
}
}
2021-04-12 11:19:11 +02:00
}
2021-04-10 17:08:14 +02:00
2020-11-12 16:37:49 +01:00
oceanJs: github {
repository(name: "ocean.js", owner: "oceanprotocol") {
name
releases(
first: 30
orderBy: { field: CREATED_AT, direction: DESC }
) {
edges {
node {
isPrerelease
isDraft
releaseAssets(first: 1, name: "lib.json") {
2020-11-12 16:37:49 +01:00
edges {
node {
name
downloadUrl
}
}
}
}
}
}
}
}
}
`
).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
2021-04-12 11:19:11 +02:00
//
// 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
}
})
})
2021-08-16 22:03:52 +02:00
// API: aquarius
await createSwaggerPages(createPage)
2021-08-16 22:03:52 +02:00
await createDeploymentsPage(createPage)
2020-11-12 16:37:49 +01:00
// API: ocean.js
2021-06-21 23:50:37 +02:00
const lastRelease =
result.data.oceanJs.repository.releases.edges.filter(
({ node }) =>
!node.isPrerelease &&
!node.isDraft &&
node.releaseAssets.edges.length > 0
)[0].node.releaseAssets.edges[0].node
2020-11-12 16:37:49 +01:00
await createTypeDocPage(
createPage,
result.data.oceanJs.repository.name,
lastRelease.downloadUrl
)
//
// create redirects
//
redirects.forEach(({ from, to }) => {
createRedirect({
fromPath: from,
redirectInBrowser: true,
toPath: to
})
console.log('Create redirect: ' + from + ' --> ' + to)
})
2021-04-10 17:08:14 +02:00
const markdowns = result.data.allRepoMarkdown.edges
const oceanPyList = filterMarkdownList(markdowns, 'ocean.py')
const aquariusList = filterMarkdownList(markdowns, 'aquarius')
const providerList = filterMarkdownList(markdowns, 'provider')
2021-06-01 10:56:53 +02:00
const subgraphList = filterMarkdownList(markdowns, 'ocean-subgraph')
await createReadTheDocsPage(createPage, 'ocean-py', oceanPyList)
2021-04-11 12:15:26 +02:00
await createReadTheDocsPage(createPage, 'aquarius', aquariusList)
await createReadTheDocsPage(createPage, 'provider', providerList)
2021-06-01 10:56:53 +02:00
await createReadTheDocsPage(createPage, 'ocean-subgraph', subgraphList)
resolve()
})
)
})
}
2021-08-16 22:03:52 +02:00
const createDeploymentsPage = async (createPage) => {
const template = path.resolve('./src/components/Deployments.jsx')
const slug = `/concepts/deployments/`
createPage({
path: slug,
component: template
})
}
//
// Create pages from TypeDoc json files
//
const createTypeDocPage = async (createPage, name, downloadUrl) => {
try {
2021-06-01 10:56:53 +02:00
const typedoc = await fetch(downloadUrl)
const typedocTemplate = path.resolve('./src/templates/Typedoc/index.jsx')
const slug = `/references/${name}/`
createPage({
path: slug,
component: typedocTemplate,
context: {
slug,
2021-06-01 10:56:53 +02:00
typedoc: await typedoc.json()
}
})
} catch (error) {
console.log(error.message)
}
}
//
// Create pages from swagger json files
//
// https://github.com/swagger-api/swagger-js
const fetchSwaggerSpec = async (url) => {
try {
const client = await Swagger(url)
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(...)
} catch (error) {
console.error(error.message)
}
}
2020-03-24 09:04:30 +01:00
const createSwaggerPages = async (createPage) => {
const apiSwaggerTemplate = path.resolve('./src/templates/Swagger/index.jsx')
2020-11-14 00:43:06 +01:00
const getSlug = (name) => `/references/${name}/`
2020-11-14 00:43:06 +01:00
for (const component of swaggerComponents) {
const slug = getSlug(component.name)
2020-11-14 00:43:06 +01:00
createPage({
path: slug,
component: apiSwaggerTemplate,
context: {
slug,
name: component.name,
api: await fetchSwaggerSpec(component.url)
2020-11-14 00:43:06 +01:00
}
})
}
// Swagger Pet Store example
const petStoreSlug = '/references/petstore/'
try {
const client = await Swagger(`http://petstore.swagger.io/v2/swagger.json`)
createPage({
path: petStoreSlug,
component: apiSwaggerTemplate,
context: {
slug: petStoreSlug,
api: client.spec
}
})
} catch (error) {
console.error(error.message)
}
}
2021-04-10 17:08:14 +02:00
2021-04-12 11:19:11 +02:00
const createReadTheDocsPage = async (createPage, name, list) => {
const markdownListTemplate = path.resolve(
'./src/templates/Markdown/MarkdownList.jsx'
)
2021-04-10 17:08:14 +02:00
createPage({
2021-06-21 22:51:52 +02:00
path: `/references/read-the-docs/${name}`,
matchPath: `/references/read-the-docs/${name}/*`,
2021-04-10 17:08:14 +02:00
component: markdownListTemplate,
context: {
markdownList: list,
2021-05-24 17:11:41 +02:00
name: name,
2021-06-21 22:51:52 +02:00
baseUrl: `/references/read-the-docs/${name}`
2021-04-10 17:08:14 +02:00
}
})
}
2021-04-12 11:19:11 +02:00
const filterMarkdownList = (markdownList, string) => {
return markdownList.filter(({ node }) => node.frontmatter.app === string)
2021-04-12 11:19:11 +02:00
}