Skip to content

Commit

Permalink
Fix links inside generated docs (#1816)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrearden authored Jul 22, 2020
1 parent b49acfb commit 71a91fe
Show file tree
Hide file tree
Showing 66 changed files with 1,060 additions and 639 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"nock": "13.0.2",
"prettier": "2.0.5",
"ts-jest": "26.1.3",
"typedoc": "0.17.8",
"typedoc": "0.17.0-3",
"typedoc-plugin-markdown": "2.3.1",
"typescript": "3.9.7"
},
Expand All @@ -84,4 +84,4 @@
"resolutions": {
"graphql": "15.3.0"
}
}
}
91 changes: 51 additions & 40 deletions scripts/build-api-docs.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
const fs = require('fs')
const path = require('path')
const rimraf = require('rimraf')
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const TypeDoc = require('typedoc');
const { transpileModule } = require('typescript');
const { execSync } = require('child_process')
const { execSync } = require('child_process');

// Where to generate the API docs
const outputDir = path.join(__dirname, '../website/docs/api');
// sidebars.json
const sidebarsPath = path.join(__dirname, '../website/sidebars.json')
const sidebarsPath = path.join(__dirname, '../website/sidebars.json');

// Get the upstream git remote -- we don't want to assume it exists or is named "upstream"
const gitRemote = execSync('git remote -v', { encoding: 'utf-8' })
.split('\n')
.map(line => line.split('\t'))
.find(([_name, description]) => description === '[email protected]:ardatan/graphql-tools.git (fetch)' || description === 'https://github.com/ardatan/graphql-tools (fetch)')
const gitRemoteName = gitRemote && gitRemote[0]
.find(
([_name, description]) =>
description === '[email protected]:ardatan/graphql-tools.git (fetch)' ||
description === 'https://github.com/ardatan/graphql-tools (fetch)'
);
const gitRemoteName = gitRemote && gitRemote[0];
if (!gitRemoteName) {
console.log('Unable to locate upstream git remote')
process.exit(1)
console.log('Unable to locate upstream git remote');
process.exit(1);
}

// An array of tuples where the first element is the package's name and the
Expand All @@ -42,10 +46,10 @@ const modules = [
['@graphql-tools/prisma-loader', 'packages/loaders/prisma/src/index.ts'],
['@graphql-tools/graphql-tag-pluck', 'packages/graphql-tag-pluck/src/index.ts'],
['@graphql-tools/utils', 'packages/utils/src/index.ts'],
]
];

// Delete existing docs
rimraf.sync(outputDir)
rimraf.sync(outputDir);

// Initialize TypeDoc
const typeDoc = new TypeDoc.Application();
Expand Down Expand Up @@ -76,42 +80,48 @@ typeDoc.generateDocs(project, outputDir);
// See https://github.com/tgreyuk/typedoc-plugin-markdown/pull/128
['classes', 'enums', 'interfaces', 'modules'].forEach(dirName => {
fs.readdirSync(path.join(outputDir, dirName)).forEach(fileName => {
const filePath = path.join(outputDir, dirName, fileName)
const contents = fs.readFileSync(filePath, 'utf-8')
const filePath = path.join(outputDir, dirName, fileName);
const contents = fs
.readFileSync(filePath, 'utf-8')
// Escape angle brackets
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
// Strip .md extension from any links
// Fix links
.replace(/\[([^\]]+)\]\(([^)]+).md\)/g, '[$1]($2)')
fs.writeFileSync(filePath, contents)
})
})
.replace(/\[([^\]]+)\]\((\.\.\/(classes|interfaces|enums)\/([^\)]+))\)/g, '[$1](/docs/api/$3/$4)');
fs.writeFileSync(filePath, contents);
});
});

// Remove the generated "index.md" file
fs.unlinkSync(path.join(outputDir, 'index.md'))
fs.unlinkSync(path.join(outputDir, 'index.md'));

// Update each module 's frontmatter and title
modules.forEach(([name, originalFilePath]) => {
const filePath = path.join(outputDir, 'modules', convertEntryFilePath(originalFilePath))
const filePath = path.join(outputDir, 'modules', convertEntryFilePath(originalFilePath));
if (!fs.existsSync(filePath)) {
return
return;
}
const id = convertNameToId(name)
fs.writeFileSync(filePath, fs.readFileSync(filePath, 'utf-8').replace(
/^---.+---\n\n## Index/s, `
const id = convertNameToId(name);
fs.writeFileSync(
filePath,
fs.readFileSync(filePath, 'utf-8').replace(
/^---.+---\n\n## Index/s,
`
---
id: "${id}"
title: "${name}"
sidebar_label: "${id}"
---`.substring(1)
))
})
)
);
});

