Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MarkdownToJsxString implements IFormatter {
.use(remarkParse)
.use(remarkGfm) // support GitHub Flavored Markdown
.use(remarkRedisCode) // Add custom component for Redis code block
.use(remarkImage) // Add custom component for Redis code block
.use(remarkImage, config ? { history: config.history } : undefined) // Add custom component for Redis code block
.use(remarkRehype, { allowDangerousHtml: true }) // Pass raw HTML strings through.
.use(rehypeLinks, config ? { history: config.history } : undefined) // Customise links
.use(MarkdownToJsxString.rehypeWrapSymbols) // Wrap special symbols inside curly braces for JSX parse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { visit } from 'unist-util-visit'
import { IS_ABSOLUTE_PATH } from 'uiSrc/constants/regex'
import { RESOURCES_BASE_URL } from 'uiSrc/services/resourcesService'
import { ApiEndpoints } from 'uiSrc/constants'
import { IFormatterConfig } from './formatter/formatter.interfaces'

export const remarkImage = (): (tree: Node) => void => (tree: any) => {
const getSourcelPath = (search?: string) => {
switch (true) {
case search?.indexOf(ApiEndpoints.GUIDES_PATH) !== -1:
return 'static/guides/'
case search?.indexOf(ApiEndpoints.TUTORIALS_PATH) !== -1:
return 'static/tutorials/'
default:
return ''
}
}

const updateUrl = (url: string) => url.replace(/^\//, '')

export const remarkImage = (config?: IFormatterConfig): (tree: Node) => void => (tree: any) => {
const sourcePath = getSourcelPath(config?.history?.location?.search)
// Find img node in syntax tree
visit(tree, 'image', (node) => {
node.url = IS_ABSOLUTE_PATH.test(node.url || '') ? node.url : `${RESOURCES_BASE_URL}${node.url.replace(/^\//, '')}`
node.url = IS_ABSOLUTE_PATH.test(node.url || '') ? node.url : `${RESOURCES_BASE_URL}${sourcePath}${updateUrl(node.url)}`
})
}