-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[EPM] rewrite relative image paths #49637
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,7 +7,8 @@ import React, { useEffect, useState, Fragment } from 'react'; | |||||
import { EuiLoadingContent, EuiText } from '@elastic/eui'; | ||||||
import ReactMarkdown from 'react-markdown'; | ||||||
import { getFileByPath } from '../../data'; | ||||||
import { markdownRenderers, WrappedEuiImage } from './markdown_renderers'; | ||||||
import { markdownRenderers } from './markdown_renderers'; | ||||||
import { useLinks } from '../../hooks'; | ||||||
|
||||||
export function Readme({ | ||||||
readmePath, | ||||||
|
@@ -20,6 +21,17 @@ export function Readme({ | |||||
}) { | ||||||
const [markdown, setMarkdown] = useState<string | undefined>(undefined); | ||||||
|
||||||
const handleImageUri = React.useCallback( | ||||||
(uri: string) => { | ||||||
const { toRelativeImage } = useLinks(); | ||||||
const isRelative = | ||||||
uri.indexOf('http://') === 0 || uri.indexOf('https://') === 0 ? false : true; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also use this to tell if it starts with
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to avoid using a regex here, unless you see a case the condition is missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I don't a missing case. Why did you want to avoid a regex? Legability? Something else? I understand that regexes aren't obvious in many cases, but I think this one is pretty straightforward. Less subjectively, it has less statements and concepts than the multiple
uri.startsWith('http://') || uri.startsWith('https://') ? false : true; I'm pretty sure corejs will polyfill this for IE, but we would want to confirm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Legibility, but they are often less efficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, legibility is the only one we should be concerned with for now. I haven't profiled them, but every approach we discussed is likely to do millions of operations per second (e.g. fractions of a millisecond). The difference between 0.000033 ms and .000017 ms doesn't matter to our users or computers. Also, what's slower today can be faster tomorrow when the engine adds an optimization for it. I think |
||||||
const fullUri = isRelative ? toRelativeImage({ packageName, version, path: uri }) : uri; | ||||||
return fullUri; | ||||||
}, | ||||||
[packageName, version] | ||||||
); | ||||||
|
||||||
useEffect(() => { | ||||||
getFileByPath(readmePath).then(res => { | ||||||
setMarkdown(res); | ||||||
|
@@ -29,14 +41,9 @@ export function Readme({ | |||||
return ( | ||||||
<Fragment> | ||||||
{markdown !== undefined ? ( | ||||||
// pass down package name and version props to the image renderer to create image path | ||||||
<ReactMarkdown | ||||||
renderers={{ | ||||||
image: ({ ...props }) => ( | ||||||
<WrappedEuiImage {...props} packageName={packageName} version={version} /> | ||||||
), | ||||||
...markdownRenderers, | ||||||
}} | ||||||
transformImageUri={handleImageUri} | ||||||
renderers={markdownRenderers} | ||||||
source={markdown} | ||||||
/> | ||||||
) : ( | ||||||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We decided to not use EuiImage here as we don't need Figure and Figcaption and they are causing invalid html warnings due to being wrapped in paragraph tags.