Skip to content

Commit

Permalink
Add includeImageAlt option
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 26, 2021
1 parent 1b5d8b3 commit bc43530
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
29 changes: 23 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
/**
* @typedef Options
* @property {boolean} [includeImageAlt=true]
*/

/**
* Get the text content of a node.
* Prefer the node’s plain-text fields, otherwise serialize its children,
* and if the given value is an array, serialize the nodes in it.
*
* @param {unknown} node
* @param {Options} [options]
* @returns {string}
*/
export function toString(node, options) {
var {includeImageAlt = true} = options || {}
return one(node, includeImageAlt)
}

/**
* @param {unknown} node
* @param {boolean} includeImageAlt
* @returns {string}
*/
export function toString(node) {
function one(node, includeImageAlt) {
return (
(node &&
typeof node === 'object' &&
// @ts-ignore looks like a literal.
(node.value ||
// @ts-ignore looks like an image.
node.alt ||
(includeImageAlt ? node.alt : '') ||
// @ts-ignore looks like a parent.
('children' in node && all(node.children)) ||
(Array.isArray(node) && all(node)))) ||
('children' in node && all(node.children, includeImageAlt)) ||
(Array.isArray(node) && all(node, includeImageAlt)))) ||
''
)
}

/**
* @param {Array.<unknown>} values
* @param {boolean} includeImageAlt
* @returns {string}
*/
function all(values) {
function all(values, includeImageAlt) {
/** @type {Array.<string>} */
var result = []
var index = -1

while (++index < values.length) {
result[index] = toString(values[index])
result[index] = one(values[index], includeImageAlt)
}

return result.join('')
Expand Down
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ console.log(toString(tree)) // => 'Some emphasis, importance, and code.'
This package exports the following identifiers: `toString`.
There is no default export.

### `toString(node)`
### `toString(node[, options])`

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

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

> This is not a markdown to plain-text library.
> Use [`strip-markdown`][strip-markdown] for that.
###### `options.includeImageAlt`

Whether to use `alt` (`boolean`, default: `true`)

## Security

Use of `mdast-util-to-string` does not involve **[hast][]**, user content, or
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ test('toString', function (t) {
'should *not* prefer `title` over all others'
)

t.equal(
toString({alt: 'bar'}, {includeImageAlt: false}),
'',
'should *not* include `alt` w/ `includeImageAlt: false`'
)

t.equal(
toString({children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]}),
'foobar',
Expand Down

0 comments on commit bc43530

Please sign in to comment.