Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitb090c86

Browse files
committed
refactor(editor-parser): basic json -> html workflow
1 parentf618493 commitb090c86

File tree

2 files changed

+209
-110
lines changed

2 files changed

+209
-110
lines changed

‎lib/helper/rich_text_parser.ex‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,76 @@ defmodule Helper.RichTextParser do
44
55
see https://editorjs.io/
66
"""
7+
@html_class_prefix"cps-viewer"
78

8-
defstring_to_json(string)do
9-
Jason.decode(string)
9+
defconvert_to_html(string)whenis_binary(string)do
10+
with{:ok,parsed}=string_to_json(string),
11+
true<-valid_editor_data?(parsed)do
12+
content=
13+
Enum.reduce(parsed["blocks"],"",fnblock,acc->
14+
acc<>parse_block(block)
15+
end)
16+
17+
"<div class=\"#{@html_class_prefix}\">#{content}<div>"
18+
# |> IO.inspect(label: "hello")
19+
end
20+
end
21+
22+
defpparse_block(%{"type"=>"header","data"=>data})do
23+
# IO.inspect(data, label: "parse header")
24+
text=get_in(data,["text"])
25+
level=get_in(data,["level"])
26+
27+
"<h#{level}>#{text}</h#{level}>"
28+
end
29+
30+
defpparse_block(%{"type"=>"paragraph","data"=>data})do
31+
# IO.inspect(data, label: "parse paragraph")
32+
text=get_in(data,["text"])
33+
34+
"<p>#{text}</p>"
35+
end
36+
37+
defpparse_block(%{"type"=>"image","data"=>data})do
38+
IO.inspect(data,label:"parse image")
39+
url=get_in(data,["file","url"])
40+
41+
"<div class=\"#{@html_class_prefix}-image\"><img src=\"#{url}\"></div>"
42+
|>IO.inspect(label:"iamge ret")
43+
end
44+
45+
# defp parse_block(%{"type" => "list", "data" => data}) do
46+
# IO.inspect(data, label: "parse list")
47+
# end
48+
49+
# defp parse_block(%{"type" => "delimiter", "data" => data}) do
50+
# IO.inspect(data, label: "parse delimiter")
51+
# end
52+
53+
# defp parse_block(%{"type" => "linkTool", "data" => data}) do
54+
# IO.inspect(data, label: "parse linkTool")
55+
# data |> get_in(["link"]) |> IO.inspect(label: "linkTool ret")
56+
# end
57+
58+
# defp parse_block(%{"type" => "quote", "data" => data}) do
59+
# IO.inspect(data, label: "parse quote")
60+
# data |> get_in(["text"]) |> IO.inspect(label: "quote ret")
61+
# end
62+
63+
defpparse_block(block)do
64+
IO.puts(".")
65+
""
66+
# IO.inspect(block, label: "parse unknow")
67+
end
68+
69+
defstring_to_json(string),do:Jason.decode(string)
70+
71+
defpvalid_editor_data?(map)whenis_map(map)do
72+
Map.has_key?(map,"time")and
73+
Map.has_key?(map,"version")and
74+
Map.has_key?(map,"blocks")and
75+
is_list(map["blocks"])and
76+
is_binary(map["version"])and
77+
is_integer(map["time"])
1078
end
1179
end

‎test/helper/rich_text_parser_test.exs‎

Lines changed: 139 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,131 +5,162 @@ defmodule GroupherServer.Test.Helper.RichTextParserTest do
55

66
aliasHelper.RichTextParser,as:Parser
77

8+
@real_editor_data~S({
9+
"time" : 1567250876713,
10+
"blocks" : [
11+
{
12+
"type" : "header",
13+
"data" : {
14+
"text" : "Editor.js",
15+
"level" : 2
16+
}
17+
},
18+
{
19+
"type" : "paragraph",
20+
"data" : {
21+
"text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
22+
}
23+
},
24+
{
25+
"type" : "header",
26+
"data" : {
27+
"text" : "Key features",
28+
"level" : 3
29+
}
30+
},
31+
{
32+
"type" : "list",
33+
"data" : {
34+
"style" : "unordered",
35+
"items" : [
36+
"It is a block-styled editor",
37+
"It returns clean data output in JSON",
38+
"Designed to be extendable and pluggable with a simple API"
39+
]
40+
}
41+
},
42+
{
43+
"type" : "header",
44+
"data" : {
45+
"text" : "What does it mean «block-styled editor»",
46+
"level" : 3
47+
}
48+
},
49+
{
50+
"type" : "paragraph",
51+
"data" : {
52+
"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."
53+
}
54+
},
55+
{
56+
"type" : "paragraph",
57+
"data" : {
58+
"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."
59+
}
60+
},
61+
{
62+
"type" : "header",
63+
"data" : {
64+
"text" : "What does it mean clean data output",
65+
"level" : 3
66+
}
67+
},
68+
{
69+
"type" : "paragraph",
70+
"data" : {
71+
"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"
72+
}
73+
},
74+
{
75+
"type" : "paragraph",
76+
"data" : {
77+
"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."
78+
}
79+
},
80+
{
81+
"type" : "paragraph",
82+
"data" : {
83+
"text" : "Clean data is useful to sanitize, validate and process on the backend."
84+
}
85+
},
86+
{
87+
"type" : "delimiter",
88+
"data" : {}
89+
},
90+
{
91+
"type" : "paragraph",
92+
"data" : {
93+
"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 it's 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. 😏"
94+
}
95+
},
96+
{
97+
"type" : "image",
98+
"data" : {
99+
"file" : {
100+
"url" : "https://codex.so/upload/redactor_images/o_e48549d1855c7fc1807308dd14990126.jpg"
101+
},
102+
"caption" : "",
103+
"withBorder" : true,
104+
"stretched" : false,
105+
"withBackground" : false
106+
}
107+
},
108+
{
109+
"type" : "linkTool",
110+
"data" : {
111+
"link" : "https://www.github.com",
112+
"meta" : {
113+
"url" : "https://www.github.com",
114+
"domain" : "www.github.com",
115+
"title" : "Build software better, together",
116+
"description" : "GitHub is where people build software. More than 40 million people use GitHub to discover, fork, and contribute to over 100 million projects.",
117+
"image" : {
118+
"url" : "https://github.githubassets.com/images/modules/open_graph/github-logo.png"
119+
}
120+
}
121+
}
122+
},
123+
{
124+
"type" : "quote",
125+
"data" : {
126+
"text" : "hello world",
127+
"caption" : "desc?",
128+
"alignment" : "left"
129+
}
130+
}
131+
],
132+
"version" : "2.15.0"
133+
})
134+
8135
describe"[basic convert]"do
9-
@tag:wip
10-
test"string_json should work"do
136+
test"basic string_json should work"do
11137
string=~S({"time":1566184478687,"blocks":[{}],"version":"2.15.0"})
12138
{:ok,converted}=Parser.string_to_json(string)
13139

14140
assertconverted["time"]==1_566_184_478_687
15141
assertconverted["version"]=="2.15.0"
16142
end
17143

18-
@tag:wip
19144
test"invalid string data should get error"do
20145
string=~S({"time":1566184478687,"blocks":[{}],"version":})
21146
assert{:error,converted}=Parser.string_to_json(string)
22147
end
23148

24149
@tag:wip
25150
test"real-world editor.js data should work"do
26-
string=~S({
27-
"time" : 1567250876713,
28-
"blocks" : [
29-
{
30-
"type" : "header",
31-
"data" : {
32-
"text" : "Editor.js",
33-
"level" : 2
34-
}
35-
},
36-
{
37-
"type" : "paragraph",
38-
"data" : {
39-
"text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
40-
}
41-
},
42-
{
43-
"type" : "header",
44-
"data" : {
45-
"text" : "Key features",
46-
"level" : 3
47-
}
48-
},
49-
{
50-
"type" : "list",
51-
"data" : {
52-
"style" : "unordered",
53-
"items" : [
54-
"It is a block-styled editor",
55-
"It returns clean data output in JSON",
56-
"Designed to be extendable and pluggable with a simple API"
57-
]
58-
}
59-
},
60-
{
61-
"type" : "header",
62-
"data" : {
63-
"text" : "What does it mean «block-styled editor»",
64-
"level" : 3
65-
}
66-
},
67-
{
68-
"type" : "paragraph",
69-
"data" : {
70-
"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."
71-
}
72-
},
73-
{
74-
"type" : "paragraph",
75-
"data" : {
76-
"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."
77-
}
78-
},
79-
{
80-
"type" : "header",
81-
"data" : {
82-
"text" : "What does it mean clean data output",
83-
"level" : 3
84-
}
85-
},
86-
{
87-
"type" : "paragraph",
88-
"data" : {
89-
"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"
90-
}
91-
},
92-
{
93-
"type" : "paragraph",
94-
"data" : {
95-
"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."
96-
}
97-
},
98-
{
99-
"type" : "paragraph",
100-
"data" : {
101-
"text" : "Clean data is useful to sanitize, validate and process on the backend."
102-
}
103-
},
104-
{
105-
"type" : "delimiter",
106-
"data" : {}
107-
},
108-
{
109-
"type" : "paragraph",
110-
"data" : {
111-
"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 it's 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. 😏"
112-
}
113-
},
114-
{
115-
"type" : "image",
116-
"data" : {
117-
"file" : {
118-
"url" : "https://codex.so/upload/redactor_images/o_e48549d1855c7fc1807308dd14990126.jpg"
119-
},
120-
"caption" : "",
121-
"withBorder" : true,
122-
"stretched" : false,
123-
"withBackground" : false
124-
}
125-
}
126-
],
127-
"version" : "2.15.0"
128-
})
129-
{:ok,converted}=Parser.string_to_json(string)
130-
IO.inspect(converted,label:"haha")
151+
{:ok,converted}=Parser.string_to_json(@real_editor_data)
131152

132153
assertnotEnum.empty?(converted["blocks"])
154+
assertconverted["blocks"]|>is_list
155+
assertconverted["version"]|>is_binary
156+
assertconverted["time"]|>is_integer
157+
end
158+
159+
@tag:wip
160+
test"todo"do
161+
# IO.inspect(converted, label: "haha")
162+
Parser.convert_to_html(@real_editor_data)
163+
# assert not Enum.empty?(converted["blocks"])
133164
end
134165
end
135166
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp