Skip to content

Commit

Permalink
🤝 Combine site options when using config extend (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch authored Jun 13, 2024
1 parent fec350a commit 4cea894
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-games-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-frontmatter': patch
'myst-cli': patch
---

Combine site options when using config extend
10 changes: 5 additions & 5 deletions packages/myst-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { writeFileToFolder, isUrl, computeHash } from 'myst-cli-utils';
import { fileError, fileWarn, RuleId } from 'myst-common';
import type { Config, ProjectConfig, SiteConfig, SiteProject } from 'myst-config';
import { validateProjectConfig, validateSiteConfig } from 'myst-config';
import { fillProjectFrontmatter } from 'myst-frontmatter';
import { fillProjectFrontmatter, fillSiteFrontmatter } from 'myst-frontmatter';
import type { ValidationOptions } from 'simple-validators';
import {
incrementOptions,
validateObjectKeys,
validationError,
validateList,
validateString,
fillMissingKeys,
} from 'simple-validators';
import { VFile } from 'vfile';
import { prepareToWrite } from './frontmatter.js';
Expand Down Expand Up @@ -84,8 +83,8 @@ function configValidationOpts(vfile: VFile, property: string, ruleId: RuleId): V
/**
* Function to add filler keys to base if the keys are not defined in base
*/
function fillSiteConfig(base: SiteConfig, filler: SiteConfig) {
return fillMissingKeys(base, filler, Object.keys(filler));
function fillSiteConfig(base: SiteConfig, filler: SiteConfig, opts: ValidationOptions) {
return fillSiteFrontmatter(base, filler, opts, Object.keys(filler));
}

/**
Expand Down Expand Up @@ -190,7 +189,7 @@ async function getValidatedConfigsFromFile(
);
session.store.dispatch(config.actions.receiveConfigExtension({ file: extFile }));
if (extSite) {
site = site ? fillSiteConfig(extSite, site) : extSite;
site = site ? fillSiteConfig(extSite, site, incrementOptions('extend', opts)) : extSite;
}
if (extProject) {
project = project ? fillProjectFrontmatter(extProject, project, projectOpts) : extProject;
Expand All @@ -204,6 +203,7 @@ async function getValidatedConfigsFromFile(
site = fillSiteConfig(
await validateSiteConfigAndThrow(session, path, vfile, rawSite),
site ?? {},
incrementOptions('extend', opts),
);
}
if (site) {
Expand Down
19 changes: 18 additions & 1 deletion packages/myst-frontmatter/src/utils/fillPageFrontmatter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, beforeEach } from 'vitest';
import type { ValidationOptions } from 'simple-validators';
import { fillPageFrontmatter } from './fillPageFrontmatter';
import { fillPageFrontmatter, fillSiteFrontmatter } from './fillPageFrontmatter';
import type { PageFrontmatter } from '../page/types';
import type { ProjectFrontmatter } from '../project/types';

Expand Down Expand Up @@ -706,3 +706,20 @@ describe('fillPageFrontmatter', () => {
});
});
});

describe('fillSiteFrontmatter', () => {
it('empty frontmatters return empty', async () => {
expect(fillSiteFrontmatter({}, {}, opts)).toEqual({});
});
it('site options are combined', async () => {
expect(
fillSiteFrontmatter(
{ options: { logo: 'my-logo.png' } },
{ options: { hide_outline: true } },
opts,
),
).toEqual({
options: { logo: 'my-logo.png', hide_outline: true },
});
});
});
73 changes: 45 additions & 28 deletions packages/myst-frontmatter/src/utils/fillPageFrontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { fillNumbering } from '../numbering/validators.js';
import { USE_PROJECT_FALLBACK } from '../page/validators.js';
import type { PageFrontmatter } from '../page/types.js';
import type { ProjectFrontmatter } from '../project/types.js';
import type { SiteFrontmatter } from '../site/types.js';
import { normalizeJsonToString } from './normalizeString.js';
import { isStashPlaceholder, stashPlaceholder } from './referenceStash.js';

Expand All @@ -23,31 +24,14 @@ export function fillPageFrontmatter(
return fillProjectFrontmatter(pageFrontmatter, projectFrontmatter, opts, USE_PROJECT_FALLBACK);
}

export function fillProjectFrontmatter(
base: ProjectFrontmatter,
filler: ProjectFrontmatter,
export function fillSiteFrontmatter(
base: SiteFrontmatter,
filler: SiteFrontmatter,
opts: ValidationOptions,
keys?: string[],
) {
const frontmatter = fillMissingKeys(base, filler, keys ?? Object.keys(filler));

if (filler.numbering || base.numbering) {
frontmatter.numbering = fillNumbering(base.numbering, filler.numbering);
}

// Combine all math macros defined on page and project
if (filler.math || base.math) {
frontmatter.math = { ...(filler.math ?? {}), ...(base.math ?? {}) };
}

// Combine all abbreviation defined on page and project
if (filler.abbreviations || base.abbreviations) {
frontmatter.abbreviations = {
...(filler.abbreviations ?? {}),
...(base.abbreviations ?? {}),
};
}

// Combine all options defined on page and project
if (filler.options || base.options) {
frontmatter.options = {
Expand All @@ -56,14 +40,6 @@ export function fillProjectFrontmatter(
};
}

// Combine all settings defined on page and project
if (filler.settings || base.settings) {
frontmatter.settings = {
...(filler.settings ?? {}),
...(base.settings ?? {}),
};
}

// Gather all contributor and affiliation ids from funding sources
const contributorIds: Set<string> = new Set();
const affiliationIds: Set<string> = new Set();
Expand Down Expand Up @@ -155,3 +131,44 @@ export function fillProjectFrontmatter(

return frontmatter;
}

export function fillProjectFrontmatter(
base: ProjectFrontmatter,
filler: ProjectFrontmatter,
opts: ValidationOptions,
keys?: string[],
) {
const frontmatter: ProjectFrontmatter = fillSiteFrontmatter(
base,
filler,
opts,
keys ?? Object.keys(filler),
);

if (filler.numbering || base.numbering) {
frontmatter.numbering = fillNumbering(base.numbering, filler.numbering);
}

// Combine all math macros defined on page and project
if (filler.math || base.math) {
frontmatter.math = { ...(filler.math ?? {}), ...(base.math ?? {}) };
}

// Combine all abbreviation defined on page and project
if (filler.abbreviations || base.abbreviations) {
frontmatter.abbreviations = {
...(filler.abbreviations ?? {}),
...(base.abbreviations ?? {}),
};
}

// Combine all settings defined on page and project
if (filler.settings || base.settings) {
frontmatter.settings = {
...(filler.settings ?? {}),
...(base.settings ?? {}),
};
}

return frontmatter;
}

0 comments on commit 4cea894

Please sign in to comment.