UNPKG

2.62 kBJavaScriptView Raw
1"use strict"
2
3const oneLine = require("./utils").oneLine
4
5const defaultHTMLExtensions = [
6 ".erb",
7 ".handlebars",
8 ".hbs",
9 ".htm",
10 ".html",
11 ".mustache",
12 ".nunjucks",
13 ".php",
14 ".tag",
15 ".twig",
16 ".we",
17]
18
19const defaultXMLExtensions = [".xhtml", ".xml"]
20
21function filterOut(array, excludeArray) {
22 if (!excludeArray) return array
23 return array.filter((item) => excludeArray.indexOf(item) < 0)
24}
25
26function compileRegExp(re) {
27 const tokens = re.split("/")
28 if (tokens.shift()) {
29 // Ignore first token
30 throw new Error(`Invalid regexp: ${re}`)
31 }
32 const flags = tokens.pop()
33 return new RegExp(tokens.join("/"), flags)
34}
35
36function getSetting(settings, name) {
37 if (typeof settings.html === "object" && name in settings.html) {
38 return settings.html[name]
39 }
40 return settings[`html/${name}`]
41}
42
43function getSettings(settings) {
44 const htmlExtensions =
45 getSetting(settings, "html-extensions") ||
46 filterOut(defaultHTMLExtensions, getSetting(settings, "xml-extensions"))
47
48 const xmlExtensions =
49 getSetting(settings, "xml-extensions") ||
50 filterOut(defaultXMLExtensions, getSetting(settings, "html-extensions"))
51
52 let reportBadIndent
53 switch (getSetting(settings, "report-bad-indent")) {
54 case undefined:
55 case false:
56 case 0:
57 case "off":
58 reportBadIndent = 0
59 break
60 case true:
61 case 1:
62 case "warn":
63 reportBadIndent = 1
64 break
65 case 2:
66 case "error":
67 reportBadIndent = 2
68 break
69 default:
70 throw new Error(
71 oneLine`
72 Invalid value for html/report-bad-indent,
73 expected one of 0, 1, 2, "off", "warn" or "error"
74 `
75 )
76 }
77
78 const parsedIndent = /^(\+)?(tab|\d+)$/.exec(getSetting(settings, "indent"))
79 const indent = parsedIndent && {
80 relative: parsedIndent[1] === "+",
81 spaces: parsedIndent[2] === "tab" ? "\t" : " ".repeat(parsedIndent[2]),
82 }
83
84 const rawJavaScriptMIMETypes = getSetting(settings, "javascript-mime-types")
85 const javaScriptMIMETypes = rawJavaScriptMIMETypes
86 ? (Array.isArray(rawJavaScriptMIMETypes)
87 ? rawJavaScriptMIMETypes
88 : [rawJavaScriptMIMETypes]
89 ).map((s) => (s.startsWith("/") ? compileRegExp(s) : s))
90 : [
91 /^(application|text)\/(x-)?(javascript|babel|ecmascript-6)$/i,
92 /^module$/i,
93 ]
94
95 function isJavaScriptMIMEType(type) {
96 return javaScriptMIMETypes.some((o) =>
97 typeof o === "string" ? type === o : o.test(type)
98 )
99 }
100
101 return {
102 htmlExtensions,
103 xmlExtensions,
104 indent,
105 reportBadIndent,
106 isJavaScriptMIMEType,
107 }
108}
109
110module.exports = {
111 getSettings,
112}