Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit d75cb02

Browse files
committed
feat(plugins/plugin-client-common): allow wizard steps to be specified in topmatter
1 parent 6652266 commit d75cb02

File tree

8 files changed

+270
-139
lines changed

8 files changed

+270
-139
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2022 The Kubernetes Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export interface WizardSteps {
18+
/** An alternate way to define the steps of a wizard layout */
19+
wizard: {
20+
steps: string[]
21+
}
22+
}
23+
24+
type KuiFrontmatter = Partial<WizardSteps> & {
25+
/** Title of the Notebook */
26+
title?: string
27+
28+
layoutCount?: Record<string, number>
29+
30+
/**
31+
* A mapping that indicates which section (the `number` values) should be rendered in a given split position.
32+
*/
33+
layout?: 'wizard' | Record<number | 'default', SplitPositionSpec>
34+
}
35+
36+
export function hasWizardSteps(frontmatter: KuiFrontmatter): frontmatter is KuiFrontmatter & Required<WizardSteps> {
37+
return (
38+
frontmatter.wizard &&
39+
frontmatter.wizard.steps &&
40+
Array.isArray(frontmatter.wizard.steps) &&
41+
frontmatter.wizard.steps.length > 0
42+
)
43+
}
44+
45+
type SplitPosition = 'left' | 'bottom' | 'default' | 'wizard'
46+
type SplitPositionObj = { position: SplitPosition; placeholder?: string; maximized?: boolean | 'true' | 'false' }
47+
type SplitPositionSpec = SplitPosition | SplitPositionObj
48+
49+
export type PositionProps = {
50+
'data-kui-split': SplitPosition
51+
}
52+
53+
export function isValidPosition(position: SplitPositionSpec): position is SplitPosition {
54+
return (
55+
typeof position === 'string' &&
56+
(position === 'default' || position === 'left' || position === 'bottom' || position === 'wizard')
57+
)
58+
}
59+
60+
export function isValidPositionObj(position: SplitPositionSpec): position is SplitPositionObj {
61+
const pos = position as SplitPositionObj
62+
return typeof pos === 'object' && typeof pos.position === 'string'
63+
}
64+
65+
export default KuiFrontmatter
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2022 The Kubernetes Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { isCodedError, isWatchable } from '@kui-shell/core'
18+
import { CodeBlockResponse } from '../components/code'
19+
20+
function stringifyError(response: Error) {
21+
return { code: isCodedError(response) ? response.code : 1, message: response.message }
22+
}
23+
24+
/** Blunt attempt to avoid serializing React bits */
25+
function reactRedactor(key: string, value: any) {
26+
if (key === 'tab') {
27+
return undefined
28+
} else if (key === 'block') {
29+
return undefined
30+
} else if (value && typeof value === 'object' && value.constructor === Error) {
31+
// the first check guards against typeof null === 'object'
32+
return stringifyError(value)
33+
} else {
34+
return value
35+
}
36+
}
37+
38+
export default function encodePriorResponses(responses: CodeBlockResponse[]): string {
39+
return JSON.stringify(
40+
responses.map(response => {
41+
if (response.response.constructor === Error) {
42+
return Object.assign({}, response, { response: stringifyError(response.response) })
43+
} else if (isWatchable(response.response)) {
44+
delete response.response.watch
45+
}
46+
return response
47+
}),
48+
reactRedactor
49+
)
50+
}

plugins/plugin-client-common/src/components/Content/Markdown/components/Wizard/rehype-wizard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { u } from 'unist-builder'
1818
import { visitParents } from 'unist-util-visit-parents'
1919

20-
import { PositionProps } from '../../frontmatter'
20+
import { PositionProps } from '../../KuiFrontmatter'
2121

2222
interface Primordial {
2323
title: string

plugins/plugin-client-common/src/components/Content/Markdown/components/div.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { TextContent } from '@patternfly/react-core'
2020
import SplitInjector from '../../../Views/Terminal/SplitInjector'
2121
import SplitPosition from '../../../Views/Terminal/SplitPosition'
2222

23-
import { PositionProps } from '../frontmatter'
23+
import { PositionProps } from '../KuiFrontmatter'
2424

2525
import { isWizard } from './Wizard/rehype-wizard'
2626
const Wizard = React.lazy(() => import('./Wizard'))

0 commit comments

Comments
 (0)