motion-markdown-it-plugins 1.0.0 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 932bce733bfd0e1070663a7b560a5e255817c85c
4
- data.tar.gz: 54572d459acb95df6d00fd0736ee71f5ecc3691d
3
+ metadata.gz: 94704fbe5fa8a967bf999b985367ab32dba0486e
4
+ data.tar.gz: f839bf41bb8c37eb15c4ac327e9e607172d75333
5
5
  SHA512:
6
- metadata.gz: 1bf53209e1279e1467415f9c9b8b91f59e57ec526edac36cad5b287993a8ea6b7409202490e84017b9487d98915e359a4012f59f232aaf568335546c5f9632b3
7
- data.tar.gz: 9b67f602c172395eb74d7d9c2a114a6315df287eb3ae404c821eeb253c11fe0cd828e705bf2f2c17b59827ea47279d277a6882aa8cda0a6753b8d2d9769cbc91
6
+ metadata.gz: ee8f65ccb731bde283c02f26432607e83c65b6bef7cf64b4f4f3599a81b57931ff5cc4103b7e7db38b3f339cad733ba21f69adb60650b8e2d1adcfc53fe7515e
7
+ data.tar.gz: 20dc882df2b9cc935df9ee03d4f1f4d10e4197a48f39ed7f183fa8b817aa5e8f290890da967f2d3377fb1efe6a5dc253d0999d13b4a1e12205647ea29ec62e85
data/README.md CHANGED
@@ -12,6 +12,7 @@ Each one has a README with details
12
12
  * [Checkbox/Tasklists](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/checkbox_replace)
13
13
  * [Containers](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/container)
14
14
  * [Definition Lists](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/deflist)
15
+ * [Header Sections](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/header_sections)
15
16
  * [Insert](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/ins)
16
17
  * [Mark](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/mark)
17
18
  * [Subscript](https://github.com/digitalmoksha/motion-markdown-it-plugins/tree/master/lib/motion-markdown-it-plugins/sub)
@@ -20,5 +20,6 @@ else
20
20
  require 'motion-markdown-it-plugins/sub/sub'
21
21
  require 'motion-markdown-it-plugins/sup/sup'
22
22
  require 'motion-markdown-it-plugins/container/container'
23
+ require 'motion-markdown-it-plugins/header_sections/header_sections'
23
24
 
24
- end
25
+ end
@@ -20,7 +20,7 @@ module MotionMarkdownItPlugins
20
20
  end
21
21
 
22
22
  #------------------------------------------------------------------------------
23
- def createTokens(checked, label)
23
+ def createTokens(checked, label, original)
24
24
  nodes = []
25
25
 
26
26
  # <div class="checkbox">
@@ -48,7 +48,8 @@ module MotionMarkdownItPlugins
48
48
  # content of label tag
49
49
  token = MarkdownIt::Token.new("text", "", 0)
50
50
  token.content = label
51
- nodes.push token
51
+ original.children[0].content = label
52
+ original.children.each {|tok| nodes.push tok}
52
53
 
53
54
  # closing tags
54
55
  nodes.push MarkdownIt::Token.new("label_close", "label", -1)
@@ -56,23 +57,32 @@ module MotionMarkdownItPlugins
56
57
  nodes.push MarkdownIt::Token.new("checkbox_close", "div", -1)
57
58
  end
58
59
 
59
- return nodes
60
+ original.children = nodes
61
+ return original
60
62
  end
61
63
 
64
+ # original should be an inline node. it can only be a task item if the first
65
+ # child node has the right text
62
66
  #------------------------------------------------------------------------------
63
67
  def splitTextToken(original)
64
68
 
65
- text = original.content
66
- matches = text.match(PATTERN)
69
+ if original.children
70
+ first_node = original.children[0]
71
+ if first_node
72
+ text = first_node.content
73
+ matches = text.match(PATTERN)
67
74
 
68
- return original if matches == nil
75
+ return original if matches == nil
69
76
 
70
- value = matches[1]
71
- label = matches[2]
72
- checked = false
73
- checked = true if (value == "X" || value == "x")
77
+ value = matches[1]
78
+ label = matches[2]
79
+ checked = false
80
+ checked = true if (value == "X" || value == "x")
74
81
 
75
- return createTokens(checked, label)
82
+ return createTokens(checked, label, original)
83
+ end
84
+ end
85
+ return original
76
86
  end
77
87
 
78
88
  #------------------------------------------------------------------------------
@@ -80,21 +90,30 @@ module MotionMarkdownItPlugins
80
90
  blockTokens = state.tokens
81
91
  j = 0
82
92
  l = blockTokens.length
93
+
94
+ # blockTokens.each do |xtoken|
95
+ # puts " " * xtoken.level + "#{xtoken.inspect}"
96
+ # end
97
+
83
98
  while j < l
84
99
  if blockTokens[j].type != "inline"
85
100
  j += 1
86
101
  next
87
102
  end
88
103
  tokens = blockTokens[j].children
104
+
105
+ # # We scan from the end, to keep position when new tags added.
106
+ # # Use reversed logic in links start/end match
107
+ # i = tokens.length - 1
108
+ # while i >= 0
109
+ # token = tokens[i]
110
+ # blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, splitTextToken(token))
111
+ # i -= 1
112
+ # end
113
+
114
+ token = blockTokens[j]
115
+ arrayReplaceAt(blockTokens, j, splitTextToken(token))
89
116
 
90
- # We scan from the end, to keep position when new tags added.
91
- # Use reversed logic in links start/end match
92
- i = tokens.length - 1
93
- while i >= 0
94
- token = tokens[i]
95
- blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, splitTextToken(token))
96
- i -= 1
97
- end
98
117
  j += 1
