kramdown-prismic 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +39 -0
- data/CHANGELOG.md +36 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +8 -6
- data/LICENSE +1 -0
- data/README.md +36 -9
- data/Rakefile +3 -2
- data/bin/html2prismic +6 -0
- data/bin/markdown2prismic +6 -0
- data/bin/prismic2markdown +8 -0
- data/kramdown-prismic.gemspec +12 -8
- data/kramdown1.gemfile +6 -0
- data/kramdown2.gemfile +6 -0
- data/lib/kramdown-prismic.rb +2 -0
- data/lib/kramdown-prismic/version.rb +3 -1
- data/lib/kramdown/converter/prismic.rb +82 -39
- data/lib/kramdown/parser/prismic.rb +23 -19
- data/test/converter_test.rb +190 -124
- data/test/parser_test.rb +56 -48
- metadata +30 -15
- data/.travis.yml +0 -6
- data/Changelog.md +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e34361d43f02e8ee666fb78685a040128617accc25d44221ed0f98e45fcc7eae
|
4
|
+
data.tar.gz: 58ceb9f450d5d719b3698dcc37d417d27f6ecff3a8873812426a086bb6296695
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdd15d2fe0bb82ab3528f818579efe067f82274727c4f59ec262e899ca44224aa18b3c766f73f7c82396a8d803c93d9d92cfd864c4c24587a2743297cc60e0e1
|
7
|
+
data.tar.gz: 4cc36e4dbba12aaad5318267b268773a7010481959f9a4df4ddafaa3fc912cb9bb8830f4ac7fc8709e0c7b38c6ba2fc6a84a3436754c5d044ec4a7ab5be4646e
|
@@ -0,0 +1,39 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
jobs:
|
5
|
+
test-kramdown2:
|
6
|
+
strategy:
|
7
|
+
fail-fast: false
|
8
|
+
matrix:
|
9
|
+
os: [ubuntu-latest]
|
10
|
+
ruby: [2.6, 2.7, "3.0"]
|
11
|
+
gemfile: [ kramdown2 ]
|
12
|
+
env:
|
13
|
+
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}.gemfile
|
14
|
+
runs-on: ${{ matrix.os }}
|
15
|
+
steps:
|
16
|
+
- uses: actions/checkout@v2
|
17
|
+
- uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: ${{ matrix.ruby }}
|
20
|
+
bundler-cache: true
|
21
|
+
- run: bundle exec rake test
|
22
|
+
|
23
|
+
test-kramdown1:
|
24
|
+
strategy:
|
25
|
+
fail-fast: false
|
26
|
+
matrix:
|
27
|
+
os: [ubuntu-latest]
|
28
|
+
ruby: [2.6, 2.7]
|
29
|
+
gemfile: [ kramdown1 ]
|
30
|
+
env:
|
31
|
+
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}.gemfile
|
32
|
+
runs-on: ${{ matrix.os }}
|
33
|
+
steps:
|
34
|
+
- uses: actions/checkout@v2
|
35
|
+
- uses: ruby/setup-ruby@v1
|
36
|
+
with:
|
37
|
+
ruby-version: ${{ matrix.ruby }}
|
38
|
+
bundler-cache: true
|
39
|
+
- run: bundle exec rake test
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## Version 0.3.1
|
4
|
+
|
5
|
+
- Add binaries `html2primisc`, `markdown2prismic` and `primisc2markdown`.
|
6
|
+
- Add parsed embed support
|
7
|
+
|
8
|
+
## Version 0.3.0
|
9
|
+
|
10
|
+
- Relax the kramdown dependency to allows the version 2.
|
11
|
+
- XML elements support have been removed.
|
12
|
+
|
13
|
+
## Version 0.2.2
|
14
|
+
|
15
|
+
- Convert links with only an image inside. See #1.
|
16
|
+
|
17
|
+
## Version 0.2.1
|
18
|
+
|
19
|
+
- Parse embed elements
|
20
|
+
- Convert inline code
|
21
|
+
|
22
|
+
## Version 0.2.0
|
23
|
+
|
24
|
+
- Add parser to convert prismic to markdown
|
25
|
+
|
26
|
+
## Version 0.1.2
|
27
|
+
|
28
|
+
- fix output with empty paragraph
|
29
|
+
|
30
|
+
## Version 0.1.1
|
31
|
+
|
32
|
+
- Fix json of the image
|
33
|
+
|
34
|
+
## Version 0.1.0
|
35
|
+
|
36
|
+
- Initial version
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
kramdown-prismic (0.
|
5
|
-
kramdown (
|
4
|
+
kramdown-prismic (0.3.0)
|
5
|
+
kramdown (>= 1, < 3)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
kramdown (
|
10
|
+
kramdown (2.3.1)
|
11
|
+
rexml
|
11
12
|
minitest (5.10.3)
|
12
|
-
rake (
|
13
|
+
rake (13.0.6)
|
14
|
+
rexml (3.2.5)
|
13
15
|
|
14
16
|
PLATFORMS
|
15
17
|
ruby
|
@@ -17,7 +19,7 @@ PLATFORMS
|
|
17
19
|
DEPENDENCIES
|
18
20
|
kramdown-prismic!
|
19
21
|
minitest (~> 5.0)
|
20
|
-
rake (~>
|
22
|
+
rake (~> 13.0)
|
21
23
|
|
22
24
|
BUNDLED WITH
|
23
|
-
1.
|
25
|
+
1.17.2
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Kramdown Prismic
|
2
2
|
|
3
|
-
A [Kramdown][] parser and converter to convert documents into [prismic][] rich text format and
|
3
|
+
A [Kramdown][] parser and converter to convert documents into [prismic][] rich text format and the other way around.
|
4
4
|
|
5
|
-
A useful usage is to convert markdown documents to prismic for import purpose. Then you can even convert prismic format back to markdown.
|
5
|
+
A useful usage is to convert markdown documents to prismic for [import purpose][prismic-import]. Then you can even convert prismic format back to markdown.
|
6
6
|
|
7
7
|
## Status
|
8
8
|
|
@@ -18,7 +18,23 @@ gem 'kramdown-prismic', '~> 0.1'
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
###
|
21
|
+
### With executables
|
22
|
+
|
23
|
+
*markdown2prismic*:
|
24
|
+
|
25
|
+
markdown2prismic $'# My Title\n\nHello world'
|
26
|
+
|
27
|
+
*html2prismic*:
|
28
|
+
|
29
|
+
html2prismic '<h1>My Title</h1><p>Hello world</p>'
|
30
|
+
|
31
|
+
*prismic2markdown*:
|
32
|
+
|
33
|
+
prismic2markdown '[{"type":"heading1","content":{"text":"My Title","spans":[]}},{"type":"paragraph","content":{"text":"Test","spans":[]}}]'
|
34
|
+
|
35
|
+
### As a library
|
36
|
+
|
37
|
+
**Convert kramdown documents to Prismic**
|
22
38
|
|
23
39
|
```ruby
|
24
40
|
require 'kramdown-prismic'
|
@@ -27,7 +43,7 @@ kramdown = '# Hello world'
|
|
27
43
|
Kramdown::Document.new(kramdown).to_prismic
|
28
44
|
```
|
29
45
|
|
30
|
-
|
46
|
+
**Convert markdown documents to Prismic**
|
31
47
|
|
32
48
|
```ruby
|
33
49
|
require 'kramdown-prismic'
|
@@ -36,7 +52,7 @@ markdown = '# Hello world'
|
|
36
52
|
Kramdown::Document.new(markdown, input: :markdown).to_prismic
|
37
53
|
```
|
38
54
|
|
39
|
-
|
55
|
+
**Convert HTML documents to Prismic**
|
40
56
|
|
41
57
|
```ruby
|
42
58
|
require 'kramdown-prismic'
|
@@ -45,7 +61,7 @@ html = '<h1>Hello world</h1>'
|
|
45
61
|
Kramdown::Document.new(html, input: :html).to_prismic
|
46
62
|
```
|
47
63
|
|
48
|
-
|
64
|
+
**Convert Prismic to markdown**
|
49
65
|
|
50
66
|
```ruby
|
51
67
|
require 'kramdown-prismic'
|
@@ -86,6 +102,9 @@ Some elements cannot be converted, due to some Prismic limitations. The table be
|
|
86
102
|
| hr | nothing |
|
87
103
|
| img | moved to the top level |
|
88
104
|
| nested list | moved to the top level |
|
105
|
+
| entity | converted to unicode |
|
106
|
+
| typographic_sym | converted to unicode |
|
107
|
+
| smart_quote | converted to unicode |
|
89
108
|
| dl | not supported |
|
90
109
|
| dt | not supported |
|
91
110
|
| dd | not supported |
|
@@ -97,9 +116,6 @@ Some elements cannot be converted, due to some Prismic limitations. The table be
|
|
97
116
|
| td | not supported |
|
98
117
|
| math | not supported |
|
99
118
|
| footnote | not supported |
|
100
|
-
| entity | converted to unicode |
|
101
|
-
| typographic_sym | converted to unicode |
|
102
|
-
| smart_quote | converted to unicode |
|
103
119
|
| abbreviation | not supported |
|
104
120
|
| html_element | not supported |
|
105
121
|
| xml_comment | not supported |
|
@@ -107,9 +123,20 @@ Some elements cannot be converted, due to some Prismic limitations. The table be
|
|
107
123
|
| comment | not supported |
|
108
124
|
| raw | not supported |
|
109
125
|
|
126
|
+
## Develop
|
127
|
+
|
128
|
+
Install dependencies:
|
129
|
+
|
130
|
+
bundle install
|
131
|
+
|
132
|
+
Run tests:
|
133
|
+
|
134
|
+
bundle exec rake test
|
135
|
+
|
110
136
|
## License
|
111
137
|
|
112
138
|
MIT
|
113
139
|
|
114
140
|
[Kramdown]: https://kramdown.gettalong.org/
|
115
141
|
[prismic]: https://prismic.io/
|
142
|
+
[prismic-import]: https://prismic.io/docs/core-concepts/how-to-import-content
|
data/Rakefile
CHANGED
data/bin/html2prismic
ADDED
data/kramdown-prismic.gemspec
CHANGED
@@ -1,21 +1,25 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.expand_path('lib/kramdown-prismic/version', __dir__)
|
3
4
|
|
4
5
|
Gem::Specification.new do |s|
|
5
6
|
s.name = 'kramdown-prismic'
|
6
7
|
s.version = KramdownPrismic::VERSION
|
7
|
-
s.summary =
|
8
|
-
s.description =
|
9
|
-
s.authors = [
|
8
|
+
s.summary = 'A Kramdown converter to convert documents into prismic rich text format and the other way around.'
|
9
|
+
s.description = 'A Kramdown converter to convert documents into prismic rich text format and the other way around.'
|
10
|
+
s.authors = ['François de Metz']
|
10
11
|
s.email = 'francois@2metz.fr'
|
11
12
|
|
13
|
+
s.executables << 'markdown2prismic'
|
14
|
+
s.executables << 'html2prismic'
|
15
|
+
s.executables << 'prismic2markdown'
|
12
16
|
s.files = `git ls-files`.split("\n")
|
13
17
|
s.test_files = `git ls-files -- test/*`.split("\n")
|
14
18
|
|
15
19
|
s.homepage = 'https://github.com/stormz/kramdown-prismic'
|
16
20
|
s.license = 'MIT'
|
17
21
|
|
18
|
-
s.add_dependency
|
19
|
-
s.add_development_dependency
|
20
|
-
s.add_development_dependency
|
22
|
+
s.add_dependency 'kramdown', '>= 1', '< 3'
|
23
|
+
s.add_development_dependency 'minitest', '~> 5.0'
|
24
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
21
25
|
end
|
data/kramdown1.gemfile
ADDED
data/kramdown2.gemfile
ADDED
data/lib/kramdown-prismic.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'kramdown/converter/base'
|
2
4
|
|
3
5
|
module Kramdown
|
4
6
|
module Converter
|
5
7
|
class Prismic < Base
|
6
8
|
def convert(root)
|
7
|
-
cleanup_ast(root).map
|
9
|
+
cleanup_ast(root).map do |child|
|
8
10
|
convert_element(child)
|
9
|
-
|
11
|
+
end.compact.flatten
|
10
12
|
end
|
11
13
|
|
12
14
|
private
|
@@ -21,20 +23,23 @@ module Kramdown
|
|
21
23
|
end
|
22
24
|
|
23
25
|
def remove_blanks(root)
|
24
|
-
root.children = root.children.
|
26
|
+
root.children = root.children.each_with_object([]) do |child, memo|
|
25
27
|
unless child.type == :blank
|
26
28
|
remove_blanks(child)
|
27
29
|
memo << child
|
28
30
|
end
|
29
|
-
memo
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
34
|
def extract_non_nestable_elements(child, elements)
|
34
|
-
child.children = child.children.
|
35
|
-
if element.type == :img
|
35
|
+
child.children = child.children.each_with_object([]) do |element, memo|
|
36
|
+
if element.type == :a && element.children.size == 1 && element.children.first.type == :img
|
37
|
+
elements << element
|
38
|
+
elsif element.type == :img
|
36
39
|
elements << element
|
37
|
-
|
40
|
+
if child.children.size > 1
|
41
|
+
warning('images inside content will be moved to the top level and may be rendered differently')
|
42
|
+
end
|
38
43
|
elsif element.type == :ul
|
39
44
|
warning('nested list moved to the top level')
|
40
45
|
elements << element
|
@@ -43,7 +48,6 @@ module Kramdown
|
|
43
48
|
memo << element
|
44
49
|
extract_non_nestable_elements(element, elements)
|
45
50
|
end
|
46
|
-
memo
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -59,9 +63,10 @@ module Kramdown
|
|
59
63
|
end
|
60
64
|
|
61
65
|
def convert_p(element)
|
62
|
-
return nil if element.children.size
|
66
|
+
return nil if element.children.size.zero?
|
67
|
+
|
63
68
|
{
|
64
|
-
type:
|
69
|
+
type: 'paragraph',
|
65
70
|
content: extract_content(element)
|
66
71
|
}
|
67
72
|
end
|
@@ -104,8 +109,7 @@ module Kramdown
|
|
104
109
|
}
|
105
110
|
end
|
106
111
|
|
107
|
-
def convert_hr(element)
|
108
|
-
end
|
112
|
+
def convert_hr(element); end
|
109
113
|
|
110
114
|
def convert_img(element)
|
111
115
|
{
|
@@ -116,57 +120,91 @@ module Kramdown
|
|
116
120
|
},
|
117
121
|
data: {
|
118
122
|
origin: {
|
119
|
-
url: element.attr[
|
123
|
+
url: element.attr['src']
|
120
124
|
},
|
121
|
-
alt: element.attr[
|
125
|
+
alt: element.attr['alt']
|
126
|
+
}
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
# This can only apply when an link with an image inside has been detected
|
131
|
+
def convert_a(element)
|
132
|
+
image = element.children.first
|
133
|
+
{
|
134
|
+
type: 'image',
|
135
|
+
content: {
|
136
|
+
text: '',
|
137
|
+
spans: []
|
138
|
+
},
|
139
|
+
data: {
|
140
|
+
origin: {
|
141
|
+
url: image.attr['src']
|
142
|
+
},
|
143
|
+
alt: image.attr['alt'],
|
144
|
+
linkTo: {
|
145
|
+
url: element.attr['href']
|
146
|
+
}
|
122
147
|
}
|
123
148
|
}
|
124
149
|
end
|
125
150
|
|
126
151
|
def convert_html_element(element)
|
127
|
-
|
128
|
-
|
152
|
+
if element.value == 'iframe'
|
153
|
+
{
|
154
|
+
content: {
|
155
|
+
spans: [],
|
156
|
+
text: ''
|
157
|
+
},
|
158
|
+
type: 'embed',
|
159
|
+
data: {
|
160
|
+
embed_url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
|
161
|
+
type: 'link'
|
162
|
+
}
|
163
|
+
}
|
164
|
+
else
|
165
|
+
warning('translating html elements is not supported')
|
166
|
+
nil
|
167
|
+
end
|
129
168
|
end
|
130
169
|
|
131
|
-
def convert_table(
|
170
|
+
def convert_table(_element)
|
132
171
|
warning('translating table is not supported')
|
133
172
|
nil
|
134
173
|
end
|
135
174
|
|
136
|
-
def convert_dl(
|
175
|
+
def convert_dl(_element)
|
137
176
|
warning('translating dl is not supported')
|
138
177
|
nil
|
139
178
|
end
|
140
179
|
|
141
|
-
def convert_math(
|
180
|
+
def convert_math(_element)
|
142
181
|
warning('translating math is not supported')
|
143
182
|
nil
|
144
183
|
end
|
145
184
|
|
146
|
-
def convert_comment(
|
185
|
+
def convert_comment(_element)
|
147
186
|
warning('translating comment is not supported')
|
148
187
|
nil
|
149
188
|
end
|
150
189
|
|
151
|
-
def
|
152
|
-
warning('translating
|
153
|
-
nil
|
154
|
-
end
|
155
|
-
|
156
|
-
def convert_xml_pi(element)
|
157
|
-
warning('translating xml_pi is not supported')
|
190
|
+
def convert_raw(_element)
|
191
|
+
warning('translating raw is not supported')
|
158
192
|
nil
|
159
193
|
end
|
160
194
|
|
161
|
-
def
|
162
|
-
|
163
|
-
|
195
|
+
def convert_text(element)
|
196
|
+
{
|
197
|
+
type: 'paragraph',
|
198
|
+
content: {
|
199
|
+
text: element.value,
|
200
|
+
spans: []
|
201
|
+
}
|
202
|
+
}
|
164
203
|
end
|
165
204
|
|
166
|
-
def extract_content(element, memo={text: '', spans: []})
|
167
|
-
element.children.
|
205
|
+
def extract_content(element, memo = { text: '', spans: [] })
|
206
|
+
element.children.each_with_object(memo) do |child, memo2|
|
168
207
|
send("extract_span_#{child.type}", child, memo2)
|
169
|
-
memo2
|
170
208
|
end
|
171
209
|
end
|
172
210
|
|
@@ -187,7 +225,7 @@ module Kramdown
|
|
187
225
|
insert_span(element, memo, {
|
188
226
|
type: 'hyperlink',
|
189
227
|
data: {
|
190
|
-
url: element.attr[
|
228
|
+
url: element.attr['href']
|
191
229
|
}
|
192
230
|
})
|
193
231
|
end
|
@@ -208,15 +246,20 @@ module Kramdown
|
|
208
246
|
extract_content(element, memo)
|
209
247
|
end
|
210
248
|
|
211
|
-
def extract_span_br(
|
249
|
+
def extract_span_br(_element, memo)
|
212
250
|
memo[:text] += "\n"
|
213
251
|
end
|
214
252
|
|
215
|
-
def
|
253
|
+
def extract_span_codespan(element, memo)
|
254
|
+
warning('translating inline code is not supported')
|
255
|
+
memo[:text] += element.value
|
256
|
+
end
|
257
|
+
|
258
|
+
def extract_span_html_element(_element, _memo)
|
216
259
|
warning('translating html elements is not supported')
|
217
260
|
end
|
218
261
|
|
219
|
-
def extract_span_footnote(
|
262
|
+
def extract_span_footnote(_element, _memo)
|
220
263
|
warning('translating footnote is not supported')
|
221
264
|
end
|
222
265
|
|
@@ -233,9 +276,9 @@ module Kramdown
|
|
233
276
|
raquo_space: [::Kramdown::Utils::Entities.entity('nbsp'), ::Kramdown::Utils::Entities.entity('raquo')],
|
234
277
|
laquo: [::Kramdown::Utils::Entities.entity('laquo')],
|
235
278
|
raquo: [::Kramdown::Utils::Entities.entity('raquo')]
|
236
|
-
}
|
279
|
+
}.freeze
|
237
280
|
def extract_span_typographic_sym(element, memo)
|
238
|
-
value = TYPOGRAPHIC_SYMS[element.value].map
|
281
|
+
value = TYPOGRAPHIC_SYMS[element.value].map(&:char).join('')
|
239
282
|
memo[:text] += value
|
240
283
|
end
|
241
284
|
|