1
0
mirror of synced 2025-12-20 02:19:14 -05:00
Files
docs/script/graphql/utils/prerender-input-objects.js
Kevin Heis 0b1ff73a46 Update some readFileSync to await readFile with top level await (#20525)
* Update some readFileSync to await readFile with top level await

* More updates

* Update all-products.js

* Use 'lib/readfile-async.js' in runtime files for better performance

* Remove unnecessary use of 'for await...of' loops

* Revert to importing 'fs/promises'

Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-29 16:45:46 +00:00

34 lines
1.2 KiB
JavaScript

#!/usr/bin/env node
import fs from 'fs/promises'
import path from 'path'
import cheerio from 'cheerio'
import { liquid } from '../../../lib/render-content/index.js'
import getMiniTocItems from '../../../lib/get-mini-toc-items.js'
import rewriteLocalLinks from '../../../lib/rewrite-local-links.js'
const includes = path.join(process.cwd(), 'includes')
const inputObjectIncludeFile = await fs.readFile(
path.join(includes, 'graphql-input-object.html'),
'utf8'
)
export default async function prerenderInputObjects(context) {
const inputObjectsArray = []
// render the graphql-object.html layout for every object
for (const inputObject of context.graphql.schemaForCurrentVersion.inputObjects) {
context.item = inputObject
const inputObjectHtml = await liquid.parseAndRender(inputObjectIncludeFile, context)
const $ = cheerio.load(inputObjectHtml, { xmlMode: true })
rewriteLocalLinks($, context.currentVersion, context.currentLanguage)
const htmlWithVersionedLinks = $.html()
inputObjectsArray.push(htmlWithVersionedLinks)
}
const inputObjectsHtml = inputObjectsArray.join('\n')
return {
html: inputObjectsHtml,
miniToc: getMiniTocItems(inputObjectsHtml),
}
}