99
118
  end
100
119
  end
@@ -0,0 +1,98 @@
1
+ # Process header sections
2
+ #
3
+ # Based on Javascript version: https://github.com/arve0/markdown-it-header-sections
4
+ #------------------------------------------------------------------------------
5
+
6
+ include MarkdownIt::Common::Utils
7
+
8
+ module MotionMarkdownItPlugins
9
+ class HeaderSections
10
+
11
+ def self.init_plugin(md, *options)
12
+ headerSections = HeaderSections.new(md, options)
13
+ md.core.ruler.push('header_sections', lambda { |state| headerSections.addSections(state) })
14
+ end
15
+
16
+ def initialize(md, options)
17
+ reset
18
+ end
19
+
20
+ def reset()
21
+ @tokens = [] # output
22
+ @sections = []
23
+ @nestedLevel = 0
24
+ end
25
+
26
+ def openSection(attrs)
27
+ t = MarkdownIt::Token.new('section_open', 'section', 1)
28
+ t.block = true
29
+ # This adds support for markdown-it-attrs. It's still in JS.
30
+ # t.attrs = attrs && attrs.map(function (attr) { return [attr[0], attr[1]] })
31
+ return t
32
+ end
33
+
34
+ def closeSection()
35
+ t = MarkdownIt::Token.new('section_close', 'section', -1)
36
+ t.block = true
37
+ return t
38
+ end
39
+
40
+ def closeSections(section)
41
+ while (@sections.last && section[:header] <= @sections.last[:header])
42
+ @sections.pop()
43
+ @tokens.push(closeSection())
44
+ end
45
+ end
46
+
47
+ def closeAllSections()
48
+ while (@sections.pop())
49
+ @tokens.push(closeSection())
50
+ end
51
+ end
52
+
53
+ def closeSectionsToCurrentNesting(nesting)
54
+ while (@sections.last && nesting < @sections.last[:nesting])
55
+ @sections.pop()
56
+ @tokens.push(closeSection())
57
+ end
58
+ end
59
+
60
+ def addSections(state)
61
+ state.tokens.each do |token|
62
+ # record level of nesting
63
+ if (!token.type.start_with?('heading'))
64
+ @nestedLevel += token.nesting
65
+ end
66
+ if (@sections.last && @nestedLevel < @sections.last[:nesting])
67
+ closeSectionsToCurrentNesting(@nestedLevel)
68
+ end
69
+
70
+ # add sections before headers
71
+ if (token.type == 'heading_open')
72
+ section = {
73
+ :header => token.tag[1,1].to_i,
74
+ :nesting => @nestedLevel
75
+ }
76
+ if (@sections.last && section[:header] <= @sections.last[:header])
77
+ closeSections(section)
78
+ end
79
+ @tokens.push(openSection(token.attrs))
80
+ if (token.attrIndex('id') != -1)
81
+ # remove ID from token
82
+ token.attrs.splice(token.attrIndex('id'), 1)
83
+ end
84
+ @sections.push(section)
85
+ end
86
+
87
+ @tokens.push(token)
88
+ end # end for every token
89
+
90
+ closeAllSections()
91
+
92
+ state.tokens = @tokens
93
+ reset
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -1,5 +1,5 @@
1
1
  module MotionMarkdownItPlugins
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.10'
4
4
 
