forked from JimLiu/bbcode-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tonic-example.js
44 lines (39 loc) · 1.11 KB
/
tonic-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var bbcode = require('bbcode-to-react');
/* Code for customize tag
class YoutubeTag extends bbcode.Tag {
toReact() {
// using this.getContent(true) to get it's inner raw text.
const attributes = {
src: this.getContent(true),
width: this.params.width || 420,
height: this.params.height || 315,
};
return (
<iframe
{...attributes}
frameBorder="0"
allowFullScreen
/>
);
}
}
class BoldTag extends bbcode.Tag {
toReact() {
// using this.getComponents() to get children components.
return (
<b>{this.getComponents()}</b>
);
}
}
// [youtube width="400"]https://www.youtube.com/embed/AB6RjNeDII0[/youtube]
bbcode.registerTag('youtube', YoutubeTag); // add new tag
bbcode.registerTag('b', BoldTag); // replace exists tag
*/
class MyComponent extends React.Component {
render() {
return <p>{bbcode.toReact('[b]My Video: [/b][youtube width="400"]https://www.youtube.com/embed/AB6RjNeDII0[/youtube]')}</p>;
}
}
ReactDOMServer.renderToString(<MyComponent />);