json.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. aliases: ['jsonc'],
  36. keywords:{
  37. literal: LITERALS,
  38. },
  39. contains: [
  40. ATTRIBUTE,
  41. PUNCTUATION,
  42. hljs.QUOTE_STRING_MODE,
  43. LITERALS_MODE,
  44. hljs.C_NUMBER_MODE,
  45. hljs.C_LINE_COMMENT_MODE,
  46. hljs.C_BLOCK_COMMENT_MODE
  47. ],
  48. illegal: '\\S'
  49. };
  50. }
  51. export { json as default };