Class: MotionMarkdownItPlugins::CheckboxReplace
- Inherits:
-
Object
- Object
- MotionMarkdownItPlugins::CheckboxReplace
- Defined in:
- lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb
Constant Summary collapse
- PATTERN =
/\[(X|\s|\_|\-)\]\s(.*)/i
Class Method Summary collapse
-
.init_plugin(md, options) ⇒ Object
——————————————————————————.
Instance Method Summary collapse
-
#checkbox(state) ⇒ Object
——————————————————————————.
-
#createTokens(checked, label, original) ⇒ Object
——————————————————————————.
-
#initialize(md, options) ⇒ CheckboxReplace
constructor
——————————————————————————.
-
#splitTextToken(original) ⇒ Object
original should be an inline node.
Constructor Details
#initialize(md, options) ⇒ CheckboxReplace
16 17 18 19 20 |
# File 'lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb', line 16 def initialize(md, ) @lastId = 0 defaults = {divWrap: false, divClass: 'checkbox', idPrefix: 'checkbox'} = defaults.merge() end |
Class Method Details
.init_plugin(md, options) ⇒ Object
10 11 12 13 |
# File 'lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb', line 10 def self.init_plugin(md, ) check = CheckboxReplace.new(md, ) md.core.ruler.push('checkbox', lambda { |state| check.checkbox(state) } ) end |
Instance Method Details
#checkbox(state) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb', line 89 def checkbox(state) blockTokens = state.tokens j = 0 l = blockTokens.length # blockTokens.each do |xtoken| # puts " " * xtoken.level + "#{xtoken.inspect}" # end while j < l if blockTokens[j].type != "inline" j += 1 next end tokens = blockTokens[j].children # # We scan from the end, to keep position when new tags added. # # Use reversed logic in links start/end match # i = tokens.length - 1 # while i >= 0 # token = tokens[i] # blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, splitTextToken(token)) # i -= 1 # end token = blockTokens[j] arrayReplaceAt(blockTokens, j, splitTextToken(token)) j += 1 end end |
#createTokens(checked, label, original) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb', line 23 def createTokens(checked, label, original) nodes = [] # <div class="checkbox"> if [:divWrap] token = MarkdownIt::Token.new("checkbox_open", "div", 1) token.attrs = [["class", [:divClass]]] nodes.push token end # <input type="checkbox" id="checkbox{n}" checked="true"> id = "#{@options[:idPrefix]}#{@lastId}" @lastId += 1 token = MarkdownIt::Token.new("checkbox_input", "input", 0) token.attrs = [["type","checkbox"],["id",id]] if (checked == true) token.attrs.push ["checked","true"] end nodes.push token # <label for="checkbox{n}"> token = MarkdownIt::Token.new("label_open", "label", 1) token.attrs = [["for",id]] nodes.push token # content of label tag token = MarkdownIt::Token.new("text", "", 0) token.content = label original.children[0].content = label original.children.each {|tok| nodes.push tok} # closing tags nodes.push MarkdownIt::Token.new("label_close", "label", -1) if [:divWrap] nodes.push MarkdownIt::Token.new("checkbox_close", "div", -1) end original.children = nodes return original end |
#splitTextToken(original) ⇒ Object
original should be an inline node. it can only be a task item if the first child node has the right text
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/motion-markdown-it-plugins/checkbox_replace/checkbox_replace.rb', line 67 def splitTextToken(original) if original.children first_node = original.children[0] if first_node text = first_node.content matches = text.match(PATTERN) return original if matches == nil value = matches[1] label = matches[2] checked = false checked = true if (value == "X" || value == "x") return createTokens(checked, label, original) end end return original end |