Skip to content

Commit

Permalink
chore(website): use static json asset for .json (#38)
Browse files Browse the repository at this point in the history
#### What this PR does / why we need it:

As per title.
  • Loading branch information
fuxingloh authored Nov 15, 2023
1 parent 6ad65f3 commit b0bcbb4
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 44 deletions.
15 changes: 11 additions & 4 deletions packages/crypto-frontmatter/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ export class MirrorCommand extends Command {
for (const collection of collections) {
let count = 0;
const indexArray = await getFrontmatterIndexArray(collection.caip2, collection.namespace);
for (const frontmatterIndex of indexArray) {
for (const frontmatterImage of frontmatterIndex.fields.images) {
const from = getNodeModulesPath(collection.caip2, collection.namespace, frontmatterImage.path);
const to = join(this.target, frontmatterImage.path);

for (const index of indexArray) {
const filePath = index.fileId + '.json';
const from = getNodeModulesPath(collection.caip2, collection.namespace, filePath);
const to = join(this.target, filePath);
count++;
await copyFile(from, to);

for (const image of index.fields.images) {
const from = getNodeModulesPath(collection.caip2, collection.namespace, image.path);
const to = join(this.target, image.path);
count++;
await copyFile(from, to);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/crypto-frontmatter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface FrontmatterContent extends FrontmatterIndex {
}

export interface FrontmatterCollection {
name: string;
caip2: string;
namespace: string;
}
Expand Down Expand Up @@ -104,7 +105,11 @@ export async function getInstalledFrontmatterCollection(): Promise<FrontmatterCo
const contents = await readFile(packagePath, {
encoding: 'utf-8',
});
collections.push(JSON.parse(contents).config);
const packageJson = JSON.parse(contents);
collections.push({
name: packageJson.name,
...packageJson.config,
});
}
return collections;
}
Expand Down
68 changes: 56 additions & 12 deletions packages/crypto-frontmatter/src/index.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,63 @@ it('should getFrontmatterContent of eip155:1/erc20:0', async () => {
expect(frontmatterContent).toBeUndefined();
});

it('should getInstalledFrontmatterCollection', async () => {
it('should getInstalledFrontmatterCollection()', async () => {
const collections = await getInstalledFrontmatterCollection();
expect(collections).toStrictEqual([
{ caip2: 'eip155:1', namespace: 'erc20' },
{ caip2: 'eip155:10', namespace: 'erc20' },
{ caip2: 'eip155:1313161554', namespace: 'erc20' },
{ caip2: 'eip155:137', namespace: 'erc20' },
{ caip2: 'eip155:42161', namespace: 'erc20' },
{ caip2: 'eip155:42220', namespace: 'erc20' },
{ caip2: 'eip155:43114', namespace: 'erc20' },
{ caip2: 'eip155:56', namespace: 'erc20' },
{ caip2: 'eip155:8453', namespace: 'erc20' },
{ caip2: 'tip474:728126428', namespace: 'trc10' },
{ caip2: 'tip474:728126428', namespace: 'trc20' },
{
caip2: 'eip155:1',
name: '@crypto-frontmatter/eip155-1-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:10',
name: '@crypto-frontmatter/eip155-10-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:1313161554',
name: '@crypto-frontmatter/eip155-1313161554-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:137',
name: '@crypto-frontmatter/eip155-137-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:42161',
name: '@crypto-frontmatter/eip155-42161-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:42220',
name: '@crypto-frontmatter/eip155-42220-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:43114',
name: '@crypto-frontmatter/eip155-43114-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:56',
name: '@crypto-frontmatter/eip155-56-erc20',
namespace: 'erc20',
},
{
caip2: 'eip155:8453',
name: '@crypto-frontmatter/eip155-8453-erc20',
namespace: 'erc20',
},
{
caip2: 'tip474:728126428',
name: '@crypto-frontmatter/tip474-728126428-trc10',
namespace: 'trc10',
},
{
caip2: 'tip474:728126428',
name: '@crypto-frontmatter/tip474-728126428-trc20',
namespace: 'trc20',
},
]);
});
1 change: 1 addition & 0 deletions packages/crypto-frontmatter/src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* if you want to use it.
* For example, you must install @crypto-frontmatter/eip155-1-erc20 if you want to use eip155:1/erc frontmatter.
*
* @deprecated scheduled for removal
* @param caip2 {string}
* @param type {string}
* @param path {string}
Expand Down
2 changes: 1 addition & 1 deletion website/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# next.js
/.next/
/out/
/public/crypto-frontmatter/
/public/_crypto-frontmatter/

# misc
.DS_Store
Expand Down
46 changes: 26 additions & 20 deletions website/app/[caip2]/[asset]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrontmatterContent, FrontmatterIndex, getFrontmatterContent } from 'crypto-frontmatter';
import { computeFileId, FrontmatterContent } from 'crypto-frontmatter';
import { Metadata } from 'next';
import Image from 'next/image';
import { notFound } from 'next/navigation';
Expand All @@ -8,31 +8,38 @@ import { Highlighter } from 'shiki';
import { ContentedProse } from '@/components/contented/ContentedProse';
import { loadHighlighter, ShikiHighlighter } from '@/components/contented/ShikiHighlighter';

function asCaip19(caip2: string, asset: string): string {
return `${decodeURIComponent(caip2)}/${decodeURIComponent(asset)}`;
async function getFrontmatterContent(params: {
caip2: string;
asset: string;
}): Promise<FrontmatterContent | undefined> {
const caip19 = `${decodeURIComponent(params.caip2)}/${decodeURIComponent(params.asset)}`;
const fileId = computeFileId(caip19);
const response = await fetch(`${process.env.BASE_URL}/_crypto-frontmatter/${fileId}.json`);
if (!response.ok) {
return undefined;
}
return await response.json();
}

export async function generateMetadata(props: Parameters<typeof Page>[0]): Promise<Metadata> {
const baseUrl = process.env.BASE_URL!;
const caip19 = asCaip19(props.params.caip2, props.params.asset);
const frontmatterIndex: FrontmatterIndex | undefined = await getFrontmatterContent(caip19);
if (frontmatterIndex === undefined) {
const frontmatter = await getFrontmatterContent(props.params);
if (frontmatter === undefined) {
return notFound();
}

const title = frontmatterIndex.fields.title ?? frontmatterIndex.fields.symbol;
const title = frontmatter.fields.title ?? frontmatter.fields.symbol;

return {
title: title,
description: frontmatterIndex.fields.description,
description: frontmatter.fields.description,
openGraph: {
title: title,
description: frontmatterIndex.fields.description,
url: `${baseUrl}/${frontmatterIndex.path}`,
description: frontmatter.fields.description,
url: `${process.env.BASE_URL}/${frontmatter.path}`,
siteName: `Crypto Frontmatter`,
locale: 'en_US',
type: 'article',
modifiedTime: new Date(frontmatterIndex.modifiedDate).toISOString(),
modifiedTime: new Date(frontmatter.modifiedDate).toISOString(),
},
};
}
Expand All @@ -43,31 +50,30 @@ export default async function Page(props: {
asset: string;
};
}): Promise<ReactElement> {
const caip19 = asCaip19(props.params.caip2, props.params.asset);
const content = await getFrontmatterContent(caip19);
if (content === undefined) {
const frontmatter = await getFrontmatterContent(props.params);
if (frontmatter === undefined) {
return notFound();
}

const image = content.fields.images?.find((image) => image.type === 'logo');
const image = frontmatter.fields.images?.find((image) => image.type === 'logo');

return (
<main className="flex h-full min-w-0 flex-grow flex-col">
<div className="flex-auto pb-48">
{image !== undefined && (
<div className="mb-6 h-12 w-12">
<Image
src={`/crypto-frontmatter/${image.path}`}
alt={`${content.fields.symbol} Logo`}
src={`/_crypto-frontmatter/${image.path}`}
alt={`${frontmatter.fields.symbol} Logo`}
width={image.size.width}
height={image.size.height}
/>
</div>
)}

<ContentedProse html={content.html} />
<ContentedProse html={frontmatter.html} />

<FrontmatterJson content={content} highlighter={await loadHighlighter()} />
<FrontmatterJson content={frontmatter} highlighter={await loadHighlighter()} />
</div>
</main>
);
Expand Down
13 changes: 12 additions & 1 deletion website/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
function getBaseUrl() {
if (process.env.NEXT_PUBLIC_URL) {
return process.env.NEXT_PUBLIC_URL;
}
if (process.env.VERCEL_URL) {
return `https://${process.env.VERCEL_URL}`;
}

return `http://localhost:${process.env.PORT ?? 3000}`;
}

/** @type {import('next').NextConfig} */
const nextConfig = {
env: {
BASE_URL: process.env.NEXT_PUBLIC_URL ?? `https://${process.env.VERCEL_URL}` ?? 'https://frontmatter.levain.tech',
BASE_URL: getBaseUrl(),
},
trailingSlash: false,
reactStrictMode: true,
Expand Down
4 changes: 2 additions & 2 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"private": true,
"scripts": {
"build": "next build --no-lint",
"clean": "rm -rf .next public/crypto-frontmatter",
"crypto-frontmatter-mirror": "crypto-frontmatter mirror ./public/crypto-frontmatter",
"clean": "rm -rf .next public/_crypto-frontmatter",
"dev": "next dev",
"lint": "eslint .",
"mirror": "crypto-frontmatter mirror public/_crypto-frontmatter",
"start": "next start"
},
"lint-staged": {
Expand Down
6 changes: 3 additions & 3 deletions website/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"$schema": "https://turborepo.org/schema.json",
"extends": ["//"],
"pipeline": {
"crypto-frontmatter-mirror": {
"mirror": {
"dependsOn": ["^build"]
},
"build": {
"cache": false,
"dependsOn": ["^build", "crypto-frontmatter-mirror"]
"dependsOn": ["^build", "mirror"]
},
"dev": {
"cache": false,
"persistent": true,
"dependsOn": ["^build", "crypto-frontmatter-mirror"]
"dependsOn": ["^build", "mirror"]
}
}
}

0 comments on commit b0bcbb4

Please sign in to comment.