Skip to content

Commit bc43530

Browse files
committed
Add includeImageAlt option
1 parent 1b5d8b3 commit bc43530

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

index.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
1+
/**
2+
* @typedef Options
3+
* @property {boolean} [includeImageAlt=true]
4+
*/
5+
16
/**
27
* Get the text content of a node.
38
* Prefer the node’s plain-text fields, otherwise serialize its children,
49
* and if the given value is an array, serialize the nodes in it.
510
*
611
* @param {unknown} node
12+
* @param {Options} [options]
13+
* @returns {string}
14+
*/
15+
export function toString(node, options) {
16+
var {includeImageAlt = true} = options || {}
17+
return one(node, includeImageAlt)
18+
}
19+
20+
/**
21+
* @param {unknown} node
22+
* @param {boolean} includeImageAlt
723
* @returns {string}
824
*/
9-
export function toString(node) {
25+
function one(node, includeImageAlt) {
1026
return (
1127
(node &&
1228
typeof node === 'object' &&
1329
// @ts-ignore looks like a literal.
1430
(node.value ||
1531
// @ts-ignore looks like an image.
16-
node.alt ||
32+
(includeImageAlt ? node.alt : '') ||
1733
// @ts-ignore looks like a parent.
18-
('children' in node && all(node.children)) ||
19-
(Array.isArray(node) && all(node)))) ||
34+
('children' in node && all(node.children, includeImageAlt)) ||
35+
(Array.isArray(node) && all(node, includeImageAlt)))) ||
2036
''
2137
)
2238
}
2339

2440
/**
2541
* @param {Array.<unknown>} values
42+
* @param {boolean} includeImageAlt
2643
* @returns {string}
2744
*/
28-
function all(values) {
45+
function all(values, includeImageAlt) {
2946
/** @type {Array.<string>} */
3047
var result = []
3148
var index = -1
3249

3350
while (++index < values.length) {
34-
result[index] = toString(values[index])
51+
result[index] = one(values[index], includeImageAlt)
3552
}
3653

3754
return result.join('')

readme.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,21 @@ console.log(toString(tree)) // => 'Some emphasis, importance, and code.'
4040
This package exports the following identifiers: `toString`.
4141
There is no default export.
4242

43-
### `toString(node)`
43+
### `toString(node[, options])`
4444

4545
Get the text content of a [node][] or list of nodes.
4646

47-
The algorithm checks `value` of `node`, then `alt`, and finally `title`.
47+
The algorithm checks `value` of `node` and then `alt`.
4848
If no value is found, the algorithm checks the children of `node` and joins them
4949
(without spaces or newlines).
5050

5151
> This is not a markdown to plain-text library.
5252
> Use [`strip-markdown`][strip-markdown] for that.
5353
54+
###### `options.includeImageAlt`
55+
56+
Whether to use `alt` (`boolean`, default: `true`)
57+
5458
## Security
5559

5660
Use of `mdast-util-to-string` does not involve **[hast][]**, user content, or

test.js

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ test('toString', function (t) {
3030
'should *not* prefer `title` over all others'
3131
)
3232

33+
t.equal(
34+
toString({alt: 'bar'}, {includeImageAlt: false}),
35+
'',
36+
'should *not* include `alt` w/ `includeImageAlt: false`'
37+
)
38+
3339
t.equal(
3440
toString({children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]}),
3541
'foobar',

0 commit comments

Comments
 (0)