aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2021-07-21 03:59:15 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2021-07-21 03:59:15 -0500
commit437268370661087dbe31323efb18dfe1a518da33 (patch)
treecffc95cc50da55eced9ed1e3b68502c68f01ca19 /utils
parent51967b62cd274b10326a604d4db670e9e89f8fd5 (diff)
downloadhomepage-437268370661087dbe31323efb18dfe1a518da33.tar.xz
homepage-437268370661087dbe31323efb18dfe1a518da33.zip
Dynamic markdown page generation with preloading
Diffstat (limited to 'utils')
-rw-r--r--utils/Posts.tsx40
-rw-r--r--utils/markedOptions.js12
2 files changed, 37 insertions, 15 deletions
diff --git a/utils/Posts.tsx b/utils/Posts.tsx
index e82357a..d82467e 100644
--- a/utils/Posts.tsx
+++ b/utils/Posts.tsx
@@ -1,5 +1,9 @@
import { promises as fs } from 'fs';
import path from 'path';
+import marked from 'marked';
+import markedOptions from './markedOptions';
+
+marked.setOptions(markedOptions);
interface PostMetadata {
name: string;
@@ -24,21 +28,24 @@ async function getMetadata(postPath : string) : Promise<PostMetadata> {
return JSON.parse(postMetadata) as PostMetadata;
}
-async function getPosts() : Promise<Post[]> {
- const postsDirectory = path.join(process.cwd(), 'posts');
- const directories = await fs.readdir(postsDirectory);
+const POST_PATH = path.join(process.cwd(), 'posts');
+
+async function getPostFromDirectory(directory : string) {
+ const postPath = path.join(POST_PATH, directory);
+
+ const meta = await getMetadata(postPath);
- const posts = directories.map(async (directory) => {
- const postPath = path.join(postsDirectory, directory);
-
- const meta = await getMetadata(postPath);
+ return {
+ directory,
+ path: postPath,
+ meta
+ };
+}
+
+async function getPosts() : Promise<Post[]> {
+ const directories = await fs.readdir(POST_PATH);
- return {
- directory,
- path: postPath,
- meta
- };
- });
+ const posts = directories.map(getPostFromDirectory);
return await Promise.all(posts);
}
@@ -48,10 +55,13 @@ async function getMarkdown(post : Post) : Promise<string> {
const markdown = await fs.readFile(markdownPath, 'utf8');
- return markdown;
+ const html = marked(markdown);
+
+ return html;
}
export {
getPosts,
- getMetadata,
+ getMarkdown,
+ getPostFromDirectory
}; \ No newline at end of file
diff --git a/utils/markedOptions.js b/utils/markedOptions.js
new file mode 100644
index 0000000..ebc7a6f
--- /dev/null
+++ b/utils/markedOptions.js
@@ -0,0 +1,12 @@
+const hljs = require('highlight.js');
+
+function highlight(code, lang) {
+ const language = hljs.getLanguage(lang) ? lang : 'plaintext';
+
+ const highlighted = hljs.highlight(code, { language }).value;
+ return highlighted;
+}
+
+module.exports = {
+ highlight
+}; \ No newline at end of file