Skip to content

Commit a4a02f4

Browse files
committed
fixup!
1 parent 913d2d1 commit a4a02f4

File tree

6 files changed

+57
-61
lines changed

6 files changed

+57
-61
lines changed

src/generators/jsx-ast/utils/buildContent.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SKIP, visit } from 'unist-util-visit';
99
import { createJSXElement } from './ast.mjs';
1010
import { extractHeadings, extractTextContent } from './buildBarProps.mjs';
1111
import { enforceArray } from '../../../utils/array.mjs';
12-
import { extractPrimitives } from '../../../utils/misc.mjs';
12+
import { omitKeys } from '../../../utils/misc.mjs';
1313
import { JSX_IMPORTS } from '../../web/constants.mjs';
1414
import {
1515
STABILITY_LEVELS,
@@ -296,7 +296,8 @@ export const createDocumentLayout = (entries, metadata) =>
296296
* @returns {Promise<JSXContent>}
297297
*/
298298
const buildContent = async (metadataEntries, head) => {
299-
const metadata = extractPrimitives(head);
299+
// The metadata is the heading without the node children
300+
const metadata = omitKeys(head, ['content', 'heading', 'stability']);
300301

301302
// Create root document AST with all layout components and processed content
302303
const root = createDocumentLayout(metadataEntries, metadata);

src/generators/web/types.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { JSXContent } from '../jsx-ast/utils/buildContent.mjs';
22

3+
export type Configuration = {
4+
templatePath: string;
5+
title: string;
6+
imports: Record<string, string>;
7+
virtualImports: Record<string, string>;
8+
};
9+
310
export type Generator = GeneratorMetadata<
4-
{
5-
templatePath: string;
6-
title: string;
7-
imports: Record<string, string>;
8-
virtualImports: Record<string, string>;
9-
},
11+
Configuration,
1012
Generate<Array<JSXContent>, AsyncGenerator<{ html: string; css: string }>>
1113
>;

src/generators/web/ui/types.d.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1+
import { GlobalConfiguration } from '../../../utils/configuration/types';
12
import { MetadataEntry } from '../../metadata/types';
3+
import { Configuration } from '../types';
24

35
declare global {
46
const SERVER: boolean;
57
const CLIENT: boolean;
68
}
79

810
declare module '#theme/config' {
9-
export const title: string;
10-
export const repository: string;
11+
// From global configuration
12+
export const repository: GlobalConfiguration['repository'];
13+
export const input: GlobalConfiguration['input'];
14+
export const ignore: GlobalConfiguration['ignore'];
15+
export const output: GlobalConfiguration['output'];
16+
export const minify: GlobalConfiguration['minify'];
17+
export const baseURL: GlobalConfiguration['baseURL'];
18+
export const ref: GlobalConfiguration['ref'];
19+
20+
// From web configuration
21+
export const templatePath: Configuration['templatePath'];
22+
export const title: Configuration['title'];
23+
24+
// From config generation
1125
export const version: string;
1226
export const versions: Array<{
1327
url: string;
@@ -16,7 +30,7 @@ declare module '#theme/config' {
1630
}>;
1731
export const editURL: string;
1832
export const pages: Array<[string, string]>;
19-
export const languageDisplayNameMap: Map<string, string>;
33+
export const languageDisplayNameMap: Map<string[], string>;
2034
}
2135

2236
// Omit Primitives from Metadata

src/generators/web/utils/config.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LANGS } from '@node-core/rehype-shiki';
55
import getConfig from '../../../utils/configuration/index.mjs';
66
import { populate } from '../../../utils/configuration/templates.mjs';
77
import { getVersionFromSemVer } from '../../../utils/generators.mjs';
8+
import { omitKeys } from '../../../utils/misc.mjs';
89
import { getSortedHeadNodes } from '../../jsx-ast/utils/getSortedHeadNodes.mjs';
910

1011
/**
@@ -79,10 +80,10 @@ export default function createConfigSource(input) {
7980
const pageURL = populate(config.pageURL, config);
8081

8182
const exports = {
82-
...Object.fromEntries(
83-
Object.entries(config).filter(
84-
([, v]) => v === null || typeof v !== 'object'
85-
)
83+
...omitKeys(
84+
config,
85+
// These are keys that are large, and not needed by components, so we ignore them
86+
['changelog', 'index', 'imports', 'virtualImport']
8687
),
8788
version,
8889
versions: buildVersionEntries(config.changelog, pageURL),

src/utils/__tests__/misc.test.mjs

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import assert from 'node:assert/strict';
44
import { describe, it } from 'node:test';
55

6-
import { lazy, isPlainObject, extractPrimitives, deepMerge } from '../misc.mjs';
6+
import { lazy, isPlainObject, omitKeys, deepMerge } from '../misc.mjs';
77

88
describe('lazy', () => {
99
it('should call the function only once and cache the result', () => {
@@ -38,52 +38,31 @@ describe('isPlainObject', () => {
3838
});
3939
});
4040

41-
describe('extractPrimitives', () => {
42-
it('should keep string, number, boolean, and null values', () => {
43-
const obj = { a: 'hello', b: 42, c: true, d: null };
44-
assert.deepStrictEqual(extractPrimitives(obj), {
45-
a: 'hello',
46-
b: 42,
47-
c: true,
48-
d: null,
49-
});
50-
});
51-
52-
it('should remove object and function values', () => {
53-
const obj = {
54-
name: 'test',
55-
nested: { foo: 'bar' },
56-
fn: () => {},
57-
count: 5,
58-
};
59-
const result = extractPrimitives(obj);
60-
assert.deepStrictEqual(result, { name: 'test', count: 5 });
41+
describe('omitKeys', () => {
42+
it('should return all properties when no keys are excluded', () => {
43+
const obj = { a: 'hello', b: 42, c: true };
44+
assert.deepStrictEqual(omitKeys(obj), { a: 'hello', b: 42, c: true });
6145
});
6246

63-
it('should keep arrays of primitives', () => {
64-
const obj = { tags: ['a', 'b'], name: 'test' };
65-
assert.deepStrictEqual(extractPrimitives(obj), {
66-
tags: ['a', 'b'],
67-
name: 'test',
68-
});
47+
it('should omit specified keys', () => {
48+
const obj = { a: 1, b: 2, c: 3 };
49+
assert.deepStrictEqual(omitKeys(obj, ['a', 'c']), { b: 2 });
6950
});
7051

71-
it('should remove arrays containing objects', () => {
72-
const obj = { items: [{ id: 1 }], name: 'test' };
73-
assert.deepStrictEqual(extractPrimitives(obj), { name: 'test' });
52+
it('should ignore keys that do not exist', () => {
53+
const obj = { a: 1, b: 2 };
54+
assert.deepStrictEqual(omitKeys(obj, ['z']), { a: 1, b: 2 });
7455
});
7556

76-
it('should keep undefined values', () => {
77-
const obj = { a: undefined, b: 'yes' };
78-
const result = extractPrimitives(obj);
79-
assert.strictEqual('a' in result, true);
80-
assert.strictEqual(result.a, undefined);
81-
assert.strictEqual(result.b, 'yes');
57+
it('should return an empty object when all keys are excluded', () => {
58+
const obj = { a: 1, b: 2 };
59+
assert.deepStrictEqual(omitKeys(obj, ['a', 'b']), {});
8260
});
8361

84-
it('should return an empty object when all values are non-primitive', () => {
85-
const obj = { a: {}, b: [{ x: 1 }], c: () => {} };
86-
assert.deepStrictEqual(extractPrimitives(obj), {});
62+
it('should preserve any value type', () => {
63+
const fn = () => {};
64+
const obj = { a: fn, b: new Map(), c: null, d: [1, 2] };
65+
assert.deepStrictEqual(omitKeys(obj, ['b']), { a: fn, c: null, d: [1, 2] });
8766
});
8867
});
8968

src/utils/misc.mjs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ export const isPlainObject = value =>
2020
value !== null && typeof value === 'object' && !Array.isArray(value);
2121

2222
/**
23-
* Extracts all the primitives from an object
23+
* Returns a shallow copy of `obj` without the specified keys.
24+
* @param {Record<string, any>} obj
25+
* @param {string[]} keys - Keys to exclude
26+
* @returns {Record<string, any>}
2427
*/
25-
export const extractPrimitives = obj =>
28+
export const omitKeys = (obj, keys = []) =>
2629
Object.fromEntries(
27-
Object.entries(obj).filter(
28-
([, value]) =>
29-
value !== Object(value) ||
30-
(Array.isArray(value) && value.every(item => item !== Object(item)))
31-
)
30+
Object.entries(obj).filter(([key]) => !keys.includes(key))
3231
);
3332

3433
/**

0 commit comments

Comments
 (0)