5
5
  end
@@ -0,0 +1,9 @@
1
+ fixture_dir = fixture_path('header_sections/fixtures')
2
+
3
+ #------------------------------------------------------------------------------
4
+ describe 'markdown-it-header-sections' do
5
+ md = MarkdownIt::Parser.new
6
+ md.use(MotionMarkdownItPlugins::HeaderSections)
7
+
8
+ generate(File.join(fixture_dir, 'header_sections.txt'), md)
9
+ end
@@ -1,5 +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
2
+ # spec_helper.rb included automatically in spec files
3
3
  require 'byebug'
4
4
  require 'benchmark'
5
5
  require 'motion-markdown-it-plugins'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-markdown-it-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Walker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-03 00:00:00.000000000 Z
11
+ date: 2018-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: motion-markdown-it
@@ -36,6 +36,7 @@ files:
36
36
  - lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb
37
37
  - lib/motion-markdown-it-plugins/container/container.rb
38
38
  - lib/motion-markdown-it-plugins/deflist/deflist.rb
39
+ - lib/motion-markdown-it-plugins/header_sections/header_sections.rb
39
40
  - lib/motion-markdown-it-plugins/ins/ins.rb
40
41
  - lib/motion-markdown-it-plugins/mark/mark.rb
41
42
  - lib/motion-markdown-it-plugins/sub/sub.rb
@@ -47,6 +48,7 @@ files:
47
48
  - spec/container/default_spec.rb
48
49
  - spec/container/misc_spec.rb
49
50
  - spec/deflist/deflist_spec.rb
51
+ - spec/header_sections/header_sections_spec.rb
50
52
  - spec/ins/ins_spec.rb
51
53
  - spec/mark/mark_spec.rb
52
54
  - spec/spec_helper.rb
@@ -73,20 +75,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
75
  version: '0'
74
76
  requirements: []
75
77
  rubyforge_project:
76
- rubygems_version: 2.4.5
78
+ rubygems_version: 2.6.8
77
79
  signing_key:
78
80
  specification_version: 4
79
81
  summary: Plugins for motion-markdown-it
80
82
  test_files:
81
- - spec/abbr/abbr_spec.rb
82
- - spec/checkbox_replace/checkbox_replace_spec.rb
83
+ - spec/spec_helper.rb
84
+ - spec/mark/mark_spec.rb
85
+ - spec/sup/sup_spec.rb
83
86
  - spec/container/api_spec.rb
84
87
  - spec/container/default_spec.rb
85
88
  - spec/container/misc_spec.rb
86
- - spec/deflist/deflist_spec.rb
87
- - spec/ins/ins_spec.rb
88
- - spec/mark/mark_spec.rb
89
- - spec/spec_helper.rb
90
89
  - spec/sub/sub_spec.rb
91
- - spec/sup/sup_spec.rb
90
+ - spec/ins/ins_spec.rb
91
+ - spec/deflist/deflist_spec.rb
92
92
  - spec/testgen_helper.rb
93
+ - spec/checkbox_replace/checkbox_replace_spec.rb
94
+ - spec/abbr/abbr_spec.rb
95
+ - spec/header_sections/header_sections_spec.rb