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