Skip to content

Commit

Permalink
Add includeHtml option
Browse files Browse the repository at this point in the history
Previously, HTML was always included.
Sometimes you don’t want that.
Set `includeHtml: false` to ignore it.

Related-to: remarkjs/remark-validate-links#73.
  • Loading branch information
wooorm committed Apr 3, 2023
1 parent 5bddb44 commit 862d7ea
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
56 changes: 40 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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)
}

/**
Expand All @@ -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 ''
}

/**
Expand All @@ -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<string>} */
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('')
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 862d7ea

Please sign in to comment.