diff --git a/lib/index.js b/lib/index.js index d0e7314..7e792e2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,8 +5,13 @@ * Configuration (optional). * @property {boolean | null | undefined} [includeImageAlt=true] * Whether to use `alt` for `image`s. + * @property {boolean | null | undefined} [includeHtml=true] + * Whether to use `value` of HTML. */ +/** @type {Options} */ +const emptyOptions = {} + /** * Get the text content of a node or list of nodes. * @@ -21,11 +26,15 @@ * Serialized `value`. */ export function toString(value, options) { - const includeImageAlt = (options || {}).includeImageAlt - return one( - value, - typeof includeImageAlt === 'boolean' ? includeImageAlt : true - ) + const settings = options || emptyOptions + const includeImageAlt = + typeof settings.includeImageAlt === 'boolean' + ? settings.includeImageAlt + : true + const includeHtml = + typeof settings.includeHtml === 'boolean' ? settings.includeHtml : true + + return one(value, includeImageAlt, includeHtml) } /** @@ -35,18 +44,31 @@ export function toString(value, options) { * Thing to serialize. * @param {boolean} includeImageAlt * Include image `alt`s. + * @param {boolean} includeHtml + * Include HTML. * @returns {string} * Serialized node. */ -function one(value, includeImageAlt) { - return ( - (node(value) && - (('value' in value && value.value) || - (includeImageAlt && 'alt' in value && value.alt) || - ('children' in value && all(value.children, includeImageAlt)))) || - (Array.isArray(value) && all(value, includeImageAlt)) || - '' - ) +function one(value, includeImageAlt, includeHtml) { + if (node(value)) { + if ('value' in value) { + return value.type === 'html' && !includeHtml ? '' : value.value + } + + if (includeImageAlt && 'alt' in value && value.alt) { + return value.alt + } + + if ('children' in value) { + return all(value.children, includeImageAlt, includeHtml) + } + } + + if (Array.isArray(value)) { + return all(value, includeImageAlt, includeHtml) + } + + return '' } /** @@ -56,16 +78,18 @@ function one(value, includeImageAlt) { * Thing to serialize. * @param {boolean} includeImageAlt * Include image `alt`s. + * @param {boolean} includeHtml + * Include HTML. * @returns {string} * Serialized nodes. */ -function all(values, includeImageAlt) { +function all(values, includeImageAlt, includeHtml) { /** @type {Array} */ const result = [] let index = -1 while (++index < values.length) { - result[index] = one(values[index], includeImageAlt) + result[index] = one(values[index], includeImageAlt, includeHtml) } return result.join('') diff --git a/package.json b/package.json index 10edf20..682e504 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mdast-util-to-string", - "version": "3.1.1", + "version": "3.2.0", "description": "mdast utility to get the plain text content of a node", "license": "MIT", "keywords": [ @@ -44,7 +44,7 @@ "remark-preset-wooorm": "^9.0.0", "tape": "^5.0.0", "type-coverage": "^2.0.0", - "typescript": "^4.0.0", + "typescript": "^5.0.0", "xo": "^0.53.0" }, "scripts": { diff --git a/readme.md b/readme.md index 94dac6e..552bc4e 100644 --- a/readme.md +++ b/readme.md @@ -107,6 +107,8 @@ Configuration (TypeScript type). * `includeImageAlt` (`boolean`, default: `true`) — whether to use `alt` for `image`s +* `includeHtml` (`boolean`, default: `true`) + — whether to use `value` of HTML ## Types diff --git a/test.js b/test.js index 1096c35..ee2c9a4 100644 --- a/test.js +++ b/test.js @@ -49,6 +49,12 @@ test('toString', () => { 'should *not* include `alt` w/ `includeImageAlt: false`' ) + assert.equal( + toString({type: 'html', value: 'a'}, {includeHtml: false}), + '', + 'should *not* include `html` w/ `includeHtml: false`' + ) + assert.equal( toString({children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]}), 'foobar',