Skip to content

Commit 862d7ea

Browse files
committed
Add includeHtml option
Previously, HTML was always included. Sometimes you don’t want that. Set `includeHtml: false` to ignore it. Related-to: remarkjs/remark-validate-links#73.
1 parent 5bddb44 commit 862d7ea

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

lib/index.js

+40-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
* Configuration (optional).
66
* @property {boolean | null | undefined} [includeImageAlt=true]
77
* Whether to use `alt` for `image`s.
8+
* @property {boolean | null | undefined} [includeHtml=true]
9+
* Whether to use `value` of HTML.
810
*/
911

12+
/** @type {Options} */
13+
const emptyOptions = {}
14+
1015
/**
1116
* Get the text content of a node or list of nodes.
1217
*
@@ -21,11 +26,15 @@
2126
* Serialized `value`.
2227
*/
2328
export function toString(value, options) {
24-
const includeImageAlt = (options || {}).includeImageAlt
25-
return one(
26-
value,
27-
typeof includeImageAlt === 'boolean' ? includeImageAlt : true
28-
)
29+
const settings = options || emptyOptions
30+
const includeImageAlt =
31+
typeof settings.includeImageAlt === 'boolean'
32+
? settings.includeImageAlt
33+
: true
34+
const includeHtml =
35+
typeof settings.includeHtml === 'boolean' ? settings.includeHtml : true
36+
37+
return one(value, includeImageAlt, includeHtml)
2938
}
3039

3140
/**
@@ -35,18 +44,31 @@ export function toString(value, options) {
3544
* Thing to serialize.
3645
* @param {boolean} includeImageAlt
3746
* Include image `alt`s.
47+
* @param {boolean} includeHtml
48+
* Include HTML.
3849
* @returns {string}
3950
* Serialized node.
4051
*/
41-
function one(value, includeImageAlt) {
42-
return (
43-
(node(value) &&
44-
(('value' in value && value.value) ||
45-
(includeImageAlt && 'alt' in value && value.alt) ||
46-
('children' in value && all(value.children, includeImageAlt)))) ||
47-
(Array.isArray(value) && all(value, includeImageAlt)) ||
48-
''
49-
)
52+
function one(value, includeImageAlt, includeHtml) {
53+
if (node(value)) {
54+
if ('value' in value) {
55+
return value.type === 'html' && !includeHtml ? '' : value.value
56+
}
57+
58+
if (includeImageAlt && 'alt' in value && value.alt) {
59+
return value.alt
60+
}
61+
62+
if ('children' in value) {
63+
return all(value.children, includeImageAlt, includeHtml)
64+
}
65+
}
66+
67+
if (Array.isArray(value)) {
68+
return all(value, includeImageAlt, includeHtml)
69+
}
70+
71+
return ''
5072
}
5173

5274
/**
@@ -56,16 +78,18 @@ function one(value, includeImageAlt) {
5678
* Thing to serialize.
5779
* @param {boolean} includeImageAlt
5880
* Include image `alt`s.
81+
* @param {boolean} includeHtml
82+
* Include HTML.
5983
* @returns {string}
6084
* Serialized nodes.
6185
*/
62-
function all(values, includeImageAlt) {
86+
function all(values, includeImageAlt, includeHtml) {
6387
/** @type {Array<string>} */
6488
const result = []
6589
let index = -1
6690

6791
while (++index < values.length) {
68-
result[index] = one(values[index], includeImageAlt)
92+
result[index] = one(values[index], includeImageAlt, includeHtml)
6993
}
7094

7195
return result.join('')

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Configuration (TypeScript type).
107107

108108
* `includeImageAlt` (`boolean`, default: `true`)
109109
— whether to use `alt` for `image`s
110+
* `includeHtml` (`boolean`, default: `true`)
111+
— whether to use `value` of HTML
110112

111113
## Types
112114

test.js

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ test('toString', () => {
4949
'should *not* include `alt` w/ `includeImageAlt: false`'
5050
)
5151

52+
assert.equal(
53+
toString({type: 'html', value: 'a'}, {includeHtml: false}),
54+
'',
55+
'should *not* include `html` w/ `includeHtml: false`'
56+
)
57+
5258
assert.equal(
5359
toString({children: [{value: 'foo'}, {alt: 'bar'}, {title: 'baz'}]}),
5460
'foobar',

0 commit comments

Comments
 (0)