Skip to content

Commit 6e7e94f

Browse files
committed
Refactor code-style
1 parent 6dbc3c1 commit 6e7e94f

File tree

3 files changed

+46
-56
lines changed

3 files changed

+46
-56
lines changed

index.js

+43-46
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import containerFlow from 'mdast-util-to-markdown/lib/util/container-flow.js'
66
import containerPhrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
77
import checkQuote from 'mdast-util-to-markdown/lib/util/check-quote.js'
88

9-
var own = {}.hasOwnProperty
9+
const own = {}.hasOwnProperty
1010

11-
var shortcut = /^[^\t\n\r "#'.<=>`}]+$/
11+
const shortcut = /^[^\t\n\r "#'.<=>`}]+$/
1212

1313
handleDirective.peek = peekDirective
1414

@@ -129,7 +129,7 @@ function exitAttributeClassValue(token) {
129129
}
130130

131131
function exitAttributeValue(token) {
132-
var attributes = this.getData('directiveAttributes')
132+
const attributes = this.getData('directiveAttributes')
133133
attributes[attributes.length - 1][1] = decodeLight(this.sliceSerialize(token))
134134
}
135135

@@ -140,13 +140,12 @@ function exitAttributeName(token) {
140140
}
141141

142142
function exitAttributes() {
143-
var attributes = this.getData('directiveAttributes')
144-
var cleaned = {}
145-
var index = -1
146-
var attribute
143+
const attributes = this.getData('directiveAttributes')
144+
const cleaned = {}
145+
let index = -1
147146

148147
while (++index < attributes.length) {
149-
attribute = attributes[index]
148+
const attribute = attributes[index]
150149

151150
if (attribute[0] === 'class' && cleaned.class) {
152151
cleaned.class += ' ' + attribute[1]
@@ -165,17 +164,16 @@ function exit(token) {
165164
}
166165

167166
function handleDirective(node, _, context) {
168-
var prefix = fence(node)
169-
var exit = context.enter(node.type)
170-
var value =
167+
const prefix = fence(node)
168+
const exit = context.enter(node.type)
169+
let value =
171170
prefix +
172171
(node.name || '') +
173172
label(node, context) +
174173
attributes(node, context)
175-
var subvalue
176174

177175
if (node.type === 'containerDirective') {
178-
subvalue = content(node, context)
176+
const subvalue = content(node, context)
179177
if (subvalue) value += '\n' + subvalue
180178
value += '\n' + prefix
181179
}
@@ -189,58 +187,56 @@ function peekDirective() {
189187
}
190188

191189
function label(node, context) {
192-
var label = node
193-
var exit
194-
var subexit
195-
var value
190+
let label = node
196191

197192
if (node.type === 'containerDirective') {
198193
if (!inlineDirectiveLabel(node)) return ''
199194
label = node.children[0]
200195
}
201196

202-
exit = context.enter('label')
203-
subexit = context.enter(node.type + 'Label')
204-
value = containerPhrasing(label, context, {before: '[', after: ']'})
197+
const exit = context.enter('label')
198+
const subexit = context.enter(node.type + 'Label')
199+
const value = containerPhrasing(label, context, {before: '[', after: ']'})
205200
subexit()
206201
exit()
207202
return value ? '[' + value + ']' : ''
208203
}
209204

210205
function attributes(node, context) {
211-
var quote = checkQuote(context)
212-
var subset = node.type === 'textDirective' ? [quote] : [quote, '\n', '\r']
213-
var attrs = node.attributes || {}
214-
var values = []
215-
var id
216-
var classesFull
217-
var classes
218-
var value
219-
var key
220-
var index
206+
const quote = checkQuote(context)
207+
const subset = node.type === 'textDirective' ? [quote] : [quote, '\n', '\r']
208+
const attrs = node.attributes || {}
209+
const values = []
210+
let classesFull
211+
let classes
212+
let id
213+
let key
221214

222215
for (key in attrs) {
223-
if (own.call(attrs, key) && attrs[key] != null) {
224-
value = String(attrs[key])
216+
if (
217+
own.call(attrs, key) &&
218+
attrs[key] !== undefined &&
219+
attrs[key] !== null
220+
) {
221+
let value = String(attrs[key])
225222

226223
if (key === 'id') {
227224
id = shortcut.test(value) ? '#' + value : quoted('id', value)
228225
} else if (key === 'class') {
229226
value = value.split(/[\t\n\r ]+/g)
230227
classesFull = []
231228
classes = []
232-
index = -1
229+
let index = -1
233230

234231
while (++index < value.length) {
235232
;(shortcut.test(value[index]) ? classes : classesFull).push(
236233
value[index]
237234
)
238235
}
239236

240-
classesFull = classesFull.length
241-
? quoted('class', classesFull.join(' '))
242-
: ''
243-
classes = classes.length ? '.' + classes.join('.') : ''
237+
classesFull =
238+
classesFull.length > 0 ? quoted('class', classesFull.join(' ')) : ''
239+
classes = classes.length > 0 ? '.' + classes.join('.') : ''
244240
} else {
245241
values.push(quoted(key, value))
246242
}
@@ -259,7 +255,7 @@ function attributes(node, context) {
259255
values.unshift(id)
260256
}
261257

262-
return values.length ? '{' + values.join(' ') + '}' : ''
258+
return values.length > 0 ? '{' + values.join(' ') + '}' : ''
263259

264260
function quoted(key, value) {
265261
return (
@@ -272,11 +268,12 @@ function attributes(node, context) {
272268
}
273269

274270
function content(node, context) {
275-
var content = inlineDirectiveLabel(node)
276-
? Object.assign({}, node, {children: node.children.slice(1)})
277-
: node
278-
279-
return containerFlow(content, context)
271+
return containerFlow(
272+
inlineDirectiveLabel(node)
273+
? Object.assign({}, node, {children: node.children.slice(1)})
274+
: node,
275+
context
276+
)
280277
}
281278

282279
function inlineDirectiveLabel(node) {
@@ -300,7 +297,7 @@ function decodeIfPossible($0, $1) {
300297
}
301298

302299
function fence(node) {
303-
var size = 0
300+
let size = 0
304301

305302
if (node.type === 'containerDirective') {
306303
visitParents(node, 'containerDirective', onvisit)
@@ -314,8 +311,8 @@ function fence(node) {
314311
return repeatString(':', size)
315312

316313
function onvisit(node, parents) {
317-
var index = parents.length
318-
var nesting = 0
314+
let index = parents.length
315+
let nesting = 0
319316

320317
while (index--) {
321318
if (parents[index].type === 'containerDirective') {

package.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,7 @@
6565
"trailingComma": "none"
6666
},
6767
"xo": {
68-
"prettier": true,
69-
"rules": {
70-
"no-var": "off",
71-
"prefer-arrow-callback": "off",
72-
"unicorn/explicit-length-check": "off",
73-
"no-eq-null": "off",
74-
"eqeqeq": "off"
75-
}
68+
"prettier": true
7669
},
7770
"remarkConfig": {
7871
"plugins": [

test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {removePosition} from 'unist-util-remove-position'
55
import directive from 'micromark-extension-directive'
66
import {directiveFromMarkdown, directiveToMarkdown} from './index.js'
77

8-
test('markdown -> mdast', function (t) {
8+
test('markdown -> mdast', (t) => {
99
t.deepEqual(
1010
fromMarkdown('a :b[c]{d} e.', {
1111
extensions: [directive()],
@@ -243,7 +243,7 @@ test('markdown -> mdast', function (t) {
243243
t.end()
244244
})
245245

246-
test('mdast -> markdown', function (t) {
246+
test('mdast -> markdown', (t) => {
247247
t.deepEqual(
248248
toMarkdown(
249249
{

0 commit comments

Comments
 (0)