Skip to content

Commit 705ab38

Browse files
committed
style: switch to eslint
1 parent ec0230a commit 705ab38

File tree

6 files changed

+6441
-17294
lines changed

6 files changed

+6441
-17294
lines changed

.eslintrc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "module",
4+
"ecmaVersion": 2023
5+
},
6+
"plugins": ["@typescript-eslint"],
7+
"rules": {
8+
"indent": [2, 2, {"SwitchCase": 1}],
9+
"quotes": [2, "single"],
10+
"linebreak-style": [2, "unix"],
11+
"camelcase": [2, {"properties": "always"}],
12+
"brace-style": [2, "1tbs", {"allowSingleLine": true}]
13+
},
14+
"env": {
15+
"es6": true,
16+
"node": true,
17+
"browser": false
18+
},
19+
"extends": [
20+
"eslint:recommended",
21+
"plugin:@typescript-eslint/recommended"
22+
]
23+
}

lib/index.js

Lines changed: 164 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,164 @@
1-
import srcset from 'srcset'
2-
import postcss from 'postcss'
3-
import merge from 'lodash.merge'
4-
import isUrl from 'is-url-superb'
5-
import { walk } from 'posthtml/lib/api.js'
6-
import postcssBaseUrl from './plugins/postcss-baseurl.js'
7-
8-
const defaultTags = {
9-
a: {
10-
href: true,
11-
},
12-
area: {
13-
href: true,
14-
},
15-
audio: {
16-
src: true,
17-
},
18-
base: {
19-
href: true,
20-
},
21-
body: {
22-
background: true,
23-
},
24-
embed: {
25-
src: true,
26-
},
27-
iframe: {
28-
src: true,
29-
},
30-
img: {
31-
src: true,
32-
srcset: true,
33-
},
34-
input: {
35-
src: true,
36-
},
37-
link: {
38-
href: true,
39-
},
40-
script: {
41-
src: true,
42-
},
43-
source: {
44-
src: true,
45-
srcset: true,
46-
},
47-
table: {
48-
background: true,
49-
},
50-
td: {
51-
background: true,
52-
},
53-
th: {
54-
background: true,
55-
},
56-
track: {
57-
src: true,
58-
},
59-
video: {
60-
poster: true,
61-
},
62-
}
63-
64-
const plugin = (options = {}) => tree => {
65-
options.url = options.url || ''
66-
options.attributes = options.attributes || {}
67-
options.allTags = options.allTags || false
68-
options.styleTag = options.styleTag || false
69-
options.inlineCss = options.inlineCss || false
70-
options.tags = options.allTags ? merge(defaultTags, options.tags) : options.tags || {}
71-
72-
tree.walk = tree.walk || walk
73-
74-
const process = node => {
75-
// Skip if `url` was not provided
76-
if (options.url && (typeof options.url !== 'string' || options.url === '')) {
77-
return node
78-
}
79-
80-
// Skip if `options.tags` is not an array or object
81-
if (!['array', 'object'].includes(typeof options.tags)) {
82-
return node
83-
}
84-
85-
// Handle embedded stylesheets
86-
if (node.tag === 'style' && node.content && options.styleTag) {
87-
node.content = postcss([
88-
postcssBaseUrl({
89-
base: options.url,
90-
})
91-
])
92-
.process(node.content.join('').trim())
93-
.css
94-
}
95-
96-
// Handle style attribute
97-
if (node.attrs && node.attrs.style && options.inlineCss) {
98-
node.attrs.style = prependUrl(node.attrs.style, options.url)
99-
}
100-
101-
// Handle defined HTML attributes
102-
Object.entries(options.attributes).forEach(([attribute, value]) => {
103-
if (node.attrs && node.attrs[attribute]) {
104-
handleSingleValueAttributes(node, attribute, value)
105-
}
106-
})
107-
108-
// Handle defined HTML tags
109-
const tags = Array.isArray(options.tags) ?
110-
Object.entries(defaultTags).filter(([tag]) => options.tags.includes(tag)) :
111-
Object.entries(options.tags)
112-
113-
tags.forEach(([tag, attributes]) => {
114-
if (node.tag !== tag) {
115-
return node
116-
}
117-
118-
// Handle tag attributes
119-
Object.entries(attributes).forEach(([attribute, value]) => {
120-
if (node.attrs && node.attrs[attribute]) {
121-
// Handle "single-value" attributes
122-
if (['href', 'src', 'poster', 'background'].includes(attribute)) {
123-
handleSingleValueAttributes(node, attribute, value)
124-
}
125-
126-
// Handle `srcset` attribute
127-
if (attribute === 'srcset') {
128-
const parsed = srcset.parse(node.attrs[attribute])
129-
130-
parsed.map(p => {
131-
if (!isUrl(p.url)) {
132-
p.url = typeof value === 'boolean' ? options.url + p.url : value + p.url
133-
}
134-
135-
return p
136-
})
137-
138-
node.attrs[attribute] = srcset.stringify(parsed)
139-
}
140-
}
141-
})
142-
})
143-
144-
// Return the node
145-
return node
146-
}
147-
148-
const handleSingleValueAttributes = (node, attribute, value) => {
149-
if (isUrl(node.attrs[attribute])) {
150-
return node
151-
}
152-
153-
node.attrs[attribute] = typeof value === 'boolean' ? options.url + node.attrs[attribute] : value + node.attrs[attribute]
154-
}
155-
156-
const prependUrl = (value, url) => {
157-
const {css} = postcss([postcssBaseUrl({base: url})]).process(`div { ${value} }`)
158-
return css.replace(/div {\s|\s}$/gm, '')
159-
}
160-
161-
return tree.walk(process)
162-
}
163-
164-
export default plugin
1+
import srcset from 'srcset'
2+
import postcss from 'postcss'
3+
import merge from 'lodash.merge'
4+
import isUrl from 'is-url-superb'
5+
import { walk } from 'posthtml/lib/api.js'
6+
import postcssBaseUrl from './plugins/postcss-baseurl.js'
7+
8+
const defaultTags = {
9+
a: {
10+
href: true,
11+
},
12+
area: {
13+
href: true,
14+
},
15+
audio: {
16+
src: true,
17+
},
18+
base: {
19+
href: true,
20+
},
21+
body: {
22+
background: true,
23+
},
24+
embed: {
25+
src: true,
26+
},
27+
iframe: {
28+
src: true,
29+
},
30+
img: {
31+
src: true,
32+
srcset: true,
33+
},
34+
input: {
35+
src: true,
36+
},
37+
link: {
38+
href: true,
39+
},
40+
script: {
41+
src: true,
42+
},
43+
source: {
44+
src: true,
45+
srcset: true,
46+
},
47+
table: {
48+
background: true,
49+
},
50+
td: {
51+
background: true,
52+
},
53+
th: {
54+
background: true,
55+
},
56+
track: {
57+
src: true,
58+
},
59+
video: {
60+
poster: true,
61+
},
62+
}
63+
64+
const plugin = (options = {}) => tree => {
65+
options.url = options.url || ''
66+
options.attributes = options.attributes || {}
67+
options.allTags = options.allTags || false
68+
options.styleTag = options.styleTag || false
69+
options.inlineCss = options.inlineCss || false
70+
options.tags = options.allTags ? merge(defaultTags, options.tags) : options.tags || {}
71+
72+
tree.walk = tree.walk || walk
73+
74+
const process = node => {
75+
// Skip if `url` was not provided
76+
if (options.url && (typeof options.url !== 'string' || options.url === '')) {
77+
return node
78+
}
79+
80+
// Skip if `options.tags` is not an array or object
81+
if (!['array', 'object'].includes(typeof options.tags)) {
82+
return node
83+
}
84+
85+
// Handle embedded stylesheets
86+
if (node.tag === 'style' && node.content && options.styleTag) {
87+
node.content = postcss([
88+
postcssBaseUrl({
89+
base: options.url,
90+
})
91+
])
92+
.process(node.content.join('').trim())
93+
.css
94+
}
95+
96+
// Handle style attribute
97+
if (node.attrs && node.attrs.style && options.inlineCss) {
98+
node.attrs.style = prependUrl(node.attrs.style, options.url)
99+
}
100+
101+
// Handle defined HTML attributes
102+
Object.entries(options.attributes).forEach(([attribute, value]) => {
103+
if (node.attrs && node.attrs[attribute]) {
104+
handleSingleValueAttributes(node, attribute, value)
105+
}
106+
})
107+
108+
// Handle defined HTML tags
109+
const tags = Array.isArray(options.tags) ?
110+
Object.entries(defaultTags).filter(([tag]) => options.tags.includes(tag)) :
111+
Object.entries(options.tags)
112+
113+
tags.forEach(([tag, attributes]) => {
114+
if (node.tag !== tag) {
115+
return node
116+
}
117+
118+
// Handle tag attributes
119+
Object.entries(attributes).forEach(([attribute, value]) => {
120+
if (node.attrs && node.attrs[attribute]) {
121+
// Handle "single-value" attributes
122+
if (['href', 'src', 'poster', 'background'].includes(attribute)) {
123+
handleSingleValueAttributes(node, attribute, value)
124+
}
125+
126+
// Handle `srcset` attribute
127+
if (attribute === 'srcset') {
128+
const parsed = srcset.parse(node.attrs[attribute])
129+
130+
parsed.map(p => {
131+
if (!isUrl(p.url)) {
132+
p.url = typeof value === 'boolean' ? options.url + p.url : value + p.url
133+
}
134+
135+
return p
136+
})
137+
138+
node.attrs[attribute] = srcset.stringify(parsed)
139+
}
140+
}
141+
})
142+
})
143+
144+
// Return the node
145+
return node
146+
}
147+
148+
const handleSingleValueAttributes = (node, attribute, value) => {
149+
if (isUrl(node.attrs[attribute])) {
150+
return node
151+
}
152+
153+
node.attrs[attribute] = typeof value === 'boolean' ? options.url + node.attrs[attribute] : value + node.attrs[attribute]
154+
}
155+
156+
const prependUrl = (value, url) => {
157+
const {css} = postcss([postcssBaseUrl({base: url})]).process(`div { ${value} }`)
158+
return css.replace(/div {\s|\s}$/gm, '')
159+
}
160+
161+
return tree.walk(process)
162+
}
163+
164+
export default plugin

0 commit comments

Comments
 (0)