From 4ac42d335f9a403967b1b36923d39fbc8dac84e0 Mon Sep 17 00:00:00 2001 From: Nick Mitchell Date: Fri, 5 Feb 2021 14:59:17 -0500 Subject: [PATCH] fix(plugins/plugin-client-common): Markdown kuiexec drilldowns do not work if Markdown isn't passed a REPL controller Fixes #6977 --- .../src/components/Content/Markdown.tsx | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/plugins/plugin-client-common/src/components/Content/Markdown.tsx b/plugins/plugin-client-common/src/components/Content/Markdown.tsx index 96a5cde138d..b54f3a01d32 100644 --- a/plugins/plugin-client-common/src/components/Content/Markdown.tsx +++ b/plugins/plugin-client-common/src/components/Content/Markdown.tsx @@ -20,7 +20,7 @@ import TurndownService from 'turndown' import ReactMarkdown from 'react-markdown' import { dirname, isAbsolute, join, relative } from 'path' import { List, ListComponent, ListItem } from '@patternfly/react-core' -import { REPL, Tab as KuiTab, getPrimaryTabId } from '@kui-shell/core' +import { REPL, Tab as KuiTab, getPrimaryTabId, pexecInCurrentTab } from '@kui-shell/core' // GitHub Flavored Markdown plugin; see https://github.com/IBM/kui/issues/6563 import gfm from 'remark-gfm' @@ -144,35 +144,40 @@ export default class Markdown extends React.PureComponent { const isLocal = !/^http/i.test(props.href) const target = !isLocal ? '_blank' : undefined - const onClick = !isLocal - ? (evt: React.MouseEvent) => evt.stopPropagation() - : async (evt: React.MouseEvent) => { - evt.stopPropagation() - let file = props.href - if (isKuiCommand) { - const raw = props.href.match(/#kuiexec\?command=([^&]+)(&quiet)?/) - if (raw) { - const cmdline = decodeURIComponent(raw[1]) - const echo = !raw[2] - return this.props.repl.pexec(cmdline, { echo }) - } - } else if (props.href.charAt(0) === '#') { - const elt = this.props.tab.querySelector( - `[data-markdown-anchor="${this.anchorFrom(props.href.slice(1))}"]` - ) - if (elt) { - return elt.scrollIntoView() - } - } else if (file) { - if (this.props.fullpath) { - const absoluteHref = join(dirname(this.props.fullpath), props.href) - const relativeToCWD = relative(process.cwd() || process.env.PWD, absoluteHref) - file = relativeToCWD - } + const onClick = + !isLocal && !isKuiCommand + ? (evt: React.MouseEvent) => evt.stopPropagation() + : async (evt: React.MouseEvent) => { + evt.stopPropagation() + let file = props.href + if (isKuiCommand) { + const raw = props.href.match(/#kuiexec\?command=([^&]+)(&quiet)?/) + if (raw) { + const cmdline = decodeURIComponent(raw[1]) + const echo = !raw[2] + if (this.props.repl) { + return this.props.repl.pexec(cmdline, { echo }) + } else { + pexecInCurrentTab(cmdline, undefined, !echo) + } + } + } else if (props.href.charAt(0) === '#') { + const elt = this.props.tab.querySelector( + `[data-markdown-anchor="${this.anchorFrom(props.href.slice(1))}"]` + ) + if (elt) { + return elt.scrollIntoView() + } + } else if (file) { + if (this.props.fullpath) { + const absoluteHref = join(dirname(this.props.fullpath), props.href) + const relativeToCWD = relative(process.cwd() || process.env.PWD, absoluteHref) + file = relativeToCWD + } - return this.props.repl.pexec(`open ${this.props.repl.encodeComponent(file)}`) + return this.props.repl.pexec(`open ${this.props.repl.encodeComponent(file)}`) + } } - } if (!props.href) { return {props.children} @@ -187,7 +192,7 @@ export default class Markdown extends React.PureComponent { return ( - + ) }