diff --git a/packages/typegen/src/metadataMd.ts b/packages/typegen/src/metadataMd.ts index 9917084f35..ac9f272a14 100644 --- a/packages/typegen/src/metadataMd.ts +++ b/packages/typegen/src/metadataMd.ts @@ -16,17 +16,21 @@ import { Text } from '@polkadot/types/primitive'; import { unwrapStorageType } from '@polkadot/types/primitive/StorageKey'; import { stringCamelCase, stringLowerFirst } from '@polkadot/util'; +interface Section { + link?: string; + name: string; + description?: string; + items: { + link?: string; + name: string; + [bullet: string]: undefined | string | Vec; + }[]; +} + interface Page { title: string; description: string; - sections: { - name: string; - description?: string; - items: { - name: string; - [bullet: string]: string | Vec; - }[]; - }[]; + sections: Section[]; } const STATIC_TEXT = '\n\n(NOTE: These were generated from a static/snapshot view of a recent Substrate master node. Some items may not be available in older nodes, or in any customized implementations.)'; @@ -71,16 +75,22 @@ function renderPage (page: Page): string { // contents page.sections.forEach((section) => { - md += `\n___\n\n\n## ${section.name}\n`; + md += '\n___\n\n\n'; + md += section.link + ? `

${section.name}

\n` + : `## ${section.name}\n`; if (section.description) { md += `\n_${section.description}_\n`; } section.items.forEach((item) => { - md += ` \n### ${item.name}`; + md += ' \n'; + md += item.link + ? `

${item.name}

` + : `### ${item.name}`; - Object.keys(item).filter((i) => i !== 'name').forEach((bullet) => { + Object.keys(item).filter((key) => !['link', 'name'].includes(key)).forEach((bullet) => { md += `\n- **${bullet}**: ${ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions item[bullet] instanceof Vec @@ -112,32 +122,41 @@ function addRpc (): string { description: DESC_RPC, sections: sections .sort() - .map((_sectionName) => { + .reduce((all: Section[], _sectionName): Section[] => { const section = definitions[_sectionName as 'babe']; - return { - // description: section.description, - items: Object.keys(section.rpc) - .sort() - .map((methodName) => { - const method = section.rpc[methodName]; - const sectionName = method.aliasSection || _sectionName; - const args = method.params.map(({ isOptional, name, type }: DefinitionRpcParam): string => { - // eslint-disable-next-line @typescript-eslint/restrict-plus-operands - return name + (isOptional ? '?' : '') + ': `' + type + '`'; - }).join(', '); - const type = '`' + method.type + '`'; + Object.keys(section.rpc) + .sort() + .forEach((methodName) => { + const method = section.rpc[methodName]; + const sectionName = method.aliasSection || _sectionName; + const topName = method.aliasSection ? `${_sectionName}/${method.aliasSection}` : _sectionName; + let container = all.find(({ name }) => name === topName); - return { - interface: '`' + `api.rpc.${sectionName}.${methodName}` + '`', - jsonrpc: '`' + (method.endpoint || `${sectionName}_${methodName}`) + '`', - name: `${methodName}(${args}): ${type}`, - ...(method.description && { summary: method.description }) - }; - }), - name: _sectionName - }; - }), + if (!container) { + container = { items: [], name: topName }; + + all.push(container); + } + + const args = method.params.map(({ isOptional, name, type }: DefinitionRpcParam): string => { + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + return name + (isOptional ? '?' : '') + ': `' + type + '`'; + }).join(', '); + const type = '`' + method.type + '`'; + const jsonrpc = (method.endpoint || `${sectionName}_${methodName}`); + + container.items.push({ + interface: '`' + `api.rpc.${sectionName}.${methodName}` + '`', + jsonrpc: '`' + jsonrpc + '`', + // link: jsonrpc, + name: `${methodName}(${args}): ${type}`, + ...(method.description && { summary: method.description }) + }); + }); + + return all; + }, []).sort((a, b) => a.name.localeCompare(b.name)), title: 'JSON-RPC' }); }