@bitrise/bitkit 13.360.0 → 13.361.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import Link from '../Link/Link';
|
|
|
8
8
|
import List from '../List/List';
|
|
9
9
|
import ListItem from '../List/ListItem';
|
|
10
10
|
import Text from '../Text/Text';
|
|
11
|
+
import codeBlockText from './codeBlockText';
|
|
11
12
|
|
|
12
13
|
type GapType = Exclude<BoxProps['gap'], undefined>;
|
|
13
14
|
|
|
@@ -44,9 +45,10 @@ const defaultComponents = (size: 'sm' | 'md' | 'lg', gap: GapType = '16'): Compo
|
|
|
44
45
|
li: ({ node: _, ...props }) => <ListItem {...props} />,
|
|
45
46
|
ol: ({ node: _, ...props }) => <List variant="simple-ordered" {...props} />,
|
|
46
47
|
p: ({ node: _, ...props }) => <Text {...props} />,
|
|
47
|
-
|
|
48
|
+
// The parser closes a code block with a newline, which would render as an empty last line.
|
|
49
|
+
pre: ({ node: _, children }) => (
|
|
48
50
|
<CodeSnippet variant="multi" size={codeSize}>
|
|
49
|
-
{
|
|
51
|
+
{codeBlockText(children).replace(/\n$/, '')}
|
|
50
52
|
</CodeSnippet>
|
|
51
53
|
),
|
|
52
54
|
ul: ({ node: _, ...props }) => <List {...props} />,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Children, isValidElement, ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collects the text of a markdown code block.
|
|
5
|
+
*
|
|
6
|
+
* A fenced code block arrives as `<pre><code>…</code></pre>`, so the nested `<code>` would also go
|
|
7
|
+
* through the `code` component of MarkdownContent and wrap the block in an inline CodeSnippet.
|
|
8
|
+
* Passing the text instead keeps the block a single element, and it is what CodeSnippet's copy
|
|
9
|
+
* button puts on the clipboard.
|
|
10
|
+
*
|
|
11
|
+
* Note that this flattens any markup inside the block, so a rehype plugin that decorates code
|
|
12
|
+
* (syntax highlighting, for instance) contributes its text but not its elements.
|
|
13
|
+
*/
|
|
14
|
+
export const codeBlockText = (children: ReactNode): string =>
|
|
15
|
+
// Children.toArray already drops null, undefined and booleans.
|
|
16
|
+
Children.toArray(children)
|
|
17
|
+
.map((child) =>
|
|
18
|
+
isValidElement<{ children?: ReactNode }>(child) ? codeBlockText(child.props.children) : String(child),
|
|
19
|
+
)
|
|
20
|
+
.join('');
|
|
21
|
+
|
|
22
|
+
export default codeBlockText;
|