json.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Language: JSON
  3. Description: JSON (JavaScript Object Notation) is a lightweight data-interchange format.
  4. Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
  5. Website: http://www.json.org
  6. Category: common, protocols, web
  7. */
  8. function json(hljs) {
  9. const ATTRIBUTE = {
  10. className: 'attr',
  11. begin: /"(\\.|[^\\"\r\n])*"(?=\s*:)/,
  12. relevance: 1.01
  13. };
  14. const PUNCTUATION = {
  15. match: /[{}[\],:]/,
  16. className: "punctuation",
  17. relevance: 0
  18. };
  19. const LITERALS = [
  20. "true",
  21. "false",
  22. "null"
  23. ];
  24. // NOTE: normally we would rely on `keywords` for this but using a mode here allows us
  25. // - to use the very tight `illegal: \S` rule later to flag any other character
  26. // - as illegal indicating that despite looking like JSON we do not truly have
  27. // - JSON and thus improve false-positively greatly since JSON will try and claim
  28. // - all sorts of JSON looking stuff
  29. const LITERALS_MODE = {
  30. scope: "literal",
  31. beginKeywords: LITERALS.join(" "),
  32. };
  33. return {
  34. name: 'JSON',
  35. keywords:{
  36. literal: LITERALS,
  37. },
  38. contains: [
  39. ATTRIBUTE,
  40. PUNCTUATION,
  41. hljs.QUOTE_STRING_MODE,
  42. LITERALS_MODE,
  43. hljs.C_NUMBER_MODE,
  44. hljs.C_LINE_COMMENT_MODE,
  45. hljs.C_BLOCK_COMMENT_MODE
  46. ],
  47. illegal: '\\S'
  48. };
  49. }
  50. module.exports = json;