49
loading...
This website collects cookies to deliver better user experience
blog-starter-typescript
example in the NextJs GitHub repo. Their example loads markdown files from a folder in the root directory and uses remark
and gray-matter
to convert the markdown into html and to get file metadata. The example code for these function is located in their lib folder as api.ts and markdownToHtml.ts. "dependencies": {
"@aws-sdk/client-s3": "^3.18.0",
"@fontsource/signika-negative": "^4.4.5",
"@fontsource/source-code-pro": "^4.4.5",
"gray-matter": "^4.0.3",
"next": "10.2.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"rehype-document": "^5.1.0",
"rehype-sanitize": "^4.0.0",
"rehype-stringify": "^8.0.0",
"remark": "^13.0.0",
"remark-parse": "^9.0.0",
"remark-rehype": "^8.1.0",
"slug": "^5.0.1",
"unified": "^9.2.1"
}
awsHandler.ts
to store the AWS related functions. In my index.tsx
file, which is typically found in the src/pages/
folder of NextJs application I would list the excerpts and descriptions of the blog posts. Initially this was done by reading all the markdown files contained in the root directory but now was changed to reading all the objects in S3 bucket.export async function requestAllBucketObjects(): Promise<RequestAllBucketObjectsOutput> {
const command = new ListObjectsV2Command({ Bucket: BUCKET_NAME, MaxKeys: 10 });
const objectsOutput = await client.send(command);
let promises: Promise<GetObjectCommandOutput>[] = [];
let contentKeys: string[] = [];
objectsOutput.Contents?.forEach((content) => {
if (content.Key) {
promises.push(requestSingleBucketObject(content.Key));
contentKeys.push(content.Key);
} else {
return; //skip
}
});
let allBucketObjects: GetObjectCommandOutput[] = await Promise.all(promises);
return { allBucketObjects: allBucketObjects, contentKeys: contentKeys };
}
{BUCKET_NAME}/{FILE_PATH}.{FILE_TYPE}
. I use these keys to request the metadata that I previously grabbed using gray-matter.export async function getPosts(fields: string[] = []) {
let posts: Items[] = [];
const { allBucketObjects, contentKeys } = await requestAllBucketObjects();
let promises: Promise<string>[] = [];
allBucketObjects.forEach((output) => {
promises.push(getMarkdownBody(output));
});
const markdowns = await Promise.all(promises);
markdowns.forEach((markdown, index) => {
const key = contentKeys[index];
posts.push(organizePostItems(markdown, key, fields));
});
return posts;
}
async function streamToString(readable: Readable): Promise<string> {
let data = "";
for await (const chunk of readable) {
data += chunk;
}
return data;
}