Skip to content

Commit 92d7b35

Browse files
committed
properly merge project metadata from extensions
1 parent 9d546c0 commit 92d7b35

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

src/command/render/project.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { ensureDirSync, existsSync } from "fs/mod.ts";
88
import { dirname, isAbsolute, join, relative } from "../../deno_ral/path.ts";
99
import { info, warning } from "../../deno_ral/log.ts";
10+
import { mergeProjectMetadata } from "../../config/metadata.ts";
1011

1112
import * as colors from "fmt/colors.ts";
1213

@@ -72,6 +73,8 @@ import { Format } from "../../config/types.ts";
7273
import { fileExecutionEngine } from "../../execute/engine.ts";
7374
import { projectContextForDirectory } from "../../project/project-context.ts";
7475
import { ProjectType } from "../../project/types/types.ts";
76+
import { ProjectConfig as ProjectConfig_Project } from "../../resources/types/schema-types.ts";
77+
import { Extension } from "../../extension/types.ts";
7578

7679
const noMutationValidations = (
7780
projType: ProjectType,
@@ -217,14 +220,7 @@ const computeProjectRenderConfig = async (
217220

218221
const getProjectRenderScripts = async (
219222
context: ProjectContext,
220-
pOptions: RenderOptions,
221223
) => {
222-
const extensions = await pOptions.services.extension.extensions(
223-
undefined,
224-
context.config,
225-
context.isSingleFile ? undefined : context.dir,
226-
{ builtIn: false },
227-
);
228224
const preRenderScripts: string[] = [],
229225
postRenderScripts: string[] = [];
230226
if (context.config?.project?.[kProjectPreRender]) {
@@ -237,35 +233,40 @@ const getProjectRenderScripts = async (
237233
...asArray(context.config?.project?.[kProjectPostRender]!),
238234
);
239235
}
240-
extensions.forEach((extension) => {
241-
if (extension.contributes.metadata?.project) {
242-
const project = extension.contributes.metadata.project as Record<
243-
string,
244-
unknown
245-
>;
246-
if (project[kProjectPreRender]) {
247-
preRenderScripts.push(
248-
...asArray(project[kProjectPreRender] as string | string[]),
249-
);
250-
}
251-
if (project[kProjectPostRender]) {
252-
postRenderScripts.push(
253-
...asArray(project[kProjectPostRender] as string | string[]),
254-
);
255-
}
256-
}
257-
});
258236
return { preRenderScripts, postRenderScripts };
259237
};
260238

239+
const mergeExtensionMetadata = async (
240+
context: ProjectContext,
241+
pOptions: RenderOptions,
242+
) => {
243+
// this will mutate context.config.project to merge
244+
// in any project metadata from extensions
245+
if (context.config) {
246+
const extensions = await pOptions.services.extension.extensions(
247+
undefined,
248+
context.config,
249+
context.isSingleFile ? undefined : context.dir,
250+
{ builtIn: false },
251+
);
252+
const projectMetadata = extensions.map((extension) =>
253+
extension.contributes.metadata?.project
254+
).filter((project) => project) as ProjectConfig_Project[];
255+
context.config.project = mergeProjectMetadata(
256+
context.config.project,
257+
...projectMetadata,
258+
);
259+
}
260+
};
261+
261262
export async function renderProject(
262263
context: ProjectContext,
263264
pOptions: RenderOptions,
264265
pFiles?: string[],
265266
): Promise<RenderResult> {
267+
mergeExtensionMetadata(context, pOptions);
266268
const { preRenderScripts, postRenderScripts } = await getProjectRenderScripts(
267269
context,
268-
pOptions,
269270
);
270271

271272
// lookup the project type

src/extension/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ function findExtensions(
347347
return true;
348348
} else if (contributes === "project" && ext.contributes.project) {
349349
return true;
350+
} else if (contributes === "metadata" && ext.contributes.metadata) {
351+
return true;
350352
} else if (
351353
contributes === kRevealJSPlugins && ext.contributes[kRevealJSPlugins]
352354
) {

src/extension/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export type Contributes =
2323
| "filters"
2424
| "formats"
2525
| "project"
26-
| "revealjs-plugins";
26+
| "revealjs-plugins"
27+
| "metadata";
2728

2829
export interface Extension extends Record<string, unknown> {
2930
id: ExtensionId;
@@ -76,7 +77,8 @@ export interface ExtensionContext {
7677
| "filters"
7778
| "formats"
7879
| "project"
79-
| "revealjs-plugins",
80+
| "revealjs-plugins"
81+
| "metadata",
8082
config?: ProjectConfig,
8183
projectDir?: string,
8284
options?: ExtensionOptions,

0 commit comments

Comments
 (0)