Commit 5046e549 by Zoltán Bedi Committed by GitHub

Suggestion: render documentation as markdown in suggestion's info (#25953)

* Panel: render documentation as markdown in TypeaheadInfo

* Modify TypeaheadInfo to sanitize markdown output
parent 9d8ae391
import React from 'react';
import { mount } from 'enzyme';
import { TypeaheadInfo } from './TypeaheadInfo';
import { CompletionItem } from '../../types';
describe('TypeaheadInfo component', () => {
it('should show documentation as rendered markdown if passed as a markdown', () => {
const item: CompletionItem = { label: 'markdown', documentation: '**bold**' };
const wrapper = mount(<TypeaheadInfo item={item} height={100} />);
expect(wrapper.find('div>div').html()).toMatch('strong');
});
});
...@@ -2,7 +2,7 @@ import React, { useContext } from 'react'; ...@@ -2,7 +2,7 @@ import React, { useContext } from 'react';
import { css, cx } from 'emotion'; import { css, cx } from 'emotion';
import { CompletionItem, selectThemeVariant, ThemeContext } from '../..'; import { CompletionItem, selectThemeVariant, ThemeContext } from '../..';
import { GrafanaTheme } from '@grafana/data'; import { GrafanaTheme, renderMarkdown, textUtil } from '@grafana/data';
const getStyles = (theme: GrafanaTheme, height: number, visible: boolean) => { const getStyles = (theme: GrafanaTheme, height: number, visible: boolean) => {
return { return {
...@@ -41,7 +41,7 @@ interface Props { ...@@ -41,7 +41,7 @@ interface Props {
export const TypeaheadInfo: React.FC<Props> = ({ item, height }) => { export const TypeaheadInfo: React.FC<Props> = ({ item, height }) => {
const visible = item && !!item.documentation; const visible = item && !!item.documentation;
const label = item ? item.label : ''; const label = item ? item.label : '';
const documentation = item && item.documentation ? item.documentation : ''; const documentation = textUtil.sanitize(renderMarkdown(item?.documentation));
const theme = useContext(ThemeContext); const theme = useContext(ThemeContext);
const styles = getStyles(theme, height, visible); const styles = getStyles(theme, height, visible);
...@@ -49,7 +49,7 @@ export const TypeaheadInfo: React.FC<Props> = ({ item, height }) => { ...@@ -49,7 +49,7 @@ export const TypeaheadInfo: React.FC<Props> = ({ item, height }) => {
<div className={cx([styles.typeaheadItem])}> <div className={cx([styles.typeaheadItem])}>
<b>{label}</b> <b>{label}</b>
<hr /> <hr />
<span>{documentation}</span> <div dangerouslySetInnerHTML={{ __html: documentation }} />
</div> </div>
); );
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment