motion-markdown-it 4.4.0 → 8.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +69 -16
  3. data/lib/motion-markdown-it.rb +7 -5
  4. data/lib/motion-markdown-it/common/html_blocks.rb +6 -2
  5. data/lib/motion-markdown-it/common/utils.rb +19 -4
  6. data/lib/motion-markdown-it/helpers/helper_wrapper.rb +9 -0
  7. data/lib/motion-markdown-it/helpers/parse_link_destination.rb +8 -7
  8. data/lib/motion-markdown-it/index.rb +60 -18
  9. data/lib/motion-markdown-it/parser_block.rb +7 -10
  10. data/lib/motion-markdown-it/parser_inline.rb +50 -14
  11. data/lib/motion-markdown-it/presets/commonmark.rb +7 -1
  12. data/lib/motion-markdown-it/presets/default.rb +4 -3
  13. data/lib/motion-markdown-it/presets/zero.rb +6 -1
  14. data/lib/motion-markdown-it/renderer.rb +46 -14
  15. data/lib/motion-markdown-it/rules_block/blockquote.rb +167 -31
  16. data/lib/motion-markdown-it/rules_block/code.rb +4 -3
  17. data/lib/motion-markdown-it/rules_block/fence.rb +9 -4
  18. data/lib/motion-markdown-it/rules_block/heading.rb +8 -3
  19. data/lib/motion-markdown-it/rules_block/hr.rb +10 -5
  20. data/lib/motion-markdown-it/rules_block/html_block.rb +6 -3
  21. data/lib/motion-markdown-it/rules_block/lheading.rb +64 -26
  22. data/lib/motion-markdown-it/rules_block/list.rb +91 -22
  23. data/lib/motion-markdown-it/rules_block/paragraph.rb +14 -9
  24. data/lib/motion-markdown-it/rules_block/reference.rb +24 -14
  25. data/lib/motion-markdown-it/rules_block/state_block.rb +79 -24
  26. data/lib/motion-markdown-it/rules_block/table.rb +52 -26
  27. data/lib/motion-markdown-it/rules_core/normalize.rb +1 -23
  28. data/lib/motion-markdown-it/rules_core/replacements.rb +22 -2
  29. data/lib/motion-markdown-it/rules_core/smartquotes.rb +41 -12
  30. data/lib/motion-markdown-it/rules_inline/autolink.rb +5 -4
  31. data/lib/motion-markdown-it/rules_inline/balance_pairs.rb +48 -0
  32. data/lib/motion-markdown-it/rules_inline/emphasis.rb +104 -149
  33. data/lib/motion-markdown-it/rules_inline/entity.rb +2 -2
  34. data/lib/motion-markdown-it/rules_inline/escape.rb +5 -3
  35. data/lib/motion-markdown-it/rules_inline/image.rb +12 -23
  36. data/lib/motion-markdown-it/rules_inline/link.rb +20 -25
  37. data/lib/motion-markdown-it/rules_inline/newline.rb +2 -1
  38. data/lib/motion-markdown-it/rules_inline/state_inline.rb +60 -1
  39. data/lib/motion-markdown-it/rules_inline/strikethrough.rb +81 -97
  40. data/lib/motion-markdown-it/rules_inline/text_collapse.rb +40 -0
  41. data/lib/motion-markdown-it/token.rb +46 -1
  42. data/lib/motion-markdown-it/version.rb +1 -1
  43. data/spec/motion-markdown-it/markdown_it_spec.rb +2 -2
  44. data/spec/motion-markdown-it/misc_spec.rb +90 -14
  45. data/spec/motion-markdown-it/testgen_helper.rb +1 -1
  46. data/spec/spec_helper.rb +2 -3
  47. metadata +13 -13
  48. data/lib/motion-markdown-it/common/url_schemas.rb +0 -173
  49. data/spec/motion-markdown-it/bench_mark_spec.rb +0 -44
@@ -0,0 +1,40 @@
1
+ # Merge adjacent text nodes into one, and re-calculate all token levels
2
+ #------------------------------------------------------------------------------
3
+ module MarkdownIt
4
+ module RulesInline
5
+ class TextCollapse
6
+
7
+ #------------------------------------------------------------------------------
8
+ def self.text_collapse(state)
9
+ level = 0
10
+ tokens = state.tokens
11
+ max = state.tokens.length
12
+
13
+ last = curr = 0
14
+ while curr < max
15
+ # re-calculate levels
16
+ level += tokens[curr].nesting
17
+ tokens[curr].level = level
18
+
19
+ if tokens[curr].type == 'text' &&
20
+ curr + 1 < max &&
21
+ tokens[curr + 1].type == 'text'
22
+
23
+ # collapse two adjacent text nodes
24
+ tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content
25
+ else
26
+ tokens[last] = tokens[curr] if curr != last
27
+
28
+ last += 1
29
+ end
30
+
31
+ curr += 1
32
+ end
33
+
34
+ if curr != last
35
+ tokens.slice!(last..max)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -111,7 +111,52 @@ module MarkdownIt
111
111
  @attrs = [ attrData ]
112
112
  end
113
113
  end
114
-
114
+
115
+ # Token.attrSet(name, value)
116
+ #
117
+ # Set `name` attribute to `value`. Override old value if exists.
118
+ #------------------------------------------------------------------------------
119
+ def attrSet(name, value)
120
+ idx = attrIndex(name)
121
+ attrData = [ name, value ]
122
+
123
+ if idx < 0
124
+ attrPush(attrData)
125
+ else
126
+ @attrs[idx] = attrData
127
+ end
128
+ end
129
+
130
+ # Token.attrGet(name)
131
+ #
132
+ # Get the value of attribute `name`, or null if it does not exist.
133
+ #------------------------------------------------------------------------------
134
+ def attrGet(name)
135
+ idx = attrIndex(name)
136
+ value = nil
137
+
138
+ if idx >= 0
139
+ value = @attrs[idx][1]
140
+ end
141
+
142
+ return value
143
+ end
144
+
145
+ # Token.attrJoin(name, value)
146
+ #
147
+ # Join value to existing attribute via space. Or create new attribute if not
148
+ # exists. Useful to operate with token classes.
149
+ #------------------------------------------------------------------------------
150
+ def attrJoin(name, value)
151
+ idx = attrIndex(name)
152
+
153
+ if idx < 0
154
+ attrPush([ name, value ])
155
+ else
156
+ @attrs[idx][1] = @attrs[idx][1] + ' ' + value
157
+ end
158
+ end
159
+
115
160
  #------------------------------------------------------------------------------
116
161
  def to_json
