|
| 1 | +# xast-util-to-string |
| 2 | + |
| 3 | +[![Build][build-badge]][build] |
| 4 | +[![Coverage][coverage-badge]][coverage] |
| 5 | +[![Downloads][downloads-badge]][downloads] |
| 6 | +[![Size][size-badge]][size] |
| 7 | +[![Sponsors][sponsors-badge]][collective] |
| 8 | +[![Backers][backers-badge]][collective] |
| 9 | +[![Chat][chat-badge]][chat] |
| 10 | + |
| 11 | +[**xast**][xast] utility to get the plain text value of a [*node*][node]. |
| 12 | + |
| 13 | +This is like the DOMs `Node#textContent` getter but there are some deviations. |
| 14 | +The resulting text is returned. |
| 15 | + |
| 16 | +## Install |
| 17 | + |
| 18 | +[npm][]: |
| 19 | + |
| 20 | +```sh |
| 21 | +npm install xast-util-to-string |
| 22 | +``` |
| 23 | + |
| 24 | +## Use |
| 25 | + |
| 26 | +```js |
| 27 | +var x = require('xastscript') |
| 28 | +var toString = require('xast-util-to-string') |
| 29 | + |
| 30 | +var tree = x( |
| 31 | + 'ncx', |
| 32 | + {xmlns: 'http://www.daisy.org/z3986/2005/ncx/', version: '2005-1'}, |
| 33 | + [ |
| 34 | + x('head', [ |
| 35 | + x('meta', {name: 'dtb:uid', content: 'urn:isbn:9781234567891'}) |
| 36 | + ]), |
| 37 | + x('docTitle', [x('text', 'A Christmas Carol')]), |
| 38 | + x('docAuthor', [x('text', 'Charles Dickens')]) |
| 39 | + ] |
| 40 | +) |
| 41 | + |
| 42 | +console.log(toString(tree)) |
| 43 | +``` |
| 44 | + |
| 45 | +Yields: |
| 46 | + |
| 47 | +```txt |
| 48 | +A Christmas CarolCharles Dickens |
| 49 | +``` |
| 50 | + |
| 51 | +## API |
| 52 | + |
| 53 | +### `toString(node)` |
| 54 | + |
| 55 | +Utility to get the plain text value of a [*node*][node]. |
| 56 | +If the node has a `value` field ([*cdata*][cdata], [*comment*][comment], |
| 57 | +[*doctype*][doctype], [*instruction*][instruction], or [*text*][text]), returns |
| 58 | +it. |
| 59 | +If the node has a `children` field ([*root*][root] or [*element*][element]), |
| 60 | +recurses into it to concatenate all [*text*][text]s. |
| 61 | + |
| 62 | +###### Returns |
| 63 | + |
| 64 | +`string` — Serialized `node`. |
| 65 | + |
| 66 | +## Security |
| 67 | + |
| 68 | +`xast-util-to-string` does not change the syntax tree so there are no openings |
| 69 | +for [cross-site scripting (XSS)][xss] attacks. |
| 70 | + |
| 71 | +## Related |
| 72 | + |
| 73 | +* [`xast-util-to-xml`](https://github.com/syntax-tree/xast-util-to-xml) |
| 74 | + — serialize xast to XML |
| 75 | +* [`hast-util-to-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-to-string) |
| 76 | + — get the plain-text value (`textContent`) |
| 77 | +* [`hast-util-to-text`](https://github.com/syntax-tree/hast-util-to-text) |
| 78 | + — get the plain-text value (`innerText`) |
| 79 | +* [`hast-util-from-text`](https://github.com/syntax-tree/hast-util-from-text) |
| 80 | + — set the plain-text value (`innerText`) |
| 81 | +* [`hast-util-from-string`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-from-string) |
| 82 | + — set the plain-text value (`textContent`) |
| 83 | + |
| 84 | +## Contribute |
| 85 | + |
| 86 | +See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get |
| 87 | +started. |
| 88 | +See [`support.md`][support] for ways to get help. |
| 89 | + |
| 90 | +This project has a [code of conduct][coc]. |
| 91 | +By interacting with this repository, organization, or community you agree to |
| 92 | +abide by its terms. |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +[MIT][license] © [Titus Wormer][author] |
| 97 | + |
| 98 | +<!-- Definitions --> |
| 99 | + |
| 100 | +[build-badge]: https://img.shields.io/travis/syntax-tree/xast-util-to-string.svg |
| 101 | + |
| 102 | +[build]: https://travis-ci.org/syntax-tree/xast-util-to-string |
| 103 | + |
| 104 | +[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/xast-util-to-string.svg |
| 105 | + |
| 106 | +[coverage]: https://codecov.io/github/syntax-tree/xast-util-to-string |
| 107 | + |
| 108 | +[downloads-badge]: https://img.shields.io/npm/dm/xast-util-to-string.svg |
| 109 | + |
| 110 | +[downloads]: https://www.npmjs.com/package/xast-util-to-string |
| 111 | + |
| 112 | +[size-badge]: https://img.shields.io/bundlephobia/minzip/xast-util-to-string.svg |
| 113 | + |
| 114 | +[size]: https://bundlephobia.com/result?p=xast-util-to-string |
| 115 | + |
| 116 | +[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg |
| 117 | + |
| 118 | +[backers-badge]: https://opencollective.com/unified/backers/badge.svg |
| 119 | + |
| 120 | +[collective]: https://opencollective.com/unified |
| 121 | + |
| 122 | +[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg |
| 123 | + |
| 124 | +[chat]: https://github.com/syntax-tree/unist/discussions |
| 125 | + |
| 126 | +[npm]: https://docs.npmjs.com/cli/install |
| 127 | + |
| 128 | +[license]: license |
| 129 | + |
| 130 | +[author]: https://wooorm.com |
| 131 | + |
| 132 | +[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md |
| 133 | + |
| 134 | +[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md |
| 135 | + |
| 136 | +[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md |
| 137 | + |
| 138 | +[xast]: https://github.com/syntax-tree/xast |
| 139 | + |
| 140 | +[node]: https://github.com/syntax-tree/xast#nodes |
| 141 | + |
| 142 | +[root]: https://github.com/syntax-tree/xast#root |
| 143 | + |
| 144 | +[comment]: https://github.com/syntax-tree/xast#comment |
| 145 | + |
| 146 | +[cdata]: https://github.com/syntax-tree/xast#cdata |
| 147 | + |
| 148 | +[doctype]: https://github.com/syntax-tree/xast#doctype |
| 149 | + |
| 150 | +[element]: https://github.com/syntax-tree/xast#element |
| 151 | + |
| 152 | +[instruction]: https://github.com/syntax-tree/xast#instruction |
| 153 | + |
| 154 | +[text]: https://github.com/syntax-tree/xast#text |
| 155 | + |
| 156 | +[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting |
0 commit comments