Skip to content

Commit 52ba4aa

Browse files
committed
Refactor code-style
* Add support for `null` as input of API types * Add more docs to JSDoc * Refactor tons of code
1 parent 742f876 commit 52ba4aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1129
-673
lines changed

lib/all.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
11
/**
2+
* @typedef {import('hast').Root} Root
3+
* @typedef {import('hast').Content} Content
4+
* @typedef {import('mdast').Content} MdastContent
25
* @typedef {import('./types.js').H} H
3-
* @typedef {import('./types.js').Node} Node
4-
* @typedef {import('./types.js').MdastNode} MdastNode
6+
*/
7+
8+
/**
9+
* @typedef {Root | Content} Node
10+
* @typedef {Extract<Node, import('unist').Parent>} Parent
511
*/
612

713
import {one} from './one.js'
814

915
/**
1016
* @param {H} h
11-
* @param {Node} parent
12-
* @returns {Array<MdastNode>}
17+
* Context.
18+
* @param {Parent} parent
19+
* Parent to transform.
20+
* @returns {Array<MdastContent>}
21+
* mdast nodes.
1322
*/
1423
export function all(h, parent) {
15-
/** @type {Array<Node>} */
16-
// @ts-expect-error Assume `parent` is a parent.
17-
const nodes = parent.children || []
18-
/** @type {Array<MdastNode>} */
19-
const values = []
24+
const children = parent.children || []
25+
/** @type {Array<MdastContent>} */
26+
const results = []
2027
let index = -1
2128

22-
while (++index < nodes.length) {
23-
// @ts-expect-error assume `parent` is a parent.
24-
const result = one(h, nodes[index], parent)
29+
while (++index < children.length) {
30+
const child = children[index]
31+
const result = one(h, child, parent)
2532

2633
if (Array.isArray(result)) {
27-
values.push(...result)
34+
// @ts-expect-error: assume no `root`.
35+
results.push(...result)
2836
} else if (result) {
29-
values.push(result)
37+
// @ts-expect-error: assume no `root`.
38+
results.push(result)
3039
}
3140
}
3241

3342
let start = 0
34-
let end = values.length
43+
let end = results.length
3544

36-
while (start < end && values[start].type === 'break') {
45+
while (start < end && results[start].type === 'break') {
3746
start++
3847
}
3948

40-
while (end > start && values[end - 1].type === 'break') {
49+
while (end > start && results[end - 1].type === 'break') {
4150
end--
4251
}
4352

44-
return start === 0 && end === values.length
45-
? values
46-
: values.slice(start, end)
53+
return start === 0 && end === results.length
54+
? results
55+
: results.slice(start, end)
4756
}

lib/handlers/a.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
2+
* @typedef {import('mdast').Link} Link
3+
* @typedef {import('hast').Element} Element
24
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Element} Element
4-
* @typedef {import('../types.js').Properties} Properties
5-
* @typedef {import('../types.js').MdastNode} Content
65
*/
76

87
import {all} from '../all.js'
@@ -13,20 +12,25 @@ import {resolve} from '../util/resolve.js'
1312
* Context.
1413
* @param {Element} node
1514
* hast element to transform.
16-
* @returns {Content}
15+
* @returns {Link}
1716
* mdast node.
1817
*/
1918
export function a(h, node) {
20-
/** @type {Properties} */
21-
// @ts-expect-error: `props` are defined.
22-
const props = node.properties
23-
return h(
24-
node,
25-
'link',
26-
{
27-
title: props.title || null,
28-
url: resolve(h, String(props.href || '') || null)
29-
},
30-
all(h, node)
31-
)
19+
const properties = node.properties || {}
20+
21+
/** @type {Link} */
22+
const result = {
23+
type: 'link',
24+
url: resolve(h, String(properties.href || '') || null),
25+
title: properties.title ? String(properties.title) : null,
26+
// @ts-expect-error: assume valid children.
27+
children: all(h, node)
28+
}
29+
30+
// To do: clean.
31+
if (node.position) {
32+
result.position = node.position
33+
}
34+
35+
return result
3236
}

lib/handlers/base.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2+
* @typedef {import('hast').Element} Element
23
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Element} Element
44
*/
55

66
/**
@@ -9,12 +9,12 @@
99
* @param {Element} node
1010
* hast element to transform.
1111
* @returns {void}
12-
* Nothing
12+
* Nothing.
1313
*/
1414
export function base(h, node) {
1515
if (!h.baseFound) {
1616
h.frozenBaseUrl =
17-
String((node.properties && node.properties.href) || '') || null
17+
String((node.properties && node.properties.href) || '') || undefined
1818
h.baseFound = true
1919
}
2020
}

lib/handlers/blockquote.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('mdast').Blockquote} Blockquote
24
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Element} Element
4-
* @typedef {import('../types.js').MdastNode} Content
55
*/
66

77
import {wrapChildren} from '../util/wrap-children.js'
@@ -11,9 +11,20 @@ import {wrapChildren} from '../util/wrap-children.js'
1111
* Context.
1212
* @param {Element} node
1313
* hast element to transform.
14-
* @returns {Content}
14+
* @returns {Blockquote}
1515
* mdast node.
1616
*/
1717
export function blockquote(h, node) {
18-
return h(node, 'blockquote', wrapChildren(h, node))
18+
/** @type {Blockquote} */
19+
const result = {
20+
type: 'blockquote',
21+
children: wrapChildren(h, node)
22+
}
23+
24+
// To do: clean.
25+
if (node.position) {
26+
result.position = node.position
27+
}
28+
29+
return result
1930
}

