Skip to content

Commit 537d210

Browse files
committed
Refactor to move implementation to lib/
1 parent ae5234c commit 537d210

File tree

3 files changed

+66
-60
lines changed

3 files changed

+66
-60
lines changed

index.js

+2-60
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,5 @@
11
/**
2-
* @typedef {import('mdast').Root|import('mdast').Content} Node
3-
*
4-
* @typedef Options
5-
* Configuration (optional).
6-
* @property {boolean} [includeImageAlt=true]
7-
* Whether to use `alt` for `image`s.
2+
* @typedef {import('./lib/index.js').Options} Options
83
*/
94

10-
/**
11-
* Get the text content of a node or list of nodes.
12-
* Prefers the node’s plain-text fields, otherwise serializes its children,
13-
* and if the given value is an array, serialize the nodes in it.
14-
*
15-
* @param {unknown} value
16-
* @param {Options} [options]
17-
* @returns {string}
18-
*/
19-
export function toString(value, options = {}) {
20-
const {includeImageAlt = true} = options
21-
return one(value, includeImageAlt)
22-
}
23-
24-
/**
25-
* @param {unknown} value
26-
* @param {boolean} includeImageAlt
27-
* @returns {string}
28-
*/
29-
function one(value, includeImageAlt) {
30-
return (
31-
(node(value) &&
32-
(('value' in value && value.value) ||
33-
(includeImageAlt && 'alt' in value && value.alt) ||
34-
('children' in value && all(value.children, includeImageAlt)))) ||
35-
(Array.isArray(value) && all(value, includeImageAlt)) ||
36-
''
37-
)
38-
}
39-
40-
/**
41-
* @param {Array<unknown>} values
42-
* @param {boolean} includeImageAlt
43-
* @returns {string}
44-
*/
45-
function all(values, includeImageAlt) {
46-
/** @type {Array<string>} */
47-
const result = []
48-
let index = -1
49-
50-
while (++index < values.length) {
51-
result[index] = one(values[index], includeImageAlt)
52-
}
53-
54-
return result.join('')
55-
}
56-
57-
/**
58-
* @param {unknown} value
59-
* @returns {value is Node}
60-
*/
61-
function node(value) {
62-
return Boolean(value && typeof value === 'object')
63-
}
5+
export {toString} from './lib/index.js'

lib/index.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @typedef {import('mdast').Root|import('mdast').Content} Node
3+
*
4+
* @typedef Options
5+
* Configuration (optional).
6+
* @property {boolean} [includeImageAlt=true]
7+
* Whether to use `alt` for `image`s.
8+
*/
9+
10+
/**
11+
* Get the text content of a node or list of nodes.
12+
* Prefers the node’s plain-text fields, otherwise serializes its children,
13+
* and if the given value is an array, serialize the nodes in it.
14+
*
15+
* @param {unknown} value
16+
* @param {Options} [options]
17+
* @returns {string}
18+
*/
19+
export function toString(value, options = {}) {
20+
const {includeImageAlt = true} = options
21+
return one(value, includeImageAlt)
22+
}
23+
24+
/**
25+
* @param {unknown} value
26+
* @param {boolean} includeImageAlt
27+
* @returns {string}
28+
*/
29+
function one(value, includeImageAlt) {
30+
return (
31+
(node(value) &&
32+
(('value' in value && value.value) ||
33+
(includeImageAlt && 'alt' in value && value.alt) ||
34+
('children' in value && all(value.children, includeImageAlt)))) ||
35+
(Array.isArray(value) && all(value, includeImageAlt)) ||
36+
''
37+
)
38+
}
39+
40+
/**
41+
* @param {Array<unknown>} values
42+
* @param {boolean} includeImageAlt
43+
* @returns {string}
44+
*/
45+
function all(values, includeImageAlt) {
46+
/** @type {Array<string>} */
47+
const result = []
48+
let index = -1
49+
50+
while (++index < values.length) {
51+
result[index] = one(values[index], includeImageAlt)
52+
}
53+
54+
return result.join('')
55+
}
56+
57+
/**
58+
* @param {unknown} value
59+
* @returns {value is Node}
60+
*/
61+
function node(value) {
62+
return Boolean(value && typeof value === 'object')
63+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"main": "index.js",
3030
"types": "index.d.ts",
3131
"files": [
32+
"lib/",
3233
"index.d.ts",
3334
"index.js"
3435
],

0 commit comments

Comments
 (0)