aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-10-23 01:30:18 +0300
committereug-vs <eugene@eug-vs.xyz>2022-10-23 01:30:18 +0300
commitad393f4243a10f9a5ab0ecd8afa5e6c07a088f5d (patch)
tree8f5758b8c1509269cae297a80f1bbfb10b68fd03
parent21a182985b7fa0c55691d5595b807dfcce2f4631 (diff)
downloadbenzin-next-ad393f4243a10f9a5ab0ecd8afa5e6c07a088f5d.tar.gz
feat: add pre-render hook
-rw-r--r--README.md1
-rw-r--r--src/config.ts19
-rw-r--r--src/lib/types.ts4
-rw-r--r--src/pages/[...path].tsx6
4 files changed, 28 insertions, 2 deletions
diff --git a/README.md b/README.md
index ecb8095..3b76736 100644
--- a/README.md
+++ b/README.md
@@ -57,3 +57,4 @@ This has a nice side-effect of basically having a *continuous delivery* for free
## TODO:
- Openring support
- Pre-render hooks
+ - RSS feed
diff --git a/src/config.ts b/src/config.ts
index 544d15c..ab0ec12 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -1,3 +1,4 @@
+import _ from 'lodash';
import { BenzinConfig } from './lib/types';
import nginxAdapter from './lib/nginxAdapter';
@@ -11,6 +12,24 @@ const config: BenzinConfig = {
emojiRoot: '/public/emoji',
css: '/public/style.css?h=benzin-next-cdn',
},
+ hooks: {
+ async preRender(path, data, adapter, cdn) {
+ if (path.length === 1 && path[0] === 'index') {
+ // Inject blog preview into home page
+ const blog = await adapter.getMarkdownSource(cdn, ['blog', 'index']);
+ const previewContent = blog
+ .split('\n')
+ .filter(line => line.startsWith('-'))
+ .slice(0, 4)
+ .join('\n');
+
+ const split = data.split('# Recent blog posts');
+ return [split[0], '# Recent blog posts\n', previewContent, split[1]].join('');
+ }
+
+ return data;
+ }
+ },
};
export default config;
diff --git a/src/lib/types.ts b/src/lib/types.ts
index ed2566f..bce6b36 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -18,4 +18,8 @@ export interface BenzinConfig {
emojiRoot: string;
css: string;
}
+
+ hooks: {
+ preRender: (path: string[], data: string, adapter: Adapter, cdn: string) => Promise<string>;
+ }
}
diff --git a/src/pages/[...path].tsx b/src/pages/[...path].tsx
index 9f55cf8..cf5db8c 100644
--- a/src/pages/[...path].tsx
+++ b/src/pages/[...path].tsx
@@ -29,12 +29,14 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
: [context.params?.path]
);
- const markdownSource = await benzinConfig.adapter.getMarkdownSource(benzinConfig.CDN, path);
+ const originalSource = await benzinConfig.adapter.getMarkdownSource(benzinConfig.CDN, path);
+ const sourceWithHooks = await benzinConfig.hooks.preRender(path, originalSource, benzinConfig.adapter, benzinConfig.CDN);
+
const emojiFileNames = await benzinConfig.adapter.getEmojiFileNames(benzinConfig.CDN, benzinConfig.paths.emojiRoot);
return {
props: {
- markdownSource,
+ markdownSource: sourceWithHooks,
emojiFileNames,
path,
}