-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown-previewer.js
130 lines (100 loc) · 4.39 KB
/
markdown-previewer.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Markdown Previewer; Pseudocode:
HTML:
- Textarea; #editor + font awesome fCC logo
- Div; #previewer + font awesome fCC logo
- Maximize/minimize button
In #textarea, include:
i. a heading element (H1 size),
ii. a sub heading element (H2 size),
iii. a link,
iv. inline code,
v. a code block,
vi. a list item,
vii. a blockquote,
viii. an image, and
ix. bolded text.
Javascript: (Yes, React-Redux)
- Funtion to link #editor & #previewer; content in #editor is updated in #previewer
- Import markdown library for content to render as HTML (https://cdnjs.com/libraries/marked)
- Method to handleChange() maximize and minimize buttons onClick.
CSS:
Color Gradients;same color different intensities.
i. #editor #previewer tabs
ii. content box
iii. background
iv. code backgrounds #fff
*/
import * as React from "https://cdn.skypack.dev/[email protected]";
import * as ReactDOM from "https://cdn.skypack.dev/[email protected]";
import * as marked from "https://cdnjs.com/libraries/marked";
const defaultContent = "# Welcome to my React Markdown Previewer!\n\n## This is a sub-heading...\n### And here's some other cool stuff:\n\nHeres some code, `<div></div>`, between 2 backticks.\n\n```\n// this is multi-line code:\n\nfunction anotherExample(firstLine, lastLine) {\n if (firstLine == '```' && lastLine == '```') {\n return multiLineCode;\n }\n}\n```\n\nYou can also make text **bold**... whoa!\nOr _italic_.\nOr... wait for it... **_both!_**\nAnd feel free to go crazy ~~crossing stuff out~~.\n\nThere's also [links](https://www.freecodecamp.org), and\n> Block Quotes!\n\nAnd if you want to get really crazy, even tables:\n\nWild Header | Crazy Header | Another Header?\n------------ | ------------- | -------------\nYour content can | be here, and it | can be here....\nAnd here. | Okay. | I think we get it.\n\n- And of course there are lists.\n - Some are bulleted.\n - With different indentation levels.\n - That look like this.\n\n\n1. And there are numbered lists too.\n1. Use just 1s if you want!\n1. And last but not least, let's not forget embedded images:\n\n\n";
/*
const initialMarkdown = `
# Welcome to my React Markdown Previewer!
## This is a sub-heading...
### And here's some other cool stuff:
Heres some code, `<div></div>`, between 2 backticks.
```
// this is multi-line code:
function anotherExample(firstLine, lastLine) {
if (firstLine == '```' && lastLine == '```') {
return multiLineCode;
}
}
```
You can also make text **bold**... whoa!
Or _italic_.
Or... wait for it... **_both!_**
And feel free to go crazy ~~crossing stuff out~~.
There's also [links](https://www.freecodecamp.org), and
> Block Quotes!
And if you want to get really crazy, even tables:
Wild Header | Crazy Header | Another Header?
------------ | ------------- | -------------
Your content can | be here, and it | can be here....
And here. | Okay. | I think we get it.
- And of course there are lists.
- Some are bulleted.
- With different indentation levels.
- That look like this.
1. And there are numbered lists too.
1. Use just 1s if you want!
1. And last but not least, let's not forget embedded images:

`;
*/
marked.setOptions({
breaks: true,
highlight: function (code) {
return Prism.highlight(code, Prism.languages.javascript, 'javascript');
}
});
const renderer = new marked.Renderer();
renderer.link = function (href, title, text) {
return `<a target="_blank" href="${href}">${text}</a>`;
};
const Editor = ({ content, handleTextareaChange }) => (
<textarea id="editor" value={content} onChange={handleTextareaChange} />
);
const Previewer = ({ content }) => (
<div
id="preview"
dangerouslySetInnerHTML={{
__html: marked(content, { renderer: renderer })
}}
/>
);
const App = () => {
const [content, setContent] = React.useState(defaultContent);
const handleTextareaChange = (event) => {
setContent(event.target.value);
};
return (
<div className="main">
<Editor content={content} handleTextareaChange={handleTextareaChange} />
<Previewer content={content} />
</div>
);
};
ReactDOM.render(<App />, document.getElementById("app"));