get-lang.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. const languages = {
  3. sass: /^sass$/i,
  4. // https://github.com/Microsoft/vscode/blob/master/extensions/scss/package.json
  5. scss: /^scss$/i,
  6. // https://github.com/Microsoft/vscode/blob/master/extensions/less/package.json
  7. less: /^less$/i,
  8. // https://github.com/MhMadHamster/vscode-postcss-language/blob/master/package.json
  9. sugarss: /^s(?:ugar)?ss$/i,
  10. // https://github.com/d4rkr00t/language-stylus/blob/master/package.json
  11. stylus: /^styl(?:us)?$/i,
  12. // WXSS(WeiXin Style Sheets) See: https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html
  13. // acss(AntFinancial Style Sheet) See: https://docs.alipay.com/mini/framework/acss
  14. // `*.pcss`, `*.postcss`
  15. // https://github.com/Microsoft/vscode/blob/master/extensions/css/package.json
  16. // https://github.com/rcsole/postcss-syntax/blob/master/package.json
  17. css: /^(?:wx|\w*c)ss$/i,
  18. };
  19. const extracts = {
  20. // https://github.com/Microsoft/vscode/blob/master/extensions/javascript/package.json
  21. // https://github.com/Microsoft/vscode/blob/master/extensions/typescript-basics/package.json
  22. // https://github.com/michaelgmcd/vscode-language-babel/blob/master/package.json
  23. jsx: /^(?:m?[jt]sx?|es\d*|pac|babel|flow)$/i,
  24. // *.*html? HTML https://github.com/Microsoft/vscode/blob/master/extensions/html/package.json
  25. // *.xslt? XSLT https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx
  26. // *.vue VUE https://vue-loader.vuejs.org/spec.html
  27. // *.wpy WePY https://github.com/Tencent/wepy/blob/master/docs/md/doc.md#wpy文件说明
  28. // *.ux quickapp https://doc.quickapp.cn/framework/source-file.html
  29. // *.php* PHP https://github.com/Microsoft/vscode/blob/master/extensions/php/package.json
  30. // *.twig Twig https://github.com/mblode/vscode-twig-language/blob/master/package.json
  31. // *.liquid Liquid https://github.com/GingerBear/vscode-liquid/blob/master/package.json
  32. // *.svelte Svelte https://github.com/UnwrittenFun/svelte-vscode/blob/master/package.json
  33. html: /^(?:\w*html?|xht|xslt?|mdoc|jsp|aspx?|volt|ejs|vue|wpy|ux|php\d*|ctp|twig|liquid|svelte)$/i,
  34. // https://github.com/Microsoft/vscode/blob/master/extensions/markdown-basics/package.json
  35. markdown: /^(?:m(?:ark)?d(?:ow)?n|mk?d)$/i,
  36. // https://github.com/Microsoft/vscode/blob/master/extensions/xml/package.json
  37. xml: /^(?:xml|xsd|ascx|atom|axml|bpmn|config|cpt|csl|csproj|csproj|user|dita|ditamap|dtd|dtml|fsproj|fxml|iml|isml|jmx|launch|menu|mxml|nuspec|opml|owl|proj|props|pt|publishsettings|pubxml|pubxml|user|rdf|rng|rss|shproj|storyboard|svg|targets|tld|tmx|vbproj|vbproj|user|vcxproj|vcxproj|filters|wsdl|wxi|wxl|wxs|xaml|xbl|xib|xlf|xliff|xpdl|xul|xoml)$/i,
  38. };
  39. function sourceType (source) {
  40. source = source && source.trim();
  41. if (!source) {
  42. return;
  43. }
  44. let extract;
  45. if (
  46. // start with strict mode
  47. // start with import code
  48. // start with require code
  49. /^(?:(?:\/\/[^\r\n]*\r?\n|\/\*.*?\*\/)\s*)*(?:(?:("|')use strict\1|import(?:\s+[^;]+\s+from)?\s+("|')[^'"]+?\2|export\s+[^;]+\s+[^;]+)\s*(;|\r?\n|$)|(?:(?:var|let|const)\s+[^;]+\s*=\s*)?(?:require|import)\(.+\))/.test(source) ||
  50. // https://en.wikipedia.org/wiki/Shebang_(Unix)
  51. (/^#!([^\r\n]+)/.test(source) && /(?:^|\s+|\/)(?:ts-)?node(?:\.\w+)?(?:\s+|$)$/.test(RegExp.$1))
  52. ) {
  53. extract = "jsx";
  54. } else if (
  55. /^(?:<\?.*?\?>\s*)*<(?:!DOCTYPE\s+)?html(\s+[^<>]*)?>/i.test(source) ||
  56. /^<\?php(?:\s+[\s\S]*)?(?:\?>|$)/.test(source)
  57. ) {
  58. extract = "html";
  59. } else if (/^<\?xml(\s+[^<>]*)?\?>/i.test(source)) {
  60. // https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx
  61. if (/<xsl:\w+\b[^<>]*>/.test(source) || /<\/xsl:\w+>/i.test(source)) {
  62. extract = "html";
  63. } else {
  64. extract = "xml";
  65. }
  66. } else if (/^(?:#+\s+\S+|\S+[^\r\n]*\r?\n=+(\r?\n|$))/.test(source)) {
  67. extract = "markdown";
  68. } else if (/<(\w+)(?:\s+[^<>]*)?>[\s\S]*?<\/\1>/.test(source)) {
  69. extract = "html";
  70. } else {
  71. return;
  72. }
  73. return {
  74. extract,
  75. };
  76. }
  77. function extType (extName, languages) {
  78. for (const langName in languages) {
  79. if (languages[langName].test(extName)) {
  80. return langName;
  81. }
  82. }
  83. }
  84. function fileType (file) {
  85. if (file && /\.(\w+)(?:[?#].*?)?$/.test(file)) {
  86. const extName = RegExp.$1;
  87. const extract = extType(extName, extracts);
  88. if (extract) {
  89. return {
  90. extract,
  91. };
  92. }
  93. const lang = extType(extName, languages);
  94. if (lang) {
  95. return {
  96. lang,
  97. };
  98. }
  99. }
  100. }
  101. function getLang (file, source) {
  102. return fileType(file) || sourceType(source);
  103. }
  104. module.exports = getLang;