Skip to content

Commit 5282efe

Browse files
authored
Create demo renderer (#807)
1 parent 9b97010 commit 5282efe

File tree

97 files changed

+7252
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+7252
-18
lines changed

.changeset/nice-nights-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/renderers-core': patch
3+
---
4+
5+
Add `fragmentToRenderMap` helper

.changeset/rude-spoons-fall.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/renderers-demo': patch
3+
---
4+
5+
Add new demo renderer that generates Markdown files for a given program.

packages/renderers-core/src/renderMap.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export function renderMap(): RenderMap {
1212
return new Map<Path, string>();
1313
}
1414

15+
export function fragmentToRenderMap(fragment: BaseFragment, path: Path): RenderMap {
16+
const newMap = new Map<Path, string>();
17+
newMap.set(path, fragment.content);
18+
return newMap;
19+
}
20+
1521
export function addToRenderMap(renderMap: RenderMap, path: Path, content: BaseFragment | string): RenderMap {
1622
const newMap = new Map(renderMap);
1723
newMap.set(path, typeof content === 'string' ? content : content.content);

packages/renderers-demo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dist/
2+
test/e2e/
3+
test-ledger/
4+
target/
5+
CHANGELOG.md

packages/renderers-demo/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Codama
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/renderers-demo/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Codama ➤ Renderers ➤ Demo
2+
3+
[![npm][npm-image]][npm-url]
4+
[![npm-downloads][npm-downloads-image]][npm-url]
5+
6+
[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/renderers-demo.svg?style=flat
7+
[npm-image]: https://img.shields.io/npm/v/@codama/renderers-demo.svg?style=flat&label=%40codama%2Frenderers-demo
8+
[npm-url]: https://www.npmjs.com/package/@codama/renderers-demo
9+
10+
This package provides a demo implementation of a Codama renderer to help developers create their own.
11+
12+
## Installation
13+
14+
```sh
15+
pnpm install @codama/renderers-demo
16+
```
17+
18+
## Usage
19+
20+
Add the following script to your Codama configuration file.
21+
22+
```json
23+
{
24+
// ...
25+
"scripts": {
26+
"demo": {
27+
"from": "@codama/renderers-demo",
28+
"args": ["docs"]
29+
}
30+
}
31+
}
32+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "@codama/renderers-demo",
3+
"version": "1.0.0",
4+
"description": "Demo renderer to help creating new ones",
5+
"exports": {
6+
"types": "./dist/types/index.d.ts",
7+
"node": {
8+
"import": "./dist/index.node.mjs",
9+
"require": "./dist/index.node.cjs"
10+
}
11+
},
12+
"main": "./dist/index.node.cjs",
13+
"module": "./dist/index.node.mjs",
14+
"types": "./dist/types/index.d.ts",
15+
"type": "commonjs",
16+
"files": [
17+
"./dist/types",
18+
"./dist/index.*"
19+
],
20+
"sideEffects": false,
21+
"keywords": [
22+
"solana",
23+
"framework",
24+
"standard",
25+
"renderers",
26+
"demo"
27+
],
28+
"scripts": {
29+
"build": "rimraf dist && pnpm build:src && pnpm build:types",
30+
"build:src": "zx ../../node_modules/@codama/internals/scripts/build-src.mjs node",
31+
"build:types": "zx ../../node_modules/@codama/internals/scripts/build-types.mjs",
32+
"dev": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs node --watch",
33+
"lint": "zx ../../node_modules/@codama/internals/scripts/lint.mjs",
34+
"lint:fix": "zx ../../node_modules/@codama/internals/scripts/lint.mjs --fix",
35+
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:node && pnpm test:e2e && pnpm test:exports",
36+
"test:e2e": "./test/e2e/test.sh",
37+
"test:exports": "node ./test/exports/module.mjs && node ./test/exports/commonjs.cjs",
38+
"test:node": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs node",
39+
"test:treeshakability": "zx ../../node_modules/@codama/internals/scripts/test-treeshakability.mjs",
40+
"test:types": "zx ../../node_modules/@codama/internals/scripts/test-types.mjs"
41+
},
42+
"dependencies": {
43+
"@codama/errors": "workspace:*",
44+
"@codama/nodes": "workspace:*",
45+
"@codama/renderers-core": "workspace:*",
46+
"@codama/visitors-core": "workspace:*",
47+
"@solana/codecs": "^3.0.0"
48+
},
49+
"devDependencies": {
50+
"@codama/cli": "workspace:*"
51+
},
52+
"license": "MIT",
53+
"repository": {
54+
"type": "git",
55+
"url": "https://github.com/codama-idl/codama"
56+
},
57+
"bugs": {
58+
"url": "http://github.com/codama-idl/codama/issues"
59+
},
60+
"browserslist": [
61+
"supports bigint and not dead",
62+
"maintained node versions"
63+
]
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { AccountNode, PdaNode, titleCase } from '@codama/nodes';
2+
import { visit } from '@codama/visitors-core';
3+
4+
import {
5+
Fragment,
6+
fragment,
7+
getCodeBlockFragment,
8+
getFrontmatterFragment,
9+
getPageFragment,
10+
getTitleAndDescriptionFragment,
11+
} from '../utils';
12+
import { TypeVisitor } from '../visitors/getTypeVisitor';
13+
import { getPdaFunctionUsageFragment } from './pdaFunctionUsage';
14+
15+
export function getAccountPageFragment(
16+
node: AccountNode,
17+
typeVisitor: TypeVisitor,
18+
size?: number,
19+
pda?: PdaNode,
20+
): Fragment {
21+
const title = titleCase(node.name);
22+
const type = visit(node, typeVisitor);
23+
24+
return getPageFragment(
25+
[
26+
getFrontmatterFragment(title, `Overview of the ${title} account`),
27+
getTitleAndDescriptionFragment(title, node.docs),
28+
fragment('## Account data'),
29+
getCodeBlockFragment(type, 'ts'),
30+
...(size ? [fragment(`This account has a fixed size of ${size} bytes.`)] : []),
31+
...(pda ? [fragment('## PDA'), getCodeBlockFragment(getPdaFunctionUsageFragment(pda), 'ts')] : []),
32+
],
33+
// Generated accounts are within the same directory.
34+
{ generatedAccounts: '.' },
35+
);
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { DefinedTypeNode, titleCase } from '@codama/nodes';
2+
import { visit } from '@codama/visitors-core';
3+
4+
import {
5+
Fragment,
6+
fragment,
7+
getCodeBlockFragment,
8+
getFrontmatterFragment,
9+
getPageFragment,
10+
getTitleAndDescriptionFragment,
11+
} from '../utils';
12+
import { TypeVisitor } from '../visitors/getTypeVisitor';
13+
14+
export function getDefinedTypePageFragment(node: DefinedTypeNode, typeVisitor: TypeVisitor): Fragment {
15+
const title = titleCase(node.name);
16+
const type = visit(node, typeVisitor);
17+
18+
return getPageFragment(
19+
[
20+
getFrontmatterFragment(title, `Overview of the ${title} type`),
21+
getTitleAndDescriptionFragment(title, node.docs),
22+
fragment('## Type definition'),
23+
getCodeBlockFragment(type, 'ts'),
24+
],
25+
// Generated types are within the same directory.
26+
{ generatedTypes: '.' },
27+
);
28+
}

0 commit comments

Comments
 (0)