|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +// https://github.com/Microsoft/vscode/blob/master/extensions/markdown-basics/package.json |
| 4 | +// https://github.com/Microsoft/vscode/blob/master/extensions/html/package.json |
| 5 | +// https://github.com/Microsoft/vscode/blob/master/extensions/javascript/package.json |
| 6 | +// https://github.com/Microsoft/vscode/blob/master/extensions/typescript-basics/package.json |
| 7 | +// https://github.com/Microsoft/vscode/blob/master/extensions/xml/package.json |
| 8 | +// https://github.com/Microsoft/vscode/blob/master/extensions/css/package.json |
| 9 | +// https://github.com/Microsoft/vscode/blob/master/extensions/less/package.json |
| 10 | +// https://github.com/vuejs/vetur/blob/master/package.json |
| 11 | + |
| 12 | +const languages = { |
| 13 | + sass: /^sass$/i, |
| 14 | + scss: /^scss$/i, |
| 15 | + less: /^less$/i, |
| 16 | + sugarss: /^s(?:ugar)?ss$/i, |
| 17 | + stylus: /^styl(?:us)?$/i, |
| 18 | + // WXSS(WeiXin Style Sheets) See: https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html |
| 19 | + // acss(AntFinancial Style Sheet) See: https://docs.alipay.com/mini/framework/acss |
| 20 | + // `*.pcss`, `*.postcss` |
| 21 | + css: /^(?:wx|\w*c)ss$/i, |
| 22 | +}; |
| 23 | + |
| 24 | +const extracts = { |
| 25 | + jsx: /^(?:m?[jt]sx?|es\d*|pac)$/i, |
| 26 | + // *.xslt? https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx |
| 27 | + // *.Vue Single-File Component (SFC) Spec https://vue-loader.vuejs.org/spec.html |
| 28 | + // *.ux quickapp https://doc.quickapp.cn/framework/source-file.html |
| 29 | + html: /^(?:\w*html?|xht|mdoc|jsp|aspx?|volt|ejs|php|vue|ux)$/i, |
| 30 | + markdown: /^(?:m(?:ark)?d(?:ow)?n|mk?d)$/i, |
| 31 | + xsl: /^xslt?$/i, |
| 32 | + 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, |
| 33 | +}; |
| 34 | + |
| 35 | +function sourceType (source) { |
| 36 | + source = source && source.trim(); |
| 37 | + if (!source) { |
| 38 | + return; |
| 39 | + } |
| 40 | + let extract; |
| 41 | + if ((/^#!([^\r\n]+)/.test(source) && /(?:^|\s+)(?:ts-)?node(?:\.\w+)?(?:\s+|$)$/.test(RegExp.$1)) || /^("|')use strict\1;*\s*(\r?\n|$)/.test(source) || /^import(?:\s+[^;]+\s+from)?\s+("|')[^'"]+?\1;*\s*(\r?\n|$)/.test(source) || /^(?:(?:var|let|const)\s+[^;]+\s*=)?\s*(?:require|import)\(.+\)/.test(source)) { |
| 42 | + // https://en.wikipedia.org/wiki/Shebang_(Unix) |
| 43 | + // or start with strict mode |
| 44 | + // or start with import code |
| 45 | + extract = "jsx"; |
| 46 | + } else if (/^<(?:!DOCTYPE\s+)?html(\s+[^<>]*)?>/i.test(source)) { |
| 47 | + extract = "html"; |
| 48 | + } else if (/^<\?xml(\s+[^<>]*)?\?>/i.test(source)) { |
| 49 | + // https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx |
| 50 | + if (/<xsl:\w+\b[^<>]*>/.test(source) || /<\/xsl:\w+>/i.test(source)) { |
| 51 | + extract = "xsl"; |
| 52 | + } else { |
| 53 | + extract = "xml"; |
| 54 | + } |
| 55 | + } else if (/^#+\s+\S+/.test(source) || /^\S+[^\r\n]*\r?\n=+(\r?\n|$)/.test(source)) { |
| 56 | + extract = "markdown"; |
| 57 | + } else if (/<(\w+)(?:\s+[^<>]*)?>[\s\S]*?<\/\1>/.test(source)) { |
| 58 | + extract = "html"; |
| 59 | + } else { |
| 60 | + return; |
| 61 | + } |
| 62 | + return { |
| 63 | + extract, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function extType (extName, languages) { |
| 68 | + for (const langName in languages) { |
| 69 | + if (languages[langName].test(extName)) { |
| 70 | + return langName; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function fileType (file) { |
| 76 | + if (file && /\.(\w+)(?:[?#].*?)?$/.test(file)) { |
| 77 | + const extName = RegExp.$1; |
| 78 | + const extract = extType(extName, extracts); |
| 79 | + if (extract) { |
| 80 | + return { |
| 81 | + extract, |
| 82 | + }; |
| 83 | + } |
| 84 | + const lang = extType(extName, languages); |
| 85 | + if (lang) { |
| 86 | + return { |
| 87 | + lang, |
| 88 | + }; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function getLang (opts, source) { |
| 94 | + const file = opts.from; |
| 95 | + const rules = opts.syntax.config.rules; |
| 96 | + return (rules && rules.find( |
| 97 | + rule => rule.test.test ? rule.test.test(file) : rule.test(file, source) |
| 98 | + )) || fileType(file) || sourceType(source) || { |
| 99 | + lang: "css", |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = getLang; |
0 commit comments