Class: MarkdownIt::RulesBlock::Heading

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

Constant Summary

Constants included from Common::Utils

Common::Utils::DIGITAL_ENTITY_TEST_RE, Common::Utils::ENTITY_RE, Common::Utils::HTML_ESCAPE_REPLACE_RE, Common::Utils::HTML_ESCAPE_TEST_RE, Common::Utils::HTML_REPLACEMENTS, Common::Utils::REGEXP_ESCAPE_RE, Common::Utils::UNESCAPE_ALL_RE, Common::Utils::UNESCAPE_MD_RE, Common::Utils::UNICODE_PUNCT_RE

Class Method Summary collapse

Methods included from Common::Utils

arrayReplaceAt, assign, charCodeAt, escapeHtml, escapeRE, fromCharCode, fromCodePoint, isMdAsciiPunct, isPunctChar, isSpace, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd

Class Method Details

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




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
53
54
55
56
57
# File 'lib/motion-markdown-it/rules_block/heading.rb', line 9

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

  # if it's indented more than 3 spaces, it should be a code block
  return false if state.sCount[startLine] - state.blkIndent >= 4

  ch  = charCodeAt(state.src, pos)

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

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

  return false if (level > 6 || (pos < max && !isSpace(ch)))

  return true if (silent)

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

  max = state.skipSpacesBack(max, pos)
  tmp = state.skipCharsBack(max, 0x23, pos) # '#'
  if (tmp > pos && isSpace(charCodeAt(state.src, tmp - 1)))
    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