123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- "use strict";
- const languages = {
- sass: /^sass$/i,
-
- scss: /^scss$/i,
-
- less: /^less$/i,
-
- sugarss: /^s(?:ugar)?ss$/i,
-
- stylus: /^styl(?:us)?$/i,
-
-
-
-
-
- css: /^(?:wx|\w*c)ss$/i,
- };
- const extracts = {
-
-
-
- jsx: /^(?:m?[jt]sx?|es\d*|pac|babel|flow)$/i,
-
-
-
-
-
-
-
-
-
- html: /^(?:\w*html?|xht|xslt?|mdoc|jsp|aspx?|volt|ejs|vue|wpy|ux|php\d*|ctp|twig|liquid|svelte)$/i,
-
- markdown: /^(?:m(?:ark)?d(?:ow)?n|mk?d)$/i,
-
- 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,
- };
- function sourceType (source) {
- source = source && source.trim();
- if (!source) {
- return;
- }
- let extract;
- if (
-
-
-
- /^(?:(?:\/\/[^\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) ||
-
- (/^#!([^\r\n]+)/.test(source) && /(?:^|\s+|\/)(?:ts-)?node(?:\.\w+)?(?:\s+|$)$/.test(RegExp.$1))
- ) {
- extract = "jsx";
- } else if (
- /^(?:<\?.*?\?>\s*)*<(?:!DOCTYPE\s+)?html(\s+[^<>]*)?>/i.test(source) ||
- /^<\?php(?:\s+[\s\S]*)?(?:\?>|$)/.test(source)
- ) {
- extract = "html";
- } else if (/^<\?xml(\s+[^<>]*)?\?>/i.test(source)) {
-
- if (/<xsl:\w+\b[^<>]*>/.test(source) || /<\/xsl:\w+>/i.test(source)) {
- extract = "html";
- } else {
- extract = "xml";
- }
- } else if (/^(?:#+\s+\S+|\S+[^\r\n]*\r?\n=+(\r?\n|$))/.test(source)) {
- extract = "markdown";
- } else if (/<(\w+)(?:\s+[^<>]*)?>[\s\S]*?<\/\1>/.test(source)) {
- extract = "html";
- } else {
- return;
- }
- return {
- extract,
- };
- }
- function extType (extName, languages) {
- for (const langName in languages) {
- if (languages[langName].test(extName)) {
- return langName;
- }
- }
- }
- function fileType (file) {
- if (file && /\.(\w+)(?:[?#].*?)?$/.test(file)) {
- const extName = RegExp.$1;
- const extract = extType(extName, extracts);
- if (extract) {
- return {
- extract,
- };
- }
- const lang = extType(extName, languages);
- if (lang) {
- return {
- lang,
- };
- }
- }
- }
- function getLang (file, source) {
- return fileType(file) || sourceType(source);
- }
- module.exports = getLang;
|