Skip to content

Commit 88d2136

Browse files
authored
🔑 Add key to avoid react error (#2577)
1 parent d45faf3 commit 88d2136

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

.changeset/mighty-tires-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'myst-common': patch
3+
---
4+
5+
Add a key, results in a react error

packages/myst-common/src/extractParts.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { describe, expect, it } from 'vitest';
2-
import { extractImplicitPart, extractPart } from './extractParts';
2+
import { extractImplicitPart as extractImplicitPartOriginal, extractPart } from './extractParts';
33
import type { GenericParent } from '../dist';
44
import { copyNode } from './utils';
55

6+
/** Remove keys for testing purposes from the parent nodes */
7+
function extractImplicitPart(...args: Parameters<typeof extractImplicitPartOriginal>) {
8+
const part = extractImplicitPartOriginal(...args);
9+
part?.children?.forEach((child) => {
10+
delete child.key;
11+
});
12+
return part;
13+
}
14+
615
describe('extractPart', () => {
716
it('no part returns undefined', async () => {
817
expect(
@@ -597,6 +606,36 @@ describe('extractImplicitPart', () => {
597606
],
598607
});
599608
});
609+
it('original extractImplicitPart adds keys to the children', async () => {
610+
const tree: GenericParent = {
611+
type: 'root',
612+
children: [
613+
{
614+
type: 'block',
615+
children: [
616+
{
617+
type: 'heading',
618+
children: [{ type: 'text', value: 'abstract' }],
619+
},
620+
{
621+
type: 'paragraph',
622+
children: [{ type: 'text', value: 'two' }],
623+
},
624+
],
625+
},
626+
{
627+
type: 'heading',
628+
children: [{ type: 'text', value: 'abstract' }],
629+
},
630+
{
631+
type: 'paragraph',
632+
children: [{ type: 'text', value: 'four' }],
633+
},
634+
],
635+
};
636+
const hasKeys = extractImplicitPartOriginal(tree, 'abstract');
637+
expect(hasKeys?.children?.every((child) => child.key)).toBe(true);
638+
});
600639
it('part headings with no content remain', async () => {
601640
const tree: GenericParent = {
602641
type: 'root',

packages/myst-common/src/extractParts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Block } from 'myst-spec-ext';
22
import type { FrontmatterParts, GenericNode, GenericParent } from './types.js';
33
import { remove } from 'unist-util-remove';
44
import { selectAll } from 'unist-util-select';
5-
import { copyNode, toText } from './utils.js';
5+
import { copyNode, createId, toText } from './utils.js';
66
import { FRONTMATTER_ALIASES } from 'myst-frontmatter';
77

88
function coercePart(part?: string | string[]): string[] {
@@ -76,7 +76,7 @@ function createPartBlock(
7676
removePartData?: boolean;
7777
},
7878
) {
79-
const block: GenericParent = { type: 'block', children };
79+
const block: GenericParent = { type: 'block', key: createId(), children };
8080
if (!opts?.removePartData) {
8181
block.data ??= {};
8282
block.data.part = part;

0 commit comments

Comments
 (0)