token.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Token class
  2. /**
  3. * class Token
  4. **/
  5. /**
  6. * new Token(type, tag, nesting)
  7. *
  8. * Create new token and fill passed properties.
  9. **/
  10. function Token (type, tag, nesting) {
  11. /**
  12. * Token#type -> String
  13. *
  14. * Type of the token (string, e.g. "paragraph_open")
  15. **/
  16. this.type = type
  17. /**
  18. * Token#tag -> String
  19. *
  20. * html tag name, e.g. "p"
  21. **/
  22. this.tag = tag
  23. /**
  24. * Token#attrs -> Array
  25. *
  26. * Html attributes. Format: `[ [ name1, value1 ], [ name2, value2 ] ]`
  27. **/
  28. this.attrs = null
  29. /**
  30. * Token#map -> Array
  31. *
  32. * Source map info. Format: `[ line_begin, line_end ]`
  33. **/
  34. this.map = null
  35. /**
  36. * Token#nesting -> Number
  37. *
  38. * Level change (number in {-1, 0, 1} set), where:
  39. *
  40. * - `1` means the tag is opening
  41. * - `0` means the tag is self-closing
  42. * - `-1` means the tag is closing
  43. **/
  44. this.nesting = nesting
  45. /**
  46. * Token#level -> Number
  47. *
  48. * nesting level, the same as `state.level`
  49. **/
  50. this.level = 0
  51. /**
  52. * Token#children -> Array
  53. *
  54. * An array of child nodes (inline and img tokens)
  55. **/
  56. this.children = null
  57. /**
  58. * Token#content -> String
  59. *
  60. * In a case of self-closing tag (code, html, fence, etc.),
  61. * it has contents of this tag.
  62. **/
  63. this.content = ''
  64. /**
  65. * Token#markup -> String
  66. *
  67. * '*' or '_' for emphasis, fence string for fence, etc.
  68. **/
  69. this.markup = ''
  70. /**
  71. * Token#info -> String
  72. *
  73. * Additional information:
  74. *
  75. * - Info string for "fence" tokens
  76. * - The value "auto" for autolink "link_open" and "link_close" tokens
  77. * - The string value of the item marker for ordered-list "list_item_open" tokens
  78. **/
  79. this.info = ''
  80. /**
  81. * Token#meta -> Object
  82. *
  83. * A place for plugins to store an arbitrary data
  84. **/
  85. this.meta = null
  86. /**
  87. * Token#block -> Boolean
  88. *
  89. * True for block-level tokens, false for inline tokens.
  90. * Used in renderer to calculate line breaks
  91. **/
  92. this.block = false
  93. /**
  94. * Token#hidden -> Boolean
  95. *
  96. * If it's true, ignore this element when rendering. Used for tight lists
  97. * to hide paragraphs.
  98. **/
  99. this.hidden = false
  100. }
  101. /**
  102. * Token.attrIndex(name) -> Number
  103. *
  104. * Search attribute index by name.
  105. **/
  106. Token.prototype.attrIndex = function attrIndex (name) {
  107. if (!this.attrs) { return -1 }
  108. const attrs = this.attrs
  109. for (let i = 0, len = attrs.length; i < len; i++) {
  110. if (attrs[i][0] === name) { return i }
  111. }
  112. return -1
  113. }
  114. /**
  115. * Token.attrPush(attrData)
  116. *
  117. * Add `[ name, value ]` attribute to list. Init attrs if necessary
  118. **/
  119. Token.prototype.attrPush = function attrPush (attrData) {
  120. if (this.attrs) {
  121. this.attrs.push(attrData)
  122. } else {
  123. this.attrs = [attrData]
  124. }
  125. }
  126. /**
  127. * Token.attrSet(name, value)
  128. *
  129. * Set `name` attribute to `value`. Override old value if exists.
  130. **/
  131. Token.prototype.attrSet = function attrSet (name, value) {
  132. const idx = this.attrIndex(name)
  133. const attrData = [name, value]
  134. if (idx < 0) {
  135. this.attrPush(attrData)
  136. } else {
  137. this.attrs[idx] = attrData
  138. }
  139. }
  140. /**
  141. * Token.attrGet(name)
  142. *
  143. * Get the value of attribute `name`, or null if it does not exist.
  144. **/
  145. Token.prototype.attrGet = function attrGet (name) {
  146. const idx = this.attrIndex(name)
  147. let value = null
  148. if (idx >= 0) {
  149. value = this.attrs[idx][1]
  150. }
  151. return value
  152. }
  153. /**
  154. * Token.attrJoin(name, value)
  155. *
  156. * Join value to existing attribute via space. Or create new attribute if not
  157. * exists. Useful to operate with token classes.
  158. **/
  159. Token.prototype.attrJoin = function attrJoin (name, value) {
  160. const idx = this.attrIndex(name)
  161. if (idx < 0) {
  162. this.attrPush([name, value])
  163. } else {
  164. this.attrs[idx][1] = this.attrs[idx][1] + ' ' + value
  165. }
  166. }
  167. export default Token