Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.

Commit 7024194

Browse files
committed
WIP: import md feature
1 parent 79a38e0 commit 7024194

File tree

11 files changed

+370
-148
lines changed

11 files changed

+370
-148
lines changed

dist/bundle.js

Lines changed: 19 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<div class="ce-example__button" id="saveButton">
1919
editor.save()
2020
</div>
21-
22-
21+
<form>
22+
<input class="ce-example__button" type="file" id="fileUpload" name="filename">
23+
</form>
2324
</html>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"version": "1.0.0",
3636
"main": "markdown-parser.js",
3737
"dependencies": {
38+
"remark": "^13.0.0",
3839
"require": "^2.4.20"
3940
}
4041
}

src/BlockTypeParsers/checkbox-type-parser.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ export function parseCheckboxToMarkdown(blocks) {
66
return `- [x] ${item.text}`;
77
}
88
return `- [ ] ${item.text}`;
9-
10-
return null;
119
});
1210

1311
return items.join('\n');

src/BlockTypeParsers/header-type-parser.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ export function parseHeaderToMarkdown(blocks) {
22
switch (blocks.level) {
33
case 1:
44
return `# ${blocks.text}\n`;
5-
break;
65
case 2:
76
return `## ${blocks.text}\n`;
8-
break;
97
case 3:
108
return `### ${blocks.text}\n`;
11-
break;
129
default:
1310
break;
1411
}

src/BlockTypeParsers/list-type-parser.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ export function parseListToMarkdown(blocks) {
55
items = blocks.items.map((item) => (`* ${item}`));
66

77
return items.join('\n');
8-
break;
98
case 'ordered':
109
items = blocks.items.map((item, index) => (`${index + 1} ${item}`));
1110

1211
return items.join('\n');
13-
break;
1412
default:
1513
break;
1614
}

src/data.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
export const dataField = {
2+
blocks: [
3+
{
4+
type: 'header',
5+
data: {
6+
text: 'Editor.js',
7+
level: 2,
8+
},
9+
},
10+
{
11+
type: 'paragraph',
12+
data: {
13+
text: 'Hey. Meet the new Editor. On this page you can see it in action — try to edit this text. Source code of the page contains the example of connection and configuration.',
14+
},
15+
},
16+
{
17+
type: 'header',
18+
data: {
19+
text: 'Key features',
20+
level: 3,
21+
},
22+
},
23+
{
24+
type: 'list',
25+
data: {
26+
items: [
27+
'It is a block-styled editor',
28+
'It returns clean data output in JSON',
29+
'Designed to be extendable and pluggable with a simple API',
30+
],
31+
style: 'unordered',
32+
},
33+
},
34+
{
35+
type: 'header',
36+
data: {
37+
text: 'What does it mean «block-styled editor»',
38+
level: 3,
39+
},
40+
},
41+
{
42+
type: 'paragraph',
43+
data: {
44+
text: 'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.',
45+
},
46+
},
47+
{
48+
type: 'paragraph',
49+
data: {
50+
text: 'There are dozens of <a href="https://github.com/editor-js">ready-to-use Blocks</a> and the <a href="https://editorjs.io/creating-a-block-tool">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.',
51+
},
52+
},
53+
{
54+
type: 'header',
55+
data: {
56+
text: 'What does it mean clean data output',
57+
level: 3,
58+
},
59+
},
60+
{
61+
type: 'paragraph',
62+
data: {
63+
text: 'Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below',
64+
},
65+
},
66+
{
67+
type: 'paragraph',
68+
data: {
69+
text: 'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.',
70+
},
71+
},
72+
{
73+
type: 'paragraph',
74+
data: {
75+
text: 'Clean data is useful to sanitize, validate and process on the backend.',
76+
},
77+
},
78+
{
79+
type: 'delimiter',
80+
data: {},
81+
},
82+
{
83+
type: 'paragraph',
84+
data: {
85+
text: 'We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make its core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏',
86+
},
87+
},
88+
{
89+
type: 'image',
90+
data: {
91+
url: 'assets/codex2x.png',
92+
caption: '',
93+
stretched: false,
94+
withBorder: true,
95+
withBackground: false,
96+
},
97+
},
98+
],
99+
};

src/file-handler.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function fileHandler(content, fileName) {
1+
export function fileDownloadHandler(content, fileName) {
22
const file = new File([content], { type: 'text/markdown', endings: 'transparent' });
33
const url = URL.createObjectURL(file);
44

@@ -10,3 +10,10 @@ export function fileHandler(content, fileName) {
1010
window.URL.revokeObjectURL(url);
1111
document.body.removeChild(element);
1212
}
13+
14+
export function fileUploadHandler(){
15+
const reader = new FileReader();
16+
reader.addEventListener('load', (event) => {
17+
console.log(event.target.result);
18+
});
19+
}

src/index.js

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import Table from '@editorjs/table';
1414
import Marker from '@editorjs/marker';
1515
import MarkdownParser from './markdown-parser';
1616

17+
import { dataField } from './data';
18+
1719
const saveButton = document.getElementById('saveButton');
20+
const newDataField = {};
1821
const editor = new EditorJS({
1922
autofocus: true,
2023

@@ -28,8 +31,8 @@ const editor = new EditorJS({
2831
*/
2932
tools: {
3033
/**
31-
* Each Tool is a Plugin. Pass them via 'class' option with necessary settings {@link docs/tools.md}
32-
*/
34+
* Each Tool is a Plugin. Pass them via 'class' option with necessary settings {@link docs/tools.md}
35+
*/
3336
header: {
3437
class: Header,
3538
inlineToolbar: ['link'],
@@ -108,105 +111,7 @@ const editor = new EditorJS({
108111
/**
109112
* Initial Editor data
110113
*/
111-
data: {
112-
blocks: [
113-
{
114-
type: 'header',
115-
data: {
116-
text: 'Editor.js',
117-
level: 2,
118-
},
119-
},
120-
{
121-
type: 'paragraph',
122-
data: {
123-
text: 'Hey. Meet the new Editor. On this page you can see it in action — try to edit this text. Source code of the page contains the example of connection and configuration.',
124-
},
125-
},
126-
{
127-
type: 'header',
128-
data: {
129-
text: 'Key features',
130-
level: 3,
131-
},
132-
},
133-
{
134-
type: 'list',
135-
data: {
136-
items: [
137-
'It is a block-styled editor',
138-
'It returns clean data output in JSON',
139-
'Designed to be extendable and pluggable with a simple API',
140-
],
141-
style: 'unordered',
142-
},
143-
},
144-
{
145-
type: 'header',
146-
data: {
147-
text: 'What does it mean «block-styled editor»',
148-
level: 3,
149-
},
150-
},
151-
{
152-
type: 'paragraph',
153-
data: {
154-
text: 'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.',
155-
},
156-
},
157-
{
158-
type: 'paragraph',
159-
data: {
160-
text: 'There are dozens of <a href="https://github.com/editor-js">ready-to-use Blocks</a> and the <a href="https://editorjs.io/creating-a-block-tool">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.',
161-
},
162-
},
163-
{
164-
type: 'header',
165-
data: {
166-
text: 'What does it mean clean data output',
167-
level: 3,
168-
},
169-
},
170-
{
171-
type: 'paragraph',
172-
data: {
173-
text: 'Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below',
174-
},
175-
},
176-
{
177-
type: 'paragraph',
178-
data: {
179-
text: 'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.',
180-
},
181-
},
182-
{
183-
type: 'paragraph',
184-
data: {
185-
text: 'Clean data is useful to sanitize, validate and process on the backend.',
186-
},
187-
},
188-
{
189-
type: 'delimiter',
190-
data: {},
191-
},
192-
{
193-
type: 'paragraph',
194-
data: {
195-
text: 'We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make its core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏',
196-
},
197-
},
198-
{
199-
type: 'image',
200-
data: {
201-
url: 'assets/codex2x.png',
202-
caption: '',
203-
stretched: false,
204-
withBorder: true,
205-
withBackground: false,
206-
},
207-
},
208-
],
209-
},
114+
data: dataField,
210115
onReady() {
211116
saveButton.click();
212117
},

src/markdown-parser.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import { parseParagraphToMarkdown } from './BlockTypeParsers/paragraph-type-pars
33
import { parseListToMarkdown } from './BlockTypeParsers/list-type-parser';
44
import { parseDelimiterToMarkdown } from './BlockTypeParsers/delimiter-type-parser';
55
import { parseImageToMarkdown } from './BlockTypeParsers/image-type-parser';
6-
import { parseCheckboxToMarkdown} from './BlockTypeParsers/checkbox-type-parser';
6+
import { parseCheckboxToMarkdown } from './BlockTypeParsers/checkbox-type-parser';
77
import { parseQuoteToMarkdown } from './BlockTypeParsers/quote-type-parser';
88
import { parseCodeToMarkdown } from './BlockTypeParsers/code-type-parser';
9-
import { fileHandler } from './file-handler';
9+
import { fileDownloadHandler } from './file-handler';
1010

1111
export default class MarkdownParser {
1212
constructor({ data, api }) {
1313
this.data = data;
1414
this.api = api;
1515
this.export = document.querySelector('[data-tool="markdownParser"]');
16-
this.getContent();
1716
}
1817

1918
static get toolbox() {
@@ -24,6 +23,7 @@ export default class MarkdownParser {
2423
}
2524

2625
render() {
26+
this.getContent();
2727
return document.createElement('div');
2828
}
2929

@@ -34,32 +34,31 @@ export default class MarkdownParser {
3434
a.content = data.blocks;
3535
console.log(a.content);
3636

37-
//TODO: remove html tags in paragraphs (code, highlight, a href)
38-
const b = a.content.map(item => {
39-
switch(item.type) {
40-
case 'header':
41-
return parseHeaderToMarkdown(item.data);
42-
case 'paragraph':
43-
return parseParagraphToMarkdown(item.data);
44-
case 'list':
45-
return parseListToMarkdown(item.data);
46-
case 'delimiter':
47-
return parseDelimiterToMarkdown(item);
48-
case 'image':
49-
return parseImageToMarkdown(item.data);
50-
case 'quote':
51-
return parseQuoteToMarkdown(item.data);
52-
case 'checkbox':
53-
return parseCheckboxToMarkdown(item.data);
54-
case 'code':
55-
return parseCodeToMarkdown(item.data);
56-
default:
57-
break;
58-
}
59-
});
60-
37+
// TODO: remove html tags in paragraphs (code, highlight, a href)
38+
const b = a.content.map((item) => {
39+
switch (item.type) {
40+
case 'header':
41+
return parseHeaderToMarkdown(item.data);
42+
case 'paragraph':
43+
return parseParagraphToMarkdown(item.data);
44+
case 'list':
45+
return parseListToMarkdown(item.data);
46+
case 'delimiter':
47+
return parseDelimiterToMarkdown(item);
48+
case 'image':
49+
return parseImageToMarkdown(item.data);
50+
case 'quote':
51+
return parseQuoteToMarkdown(item.data);
52+
case 'checkbox':
53+
return parseCheckboxToMarkdown(item.data);
54+
case 'code':
55+
return parseCodeToMarkdown(item.data);
56+
default:
57+
break;
58+
}
59+
});
6160

62-
fileHandler(b.join('\n'), 'example.md');
61+
fileDownloadHandler(b.join('\n'), 'example.md');
6362
console.log(b);
6463
}
6564

0 commit comments

Comments
 (0)