117
162
  {
@@ -1,5 +1,5 @@
1
1
  module MotionMarkdownIt
2
2
 
3
- VERSION = '4.4.0'
3
+ VERSION = '8.4.1'
4
4
 
5
5
  end
@@ -1,5 +1,5 @@
1
1
  #------------------------------------------------------------------------------
2
- describe "markdown-it" do
2
+ describe "markdown-it" do
3
3
 
4
4
  parser = MarkdownIt::Parser.new({ html: true, langPrefix: '', typographer: true, linkify: true })
5
5
  datadir = File.join(File.dirname(__FILE__), 'fixtures', 'markdown-it')
@@ -14,5 +14,5 @@ describe "markdown-it" do
14
14
  define_test(t, parser)
15
15
  end
16
16
  end
17
- end
17
+ end
18
18
  end
@@ -49,7 +49,7 @@ describe 'API' do
49
49
  it 'highlight' do
50
50
  md = MarkdownIt::Parser.new({
51
51
  highlight: lambda do |str, obj|
52
- return '==' + str + '=='
52
+ return '<pre><code>==' + str + '==</code></pre>'
53
53
  end
54
54
  })
55
55
 
@@ -59,7 +59,7 @@ describe 'API' do
59
59
  #------------------------------------------------------------------------------
60
60
  it 'highlight escape by default' do
61
61
  md = MarkdownIt::Parser.new({highlight: lambda {|value, obj| return nil }})
62
-
62
+
63
63
  # assert.strictEqual(md.render("```\n&\n```"), "<pre><code>&amp;\n</code></pre>\n");
64
64
  expect(md.render("```\n&\n```")).to eq "<pre><code>&amp;\n</code></pre>\n"
65
65
  end
@@ -68,7 +68,7 @@ describe 'API' do
68
68
  it 'force hardbreaks' do
69
69
  md = MarkdownIt::Parser.new({ breaks: false })
70
70
  expect(md.render("a\nb")).to eq "<p>a\nb</p>\n"
71
-
71
+
72
72
  md.set({ breaks: true })
73
73
  expect(md.render("a\nb")).to eq "<p>a<br>\nb</p>\n"
74
74
  md.set({ xhtmlOut: true })
@@ -157,6 +157,13 @@ describe 'API' do
157
157
  expect(md.renderInline('_foo_')).to eq '<em>foo</em>'
158
158
  end
159
159
 
160
+ it 'input type check' do
161
+ md = MarkdownIt::Parser.new
162
+
163
+ expect {
164
+ md.render(nil)
165
+ }.to raise_error('Input data should be a String')
166
+ end
160
167
  end
161
168
 
162
169
 
@@ -213,8 +220,8 @@ describe 'Misc' do
213
220
 
214
221
  md.enable('emphasis')
215
222
 
216
- expect(md.render('___foo___')).to eq "<p><strong><em>foo</em></strong></p>\n"
217
- expect(md.renderInline('___foo___')).to eq '<strong><em>foo</em></strong>'
223
+ expect(md.render('___foo___')).to eq "<p><em><strong>foo</strong></em></p>\n"
224
+ expect(md.renderInline('___foo___')).to eq '<em><strong>foo</strong></em>'
218
225
  end
219
226
 
220
227
  #------------------------------------------------------------------------------
@@ -234,6 +241,26 @@ describe 'Misc' do
234
241
  # assert.strictEqual(md.render('[foo](bar)'), '<p><a href="bar" target="_blank">foo</a></p>\n');
235
242
  # end
236
243
 
244
+ # TODO ------------------------------------------------------------------------------
245
+ # it 'Should normalize CR to LF' do
246
+ # var md = markdownit();
247
+ #
248
+ # assert.strictEqual(
249
+ # md.render('# test\r\r - hello\r - world\r'),
250
+ # md.render('# test\n\n - hello\n - world\n')
251
+ # );
252
+ # end
253
+
254
+ # TODO ------------------------------------------------------------------------------
255
+ # it 'Should normalize CR+LF to LF' do
256
+ # var md = markdownit();
257
+ #
258
+ # assert.strictEqual(
259
+ # md.render('# test\r\n\r\n - hello\r\n - world\r\n'),
260
+ # md.render('# test\n\n - hello\n - world\n')
261
+ # );
262
+ # end
263
+
237
264
  end
238
265
 
239
266
 
@@ -248,7 +275,7 @@ describe 'Url normalization' do
248
275
  expect(/example\.com/ =~ url).to_not eq nil
249
276
  return 'LINK'
250
277
  end
251
-
278
+
252
279
  md.normalizeLinkText = lambda do |url|
253
280
  expect(/example\.com/ =~ url).to_not eq nil
254
281
  return 'TEXT'
@@ -281,25 +308,29 @@ describe 'Links validation' do
281
308
  expect(md.render('[test](http://example.com)')).to eq "<p>[test](http://example.com)</p>\n"
282
309
  expect(md.render('![test](http://example.com)')).to eq "<p>![test](http://example.com)</p>\n"
283
310
  end
284
-
285
311
  end
286
312
 
287
313
 
288
314
  #------------------------------------------------------------------------------
289
315
  describe 'maxNesting' do
290
316
 
291
- #------------------------------------------------------------------------------
292
- it 'Inline parser should not nest above limit' do
293
- md = MarkdownIt::Parser.new({ maxNesting: 2 })
294
- expect(md.render('*foo *bar *baz* bar* foo*')).to eq "<p><em>foo <em>bar *baz* bar</em> foo</em></p>\n"
295
- end
296
-
297
317
  #------------------------------------------------------------------------------
298
318
  it 'Block parser should not nest above limit' do
299
319
  md = MarkdownIt::Parser.new({ maxNesting: 2 })
300
320
  expect(md.render(">foo\n>>bar\n>>>baz")).to eq "<blockquote>\n<p>foo</p>\n<blockquote></blockquote>\n</blockquote>\n"
301
321
  end
302
322
 
323
+ it 'Inline parser should not nest above limit' do
324
+ md = MarkdownIt::Parser.new({ maxNesting: 1 })
325
+
326
+ expect(md.render('[`foo`]()')).to eq "<p><a href=\"\">`foo`</a></p>\n"
327
+ end
328
+
329
+ it 'Inline nesting coverage' do
330
+ md = MarkdownIt::Parser.new({ maxNesting: 2 })
331
+
332
+ expect(md.render('[[[[[[[[[[[[[[[[[[foo]()')).to eq "<p>[[[[[[[[[[[[[[[[[[foo]()</p>\n"
333
+ end
303
334
  end
304
335
 
305
336
  #------------------------------------------------------------------------------
@@ -326,5 +357,50 @@ describe 'smartquotes' do
326
357
  it 'Should support multi-character quotes in different tags' do
327
358
  expect(md.render('"a *b \'c *d* e\' f* g"')).to eq "<p>[[[a <em>b (((((c <em>d</em> e)))) f</em> g]]</p>\n"
328
359
  end
329
-
330
360
  end
361
+
362
+ #------------------------------------------------------------------------------
363
+ describe 'Token attributes' do
364
+
365
+ #------------------------------------------------------------------------------
366
+ it '.attrJoin' do
367
+ md = MarkdownIt::Parser.new
368
+
369
+ tokens = md.parse('```', {})
370
+ t = tokens[0]
371
+
372
+ t.attrJoin('class', 'foo')
373
+ t.attrJoin('class', 'bar')
374
+
375
+ expect(md.renderer.render(tokens, md.options, {})).to eq "<pre><code class=\"foo bar\"></code></pre>\n"
376
+ end
377
+
378
+ #------------------------------------------------------------------------------
379
+ it '.attrSet' do
380
+ md = MarkdownIt::Parser.new
381
+
382
+ tokens = md.parse('```', {})
383
+ t = tokens[0]
384
+
385
+ t.attrSet('class', 'foo')
386
+
387
+ expect(md.renderer.render(tokens, md.options, {})).to eq "<pre><code class=\"foo\"></code></pre>\n"
388
+
389
+ t.attrSet('class', 'bar')
390
+
391
+ expect(md.renderer.render(tokens, md.options, {})).to eq "<pre><code class=\"bar\"></code></pre>\n"
392
+ end
393
+
394
+ it '.attrGet' do
395
+ md = MarkdownIt::Parser.new
396
+
397
+ tokens = md.parse('```', {})
398
+ t = tokens[0]
399
+
400
+ expect(t.attrGet('myattr')).to eq nil
401
+
402
+ t.attrSet('myattr', 'myvalue')
403
+
404
+ expect(t.attrGet('myattr')).to eq 'myvalue'
405
+ end
406
+ end
@@ -18,7 +18,7 @@ def get_tests(specfile)
18
18
  tests = []
19
19
  header_re = /#+ /
20
20
  filename = File.basename(specfile)
21
-
21
+
22
22
  File.open(specfile) do |specf|
23
23
  specf.each_line do |line|
24
24
  line_number += 1
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # make sure to have `--require spec_helper` in `.rspec` to have the
2
- # spec_helper.rb included automatically in spec files
3
- require 'byebug'
4
- require 'benchmark'
2
+ # spec_helper.rb included automatically in spec files
3
+ require 'pry-byebug'
5
4
  require 'motion-markdown-it/testgen_helper'
6
5
  require 'motion-markdown-it'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-markdown-it
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.0
4
+ version: 8.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Walker
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-11-14 00:00:00.000000000 Z
13
+ date: 2018-04-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mdurl-rb
@@ -46,14 +46,14 @@ dependencies:
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '1.2'
49
+ version: '2.0'
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
- version: '1.2'
56
+ version: '2.0'
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: bacon-expect
59
59
  requirement: !ruby/object:Gem::Requirement
@@ -81,8 +81,8 @@ files:
81
81
  - lib/motion-markdown-it/common/html_re.rb
82
82
  - lib/motion-markdown-it/common/simpleidn.rb
83
83
  - lib/motion-markdown-it/common/string.rb
84
- - lib/motion-markdown-it/common/url_schemas.rb
85
84
  - lib/motion-markdown-it/common/utils.rb
85
+ - lib/motion-markdown-it/helpers/helper_wrapper.rb
86
86
  - lib/motion-markdown-it/helpers/parse_link_destination.rb
87
87
  - lib/motion-markdown-it/helpers/parse_link_label.rb
88
88
  - lib/motion-markdown-it/helpers/parse_link_title.rb
@@ -116,6 +116,7 @@ files:
116
116
  - lib/motion-markdown-it/rules_core/state_core.rb
117
117
  - lib/motion-markdown-it/rules_inline/autolink.rb
118
118
  - lib/motion-markdown-it/rules_inline/backticks.rb
119
+ - lib/motion-markdown-it/rules_inline/balance_pairs.rb
119
120
  - lib/motion-markdown-it/rules_inline/emphasis.rb
120
121
  - lib/motion-markdown-it/rules_inline/entity.rb
121
122
  - lib/motion-markdown-it/rules_inline/escape.rb
@@ -126,9 +127,9 @@ files:
126
127
  - lib/motion-markdown-it/rules_inline/state_inline.rb
127
128
  - lib/motion-markdown-it/rules_inline/strikethrough.rb
128
129
  - lib/motion-markdown-it/rules_inline/text.rb
130
+ - lib/motion-markdown-it/rules_inline/text_collapse.rb
129
131
  - lib/motion-markdown-it/token.rb
130
132
  - lib/motion-markdown-it/version.rb
131
- - spec/motion-markdown-it/bench_mark_spec.rb
132
133
  - spec/motion-markdown-it/commonmark_spec.rb
133
134
  - spec/motion-markdown-it/markdown_it_spec.rb
134
135
  - spec/motion-markdown-it/misc_spec.rb
@@ -157,17 +158,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
158
  version: '0'
158
159
  requirements: []
159
160
  rubyforge_project:
160
- rubygems_version: 2.4.5
161
+ rubygems_version: 2.6.8
161
162
  signing_key:
162
163
  specification_version: 4
163
164
  summary: Ruby version markdown-it
164
165
  test_files:
165
- - spec/motion-markdown-it/bench_mark_spec.rb
166
+ - spec/spec_helper.rb
167
+ - spec/motion-markdown-it/token_spec.rb
166
168
  - spec/motion-markdown-it/commonmark_spec.rb
167
- - spec/motion-markdown-it/markdown_it_spec.rb
168
- - spec/motion-markdown-it/misc_spec.rb
169
- - spec/motion-markdown-it/ruler_spec.rb
170
169
  - spec/motion-markdown-it/testgen_helper.rb
171
- - spec/motion-markdown-it/token_spec.rb
172
170
  - spec/motion-markdown-it/utils_spec.rb
173
- - spec/spec_helper.rb
171
+ - spec/motion-markdown-it/misc_spec.rb
172
+ - spec/motion-markdown-it/ruler_spec.rb
173
+ - spec/motion-markdown-it/markdown_it_spec.rb
@@ -1,173 +0,0 @@
1
- # List of valid url schemas, accorting to commonmark spec
2
- # http://jgm.github.io/CommonMark/spec.html#autolinks
3
- #------------------------------------------------------------------------------
4
- module MarkdownIt
5
-
6
- URL_SCHEMAS = [
7
- 'coap',
8
- 'doi',
9
- 'javascript',
10
- 'aaa',
11
- 'aaas',
12
- 'about',
13
- 'acap',
14
- 'cap',
15
- 'cid',
16
- 'crid',
17
- 'data',
18
- 'dav',
19
- 'dict',
20
- 'dns',
21
- 'file',
22
- 'ftp',
23
- 'geo',
24
- 'go',
25
- 'gopher',
26
- 'h323',
27
- 'http',
28
- 'https',
29
- 'iax',
30
- 'icap',
31
- 'im',
32
- 'imap',
33
- 'info',
34
- 'ipp',
35
- 'iris',
36
- 'iris.beep',
37
- 'iris.xpc',
38
- 'iris.xpcs',
39
- 'iris.lwz',
40
- 'ldap',
41
- 'mailto',
42
- 'mid',
43
- 'msrp',
44
- 'msrps',
45
- 'mtqp',
46
- 'mupdate',
47
- 'news',
48
- 'nfs',
49
- 'ni',
50
- 'nih',
51
- 'nntp',
52
- 'opaquelocktoken',
53
- 'pop',
54
- 'pres',
55
- 'rtsp',
56
- 'service',
57
- 'session',
58
- 'shttp',
59
- 'sieve',
60
- 'sip',
61
- 'sips',
62
- 'sms',
63
- 'snmp',
64
- 'soap.beep',
65
- 'soap.beeps',
66
- 'tag',
67
- 'tel',
68
- 'telnet',
69
- 'tftp',
70
- 'thismessage',
71
- 'tn3270',
72
- 'tip',
73
- 'tv',
74
- 'urn',
75
- 'vemmi',
76
- 'ws',
77
- 'wss',
78
- 'xcon',
79
- 'xcon-userid',
80
- 'xmlrpc.beep',
81
- 'xmlrpc.beeps',
82
- 'xmpp',
83
- 'z39.50r',
84
- 'z39.50s',
85
- 'adiumxtra',
86
- 'afp',
87
- 'afs',
88
- 'aim',
89
- 'apt',
90
- 'attachment',
91
- 'aw',
92
- 'beshare',
93
- 'bitcoin',
94
- 'bolo',
95
- 'callto',
96
- 'chrome',
97
- 'chrome-extension',
98
- 'com-eventbrite-attendee',
99
- 'content',
100
- 'cvs',
101
- 'dlna-playsingle',
102
- 'dlna-playcontainer',
103
- 'dtn',
104
- 'dvb',
105
- 'ed2k',
106
- 'facetime',
107
- 'feed',
108
- 'finger',
109
- 'fish',
110
- 'gg',
111
- 'git',
112
- 'gizmoproject',
113
- 'gtalk',
114
- 'hcp',
115
- 'icon',
116
- 'ipn',
117
- 'irc',
118
- 'irc6',
119
- 'ircs',
120
- 'itms',
121
- 'jar',
122
- 'jms',
123
- 'keyparc',
124
- 'lastfm',
125
- 'ldaps',
126
- 'magnet',
127
- 'maps',
128
- 'market',
129
- 'message',
130
- 'mms',
131
- 'ms-help',
132
- 'msnim',
133
- 'mumble',
134
- 'mvn',
135
- 'notes',
136
- 'oid',
137
- 'palm',
138
- 'paparazzi',
139
- 'platform',
140
- 'proxy',
141
- 'psyc',
142
- 'query',
143
- 'res',
144
- 'resource',
145
- 'rmi',
146
- 'rsync',
147
- 'rtmp',
148
- 'secondlife',
149
- 'sftp',
150
- 'sgn',
151
- 'skype',
152
- 'smb',
153
- 'soldat',
154
- 'spotify',
155
- 'ssh',
156
- 'steam',
157
- 'svn',
158
- 'teamspeak',
159
- 'things',
160
- 'udp',
161
- 'unreal',
162
- 'ut2004',
163
- 'ventrilo',
164
- 'view-source',
165
- 'webcal',
166
- 'wtai',
167
- 'wyciwyg',
168
- 'xfire',
169
- 'xri',
170
- 'ymsgr'
171
- ]
172
-
173
- end