Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / helpApp / src / components / markdown.tsx
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import * as React from 'react';
19
20 import * as marked from 'marked';
21 import * as hljs from 'highlight.js';
22
23 type MarkdownComponentProps = {
24   text: string;
25   className?: string;
26   markedOptions?: marked.MarkedOptions;
27   style?: React.CSSProperties
28 }
29
30 const defaultRenderer = new marked.Renderer();
31 defaultRenderer.link = (href, title, text) => (
32   `<a target="_blank" rel="noopener noreferrer" href="${ href }" title="${ title }">${ text }</a>`
33 );
34
35
36 class MarkdownComponent extends React.Component<MarkdownComponentProps> {
37   constructor(props: MarkdownComponentProps) {
38     super(props);
39
40     const markedOptions: marked.MarkedOptions = {
41       gfm: true,
42       tables: true,
43       breaks: false,
44       pedantic: false,
45       sanitize: true,
46       smartLists: true,
47       smartypants: false,
48       langPrefix: 'hljs ',
49       ...(this.props.markedOptions || {}),
50       highlight: (code, lang) => {
51         if (!!(lang && hljs.getLanguage(lang))) {
52           return hljs.highlight(lang, code).value;
53         }
54         return code;
55       }
56     };
57
58     marked.setOptions(markedOptions);
59   }
60   render() {
61     const { text, className, style } = this.props;
62     
63  
64     const html = (marked(text || '', { renderer: this.props.markedOptions && this.props.markedOptions.renderer || defaultRenderer }));
65
66     return (
67       <div
68         dangerouslySetInnerHTML={ { __html: html } }
69         className={ className }
70         style={ style }
71       />
72     );
73   }
74 }
75
76 export const Markdown = MarkdownComponent;
77