prism-mongodb.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function (Prism) {
  2. var operators = [
  3. // query and projection
  4. '$eq', '$gt', '$gte', '$in', '$lt', '$lte', '$ne', '$nin', '$and', '$not', '$nor', '$or',
  5. '$exists', '$type', '$expr', '$jsonSchema', '$mod', '$regex', '$text', '$where', '$geoIntersects',
  6. '$geoWithin', '$near', '$nearSphere', '$all', '$elemMatch', '$size', '$bitsAllClear', '$bitsAllSet',
  7. '$bitsAnyClear', '$bitsAnySet', '$comment', '$elemMatch', '$meta', '$slice',
  8. // update
  9. '$currentDate', '$inc', '$min', '$max', '$mul', '$rename', '$set', '$setOnInsert', '$unset',
  10. '$addToSet', '$pop', '$pull', '$push', '$pullAll', '$each', '$position', '$slice', '$sort', '$bit',
  11. // aggregation pipeline stages
  12. '$addFields', '$bucket', '$bucketAuto', '$collStats', '$count', '$currentOp', '$facet', '$geoNear',
  13. '$graphLookup', '$group','$indexStats', '$limit', '$listLocalSessions', '$listSessions', '$lookup',
  14. '$match', '$merge', '$out', '$planCacheStats', '$project', '$redact', '$replaceRoot', '$replaceWith',
  15. '$sample', '$set', '$skip', '$sort', '$sortByCount', '$unionWith', '$unset', '$unwind',
  16. // aggregation pipeline operators
  17. '$abs', '$accumulator', '$acos', '$acosh', '$add', '$addToSet', '$allElementsTrue', '$and',
  18. '$anyElementTrue', '$arrayElemAt', '$arrayToObject', '$asin', '$asinh', '$atan', '$atan2',
  19. '$atanh', '$avg', '$binarySize', '$bsonSize', '$ceil', '$cmp', '$concat', '$concatArrays', '$cond',
  20. '$convert', '$cos', '$dateFromParts', '$dateToParts', '$dateFromString', '$dateToString', '$dayOfMonth',
  21. '$dayOfWeek', '$dayOfYear', '$degreesToRadians', '$divide', '$eq', '$exp', '$filter', '$first',
  22. '$floor', '$function', '$gt', '$gte', '$hour', '$ifNull', '$in', '$indexOfArray', '$indexOfBytes',
  23. '$indexOfCP', '$isArray', '$isNumber', '$isoDayOfWeek', '$isoWeek', '$isoWeekYear', '$last',
  24. '$last', '$let', '$literal', '$ln', '$log', '$log10', '$lt', '$lte', '$ltrim', '$map', '$max',
  25. '$mergeObjects', '$meta', '$min', '$millisecond', '$minute', '$mod', '$month', '$multiply', '$ne',
  26. '$not', '$objectToArray', '$or', '$pow', '$push', '$radiansToDegrees', '$range', '$reduce',
  27. '$regexFind', '$regexFindAll', '$regexMatch', '$replaceOne', '$replaceAll', '$reverseArray', '$round',
  28. '$rtrim', '$second', '$setDifference', '$setEquals', '$setIntersection', '$setIsSubset', '$setUnion',
  29. '$size', '$sin', '$slice', '$split', '$sqrt', '$stdDevPop', '$stdDevSamp', '$strcasecmp', '$strLenBytes',
  30. '$strLenCP', '$substr', '$substrBytes', '$substrCP', '$subtract', '$sum', '$switch', '$tan',
  31. '$toBool', '$toDate', '$toDecimal', '$toDouble', '$toInt', '$toLong', '$toObjectId', '$toString',
  32. '$toLower', '$toUpper', '$trim', '$trunc', '$type', '$week', '$year', '$zip',
  33. // aggregation pipeline query modifiers
  34. '$comment', '$explain', '$hint', '$max', '$maxTimeMS', '$min', '$orderby', '$query',
  35. '$returnKey', '$showDiskLoc', '$natural',
  36. ];
  37. var builtinFunctions = [
  38. 'ObjectId',
  39. 'Code',
  40. 'BinData',
  41. 'DBRef',
  42. 'Timestamp',
  43. 'NumberLong',
  44. 'NumberDecimal',
  45. 'MaxKey',
  46. 'MinKey',
  47. 'RegExp',
  48. 'ISODate',
  49. 'UUID',
  50. ];
  51. operators = operators.map(function(operator) {
  52. return operator.replace('$', '\\$');
  53. });
  54. var operatorsSource = '(?:' + operators.join('|') + ')\\b';
  55. Prism.languages.mongodb = Prism.languages.extend('javascript', {});
  56. Prism.languages.insertBefore('mongodb', 'string', {
  57. 'property': {
  58. pattern: /(?:(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)(?=\s*:)/,
  59. greedy: true,
  60. inside: {
  61. 'keyword': RegExp('^([\'"])?' + operatorsSource + '(?:\\1)?$')
  62. }
  63. }
  64. });
  65. Prism.languages.mongodb.string.inside = {
  66. url: {
  67. // url pattern
  68. pattern: /https?:\/\/[-\w@:%.+~#=]{1,256}\.[a-z0-9()]{1,6}\b[-\w()@:%+.~#?&/=]*/i,
  69. greedy: true
  70. },
  71. entity: {
  72. // ipv4
  73. pattern: /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/,
  74. greedy: true
  75. }
  76. };
  77. Prism.languages.insertBefore('mongodb', 'constant', {
  78. 'builtin': {
  79. pattern: RegExp('\\b(?:' + builtinFunctions.join('|') + ')\\b'),
  80. alias: 'keyword'
  81. }
  82. });
  83. }(Prism));