Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update graphql repo, fix & refactor links plugin #101

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/fuel-graphql-docs
Submodule fuel-graphql-docs updated 79 files
+0 −5 .env.example
+7 −0 .eslintignore
+6 −0 .eslintrc
+7 −0 .eslintrc.prod
+33 −0 .github/workflows/docs.yml
+11 −0 .github/workflows/links.yml
+5 −0 .markdownlint.yaml
+5 −0 .prettierignore
+1 −0 .prettierrc
+1 −1 README.md
+2 −2 docs/how-to-use-graphql.mdx
+32 −20 docs/how-to-use-graphql/apis-explained.mdx
+20 −15 docs/how-to-use-graphql/what-is-graphql.mdx
+3 −11 docs/overview.mdx
+26 −24 docs/querying-from-a-dapp.mdx
+16 −16 docs/recipes.mdx
+7 −8 docs/reference.mdx
+0 −8 docs/reference/enums.mdx
+12 −10 docs/reference/mutations.mdx
+286 −352 docs/reference/objects.mdx
+41 −59 docs/reference/queries.mdx
+4 −4 docs/reference/subscriptions.mdx
+28 −38 docs/reference/unions.mdx
+3 −3 examples/Balance.tsx
+5 −4 examples/Balances.tsx
+2 −2 examples/BlockByHeight.tsx
+3 −4 examples/ContractBalance.tsx
+2 −2 examples/ContractBalances.tsx
+1 −1 examples/LatestBlocks.tsx
+1 −1 examples/LatestTransactions.tsx
+2 −2 examples/MessageInfo.tsx
+2 −2 examples/Transactions.tsx
+9 −9 examples/index.tsx
+30 −21 examples/query.tsx
+27 −29 examples/tests/balance.test.ts
+24 −26 examples/tests/balances.test.ts
+22 −21 examples/tests/block.test.ts
+33 −30 examples/tests/contract-balance.test.ts
+22 −21 examples/tests/contract-balances.test.ts
+19 −18 examples/tests/latest-blocks.test.ts
+23 −20 examples/tests/latest-transactions.test.ts
+23 −22 examples/tests/messages.test.ts
+25 −22 examples/tests/transactions-by-owner.test.ts
+36 −0 next.config.js
+15,726 −20,237 package-lock.json
+70 −34 package.json
+1 −0 public/sprite.svg
+3 −9 src/components.json
+5 −2 src/components/Breadcrumb.tsx
+26 −25 src/components/CodeExamples.tsx
+1 −2 src/components/CodeImport.tsx
+10 −5 src/components/DocFooter.tsx
+24 −28 src/components/ExampleBox.tsx
+1 −9 src/components/Header.tsx
+10 −8 src/components/Heading.tsx
+3 −1 src/components/Layout.tsx
+1 −1 src/components/Link.tsx
+3 −3 src/components/MobileMenu.tsx
+1 −1 src/components/Pre.tsx
+2 −2 src/components/Provider.tsx
+0 −1 src/components/Search.tsx
+8 −5 src/components/Sidebar.tsx
+9 −3 src/components/SidebarLink.tsx
+25 −21 src/components/SidebarSubmenu.tsx
+6 −3 src/components/TableOfContent.tsx
+3 −4 src/constants.ts
+0 −1 src/hooks/useDocContext.tsx
+13 −14 src/lib/api.ts
+21 −9 src/lib/code-examples.ts
+0 −2 src/lib/toc.ts
+1 −4 src/nav.json
+3 −0 src/pages/_document.tsx
+0 −1 src/pages/index.tsx
+0 −1 src/screens/DocPage.tsx
+3 −3 src/styles/docsearch/_variables.css
+11 −11 src/styles/docsearch/modal.css
+14 −14 src/styles/docsearch/style.css
+11 −10 src/styles/index.css
+8 −24 tsconfig.json
2 changes: 1 addition & 1 deletion docs/latest/fuel-graphql-docs
Submodule fuel-graphql-docs updated 79 files
+0 −5 .env.example
+7 −0 .eslintignore
+6 −0 .eslintrc
+7 −0 .eslintrc.prod
+33 −0 .github/workflows/docs.yml
+11 −0 .github/workflows/links.yml
+5 −0 .markdownlint.yaml
+5 −0 .prettierignore
+1 −0 .prettierrc
+1 −1 README.md
+2 −2 docs/how-to-use-graphql.mdx
+32 −20 docs/how-to-use-graphql/apis-explained.mdx
+20 −15 docs/how-to-use-graphql/what-is-graphql.mdx
+3 −11 docs/overview.mdx
+26 −24 docs/querying-from-a-dapp.mdx
+16 −16 docs/recipes.mdx
+7 −8 docs/reference.mdx
+0 −8 docs/reference/enums.mdx
+12 −10 docs/reference/mutations.mdx
+286 −352 docs/reference/objects.mdx
+41 −59 docs/reference/queries.mdx
+4 −4 docs/reference/subscriptions.mdx
+28 −38 docs/reference/unions.mdx
+3 −3 examples/Balance.tsx
+5 −4 examples/Balances.tsx
+2 −2 examples/BlockByHeight.tsx
+3 −4 examples/ContractBalance.tsx
+2 −2 examples/ContractBalances.tsx
+1 −1 examples/LatestBlocks.tsx
+1 −1 examples/LatestTransactions.tsx
+2 −2 examples/MessageInfo.tsx
+2 −2 examples/Transactions.tsx
+9 −9 examples/index.tsx
+30 −21 examples/query.tsx
+27 −29 examples/tests/balance.test.ts
+24 −26 examples/tests/balances.test.ts
+22 −21 examples/tests/block.test.ts
+33 −30 examples/tests/contract-balance.test.ts
+22 −21 examples/tests/contract-balances.test.ts
+19 −18 examples/tests/latest-blocks.test.ts
+23 −20 examples/tests/latest-transactions.test.ts
+23 −22 examples/tests/messages.test.ts
+25 −22 examples/tests/transactions-by-owner.test.ts
+36 −0 next.config.js
+15,726 −20,237 package-lock.json
+70 −34 package.json
+1 −0 public/sprite.svg
+3 −9 src/components.json
+5 −2 src/components/Breadcrumb.tsx
+26 −25 src/components/CodeExamples.tsx
+1 −2 src/components/CodeImport.tsx
+10 −5 src/components/DocFooter.tsx
+24 −28 src/components/ExampleBox.tsx
+1 −9 src/components/Header.tsx
+10 −8 src/components/Heading.tsx
+3 −1 src/components/Layout.tsx
+1 −1 src/components/Link.tsx
+3 −3 src/components/MobileMenu.tsx
+1 −1 src/components/Pre.tsx
+2 −2 src/components/Provider.tsx
+0 −1 src/components/Search.tsx
+8 −5 src/components/Sidebar.tsx
+9 −3 src/components/SidebarLink.tsx
+25 −21 src/components/SidebarSubmenu.tsx
+6 −3 src/components/TableOfContent.tsx
+3 −4 src/constants.ts
+0 −1 src/hooks/useDocContext.tsx
+13 −14 src/lib/api.ts
+21 −9 src/lib/code-examples.ts
+0 −2 src/lib/toc.ts
+1 −4 src/nav.json
+3 −0 src/pages/_document.tsx
+0 −1 src/pages/index.tsx
+0 −1 src/screens/DocPage.tsx
+3 −3 src/styles/docsearch/_variables.css
+11 −11 src/styles/docsearch/modal.css
+14 −14 src/styles/docsearch/style.css
+11 −10 src/styles/index.css
+8 −24 tsconfig.json
231 changes: 135 additions & 96 deletions src/lib/plugins/links.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<any, any>,
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 === '</a>') {
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;
}
Expand All @@ -150,3 +120,72 @@ function getTSUrl(input: string): { [key: string]: string } {
}
return result;
}

function handleHTMLLink(
node: any,
idx?: number | null,
parent?: Parent<any, any>,
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<any, any>
) {
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 === '</a>') {
child.type = 'text';
child.value = '';
} else if (child.value === value) {
child.value = '';
}
});
}
}
Loading
Loading