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

Commit e5246f5

Browse files
committed
fix(plugins/plugin-client-common): improve Missing titles in guide steps
This PR detects when we don't have enough context for a code block, and if so looks for the nearest enclosing heading (or tip title) to add that missing context.
1 parent 8e42ab1 commit e5246f5

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

plugins/plugin-client-common/src/components/Content/Markdown/components/code/rehype-code-indexer.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import isExecutable from './isCodeBlock'
2424
import { isTab } from '../../rehype-tabbed'
2525
import isElementWithProperties from '../../isElement'
2626
import toMarkdownString, { Node } from '../../toMarkdownString'
27+
import { getTipTitle, isTipWithFullTitle, isTipWithoutFullTitle } from '../../rehype-tip'
28+
2729
import {
2830
isHeading,
2931
isHeadingOrRemovedHeading,
@@ -67,9 +69,9 @@ function findNearestEnclosingTitle(grandparent: Parent, parent: Node) {
6769
if (grandparent && parentIdx >= 0 && isElementWithProperties(grandparent)) {
6870
for (let idx = parentIdx - 1; idx >= 0; idx--) {
6971
const child = grandparent.children[idx]
70-
if (isHeadingOrRemovedHeading(child)) {
72+
if (isHeadingOrRemovedHeading(child) || isTipWithFullTitle(child)) {
7173
return {
72-
title: isHeading(child) ? toString(child) : '',
74+
title: isHeading(child) ? toString(child) : isTipWithFullTitle(child) ? getTipTitle(child) : '',
7375
source: grandparent.children
7476
.slice(idx, parentIdx + 1)
7577
.map(toMarkdownString)
@@ -217,6 +219,31 @@ export default function plugin(uuid: string) {
217219
if (attributes.nesting) {
218220
attributes.nesting = attributes.nesting.reverse()
219221
reserialize()
222+
223+
let parent = ancestors[ancestors.length - 1]
224+
let grandparent = ancestors[ancestors.length - 2]
225+
226+
if (isTipWithoutFullTitle(grandparent)) {
227+
parent = grandparent
228+
grandparent = ancestors[ancestors.length - 3]
229+
}
230+
231+
if (parent && grandparent && attributes.nesting.length === 1) {
232+
// Hmm, we don't have much context for this code
233+
// block. Try adding a bit more context by looking for
234+
// an enclosing heading.
235+
const parentHeading = findNearestEnclosingTitle(grandparent, parent)
236+
if (parentHeading && parentHeading.title) {
237+
const { title, source } = parentHeading
238+
addNesting(attributes, {
239+
kind: 'Import',
240+
source,
241+
title,
242+
key: title,
243+
filepath: ''
244+
})
245+
}
246+
}
220247
}
221248

222249
// go from bottom to top stopping at the first import,

plugins/plugin-client-common/src/components/Content/Markdown/rehype-tip/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Node, Element } from 'hast'
1718
import { i18n } from '@kui-shell/core'
1819

20+
import isElementWithProperties from '../isElement'
21+
1922
const strings = i18n('plugin-client-common', 'markdown')
2023

2124
const RE_TIP = /^([?!][?!][?!])(\+?)\s+(tip|todo|bug|info|note|warning|caution|success|question)(\s+"(.+)"\s*)?(\s+inline)?(\s+inline\s+end)?(\n(.|[\n\r])*)?$/i
@@ -25,6 +28,22 @@ const RE_TIP_END = /^(.*)"\s*(\n(.|[\n\r])*)?$/
2528
export const START_OF_TIP = `<!-- ____KUI_START_OF_TIP____ -->`
2629
export const END_OF_TIP = `<!-- ____KUI_END_OF_TIP____ -->`
2730

31+
export function isTip(node: Node): node is Element {
32+
return isElementWithProperties(node) && node.tagName === 'tip'
33+
}
34+
35+
export function getTipTitle(elt: Element) {
36+
return elt.properties.title.toString()
37+
}
38+
39+
export function isTipWithFullTitle(node: Node): node is Element {
40+
return isTip(node) && !!node.properties.fullTitle
41+
}
42+
43+
export function isTipWithoutFullTitle(node: Node): node is Element {
44+
return isTip(node) && !node.properties.fullTitle
45+
}
46+
2847
export default function plugin(/* options */) {
2948
return function transformer(tree) {
3049
let currentTip
@@ -108,6 +127,7 @@ export default function plugin(/* options */) {
108127
properties: {
109128
className: startMatch[3].toLowerCase(), // e.g. tip, todo, bug, warning, ...
110129
float: startMatch[6] ? 'left' : startMatch[7] ? 'right' : undefined,
130+
hasFullTitle: !!startMatch[5],
111131
title: startMatch[5] || strings(startMatch[3]),
112132
open: !!startMatch[2] || startMatch[1] === '!!!'
113133
},
@@ -127,6 +147,7 @@ export default function plugin(/* options */) {
127147
tagName: 'tip',
128148
properties: {
129149
className: startMatch[3].toLowerCase(), // e.g. tip, todo, bug, warning, ...
150+
hasFullTitle: !!startMatch[5],
130151
title: startMatch[5] || strings(startMatch[3]),
131152
open: !!startMatch[2] || startMatch[1] === '!!!',
132153
partial: true

plugins/plugin-client-common/src/components/Content/Markdown/toMarkdownString.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Node } from 'hast-util-to-mdast/lib'
2121
import { visit, CONTINUE, SKIP } from 'unist-util-visit'
2222
import { toMarkdown } from 'mdast-util-to-markdown'
2323

24+
import { isTip } from './rehype-tip'
2425
import { isImports } from './remark-import'
2526
import indent, { indentAll } from './indent'
2627
import isElementWithProperties from './isElement'
@@ -109,7 +110,7 @@ ${tabContent}
109110
delete node.children
110111
delete node.properties
111112
return SKIP
112-
} else if (node.tagName === 'tip') {
113+
} else if (isTip(node)) {
113114
const { className, title, open } = node.properties
114115

115116
const tipContent = node.children

0 commit comments

Comments
 (0)