Preview arbitrary urls within a document (#5598)
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user