@@ -18,8 +18,8 @@ import React from 'react'
18
18
import { v4 as uuid } from 'uuid'
19
19
import TurndownService from 'turndown'
20
20
import ReactMarkdown from 'react-markdown'
21
- import { dirname , join , relative } from 'path'
22
21
import { REPL , Tab as KuiTab } from '@kui-shell/core'
22
+ import { dirname , isAbsolute , join , relative } from 'path'
23
23
24
24
import {
25
25
Link ,
@@ -78,6 +78,14 @@ export default class Markdown extends React.PureComponent<Props> {
78
78
}
79
79
}
80
80
81
+ private handleImage ( src : string , width ?: number ) {
82
+ const isLocal = ! / ^ h t t p / i. test ( src )
83
+ if ( isLocal && this . props . fullpath ) {
84
+ const absoluteSrc = isAbsolute ( src ) ? src : join ( dirname ( this . props . fullpath ) , src )
85
+ return < img src = { absoluteSrc } width = { width } />
86
+ }
87
+ }
88
+
81
89
public render ( ) {
82
90
return (
83
91
< ReactMarkdown
@@ -88,6 +96,19 @@ export default class Markdown extends React.PureComponent<Props> {
88
96
( ! this . props . nested ? ' scrollable scrollable-x scrollable-auto' : ' full-height' )
89
97
}
90
98
renderers = { {
99
+ html : props => {
100
+ if ( / < i m g / . test ( props . value ) ) {
101
+ const srcMatch = props . value . match ( / s r c = " ? ( [ ^ " \s ] + ) " ? / )
102
+ const widthMatch = props . value . match ( / w i d t h = " ? ( \d + ) " ? / )
103
+ if ( srcMatch ) {
104
+ return this . handleImage ( srcMatch [ 1 ] , widthMatch [ 1 ] ) || < span />
105
+ }
106
+ }
107
+
108
+ // Render the raw string for all other raw html tags
109
+ return < span > { props . value } </ span >
110
+ } ,
111
+
91
112
link : props => {
92
113
const isLocal = ! / ^ h t t p / i. test ( props . href )
93
114
const target = ! isLocal ? '_blank' : undefined
@@ -153,14 +174,7 @@ export default class Markdown extends React.PureComponent<Props> {
153
174
)
154
175
} ,
155
176
image : props => {
156
- const isLocal = ! / ^ h t t p / i. test ( props . src )
157
- if ( isLocal && this . props . fullpath ) {
158
- const absoluteSrc = join ( dirname ( this . props . fullpath ) , props . src )
159
- const relativeToCWD = relative ( process . cwd ( ) || process . env . PWD , absoluteSrc )
160
- return < img src = { relativeToCWD } />
161
- } else {
162
- return < img { ...props } />
163
- }
177
+ return this . handleImage ( props . src , props . width ) || < img { ...props } />
164
178
} ,
165
179
list : props => {
166
180
return React . createElement (
0 commit comments