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

feat(website): use readFile architecture #21

Merged
merged 5 commits into from
Nov 10, 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
9 changes: 9 additions & 0 deletions .idea/frontmatter.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions packages/crypto-frontmatter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
"repository": {
"url": "git+https://github.com/levaintech/frontmatter"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./*": "./dist/*.js"
},
"typesVersions": {
"*": {
"index.d.ts": [
"dist/index.d.ts"
],
"*": [
"dist/*"
]
}
},
"files": [
"dist"
],
Expand Down
9 changes: 9 additions & 0 deletions packages/crypto-frontmatter/src/caip19.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Decode CAIP-19 into CAIP-2, Asset TYPE, and Asset REFERENCE
* @return {[string, string, string]}
*/
export function decodeCaip19(caip19: string): string[] {
const [caip2, asset] = caip19.split('/');
const [type, reference] = asset.split(':');
return [caip2, type, reference];
}
43 changes: 43 additions & 0 deletions packages/crypto-frontmatter/src/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readFile } from 'fs/promises';
import { join } from 'path';

import { decodeCaip19 } from './caip19';
import { FrontmatterIndex, getFrontmatterCollection } from './index';

export interface FrontmatterContent extends FrontmatterIndex {
html: string;
}

/**
* Get FrontmatterContent using CAIP-19, returns undefined if not found
* This includes the HTML content of the Frontmatter.
*
* @param caip19 {string}
* @see FrontmatterContent.html
* @return {FrontmatterContent | undefined}
*/
export async function getFrontmatterContent(caip19: string): Promise<FrontmatterContent | undefined> {
const [caip2, type, reference] = decodeCaip19(caip19);
const collection = getFrontmatterCollection(caip2, type);
const index = collection.find((value: FrontmatterIndex) => {
const [, , aReference] = decodeCaip19(value.path);
return aReference.toLowerCase() === reference.toLowerCase();
});
if (index === undefined) {
return undefined;
}

const [caip2Type, caip2Reference] = caip2.split(':');
const path = join(
'node_modules',
'@crypto-frontmatter',
`${caip2Type}-${caip2Reference}-${type}`,
'dist',
'Frontmatter',
`${index.fileId}.json`,
);
const contents = await readFile(path, {
encoding: 'utf-8',
});
return JSON.parse(contents);
}
38 changes: 38 additions & 0 deletions packages/crypto-frontmatter/src/content.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect, it } from '@jest/globals';

import { getFrontmatterContent } from './content';

it('should getFrontmatterContent of eip155:1/erc20:0x00000000008943c65cAf789FFFCF953bE156f6f8', async () => {
const frontmatterContent = await getFrontmatterContent('eip155:1/erc20:0x00000000008943c65cAf789FFFCF953bE156f6f8');
expect(frontmatterContent).toStrictEqual({
fileId: expect.stringMatching(/[0-f]{64}/),
type: 'Frontmatter',
path: 'eip155:1/erc20:0x00000000008943c65cAf789FFFCF953bE156f6f8',
modifiedDate: expect.any(Number),
fields: {
caip2: 'eip155:1',
namespace: 'erc20',
title: 'Dharma USD Coin',
symbol: 'dUSDC',
decimals: 8,
links: [
{
name: 'explorer',
url: 'https://etherscan.io/token/0x00000000008943c65cAf789FFFCF953bE156f6f8',
},
],
images: [
{
type: 'logo',
mine: 'image/png',
size: {
width: 512,
height: 512,
},
path: expect.stringMatching(/[0-f]{64}\.logo\.png/),
},
],
},
html: '<h1>Dharma USD Coin</h1>',
});
});
162 changes: 0 additions & 162 deletions packages/crypto-frontmatter/src/frontmatter.ts

This file was deleted.

Loading