// Update sidebars.json
const sidebars = require(sidebarsPath)
const sidebars = require(sidebarsPath);
sidebars.someSidebar.find(category => category['API Reference'])['API Reference'] = [
{
Modules: modules.map(([name]) => `api/modules/${convertNameToId(name)}`)
Modules: modules.map(([name]) => `api/modules/${convertNameToId(name)}`),
},
{
Classes: getSidebarItemsByDirectory('classes'),
Expand All @@ -122,29 +132,30 @@ sidebars.someSidebar.find(category => category['API Reference'])['API Reference'
{
Enums: getSidebarItemsByDirectory('enums'),
},
]
fs.writeFileSync(sidebarsPath, JSON.stringify(sidebars, null, 2))
];
fs.writeFileSync(sidebarsPath, JSON.stringify(sidebars, null, 2));

function convertEntryFilePath(filePath) {
const { dir, name } = path.parse(filePath)
return `_${dir.split('/').slice(1).join('_').replace(/-/g, '_')}_${name}_.md`
const { dir, name } = path.parse(filePath);
return `_${dir.split('/').slice(1).join('_').replace(/-/g, '_')}_${name}_.md`;
}

function convertNameToId(name) {
return name.replace(/@graphql-tools\//g, '')
return name.replace(/@graphql-tools\//g, '');
}

function getSidebarItemsByDirectory(dirName) {
return fs.readdirSync(path.join(outputDir, dirName))
.map((fileName) => `api/${dirName}/${path.parse(fileName).name}`)
return fs
.readdirSync(path.join(outputDir, dirName))
.map(fileName => `api/${dirName}/${path.parse(fileName).name}`)
.sort((a, b) => {
const aName = a.split('.').pop()
const bName = b.split('.').pop()
const aName = a.split('.').pop();
const bName = b.split('.').pop();
if (aName < bName) {
return -1
return -1;
} else if (aName > bName) {
return 1
return 1;
}
return 0
})
return 0;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This loader loads a schema from Apollo Engine

## Implements

* [Loader](../interfaces/_utils_src_index_.loader)[ApolloEngineOptions](../interfaces/_loaders_apollo_engine_src_index_.apolloengineoptions)
* [Loader](/docs/api/interfaces/_utils_src_index_.loader)[ApolloEngineOptions](/docs/api/interfaces/_loaders_apollo_engine_src_index_.apolloengineoptions)

## Index

Expand Down Expand Up @@ -54,7 +54,7 @@ ___

### load

**load**(`_`: "apollo-engine", `options`: [ApolloEngineOptions](../interfaces/_loaders_apollo_engine_src_index_.apolloengineoptions)): *Promise‹[Source](../interfaces/_utils_src_index_.source)*
**load**(`_`: "apollo-engine", `options`: [ApolloEngineOptions](/docs/api/interfaces/_loaders_apollo_engine_src_index_.apolloengineoptions)): *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source)*

*Defined in [packages/loaders/apollo-engine/src/index.ts:36](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/apollo-engine/src/index.ts#L36)*

Expand All @@ -63,9 +63,9 @@ ___
Name | Type |
------ | ------ |
`_` | "apollo-engine" |
`options` | [ApolloEngineOptions](../interfaces/_loaders_apollo_engine_src_index_.apolloengineoptions) |
`options` | [ApolloEngineOptions](/docs/api/interfaces/_loaders_apollo_engine_src_index_.apolloengineoptions) |

**Returns:** *Promise‹[Source](../interfaces/_utils_src_index_.source)*
**Returns:** *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source)*

___

Expand All @@ -83,7 +83,7 @@ ___

**loaderId**(): *string*

*Implementation of [Loader](../interfaces/_utils_src_index_.loader)*
*Implementation of [Loader](/docs/api/interfaces/_utils_src_index_.loader)*

*Defined in [packages/loaders/apollo-engine/src/index.ts:24](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/apollo-engine/src/index.ts#L24)*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Supported extensions include: `.ts`, `.tsx`, `.js`, `.jsx`, `.vue`

## Implements

* [Loader](../interfaces/_utils_src_index_.loader)[CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions)
* [Loader](/docs/api/interfaces/_utils_src_index_.loader)[CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions)

## Index

Expand Down Expand Up @@ -73,7 +73,7 @@ ___

### load

**load**(`pointer`: [SchemaPointerSingle](../modules/_utils_src_index_.md#schemapointersingle) | [DocumentPointerSingle](../modules/_utils_src_index_.md#documentpointersingle), `options`: [CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions)): *Promise‹[Source](../interfaces/_utils_src_index_.source)*
**load**(`pointer`: [SchemaPointerSingle](../modules/_utils_src_index_.md#schemapointersingle) | [DocumentPointerSingle](../modules/_utils_src_index_.md#documentpointersingle), `options`: [CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions)): *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source)*

*Defined in [packages/loaders/code-file/src/index.ts:80](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/code-file/src/index.ts#L80)*

Expand All @@ -84,13 +84,13 @@ Name | Type |
`pointer` | [SchemaPointerSingle](../modules/_utils_src_index_.md#schemapointersingle) &#124; [DocumentPointerSingle](../modules/_utils_src_index_.md#documentpointersingle) |
`options` | [CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions) |

**Returns:** *Promise‹[Source](../interfaces/_utils_src_index_.source)*
**Returns:** *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source)*

___

### loadSync

**loadSync**(`pointer`: [SchemaPointerSingle](../modules/_utils_src_index_.md#schemapointersingle) | [DocumentPointerSingle](../modules/_utils_src_index_.md#documentpointersingle), `options`: [CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions)): *[Source](../interfaces/_utils_src_index_.source)*
**loadSync**(`pointer`: [SchemaPointerSingle](../modules/_utils_src_index_.md#schemapointersingle) | [DocumentPointerSingle](../modules/_utils_src_index_.md#documentpointersingle), `options`: [CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions)): *[Source](/docs/api/interfaces/_utils_src_index_.source)*

*Defined in [packages/loaders/code-file/src/index.ts:123](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/code-file/src/index.ts#L123)*

Expand All @@ -101,15 +101,15 @@ Name | Type |
`pointer` | [SchemaPointerSingle](../modules/_utils_src_index_.md#schemapointersingle) &#124; [DocumentPointerSingle](../modules/_utils_src_index_.md#documentpointersingle) |
`options` | [CodeFileLoaderOptions](../modules/_loaders_code_file_src_index_.md#codefileloaderoptions) |

**Returns:** *[Source](../interfaces/_utils_src_index_.source)*
**Returns:** *[Source](/docs/api/interfaces/_utils_src_index_.source)*

___

### loaderId

**loaderId**(): *string*

*Implementation of [Loader](../interfaces/_utils_src_index_.loader)*
*Implementation of [Loader](/docs/api/interfaces/_utils_src_index_.loader)*

*Defined in [packages/loaders/code-file/src/index.ts:51](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/code-file/src/index.ts#L51)*

Expand Down
22 changes: 11 additions & 11 deletions website/docs/api/classes/_loaders_git_src_index_.gitloader.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const typeDefs = await loadTypedefs('git:someBranch:some/path/to/file.js', {

## Implements

* [Loader](../interfaces/_utils_src_index_.loader)
* [Loader](/docs/api/interfaces/_utils_src_index_.loader)

## Index

Expand All @@ -36,7 +36,7 @@ const typeDefs = await loadTypedefs('git:someBranch:some/path/to/file.js', {

**canLoad**(`pointer`: string): *Promise‹boolean›*

*Defined in [packages/loaders/git/src/index.ts:56](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L56)*
*Defined in [packages/loaders/git/src/index.ts:54](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L54)*

**Parameters:**

Expand All @@ -52,7 +52,7 @@ ___

**canLoadSync**(`pointer`: string): *boolean*

*Defined in [packages/loaders/git/src/index.ts:60](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L60)*
*Defined in [packages/loaders/git/src/index.ts:58](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L58)*

**Parameters:**

Expand All @@ -66,9 +66,9 @@ ___

### load

**load**(`pointer`: string, `options`: [GitLoaderOptions](../modules/_loaders_git_src_index_.md#gitloaderoptions)): *Promise‹[Source](../interfaces/_utils_src_index_.source) | object*
**load**(`pointer`: string, `options`: [GitLoaderOptions](../modules/_loaders_git_src_index_.md#gitloaderoptions)): *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source)*

*Defined in [packages/loaders/git/src/index.ts:64](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L64)*
*Defined in [packages/loaders/git/src/index.ts:62](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L62)*

**Parameters:**

Expand All @@ -77,15 +77,15 @@ Name | Type |
`pointer` | string |
`options` | [GitLoaderOptions](../modules/_loaders_git_src_index_.md#gitloaderoptions) |

**Returns:** *Promise‹[Source](../interfaces/_utils_src_index_.source) | object*
**Returns:** *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source)*

___

### loadSync

**loadSync**(`pointer`: string, `options`: [GitLoaderOptions](../modules/_loaders_git_src_index_.md#gitloaderoptions)): *[Source](../interfaces/_utils_src_index_.source) | object*
**loadSync**(`pointer`: string, `options`: [GitLoaderOptions](../modules/_loaders_git_src_index_.md#gitloaderoptions)): *[Source](/docs/api/interfaces/_utils_src_index_.source)*

*Defined in [packages/loaders/git/src/index.ts:78](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L78)*
*Defined in [packages/loaders/git/src/index.ts:79](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L79)*

**Parameters:**

Expand All @@ -94,16 +94,16 @@ Name | Type |
`pointer` | string |
`options` | [GitLoaderOptions](../modules/_loaders_git_src_index_.md#gitloaderoptions) |

**Returns:** *[Source](../interfaces/_utils_src_index_.source) | object*
**Returns:** *[Source](/docs/api/interfaces/_utils_src_index_.source)*

___

### loaderId

**loaderId**(): *string*

*Implementation of [Loader](../interfaces/_utils_src_index_.loader)*
*Implementation of [Loader](/docs/api/interfaces/_utils_src_index_.loader)*

*Defined in [packages/loaders/git/src/index.ts:52](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L52)*
*Defined in [packages/loaders/git/src/index.ts:50](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/git/src/index.ts#L50)*

**Returns:** *string*
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const typeDefs = await loadTypedefs('github:githubUser/githubRepo#branchName:pat

## Implements

* [Loader](../interfaces/_utils_src_index_.loader)[GithubLoaderOptions](../interfaces/_loaders_github_src_index_.githubloaderoptions)
* [Loader](/docs/api/interfaces/_utils_src_index_.loader)[GithubLoaderOptions](/docs/api/interfaces/_loaders_github_src_index_.githubloaderoptions)

## Index

Expand Down Expand Up @@ -61,7 +61,7 @@ ___

### load

**load**(`pointer`: string, `options`: [GithubLoaderOptions](../interfaces/_loaders_github_src_index_.githubloaderoptions)): *Promise‹[Source](../interfaces/_utils_src_index_.source) | object›*
**load**(`pointer`: string, `options`: [GithubLoaderOptions](/docs/api/interfaces/_loaders_github_src_index_.githubloaderoptions)): *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source) | object›*

*Defined in [packages/loaders/github/src/index.ts:63](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/github/src/index.ts#L63)*

Expand All @@ -70,9 +70,9 @@ ___
Name | Type |
------ | ------ |
`pointer` | string |
`options` | [GithubLoaderOptions](../interfaces/_loaders_github_src_index_.githubloaderoptions) |
`options` | [GithubLoaderOptions](/docs/api/interfaces/_loaders_github_src_index_.githubloaderoptions) |

**Returns:** *Promise‹[Source](../interfaces/_utils_src_index_.source) | object›*
**Returns:** *Promise‹[Source](/docs/api/interfaces/_utils_src_index_.source) | object›*

___

Expand All @@ -90,7 +90,7 @@ ___

**loaderId**(): *string*

*Implementation of [Loader](../interfaces/_utils_src_index_.loader)*
*Implementation of [Loader](/docs/api/interfaces/_utils_src_index_.loader)*

*Defined in [packages/loaders/github/src/index.ts:51](https://github.com/ardatan/graphql-tools/blob/master/packages/loaders/github/src/index.ts#L51)*

Expand Down
Loading

0 comments on commit 71a91fe

Please sign in to comment.