Class: MarkdownIt::RulesInline::Emphasis

Inherits:
Object
  • Object
show all
Extended by:
Common::Utils
Defined in:
lib/motion-markdown-it/rules_inline/emphasis.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, escapeHtml, escapeRE, fromCharCode, fromCodePoint, isMdAsciiPunct, isPunctChar, isSpace, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd

Class Method Details

.postProcess(state) ⇒ Object

Walk through delimiter list and replace text tokens with tags



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
120
121
# File 'lib/motion-markdown-it/rules_inline/emphasis.rb', line 71

def self.postProcess(state)
  delimiters = state.delimiters
  max = state.delimiters.length

  i = max - 1
  while i >= 0
    startDelim = delimiters[i]

    (i -= 1) and next if startDelim[:marker] != 0x5F && startDelim[:marker] != 0x2A #  _ and *

    # Process only opening markers
    (i -= 1) and next if startDelim[:end] == -1

    endDelim = delimiters[startDelim[:end]]

    # If the previous delimiter has the same marker and is adjacent to this one,
    # merge those into one strong delimiter.
    #
    # `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
    #
    isStrong = i > 0 &&
               delimiters[i - 1][:end] == startDelim[:end] + 1 &&
               delimiters[i - 1][:token] == startDelim[:token] - 1 &&
               delimiters[startDelim[:end] + 1][:token] == endDelim[:token] + 1 &&
               delimiters[i - 1][:marker] == startDelim[:marker]

    ch = fromCodePoint(startDelim[:marker])

    token         = state.tokens[startDelim[:token]]
    token.type    = isStrong ? 'strong_open' : 'em_open'
    token.tag     = isStrong ? 'strong' : 'em'
    token.nesting = 1
    token.markup  = isStrong ? ch + ch : ch
    token.content = ''

    token         = state.tokens[endDelim[:token]]
    token.type    = isStrong ? 'strong_close' : 'em_close'
    token.tag     = isStrong ? 'strong' : 'em'
    token.nesting = -1
    token.markup  = isStrong ? ch + ch : ch
    token.content = ''

    if isStrong
      state.tokens[delimiters[i - 1][:token]].content = ''
      state.tokens[delimiters[startDelim[:end] + 1][:token]].content = ''
      i -= 1
    end

    i -= 1
  end
end

.tokenize(state, silent) ⇒ Object

Insert each marker as a separate text token, and add it to delimiter list



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
58
59
60
61
62
63
64
65
66
# File 'lib/motion-markdown-it/rules_inline/emphasis.rb', line 10

def self.tokenize(state, silent)
  start   = state.pos
  marker  = state.src.charCodeAt(start)

  return false if silent

  return false if (marker != 0x5F && marker != 0x2A) #  _ and *

  scanned = state.scanDelims(state.pos, marker == 0x2A)

  0.upto(scanned[:length] - 1) do |i|
    token         = state.push('text', '', 0)
    token.content = fromCodePoint(marker)

    state.delimiters.push({
      # Char code of the starting marker (number).
      #
      marker: marker,

      # Total length of these series of delimiters.
      #
      length: scanned[:length],

      # An amount of characters before this one that's equivalent to
      # current one. In plain English: if this delimiter does not open
      # an emphasis, neither do previous `jump` characters.
      #
      # Used to skip sequences like "*****" in one step, for 1st asterisk
      # value will be 0, for 2nd it's 1 and so on.
      #
      jump:   i,

      # A position of the token this delimiter corresponds to.
      #
      token:  state.tokens.length - 1,

      # Token level.
      #
      level:  state.level,

      # If this delimiter is matched as a valid opener, `end` will be
      # equal to its position, otherwise it's `-1`.
      #
      end:    -1,

      # Boolean flags that determine if this delimiter could open or close
      # an emphasis.
      #
      open:   scanned[:can_open],
      close:  scanned[:can_close]
    })
  end

  state.pos += scanned[:length]

  return true
end