Class: MarkdownIt::RulesBlock::Heading

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-markdown-it/rules_block/heading.rb

Class Method Summary collapse

Class Method Details

.heading(state, startLine, endLine, silent) ⇒ Object




8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/motion-markdown-it/rules_block/heading.rb', line 8

def self.heading(state, startLine, endLine, silent)
  pos = state.bMarks[startLine] + state.tShift[startLine]
  max = state.eMarks[startLine]
  ch  = state.src.charCodeAt(pos)

  return false if (ch != 0x23 || pos >= max)

  # count heading level
  level = 1
  pos  += 1
  ch = state.src.charCodeAt(pos)
  while (ch == 0x23 && pos < max && level <= 6)  # '#'
    level += 1
    pos   += 1
    ch = state.src.charCodeAt(pos)
  end

  return false if (level > 6 || (pos < max && ch != 0x20))  # space

  return true if (silent)

  # Let's cut tails like '    ###  ' from the end of string

  max = state.skipCharsBack(max, 0x20, pos) # space
  tmp = state.skipCharsBack(max, 0x23, pos) # '#'
  if (tmp > pos && state.src.charCodeAt(tmp - 1) == 0x20)   # space
    max = tmp
  end

  state.line = startLine + 1

  token          = state.push('heading_open', "h#{level.to_s}", 1)
  token.markup   = '########'.slice(0...level)
  token.map      = [ startLine, state.line ]

  token          = state.push('inline', '', 0)
  token.content  = state.src.slice(pos...max).strip
  token.map      = [ startLine, state.line ]
  token.children = []

  token        = state.push('heading_close', "h#{level.to_s}", -1)
  token.markup = '########'.slice(0...level)

  return true
end