aboutsummaryrefslogtreecommitdiff
path: root/src/lib/nginxAdapter.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/nginxAdapter.ts')
-rw-r--r--src/lib/nginxAdapter.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/lib/nginxAdapter.ts b/src/lib/nginxAdapter.ts
new file mode 100644
index 0000000..fdadd87
--- /dev/null
+++ b/src/lib/nginxAdapter.ts
@@ -0,0 +1,50 @@
+import _ from 'lodash';
+import Bluebird from 'bluebird';
+import axios from 'axios';
+import mem from 'mem';
+import { Adapter } from './types';
+
+const listNginxDirectory = async (path: string): Promise<string[]> => {
+ const basePath = _.trimEnd(path.match('http[s]?://(.*?)/')?.[0], '/');
+ const response = await axios(path);
+ const [_thisDir, ...results] = response.data
+ .match(/href='(.*)'/g)
+ .map((s: string) => s.match(/'(.*)'/)?.[1])
+ .map((s: string) => basePath + s);
+ return results;
+}
+
+const deepListNginxDirectory = async (path: string): Promise<string[]> => {
+ const objects = await listNginxDirectory(path);
+ const fileUrls = objects.filter(url => !url.endsWith('/'));
+ const dirUrls = objects.filter(url => url.endsWith('/'));
+ const deepFileUrls = await Bluebird.map(dirUrls, deepListNginxDirectory);
+ return _.flattenDeep([fileUrls, deepFileUrls]);
+}
+
+const memoizedDeepListNginxDirectory = mem(deepListNginxDirectory, { maxAge: 60000 });
+
+
+// An adapter to fetch markdown & images from Nginx server with enabled directory view
+const nginxAdapter: Adapter = {
+ async getStaticMarkdownPaths(cdn) {
+ const urls = await memoizedDeepListNginxDirectory(cdn);
+ const markdownPaths = _.compact(urls.map(globalPath => globalPath.match(`${cdn}/(.*)\.md`)?.[1]));
+
+ return markdownPaths
+ .map(path => path.split('/'))
+ .map(path => ({ params: { path } }));
+ },
+
+ async getMarkdownSource(cdn, path) {
+ const { data: markdownSource } = await axios(`${cdn}/${path?.join('/')}.md`);
+ return markdownSource;
+ },
+
+ async getEmojiFileNames(cdn) {
+ const urls = await memoizedDeepListNginxDirectory(cdn);
+ return _.compact(urls.map((s: string) => s.match(/emoji\/(.*)/)?.[1]));
+ },
+}
+
+export default nginxAdapter;