hast-util-to-text
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -8,6 +8,8 @@ /** | ||
* @param {HastNode} node | ||
* @param {Options} [options={}] | ||
* @returns {string} | ||
*/ | ||
export function toText(node: HastNode): string | ||
export type TestFunctionAnything = import('hast-util-is-element').TestFunctionAnything | ||
export function toText(node: HastNode, options?: Options | undefined): string | ||
export type TestFunctionAnything = | ||
import('hast-util-is-element').TestFunctionAnything | ||
export type HastChild = import('hast').Parent['children'][number] | ||
@@ -25,4 +27,4 @@ export type HastText = import('hast').Text | ||
export type BreakForce = '\n' | ||
export type BreakBefore = BreakValue | BreakNumber | ||
export type BreakAfter = BreakValue | BreakNumber | BreakForce | ||
export type BreakBefore = BreakValue | BreakNumber | undefined | ||
export type BreakAfter = BreakValue | BreakNumber | BreakForce | undefined | ||
export type CollectionOptions = { | ||
@@ -33,1 +35,10 @@ whitespace: Whitespace | ||
} | ||
/** | ||
* Configuration. | ||
*/ | ||
export type Options = { | ||
/** | ||
* Initial CSS whitespace setting to use. | ||
*/ | ||
whitespace?: Whitespace | undefined | ||
} |
109
index.js
@@ -16,4 +16,4 @@ /** | ||
* @typedef {'\n'} BreakForce | ||
* @typedef {BreakValue|BreakNumber} BreakBefore | ||
* @typedef {BreakValue|BreakNumber|BreakForce} BreakAfter | ||
* @typedef {BreakValue|BreakNumber|undefined} BreakBefore | ||
* @typedef {BreakValue|BreakNumber|BreakForce|undefined} BreakAfter | ||
* | ||
@@ -24,19 +24,23 @@ * @typedef CollectionOptions | ||
* @property {BreakAfter} breakAfter | ||
* | ||
* @typedef Options | ||
* Configuration. | ||
* @property {Whitespace} [whitespace='normal'] | ||
* Initial CSS whitespace setting to use. | ||
*/ | ||
import repeat from 'repeat-string' | ||
import {convertElement} from 'hast-util-is-element' | ||
import {findAfter} from 'unist-util-find-after' | ||
var searchLineFeeds = /\n/g | ||
var searchTabOrSpaces = /[\t ]+/g | ||
const searchLineFeeds = /\n/g | ||
const searchTabOrSpaces = /[\t ]+/g | ||
var br = convertElement('br') | ||
var p = convertElement('p') | ||
var cell = convertElement(['th', 'td']) | ||
var row = convertElement('tr') | ||
const br = convertElement('br') | ||
const p = convertElement('p') | ||
const cell = convertElement(['th', 'td']) | ||
const row = convertElement('tr') | ||
// Note that we don’t need to include void elements here as they don’t have text. | ||
// See: <https://github.com/wooorm/html-void-elements> | ||
var notRendered = convertElement([ | ||
const notRendered = convertElement([ | ||
// List from: <https://html.spec.whatwg.org/#hidden-elements> | ||
@@ -60,3 +64,3 @@ 'datalist', | ||
// See: <https://html.spec.whatwg.org/#the-css-user-agent-style-sheet-and-presentational-hints> | ||
var blockOrCaption = convertElement([ | ||
const blockOrCaption = convertElement([ | ||
'address', // Flow content | ||
@@ -110,23 +114,22 @@ 'article', // Sections and headings | ||
* @param {HastNode} node | ||
* @param {Options} [options={}] | ||
* @returns {string} | ||
*/ | ||
export function toText(node) { | ||
export function toText(node, options = {}) { | ||
/** @type {Array.<HastChild>} */ | ||
// @ts-ignore looks like a parent. | ||
var children = node.children || [] | ||
var block = blockOrCaption(node) | ||
var whitespace = inferWhitespace(node, { | ||
whitespace: 'normal', | ||
const children = node.children || [] | ||
const block = blockOrCaption(node) | ||
const whitespace = inferWhitespace(node, { | ||
whitespace: options.whitespace || 'normal', | ||
breakBefore: false, | ||
breakAfter: false | ||
}) | ||
var index = -1 | ||
let index = -1 | ||
/** @type {Array.<string|BreakNumber>} */ | ||
var results | ||
/** @type {Array.<string>} */ | ||
var result | ||
let results | ||
/** @type {string|BreakNumber} */ | ||
var value | ||
/** @type {number} */ | ||
var count | ||
let value | ||
/** @type {number|undefined} */ | ||
let count | ||
@@ -184,3 +187,4 @@ // Treat `text` and `comment` as having normal white-space. | ||
index = -1 | ||
result = [] | ||
/** @type {Array.<string>} */ | ||
const result = [] | ||
@@ -193,3 +197,3 @@ while (++index < results.length) { | ||
} else if (value) { | ||
if (count) result.push(repeat('\n', count)) | ||
if (count) result.push('\n'.repeat(count)) | ||
count = 0 | ||
@@ -237,11 +241,11 @@ result.push(value) | ||
// First we infer the `white-space` property. | ||
var whitespace = inferWhitespace(node, options) | ||
var children = node.children || [] | ||
var index = -1 | ||
const whitespace = inferWhitespace(node, options) | ||
const children = node.children || [] | ||
let index = -1 | ||
/** @type {Array.<string|BreakNumber>} */ | ||
var items = [] | ||
/** @type {BreakNumber} */ | ||
var prefix | ||
/** @type {BreakNumber|BreakForce} */ | ||
var suffix | ||
let items = [] | ||
/** @type {BreakNumber|undefined} */ | ||
let prefix | ||
/** @type {BreakNumber|BreakForce|undefined} */ | ||
let suffix | ||
@@ -354,15 +358,15 @@ // We’re ignoring point 3, and exiting without any content here, because we | ||
function collectText(node, options) { | ||
var value = String(node.value) | ||
const value = String(node.value) | ||
/** @type {Array.<string>} */ | ||
var lines = [] | ||
const lines = [] | ||
/** @type {Array.<string>} */ | ||
var result = [] | ||
var start = 0 | ||
var index = -1 | ||
/** @type {RegExpMatchArray} */ | ||
var match | ||
const result = [] | ||
let start = 0 | ||
let index = -1 | ||
/** @type {RegExpMatchArray|null} */ | ||
let match | ||
/** @type {number} */ | ||
var end | ||
/** @type {string} */ | ||
var join | ||
let end | ||
/** @type {string|undefined} */ | ||
let join | ||
@@ -372,2 +376,3 @@ while (start < value.length) { | ||
match = searchLineFeeds.exec(value) | ||
// @ts-expect-error: `index` is set. | ||
end = match ? match.index : value.length | ||
@@ -403,5 +408,5 @@ | ||
if ( | ||
lines[index].charCodeAt(lines[index].length - 1) === 0x200b /* ZWSP */ || | ||
lines[index].charCodeAt(lines[index].length - 1) === 0x20_0b /* ZWSP */ || | ||
(index < lines.length - 1 && | ||
lines[index + 1].charCodeAt(0) === 0x200b) /* ZWSP */ | ||
lines[index + 1].charCodeAt(0) === 0x20_0b) /* ZWSP */ | ||
) { | ||
@@ -461,8 +466,8 @@ result.push(lines[index]) | ||
/** @type {Array.<string>} */ | ||
var result = [] | ||
var start = 0 | ||
/** @type {RegExpMatchArray} */ | ||
var match | ||
const result = [] | ||
let start = 0 | ||
/** @type {RegExpMatchArray|null} */ | ||
let match | ||
/** @type {number} */ | ||
var end | ||
let end | ||
@@ -472,2 +477,3 @@ while (start < value.length) { | ||
match = searchTabOrSpaces.exec(value) | ||
// @ts-expect-error: `index` is set. | ||
end = match ? match.index : value.length | ||
@@ -491,2 +497,3 @@ | ||
// into a space. | ||
// @ts-expect-error: `end` is defined. | ||
if (start !== end && !breakAfter) { | ||
@@ -508,3 +515,3 @@ result.push('') | ||
/** @type {HastProperties} */ | ||
var props | ||
let props | ||
@@ -511,0 +518,0 @@ if (node.type === 'element') { |
{ | ||
"name": "hast-util-to-text", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "hast utility to get the plain-text value of a node according to the `innerText` algorithm", | ||
@@ -38,5 +38,3 @@ "license": "MIT", | ||
"@types/hast": "^2.0.0", | ||
"@types/repeat-string": "^1.0.0", | ||
"hast-util-is-element": "^2.0.0", | ||
"repeat-string": "^1.0.0", | ||
"unist-util-find-after": "^4.0.0" | ||
@@ -56,3 +54,3 @@ }, | ||
"unist-builder": "^3.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.42.0" | ||
}, | ||
@@ -76,7 +74,3 @@ "scripts": { | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -83,0 +77,0 @@ "remarkConfig": { |
@@ -38,3 +38,3 @@ # hast-util-to-text | ||
var tree = h('div', [ | ||
const tree = h('div', [ | ||
h('h1', {hidden: true}, 'Alpha.'), | ||
@@ -64,3 +64,3 @@ h('article', [ | ||
### `toText(node)` | ||
### `toText(node, options?)` | ||
@@ -75,5 +75,5 @@ Utility to get the plain-text value of a [*node*][node]. | ||
###### Parameters | ||
###### `options.whitespace` | ||
* `node` ([`Node`][node]) — Thing to stringify | ||
Default whitespace setting to use (`'normal'` or `'pre'`, default: `'normal'`). | ||
@@ -80,0 +80,0 @@ ###### Returns |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27074
3
524
- Removed@types/repeat-string@^1.0.0
- Removedrepeat-string@^1.0.0
- Removed@types/repeat-string@1.6.5(transitive)
- Removedrepeat-string@1.6.1(transitive)