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

Commit 692b88a

Browse files
committed
WIP: import md feature
1 parent 8f744ce commit 692b88a

File tree

9 files changed

+155
-22
lines changed

9 files changed

+155
-22
lines changed

dist/bundle.js

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

index.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,4 @@
1818
<div class="ce-example__button" id="saveButton">
1919
editor.save()
2020
</div>
21-
<div class="ce-example__button" id="parseButton">
22-
Parse Markdown
23-
</div>
24-
<form>
25-
<input class="ce-example__button" type="file" id="fileUpload" name="filename">
26-
</form>
2721
</html>

src/BlockTypeParsers/delimiter-type-parser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ export function parseDelimiterToMarkdown() {
33

44
return delimiter.concat('\n');
55
}
6+
7+
export function parseMarkdownToDelimiter() {
8+
let delimiterData = {};
9+
10+
delimiterData = {
11+
data: {
12+
items: [],
13+
},
14+
type: 'delimiter',
15+
};
16+
17+
return delimiterData;
18+
}

src/BlockTypeParsers/header-type-parser.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,48 @@ export function parseHeaderToMarkdown(blocks) {
1010
break;
1111
}
1212
}
13+
14+
export function parseMarkdownToHeader(blocks) {
15+
let headerData = {};
16+
17+
switch (blocks.depth) {
18+
case 1:
19+
blocks.children.forEach((item) => {
20+
headerData = {
21+
data: {
22+
level: 1,
23+
text: item.value,
24+
},
25+
type: 'header',
26+
};
27+
});
28+
29+
return headerData;
30+
case 2:
31+
blocks.children.forEach((item) => {
32+
headerData = {
33+
data: {
34+
level: 2,
35+
text: item.value,
36+
},
37+
type: 'header',
38+
};
39+
});
40+
41+
return headerData;
42+
case 3:
43+
blocks.children.forEach((item) => {
44+
headerData = {
45+
data: {
46+
level: 3,
47+
text: item.value,
48+
},
49+
type: 'header',
50+
};
51+
});
52+
53+
return headerData;
54+
default:
55+
break;
56+
}
57+
}

src/BlockTypeParsers/list-type-parser.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,26 @@ export function parseListToMarkdown(blocks) {
1313
break;
1414
}
1515
}
16+
17+
export function parseMarkdownToList(blocks) {
18+
let listData = {};
19+
const itemData = [];
20+
21+
blocks.children.forEach((items) => {
22+
items.children.forEach((listItem) => {
23+
listItem.children.forEach((listEntry) => {
24+
itemData.push(listEntry.value);
25+
});
26+
});
27+
});
28+
29+
listData = {
30+
data: {
31+
items: itemData,
32+
style: blocks.ordered ? 'ordered' : 'unordered',
33+
},
34+
type: 'list',
35+
};
36+
37+
return listData;
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
export function parseParagraphToMarkdown(blocks) {
22
return `${blocks.text}\n`;
33
}
4+
5+
export function parseMarkdownToParagraph(blocks) {
6+
let paragraphData = {};
7+
8+
if (blocks.type === 'paragraph') {
9+
blocks.children.forEach((item) => {
10+
if (item.type === 'text') {
11+
paragraphData = {
12+
data: {
13+
text: item.value,
14+
},
15+
type: 'paragraph',
16+
};
17+
}
18+
});
19+
}
20+
return paragraphData;
21+
}

src/import-markdown.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import * as remark from 'remark';
2+
import { parseMarkdownToHeader } from './BlockTypeParsers/header-type-parser';
3+
import { parseMarkdownToParagraph } from './BlockTypeParsers/paragraph-type-parser';
4+
import { parseMarkdownToList } from './BlockTypeParsers/list-type-parser';
5+
import { parseMarkdownToDelimiter } from './BlockTypeParsers/delimiter-type-parser';
6+
7+
export const editorData = [];
28

39
export default class ImportMarkdown {
410
constructor({ data, api }) {
@@ -21,8 +27,6 @@ export default class ImportMarkdown {
2127
doc.setAttribute('name', 'files[]');
2228
doc.setAttribute('onload', this.parseMarkdown());
2329

24-
// TODO: return import method
25-
2630
return doc;
2731
}
2832

@@ -32,6 +36,11 @@ export default class ImportMarkdown {
3236
const data = await this.api.saver.save();
3337
a.content = data.blocks;
3438

39+
//let b = [];
40+
console.log('EditorJs content');
41+
console.log(a.content);
42+
console.log('--------------------');
43+
3544
const fileUpload = document.getElementById('file-upload');
3645

3746
fileUpload.onchange = (e) => {
@@ -42,11 +51,41 @@ export default class ImportMarkdown {
4251

4352
reader.onload = (readerEvent) => {
4453
const content = readerEvent.target.result;
45-
console.log(content);
4654
console.log(remark().parse(content));
55+
// TODO: implement switch case block for parsing
56+
// markdown to json (editorjs)
57+
const parsedMarkdown = remark().parse(content);
58+
59+
parsedMarkdown.children.forEach((item, index) => {
60+
//console.log(item.children.forEach((item) => console.log(item)));
61+
switch (item.type) {
62+
case 'heading':
63+
return editorData.push(parseMarkdownToHeader(item));
64+
case 'paragraph':
65+
return editorData.push(parseMarkdownToParagraph(item));
66+
case 'list':
67+
return editorData.push(parseMarkdownToList(item));
68+
case 'thematicBreak':
69+
return editorData.push(parseMarkdownToDelimiter());
70+
default:
71+
break;
72+
}
73+
});
74+
// filter through array and remove empty objects
75+
console.log(editorData.filter((value) => Object.keys(value).length !== 0));
76+
77+
// TODO rerender editor with new parsed Data
78+
// this.api.blocks.clear();
79+
// this.api.blocks.render({
80+
// time: 1550476186479,
81+
// blocks: [editorData],
82+
// version: '2.18.0',
83+
// });
84+
4785
return remark().parse(content);
4886
};
4987
};
88+
5089
fileUpload.click();
5190
}
5291

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { fileUploadHandler, parseFromMarkdown } from './file-handler';
1919
import { dataField } from './data';
2020

2121
const saveButton = document.getElementById('saveButton');
22-
const newDataField = {};
2322
const editor = new EditorJS({
2423
autofocus: true,
2524

src/markdown-parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export default class MarkdownParser {
6060
return parseCheckboxToMarkdown(item.data);
6161
case 'code':
6262
return parseCodeToMarkdown(item.data);
63+
case 'checklist':
64+
return parseCheckboxToMarkdown(item.data);
6365
default:
6466
break;
6567
}

0 commit comments

Comments
 (0)