lib/handlers/br.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
/**
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('mdast').Break} Break
4+
* @typedef {import('mdast').Text} Text
25
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Element} Element
4-
* @typedef {import('../types.js').MdastNode} Content
56
*/
67

78
/**
89
* @param {H} h
910
* Context.
1011
* @param {Element} node
1112
* hast element to transform.
12-
* @returns {Content}
13+
* @returns {Break | Text}
1314
* mdast node.
1415
*/
1516
export function br(h, node) {
16-
return h.wrapText ? h(node, 'break') : h(node, 'text', ' ')
17+
/** @type {Break | Text} */
18+
const result = h.wrapText ? {type: 'break'} : {type: 'text', value: ' '}
19+
20+
// To do: clean.
21+
if (node.position) {
22+
result.position = node.position
23+
}
24+
25+
return result
1726
}

lib/handlers/code.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
11
/**
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('mdast').Code} Code
24
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Element} Element
4-
* @typedef {import('../types.js').MdastNode} Content
55
*/
66

7-
import {convertElement} from 'hast-util-is-element'
87
import {toText} from 'hast-util-to-text'
98
import {trimTrailingLines} from 'trim-trailing-lines'
109
import {wrapText} from '../util/wrap-text.js'
1110

1211
const prefix = 'language-'
1312

14-
const pre = convertElement('pre')
15-
const isCode = convertElement('code')
16-
1713
/**
1814
* @param {H} h
1915
* Context.
2016
* @param {Element} node
2117
* hast element to transform.
22-
* @returns {Content}
18+
* @returns {Code}
2319
* mdast node.
2420
*/
2521
export function code(h, node) {
2622
const children = node.children
2723
let index = -1
28-
/** @type {Array<string|number>|undefined} */
24+
/** @type {Array<string | number> | undefined} */
2925
let classList
30-
/** @type {string|undefined} */
26+
/** @type {string | undefined} */
3127
let lang
3228

33-
if (pre(node)) {
29+
if (node.tagName === 'pre') {
3430
while (++index < children.length) {
3531
const child = children[index]
3632

3733
if (
38-
isCode(child) &&
34+
child.type === 'element' &&
35+
child.tagName === 'code' &&
3936
child.properties &&
4037
child.properties.className &&
4138
Array.isArray(child.properties.className)
@@ -57,10 +54,18 @@ export function code(h, node) {
5754
}
5855
}
5956

60-
return h(
61-
node,
62-
'code',
63-
{lang: lang || null, meta: null},
64-
trimTrailingLines(wrapText(h, toText(node)))
65-
)
57+
/** @type {Code} */
58+
const result = {
59+
type: 'code',
60+
lang: lang || null,
61+
meta: null,
62+
value: trimTrailingLines(wrapText(h, toText(node)))
63+
}
64+
65+
// To do: clean.
66+
if (node.position) {
67+
result.position = node.position
68+
}
69+
70+
return result
6671
}

lib/handlers/comment.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2+
* @typedef {import('hast').Comment} Comment
3+
* @typedef {import('mdast').HTML} HTML
24
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Comment} Comment
4-
* @typedef {import('../types.js').MdastNode} Content
55
*/
66

77
import {wrapText} from '../util/wrap-text.js'
@@ -11,9 +11,17 @@ import {wrapText} from '../util/wrap-text.js'
1111
* Context.
1212
* @param {Comment} node
1313
* hast element to transform.
14-
* @returns {Content}
14+
* @returns {HTML}
1515
* mdast node.
1616
*/
1717
export function comment(h, node) {
18-
return h(node, 'html', '<!--' + wrapText(h, node.value) + '-->')
18+
/** @type {HTML} */
19+
const result = {type: 'html', value: '<!--' + wrapText(h, node.value) + '-->'}
20+
21+
// To do: clean.
22+
if (node.position) {
23+
result.position = node.position
24+
}
25+
26+
return result
1927
}

lib/handlers/del.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2+
* @typedef {import('hast').Element} Element
3+
* @typedef {import('mdast').Delete} Delete
24
* @typedef {import('../types.js').H} H
3-
* @typedef {import('../types.js').Element} Element
4-
* @typedef {import('../types.js').MdastNode} Content
55
*/
66

77
import {all} from '../all.js'
@@ -11,9 +11,21 @@ import {all} from '../all.js'
1111
* Context.
1212
* @param {Element} node
1313
* hast element to transform.
14-
* @returns {Content}
14+
* @returns {Delete}
1515
* mdast node.
1616
*/
1717
export function del(h, node) {
18-
return h(node, 'delete', all(h, node))
18+
/** @type {Delete} */
19+
const result = {
20+
type: 'delete',
21+
// @ts-expect-error: assume valid children.
22+
children: all(h, node)
23+
}
24+
25+
// To do: clean.
26+
if (node.position) {
27+
result.position = node.position
28+
}
29+
30+
return result
1931
}

0 commit comments

Comments
 (0)