Skip to content

Commit dca759b

Browse files
committed
Fix <Code> renderer on Patterns pages to support code blocks without meta
1 parent 2acfc0f commit dca759b

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

.changeset/brown-wasps-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'polaris.shopify.com': patch
3+
---
4+
5+
Fix <Code> renderer on Patterns pages to support code blocks without meta

polaris.shopify.com/src/components/PatternPage/PatternPage.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {PatternVariantFontMatter, PatternFrontMatter} from '../../types';
2323
import PageMeta from '../PageMeta';
2424
import {Stack, Row} from '../Stack';
2525
import {Box} from '../Box';
26+
import Code from '../Code';
2627
import {Lede} from '../Lede';
2728
import {Heading} from '../Heading';
2829
import PatternsExample from '../PatternsExample';
@@ -60,10 +61,17 @@ function codeAsContext(): Plugin {
6061
meta: Record<string, any>;
6162
}[] = [];
6263
visit(tree, 'code', (node, index, parent) => {
63-
try {
64-
codes.push({node, index: index!, parent, meta: JSON.parse(node.meta)});
65-
} catch (error) {
66-
// Just ignore this block
64+
if (node.meta) {
65+
try {
66+
codes.push({
67+
node,
68+
index: index!,
69+
parent,
70+
meta: JSON.parse(node.meta),
71+
});
72+
} catch (error) {
73+
// Just ignore this block
74+
}
6775
}
6876
});
6977

@@ -263,7 +271,7 @@ const BaseMarkdown = ({
263271
<Image fill src={src} alt={alt ?? ''} />
264272
</div>
265273
) : null,
266-
code: function Code({
274+
code: function MdCode({
267275
inline,
268276
// @ts-expect-error Unsure how to tell react-markdown this prop is
269277
// being injected by a plugin
@@ -301,7 +309,9 @@ const BaseMarkdown = ({
301309
);
302310
}
303311

304-
return <code>{(children?.[0] as string) ?? ''}</code>;
312+
return (
313+
<Code code={{title: '', code: (children?.[0] as string) ?? ''}} />
314+
);
305315
},
306316
...components,
307317
}}

0 commit comments

Comments
 (0)