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 => { 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 => { 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, emojiRootPath) { const urls = await memoizedDeepListNginxDirectory(cdn); return _.compact(urls.map((s: string) => s.match(`${emojiRootPath}\/(.*)`)?.[1])); }, } export default nginxAdapter;