handlebars.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {...(RegExp | string) } args
  16. * @returns {string}
  17. */
  18. function concat(...args) {
  19. const joined = args.map((x) => source(x)).join("");
  20. return joined;
  21. }
  22. /*
  23. Language: Handlebars
  24. Requires: xml.js
  25. Author: Robin Ward <robin.ward@gmail.com>
  26. Description: Matcher for Handlebars as well as EmberJS additions.
  27. Website: https://handlebarsjs.com
  28. Category: template
  29. */
  30. function handlebars(hljs) {
  31. const BUILT_INS = {
  32. 'builtin-name': [
  33. 'action',
  34. 'bindattr',
  35. 'collection',
  36. 'component',
  37. 'concat',
  38. 'debugger',
  39. 'each',
  40. 'each-in',
  41. 'get',
  42. 'hash',
  43. 'if',
  44. 'in',
  45. 'input',
  46. 'link-to',
  47. 'loc',
  48. 'log',
  49. 'lookup',
  50. 'mut',
  51. 'outlet',
  52. 'partial',
  53. 'query-params',
  54. 'render',
  55. 'template',
  56. 'textarea',
  57. 'unbound',
  58. 'unless',
  59. 'view',
  60. 'with',
  61. 'yield'
  62. ].join(" ")
  63. };
  64. const LITERALS = {
  65. literal: [
  66. 'true',
  67. 'false',
  68. 'undefined',
  69. 'null'
  70. ].join(" ")
  71. };
  72. // as defined in https://handlebarsjs.com/guide/expressions.html#literal-segments
  73. // this regex matches literal segments like ' abc ' or [ abc ] as well as helpers and paths
  74. // like a/b, ./abc/cde, and abc.bcd
  75. const DOUBLE_QUOTED_ID_REGEX=/".*?"/;
  76. const SINGLE_QUOTED_ID_REGEX=/'.*?'/;
  77. const BRACKET_QUOTED_ID_REGEX=/\[.*?\]/;
  78. const PLAIN_ID_REGEX=/[^\s!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]+/;
  79. const PATH_DELIMITER_REGEX=/\.|\//;
  80. const IDENTIFIER_REGEX = concat(
  81. '(',
  82. SINGLE_QUOTED_ID_REGEX, '|',
  83. DOUBLE_QUOTED_ID_REGEX, '|',
  84. BRACKET_QUOTED_ID_REGEX, '|',
  85. PLAIN_ID_REGEX, '|',
  86. PATH_DELIMITER_REGEX,
  87. ')+'
  88. );
  89. // identifier followed by a equal-sign (without the equal sign)
  90. const HASH_PARAM_REGEX = concat(
  91. '(',
  92. BRACKET_QUOTED_ID_REGEX, '|',
  93. PLAIN_ID_REGEX,
  94. ')(?==)'
  95. );
  96. const HELPER_NAME_OR_PATH_EXPRESSION = {
  97. begin: IDENTIFIER_REGEX,
  98. lexemes: /[\w.\/]+/
  99. };
  100. const HELPER_PARAMETER = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
  101. keywords: LITERALS
  102. });
  103. const SUB_EXPRESSION = {
  104. begin: /\(/,
  105. end: /\)/
  106. // the "contains" is added below when all necessary sub-modes are defined
  107. };
  108. const HASH = {
  109. // fka "attribute-assignment", parameters of the form 'key=value'
  110. className: 'attr',
  111. begin: HASH_PARAM_REGEX,
  112. relevance: 0,
  113. starts: {
  114. begin: /=/,
  115. end: /=/,
  116. starts: {
  117. contains: [
  118. hljs.NUMBER_MODE,
  119. hljs.QUOTE_STRING_MODE,
  120. hljs.APOS_STRING_MODE,
  121. HELPER_PARAMETER,
  122. SUB_EXPRESSION
  123. ]
  124. }
  125. }
  126. };
  127. const BLOCK_PARAMS = {
  128. // parameters of the form '{{#with x as | y |}}...{{/with}}'
  129. begin: /as\s+\|/,
  130. keywords: { keyword: 'as' },
  131. end: /\|/,
  132. contains: [
  133. {
  134. // define sub-mode in order to prevent highlighting of block-parameter named "as"
  135. begin: /\w+/
  136. }
  137. ]
  138. };
  139. const HELPER_PARAMETERS = {
  140. contains: [
  141. hljs.NUMBER_MODE,
  142. hljs.QUOTE_STRING_MODE,
  143. hljs.APOS_STRING_MODE,
  144. BLOCK_PARAMS,
  145. HASH,
  146. HELPER_PARAMETER,
  147. SUB_EXPRESSION
  148. ],
  149. returnEnd: true
  150. // the property "end" is defined through inheritance when the mode is used. If depends
  151. // on the surrounding mode, but "endsWithParent" does not work here (i.e. it includes the
  152. // end-token of the surrounding mode)
  153. };
  154. const SUB_EXPRESSION_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
  155. className: 'name',
  156. keywords: BUILT_INS,
  157. starts: hljs.inherit(HELPER_PARAMETERS, {
  158. end: /\)/,
  159. })
  160. });
  161. SUB_EXPRESSION.contains = [
  162. SUB_EXPRESSION_CONTENTS
  163. ];
  164. const OPENING_BLOCK_MUSTACHE_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
  165. keywords: BUILT_INS,
  166. className: 'name',
  167. starts: hljs.inherit(HELPER_PARAMETERS, {
  168. end: /}}/,
  169. })
  170. });
  171. const CLOSING_BLOCK_MUSTACHE_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
  172. keywords: BUILT_INS,
  173. className: 'name'
  174. });
  175. const BASIC_MUSTACHE_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
  176. className: 'name',
  177. keywords: BUILT_INS,
  178. starts: hljs.inherit(HELPER_PARAMETERS, {
  179. end: /}}/,
  180. })
  181. });
  182. const ESCAPE_MUSTACHE_WITH_PRECEEDING_BACKSLASH = {begin: /\\\{\{/, skip: true};
  183. const PREVENT_ESCAPE_WITH_ANOTHER_PRECEEDING_BACKSLASH = {begin: /\\\\(?=\{\{)/, skip: true};
  184. return {
  185. name: 'Handlebars',
  186. aliases: ['hbs', 'html.hbs', 'html.handlebars', 'htmlbars'],
  187. case_insensitive: true,
  188. subLanguage: 'xml',
  189. contains: [
  190. ESCAPE_MUSTACHE_WITH_PRECEEDING_BACKSLASH,
  191. PREVENT_ESCAPE_WITH_ANOTHER_PRECEEDING_BACKSLASH,
  192. hljs.COMMENT(/\{\{!--/, /--\}\}/),
  193. hljs.COMMENT(/\{\{!/, /\}\}/),
  194. {
  195. // open raw block "{{{{raw}}}} content not evaluated {{{{/raw}}}}"
  196. className: 'template-tag',
  197. begin: /\{\{\{\{(?!\/)/,
  198. end: /\}\}\}\}/,
  199. contains: [OPENING_BLOCK_MUSTACHE_CONTENTS],
  200. starts: {end: /\{\{\{\{\//, returnEnd: true, subLanguage: 'xml'}
  201. },
  202. {
  203. // close raw block
  204. className: 'template-tag',
  205. begin: /\{\{\{\{\//,
  206. end: /\}\}\}\}/,
  207. contains: [CLOSING_BLOCK_MUSTACHE_CONTENTS]
  208. },
  209. {
  210. // open block statement
  211. className: 'template-tag',
  212. begin: /\{\{#/,
  213. end: /\}\}/,
  214. contains: [OPENING_BLOCK_MUSTACHE_CONTENTS],
  215. },
  216. {
  217. className: 'template-tag',
  218. begin: /\{\{(?=else\}\})/,
  219. end: /\}\}/,
  220. keywords: 'else'
  221. },
  222. {
  223. // closing block statement
  224. className: 'template-tag',
  225. begin: /\{\{\//,
  226. end: /\}\}/,
  227. contains: [CLOSING_BLOCK_MUSTACHE_CONTENTS],
  228. },
  229. {
  230. // template variable or helper-call that is NOT html-escaped
  231. className: 'template-variable',
  232. begin: /\{\{\{/,
  233. end: /\}\}\}/,
  234. contains: [BASIC_MUSTACHE_CONTENTS]
  235. },
  236. {
  237. // template variable or helper-call that is html-escaped
  238. className: 'template-variable',
  239. begin: /\{\{/,
  240. end: /\}\}/,
  241. contains: [BASIC_MUSTACHE_CONTENTS]
  242. }
  243. ]
  244. };
  245. }
  246. module.exports = handlebars;