Class: MotionMarkdownItPlugins::Sub

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

Constant Summary collapse

UNESCAPE_RE =

same as UNESCAPE_MD_RE plus a space

/\\([ \!\"\#\$\%\&\'\(\)\*\+\,\-.\/:;<=>?@\[\\\]^_`{|}~])/

Class Method Summary collapse

Class Method Details

.init_plugin(md) ⇒ Object




14
15
16
# File 'lib/motion-markdown-it-plugins/sub/sub.rb', line 14

def self.init_plugin(md)
  md.inline.ruler.after('emphasis', 'sub', lambda { |state, silent| Sub.subscript(state, silent) })
end

.subscript(state, silent) ⇒ Object




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
67
68
# File 'lib/motion-markdown-it-plugins/sub/sub.rb', line 19

def self.subscript(state, silent)
  max   = state.posMax
  start = state.pos

  return false if (state.src.charCodeAt(start) != 0x7E)  # '~'
  return false if (silent) # don't run any pairs in validation mode
  return false if (start + 2 >= max)

  state.pos = start + 1

  while (state.pos < max)
    if (state.src.charCodeAt(state.pos) == 0x7E)  # '~'
      found = true
      break
    end

    state.md.inline.skipToken(state)
  end

  if (!found || start + 1 == state.pos)
    state.pos = start
    return false
  end

  content = state.src.slice((start + 1)...state.pos)

  # don't allow unescaped spaces/newlines inside
  if (content.match(/(^|[^\\])(\\\\)*\s/))
    state.pos = start
    return false
  end

  # found!
  state.posMax = state.pos
  state.pos    = start + 1

  # Earlier we checked !silent, but this implementation does not need it
  token         = state.push('sub_open', 'sub', 1)
  token.markup  = '~'

  token         = state.push('text', '', 0)
  token.content = content.gsub(UNESCAPE_RE, '\1')

  token         = state.push('sub_close', 'sub', -1)
  token.markup  = '~'

  state.pos    = state.posMax + 1
  state.posMax = max
  return true
end