Preview arbitrary urls within a document (#5598)

This commit is contained in:
Apoorv Mishra
2023-07-30 05:21:49 +05:30
committed by GitHub
parent 67691477a9
commit ddc883bfcd
8 changed files with 83 additions and 38 deletions

View File

@@ -1,3 +1,4 @@
import { existsSync } from "fs";
import path from "path";
import glob from "glob";
import { startCase } from "lodash";
@@ -5,20 +6,36 @@ import env from "@server/env";
import Logger from "@server/logging/Logger";
import { UnfurlResolver } from "@server/types";
const resolvers: Record<string, UnfurlResolver> = {};
const rootDir = env.ENVIRONMENT === "test" ? "" : "build";
glob
.sync(path.join(rootDir, "plugins/*/server/unfurl.js"))
.forEach((filePath: string) => {
const hasResolver = (plugin: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config = require(path.join(process.cwd(), plugin, "plugin.json"));
return (
existsSync(resolverPath(plugin)) &&
(config.requiredEnvVars ?? []).every((name: string) => !!env[name])
);
};
const resolverPath = (plugin: string) =>
path.join(plugin, "server", "unfurl.js");
const plugins = glob.sync(path.join(rootDir, "plugins/*"));
const resolvers: Record<string, UnfurlResolver> = plugins
.filter(hasResolver)
.map(resolverPath)
.reduce((resolvers, resolverPath) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const resolver: UnfurlResolver = require(path.join(
process.cwd(),
filePath
resolverPath
));
const name = startCase(filePath.split("/")[2]);
const name = startCase(resolverPath.split("/")[2]);
resolvers[name] = resolver;
Logger.debug("utils", `Registered unfurl resolver ${filePath}`);
});
Logger.debug("utils", `Registered unfurl resolver ${resolverPath}`);
export const Iframely = resolvers["Iframely"];
return resolvers;
}, {});
export default resolvers;