diff --git a/docs/fuel-graphql-docs b/docs/fuel-graphql-docs index 5fad508f6..2b807e6bd 160000 --- a/docs/fuel-graphql-docs +++ b/docs/fuel-graphql-docs @@ -1 +1 @@ -Subproject commit 5fad508f628f38d7d4f092948dfb21aa021deecd +Subproject commit 2b807e6bda2b269d0d556f7cb4f4563f1427e4df diff --git a/docs/latest/fuel-graphql-docs b/docs/latest/fuel-graphql-docs index 5fad508f6..2b807e6bd 160000 --- a/docs/latest/fuel-graphql-docs +++ b/docs/latest/fuel-graphql-docs @@ -1 +1 @@ -Subproject commit 5fad508f628f38d7d4f092948dfb21aa021deecd +Subproject commit 2b807e6bda2b269d0d556f7cb4f4563f1427e4df diff --git a/src/lib/plugins/links.ts b/src/lib/plugins/links.ts index 102c4ca13..7d7c2501b 100644 --- a/src/lib/plugins/links.ts +++ b/src/lib/plugins/links.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import type { Root } from 'remark-gfm'; @@ -11,119 +12,88 @@ const configPath = join(DOCS_DIRECTORY, `../src/config/paths.json`); const pathsConfig = JSON.parse(readFileSync(configPath, 'utf8')); export function handleLinks( - // eslint-disable-next-line @typescript-eslint/no-explicit-any node: any, dirname: string, idx?: number | null, - // eslint-disable-next-line @typescript-eslint/no-explicit-any parent?: Parent, tree?: Root ) { let newUrl: string | null = null; if (node.type === 'html') { - const url = getUrl(node.value); - if ( - url && - !node.value.includes(':href') && - idx !== undefined && - idx !== null - ) { - node.type = 'link'; - node.value = null; - node.children = []; - node.children.push(parent.children[idx + 1]); - parent.children.splice(idx + 1, 2); - } else if ( - url && - node.value.includes(':href') && - idx !== undefined && - idx !== null - ) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const scriptString = tree?.children[0] as any; - const newURLs = getTSUrl(scriptString.value); - - if (newURLs) { - let newURL = newURLs[url]; - if (newURL) { - newURL = newURL.replace('/v${forc}', '').replace('/v${fuels}', ''); - for (const [key, value] of Object.entries(pathsConfig)) { - newURL = newURL.replaceAll(key, value as string); - } - const value = parent.children[idx + 1].value; - parent.children[idx] = { - type: 'link', - url: newURL, - children: [ - { - type: 'text', - value: value, - }, - ], - }; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - parent.children.forEach((child: any) => { - if (child.type === 'html' && child.value === '') { - child.type = 'text'; - child.value = ''; - } else if (child.value === value) { - child.value = ''; - } - }); - } - } - } + handleHTMLLink(node, idx, parent, tree); } else { if (!node.url.includes('http')) { - newUrl = node.url - .replace('.md', '') - .replace('/index', '') - .replace('.html', '') - .toLowerCase(); - - const configPath = join(DOCS_DIRECTORY, `../src/config/paths.json`); - const pathsConfig = JSON.parse(readFileSync(configPath, 'utf8')); - - let dir = dirname; - Object.keys(pathsConfig).forEach((key) => { - dir = dir.replaceAll(key, pathsConfig[key]); - }); - - if (node.url.startsWith('../')) { - const folder = dirname.split('/').pop(); - newUrl = `/${dir.replace(folder!, '')}${newUrl!.replace('../', '')}`; - } - if (node.url.startsWith('./') && !node.url.includes('index')) { - newUrl = `/${dir.endsWith('/') ? dir : `${dir}/`}${newUrl!.replace( - './', - '' - )}`; - } - if (/^[a-zA-Z]/.test(node.url)) { - newUrl = `/${dir}/${newUrl}`; - } - newUrl = newUrl!.replace('/sway/forc/', '/forc/'); + newUrl = getNewUrl(node, dirname); } - if (node.url.endsWith('CONTRIBUTING') && node.url.includes('github.com')) { newUrl = `${node.url}.md`; } - const duplicates = getTSAPIDuplicates(); - if (newUrl) { - duplicates.forEach((item: DuplicateAPIItem) => { - if (newUrl?.startsWith(item.path.toLowerCase())) { - newUrl = newUrl.replace( - item.originalCategory.toLowerCase(), - item.newCategory.toLowerCase() - ); - } - }); - } - if (newUrl && newUrl.startsWith('/api/')) { - newUrl = newUrl.replace('/api/', '/docs/fuels-ts/'); + newUrl = handleTSLinks(newUrl); + } + + return newUrl; +} + +function handleTSLinks(url: string | null) { + let newUrl = url; + const duplicates = getTSAPIDuplicates(); + if (newUrl) { + duplicates.forEach((item: DuplicateAPIItem) => { + if (newUrl?.startsWith(item.path.toLowerCase())) { + newUrl = newUrl.replace( + item.originalCategory.toLowerCase(), + item.newCategory.toLowerCase() + ); + } + }); + } + if (newUrl && newUrl.startsWith('/api/')) { + newUrl = newUrl.replace('/api/', '/docs/fuels-ts/'); + } + return newUrl; +} + +function getNewUrl(node: any, dirname: string) { + let newUrl; + newUrl = node.url + .replace('.md', '') + .replace('/index', '') + .replace('.html', '') + .toLowerCase(); + + const configPath = join(DOCS_DIRECTORY, `../src/config/paths.json`); + const pathsConfig = JSON.parse(readFileSync(configPath, 'utf8')); + + let dir = dirname; + Object.keys(pathsConfig).forEach((key) => { + dir = dir.replaceAll(key, pathsConfig[key]); + }); + + if (node.url.startsWith('../')) { + // TODO: remove this once wallet is updated past 13.0 + if (!dirname.includes('fuels-wallet') && !node.url.startsWith('../dev')) { + const folder = dirname.split('/').pop(); + newUrl = `/${dir.replace(folder!, '')}${newUrl!.replace('../', '')}`; } } + if (node.url.startsWith('./') && !node.url.includes('index')) { + newUrl = `/${dir.endsWith('/') ? dir : `${dir}/`}${newUrl!.replace( + './', + '' + )}`; + } + if (/^[a-zA-Z]/.test(node.url)) { + newUrl = `/${dir}/${newUrl}`; + } + newUrl = newUrl!.replace('/sway/forc/', '/forc/'); + const isLatest = dirname.includes('/latest/'); + if (dirname.includes('fuel-graphql-docs')) { + newUrl = isLatest + ? newUrl.replace('/docs/', '/docs/latest/graphql/') + : newUrl.replace('/docs/', '/docs/graphql/'); + } + // TODO: add this for the wallet once wallet is updated past 13.0 return newUrl; } @@ -150,3 +120,72 @@ function getTSUrl(input: string): { [key: string]: string } { } return result; } + +function handleHTMLLink( + node: any, + idx?: number | null, + parent?: Parent, + tree?: Root +) { + const url = getUrl(node.value); + if ( + url && + !node.value.includes(':href') && + idx !== undefined && + idx !== null + ) { + node.type = 'link'; + node.value = null; + node.children = []; + node.children.push(parent.children[idx + 1]); + parent.children.splice(idx + 1, 2); + } else if ( + url && + node.value.includes(':href') && + idx !== undefined && + idx !== null + ) { + const scriptString = tree?.children[0] as any; + const newURLs = getTSUrl(scriptString.value); + + if (newURLs) { + handleNewURLs(newURLs, url, idx, parent); + } + } +} + +function handleNewURLs( + newURLs: { + [key: string]: string; + }, + url: string, + idx: number, + parent: Parent +) { + let newURL = newURLs[url]; + if (newURL) { + newURL = newURL.replace('/v${forc}', '').replace('/v${fuels}', ''); + for (const [key, value] of Object.entries(pathsConfig)) { + newURL = newURL.replaceAll(key, value as string); + } + const value = parent.children[idx + 1].value; + parent.children[idx] = { + type: 'link', + url: newURL, + children: [ + { + type: 'text', + value: value, + }, + ], + }; + parent.children.forEach((child: any) => { + if (child.type === 'html' && child.value === '') { + child.type = 'text'; + child.value = ''; + } else if (child.value === value) { + child.value = ''; + } + }); + } +} diff --git a/src/lib/plugins/plugins.ts b/src/lib/plugins/plugins.ts index e78467717..1b4b1c871 100644 --- a/src/lib/plugins/plugins.ts +++ b/src/lib/plugins/plugins.ts @@ -49,7 +49,7 @@ const conditions = { (node.name === 'Player' && filepath.includes('/fuels-wallet/')) ); }, - mdBookLinks: (node: any) => { + links: (node: any) => { return ( ((node.type === 'link' || node.type === 'definition') && node.url !== '..') || @@ -82,11 +82,11 @@ export function handlePlugins() { : beta4Versions; if (filepath.includes('/fuel-graphql-docs/')) { - handleGraphQLDocs(tree, filepath); + handleGraphQLDocs(tree, filepath, dirname); } else if (filepath.includes('/sway/')) { handleSwayDocs(tree, filepath, rootDir, dirname); } else if (filepath.includes('/fuels-wallet/')) { - handleWalletDocs(tree, filepath); + handleWalletDocs(tree, filepath, dirname); } else if (filepath.includes('/fuels-ts/')) { handleTSDocs(tree, rootDir, dirname, versions); } else if (filepath.includes('/fuels-rs/')) { @@ -101,35 +101,42 @@ export function handlePlugins() { }; } -function handleGraphQLDocs(tree: Root, filepath: string) { +function handleGraphQLDocs(tree: Root, filepath: string, dirname: string) { const nodes: NodeArray = []; visit(tree, '', (node: any, idx, parent) => { if ( - node.name === 'a' && - node.attributes && - node.attributes[0].value.includes('/docs/') + (node.name === 'a' && + node.attributes && + node.attributes[0].value.includes('/docs/')) || + conditions.links(node) ) { nodes.push([node as any, idx ?? null, parent as Parent]); } }); nodes.forEach(([node, _idx, _idxparent]) => { - let url = node.attributes[0].value; - if (filepath.includes('latest')) { - url = url.replace('/docs/', '/docs/latest/graphql/'); + if (conditions.links(node)) { + const newUrl = handleLinks(node, dirname); + if (newUrl) node.url = newUrl; } else { - url = url.replace('/docs/', '/docs/graphql/'); + let url = node.attributes[0].value; + if (filepath.includes('latest')) { + url = url.replace('/docs/', '/docs/latest/graphql/'); + } else { + url = url.replace('/docs/', '/docs/graphql/'); + } + node.attributes[0].value = url; } - node.attributes[0].value = url; }); } -function handleWalletDocs(tree: Root, filepath: string) { +function handleWalletDocs(tree: Root, filepath: string, dirname: string) { const nodes: NodeArray = []; visit(tree, '', (node: any, idx, parent) => { if ( // update the image & video paths in the wallet docs conditions.walletImages(node, filepath) || - conditions.walletComponents(node, filepath) + conditions.walletComponents(node, filepath) || + conditions.links(node) ) { nodes.push([node as any, idx ?? null, parent as Parent]); } @@ -149,6 +156,9 @@ function handleWalletDocs(tree: Root, filepath: string) { elements; node.attributes[0].value.value = value; } + } else if (conditions.links(node)) { + const newUrl = handleLinks(node, dirname); + if (newUrl) node.url = newUrl; } }); } @@ -177,7 +187,7 @@ function handleSwayDocs( // handle example code imports in mdbook repos and the TS SDK docs conditions.exampleImport(node) || // remove .md from mdBook links - conditions.mdBookLinks(node) + conditions.links(node) ) { nodes.push([node as any, idx ?? null, parent as Parent]); } @@ -187,7 +197,7 @@ function handleSwayDocs( const content = handleExampleImports(node, dirname, rootDir, parent); node.value = content; } - if (conditions.mdBookLinks(node)) { + if (conditions.links(node)) { const newUrl = handleLinks(node, dirname, idx, parent); if (newUrl) node.url = newUrl; } @@ -210,7 +220,7 @@ function handleTSDocs( // handle example code imports in mdbook repos and the TS SDK docs conditions.exampleImport(node) || // remove .md from mdBook links - conditions.mdBookLinks(node) || + conditions.links(node) || // handle TS book versions conditions.tsBookVersions(node) || (node.type === 'code' && node.lang === 'ts:line-numbers') @@ -222,7 +232,7 @@ function handleTSDocs( if (conditions.exampleImport(node)) { const content = handleExampleImports(node, dirname, rootDir, parent); node.value = content; - } else if (conditions.mdBookLinks(node)) { + } else if (conditions.links(node)) { const newUrl = handleLinks(node, dirname, idx, parent, tree); if (newUrl) node.url = newUrl; } else if (conditions.tsBookVersions(node)) { @@ -246,7 +256,7 @@ function handleRustBooks(tree: Root, rootDir: string, dirname: string) { // handle example code imports in mdbook repos and the TS SDK docs conditions.exampleImport(node) || // remove .md from mdBook links - conditions.mdBookLinks(node) || + conditions.links(node) || // replace {{versions}} conditions.rustBookVersion(node) ) { @@ -257,7 +267,7 @@ function handleRustBooks(tree: Root, rootDir: string, dirname: string) { if (conditions.exampleImport(node)) { const content = handleExampleImports(node, dirname, rootDir, parent); node.value = content; - } else if (conditions.mdBookLinks(node)) { + } else if (conditions.links(node)) { const newUrl = handleLinks(node, dirname); if (newUrl) node.url = newUrl; } else { @@ -274,7 +284,7 @@ function handleMDBooks(tree: Root, rootDir: string, dirname: string) { // handle example code imports in mdbook repos and the TS SDK docs conditions.exampleImport(node) || // remove .md from mdBook links - conditions.mdBookLinks(node) + conditions.links(node) ) { nodes.push([node as any, idx ?? null, parent as Parent]); } diff --git a/src/lib/plugins/rehype-code.ts b/src/lib/plugins/rehype-code.ts index ec90a272f..06f51399a 100644 --- a/src/lib/plugins/rehype-code.ts +++ b/src/lib/plugins/rehype-code.ts @@ -213,6 +213,7 @@ function getGraphQLCodeTabs(node: any) { const tsCode = h('code', codeProps, tsCodeRaw); const apolloImport = `import { ApolloClient, InMemoryCache, gql } from '@apollo/client'; + const apolloClient= new ApolloClient({ uri: 'https://${FUEL_TESTNET}.fuel.network/graphql', cache: new InMemoryCache(), @@ -221,9 +222,11 @@ function getGraphQLCodeTabs(node: any) { const apolloRaw = prettier.format(apolloContentValue, prettierProps); const apolloCode = h('code', codeProps, apolloRaw); - const urlqImport = `import { createClient } from 'urql'; - const urqlClient= createClient({ - url: 'https://${FUEL_TESTNET}.fuel.network/graphql', + const urlqImport = `import { Client, cacheExchange, fetchExchange } from 'urql'; + + const urqlClient = new Client({ + url: 'https:/${FUEL_TESTNET}.fuel.network/graphql', + exchanges: [cacheExchange, fetchExchange], });\n\n`; const urlQContentValue = urlqImport + urqlContent?.value ?? ''; const urlQRaw = prettier.format(urlQContentValue, prettierProps);