json.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. module.exports = function(hljs) {
  2. var LITERALS = {literal: 'true false null'};
  3. var ALLOWED_COMMENTS = [
  4. hljs.C_LINE_COMMENT_MODE,
  5. hljs.C_BLOCK_COMMENT_MODE
  6. ]
  7. var TYPES = [
  8. hljs.QUOTE_STRING_MODE,
  9. hljs.C_NUMBER_MODE
  10. ];
  11. var VALUE_CONTAINER = {
  12. end: ',', endsWithParent: true, excludeEnd: true,
  13. contains: TYPES,
  14. keywords: LITERALS
  15. };
  16. var OBJECT = {
  17. begin: '{', end: '}',
  18. contains: [
  19. {
  20. className: 'attr',
  21. begin: /"/, end: /"/,
  22. contains: [hljs.BACKSLASH_ESCAPE],
  23. illegal: '\\n',
  24. },
  25. hljs.inherit(VALUE_CONTAINER, {begin: /:/})
  26. ].concat(ALLOWED_COMMENTS),
  27. illegal: '\\S'
  28. };
  29. var ARRAY = {
  30. begin: '\\[', end: '\\]',
  31. contains: [hljs.inherit(VALUE_CONTAINER)], // inherit is a workaround for a bug that makes shared modes with endsWithParent compile only the ending of one of the parents
  32. illegal: '\\S'
  33. };
  34. TYPES.push(OBJECT, ARRAY);
  35. ALLOWED_COMMENTS.forEach(function(rule) {
  36. TYPES.push(rule)
  37. })
  38. return {
  39. contains: TYPES,
  40. keywords: LITERALS,
  41. illegal: '\\S'
  42. };
  43. };