kramdown-prismic 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/kramdown-prismic/version.rb +1 -1
- data/lib/kramdown/converter/prismic.rb +8 -8
- data/lib/kramdown/parser/prismic.rb +44 -13
- data/test/parser_test.rb +132 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ee09d3336016df7164dd139beccbdf493e4fa5c057b2c3d6480d1db7c352da4
|
4
|
+
data.tar.gz: 2b14fca6c580875817413ec64d3f5fc015ef00544ddf7a4aff6bfe0edb8e160c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a3b05a335d537eedcd4ab32f45dfd7fd28f258c7557a7efe087a8e98b77d1b65922c012c9b5737939d7ea6d1651117061d3158aad626c510408091abb4d210a
|
7
|
+
data.tar.gz: cf73a15a0961791104991790e2f46ee031d80f0265fdaca1e14601822747b43a815af87a7e3c74bb0a8e60774ec847b6e46e6b0bd0eca5a46b2bffe962ede4fe
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ A useful usage is to convert markdown documents to prismic for [import purpose][
|
|
8
8
|
|
9
9
|
The converter part (kramdown to prismic) is working and fairly complete. See *Difference between markdown and rich text* below to know more about limitations.
|
10
10
|
|
11
|
-
The parser part is quite new and not feature complete
|
11
|
+
The parser part is quite new and not feature complete.
|
12
12
|
|
13
13
|
## Install
|
14
14
|
|
@@ -269,13 +269,13 @@ module Kramdown
|
|
269
269
|
end
|
270
270
|
|
271
271
|
TYPOGRAPHIC_SYMS = {
|
272
|
-
mdash: [
|
273
|
-
ndash: [
|
274
|
-
hellip: [
|
275
|
-
laquo_space: [
|
276
|
-
raquo_space: [
|
277
|
-
laquo: [
|
278
|
-
raquo: [
|
272
|
+
mdash: [Utils::Entities.entity('mdash')],
|
273
|
+
ndash: [Utils::Entities.entity('ndash')],
|
274
|
+
hellip: [Utils::Entities.entity('hellip')],
|
275
|
+
laquo_space: [Utils::Entities.entity('laquo'), Utils::Entities.entity('nbsp')],
|
276
|
+
raquo_space: [Utils::Entities.entity('nbsp'), Utils::Entities.entity('raquo')],
|
277
|
+
laquo: [Utils::Entities.entity('laquo')],
|
278
|
+
raquo: [Utils::Entities.entity('raquo')]
|
279
279
|
}.freeze
|
280
280
|
def extract_span_typographic_sym(element, memo)
|
281
281
|
value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
|
@@ -287,7 +287,7 @@ module Kramdown
|
|
287
287
|
end
|
288
288
|
|
289
289
|
def extract_span_smart_quote(element, memo)
|
290
|
-
memo[:text] +=
|
290
|
+
memo[:text] += Utils::Entities.entity(element.value.to_s).char
|
291
291
|
end
|
292
292
|
end
|
293
293
|
end
|
@@ -5,39 +5,70 @@ module Kramdown
|
|
5
5
|
class Prismic < Base
|
6
6
|
def parse
|
7
7
|
@root.options[:encoding] = 'UTF-8'
|
8
|
-
@root.children = @source.
|
9
|
-
|
10
|
-
type = 'heading' if type.match(/heading/)
|
11
|
-
element = send("parse_#{type}", block)
|
12
|
-
parse_spans(element, block)
|
13
|
-
element
|
8
|
+
@root.children = @source.reduce([]) do |memo, block|
|
9
|
+
parse_element(block, memo)
|
14
10
|
end
|
15
11
|
end
|
16
12
|
|
17
13
|
private
|
18
14
|
|
15
|
+
def parse_element(block, memo)
|
16
|
+
type = block[:type].gsub('-', '_')
|
17
|
+
type = 'heading' if type.match(/heading/)
|
18
|
+
if type == 'list_item'
|
19
|
+
parse_list(:ul, block, memo)
|
20
|
+
memo
|
21
|
+
elsif type == 'o_list_item'
|
22
|
+
parse_list(:ol, block, memo)
|
23
|
+
memo
|
24
|
+
else
|
25
|
+
element = send("parse_#{type}", block)
|
26
|
+
parse_spans(element, block)
|
27
|
+
memo << element
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
19
31
|
def parse_heading(block)
|
20
32
|
level = block[:type].match(/heading([1-6])/)[1].to_i
|
21
|
-
|
33
|
+
Element.new(:header, nil, nil, { level: level, raw_text: '' })
|
22
34
|
end
|
23
35
|
|
24
36
|
def parse_paragraph(_block)
|
25
|
-
|
37
|
+
Element.new(:p)
|
26
38
|
end
|
27
39
|
|
28
40
|
def parse_image(block)
|
29
|
-
p =
|
30
|
-
img =
|
31
|
-
|
41
|
+
p = Element.new(:p)
|
42
|
+
img = Element.new(:img, nil, { 'src' => block[:data][:origin][:url], 'alt' => block[:data][:alt] })
|
43
|
+
if block[:data][:linkTo]
|
44
|
+
a = Element.new(:a, nil, { 'href' => block[:data][:linkTo][:url] })
|
45
|
+
a.children << img
|
46
|
+
p.children << a
|
47
|
+
else
|
48
|
+
p.children << img
|
49
|
+
end
|
32
50
|
p
|
33
51
|
end
|
34
52
|
|
35
53
|
def parse_preformatted(_block)
|
36
|
-
|
54
|
+
Element.new(:blockquote)
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_list(type, block, memo)
|
58
|
+
list = memo.last
|
59
|
+
unless list && list.type == type
|
60
|
+
list = Element.new(type)
|
61
|
+
memo << list
|
62
|
+
end
|
63
|
+
li = Element.new(:li, nil, nil)
|
64
|
+
list.children << li
|
65
|
+
p = Element.new(:p, nil, nil, transparent: true)
|
66
|
+
li.children << p
|
67
|
+
parse_spans(p, block)
|
37
68
|
end
|
38
69
|
|
39
70
|
def parse_embed(block)
|
40
|
-
|
71
|
+
Element.new(:html_element, 'iframe', { src: block[:data][:embed_url] })
|
41
72
|
end
|
42
73
|
|
43
74
|
def parse_spans(element, block)
|
data/test/parser_test.rb
CHANGED
@@ -136,6 +136,114 @@ class KramdownPrismicParserTest < Minitest::Test
|
|
136
136
|
assert_equal expected, doc.to_kramdown
|
137
137
|
end
|
138
138
|
|
139
|
+
def test_parse_list_item
|
140
|
+
prismic = [
|
141
|
+
{
|
142
|
+
type: 'list-item',
|
143
|
+
content: {
|
144
|
+
text: 'Hello',
|
145
|
+
spans: []
|
146
|
+
}
|
147
|
+
},
|
148
|
+
{
|
149
|
+
type: 'list-item',
|
150
|
+
content: {
|
151
|
+
text: 'World',
|
152
|
+
spans: []
|
153
|
+
}
|
154
|
+
}
|
155
|
+
]
|
156
|
+
expected = "* Hello\n* World\n\n"
|
157
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
158
|
+
assert_equal expected, doc.to_kramdown
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_parse_list_item_and_spans
|
162
|
+
prismic = [
|
163
|
+
{
|
164
|
+
type: 'list-item',
|
165
|
+
content: {
|
166
|
+
text: 'Hello',
|
167
|
+
spans: [
|
168
|
+
{
|
169
|
+
type: 'em',
|
170
|
+
start: 0,
|
171
|
+
end: 5
|
172
|
+
}
|
173
|
+
]
|
174
|
+
}
|
175
|
+
},
|
176
|
+
{
|
177
|
+
type: 'list-item',
|
178
|
+
content: {
|
179
|
+
text: 'World',
|
180
|
+
spans: []
|
181
|
+
}
|
182
|
+
}
|
183
|
+
]
|
184
|
+
expected = "* *Hello*\n* World\n\n"
|
185
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
186
|
+
assert_equal expected, doc.to_kramdown
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_parse_o_list_item
|
190
|
+
prismic = [
|
191
|
+
{
|
192
|
+
type: 'o-list-item',
|
193
|
+
content: {
|
194
|
+
text: 'Hello',
|
195
|
+
spans: []
|
196
|
+
}
|
197
|
+
},
|
198
|
+
{
|
199
|
+
type: 'o-list-item',
|
200
|
+
content: {
|
201
|
+
text: 'World',
|
202
|
+
spans: []
|
203
|
+
}
|
204
|
+
}
|
205
|
+
]
|
206
|
+
expected = "1. Hello\n2. World\n\n"
|
207
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
208
|
+
assert_equal expected, doc.to_kramdown
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_parse_o_list_item_and_list_item
|
212
|
+
prismic = [
|
213
|
+
{
|
214
|
+
type: 'o-list-item',
|
215
|
+
content: {
|
216
|
+
text: 'Hello',
|
217
|
+
spans: []
|
218
|
+
}
|
219
|
+
},
|
220
|
+
{
|
221
|
+
type: 'o-list-item',
|
222
|
+
content: {
|
223
|
+
text: 'World',
|
224
|
+
spans: []
|
225
|
+
}
|
226
|
+
},
|
227
|
+
{
|
228
|
+
type: 'list-item',
|
229
|
+
content: {
|
230
|
+
text: 'Test',
|
231
|
+
spans: []
|
232
|
+
}
|
233
|
+
},
|
234
|
+
{
|
235
|
+
type: 'list-item',
|
236
|
+
content: {
|
237
|
+
text: 'roger',
|
238
|
+
spans: []
|
239
|
+
}
|
240
|
+
}
|
241
|
+
]
|
242
|
+
expected = "1. Hello\n2. World\n\n* Test\n* roger\n\n"
|
243
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
244
|
+
assert_equal expected, doc.to_kramdown
|
245
|
+
end
|
246
|
+
|
139
247
|
def test_parse_image
|
140
248
|
prismic = [
|
141
249
|
{
|
@@ -157,6 +265,30 @@ class KramdownPrismicParserTest < Minitest::Test
|
|
157
265
|
assert_equal expected, doc.to_kramdown
|
158
266
|
end
|
159
267
|
|
268
|
+
def test_parse_img_with_link
|
269
|
+
prismic = [
|
270
|
+
{
|
271
|
+
type: 'image',
|
272
|
+
content: {
|
273
|
+
text: '',
|
274
|
+
spans: []
|
275
|
+
},
|
276
|
+
data: {
|
277
|
+
origin: {
|
278
|
+
url: '/img.png'
|
279
|
+
},
|
280
|
+
alt: 'alt text',
|
281
|
+
linkTo: {
|
282
|
+
url: 'https://example.net/'
|
283
|
+
}
|
284
|
+
}
|
285
|
+
}
|
286
|
+
]
|
287
|
+
expected = "[][1]\n\n\n\n[1]: https://example.net/\n"
|
288
|
+
doc = Kramdown::Document.new(prismic, input: :prismic)
|
289
|
+
assert_equal expected, doc.to_kramdown
|
290
|
+
end
|
291
|
+
|
160
292
|
def test_parse_preformatted
|
161
293
|
prismic = [
|
162
294
|
{
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kramdown-prismic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- François de Metz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kramdown
|