Skip to content

Commit 6e5498f

Browse files
committed
Add export of Raw node, register it
1 parent a1dd758 commit 6e5498f

File tree

10 files changed

+91
-16
lines changed

10 files changed

+91
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
coverage/
55
node_modules/
66
yarn.lock
7+
!/index.d.ts

index.d.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type {Data, Literal} from 'xast'
2+
3+
export type {Options, Quote} from './lib/index.js'
4+
export {toXml} from './lib/index.js'
5+
6+
/**
7+
* Raw.
8+
*/
9+
export interface Raw extends Literal {
10+
/**
11+
* Node type of raw.
12+
*/
13+
type: 'raw'
14+
15+
/**
16+
* Data associated with the xast raw.
17+
*/
18+
data?: RawData | undefined
19+
}
20+
21+
/**
22+
* Info associated with xast raw nodes by the ecosystem.
23+
*/
24+
export interface RawData extends Data {}
25+
26+
// Add nodes to tree.
27+
declare module 'xast' {
28+
interface ElementContentMap {
29+
raw: Raw
30+
}
31+
32+
interface RootContentMap {
33+
raw: Raw
34+
}
35+
}

index.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
/**
2-
* @typedef {import('./lib/index.js').Options} Options
3-
* @typedef {import('./lib/index.js').Quote} Quote
4-
*/
5-
1+
// Note: Types exported from `index.d.ts`.
62
export {toXml} from './lib/index.js'

lib/index.js

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
* @typedef {'"' | "'"} Quote
3232
* XML quotes for attribute values.
3333
*
34-
* @typedef {Literal & {type: 'raw'}} Raw
35-
* To do: improved `Raw` type?
36-
*
37-
*
3834
* @typedef State
3935
* Info passed around about the current state.
4036
* @property {Options} options

lib/raw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('./index.js').Raw} Raw
2+
* @typedef {import('../index.js').Raw} Raw
33
* @typedef {import('./index.js').State} State
44
*/
55

lib/text.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @typedef {import('xast').Text} Text
3-
* @typedef {import('./index.js').Raw} Raw
3+
* @typedef {import('../index.js').Raw} Raw
44
*/
55

66
import {escape} from './util-escape.js'

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@
7575
"strict": true
7676
},
7777
"xo": {
78+
"overrides": [
79+
{
80+
"files": [
81+
"**/*.ts"
82+
],
83+
"rules": {
84+
"@typescript-eslint/consistent-type-definitions": "off"
85+
}
86+
}
87+
],
7888
"prettier": true,
7989
"rules": {
8090
"unicorn/prefer-string-replace-all": "off"

readme.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [`toXml(tree[, options])`](#toxmltree-options)
2121
* [`Options`](#options)
2222
* [`Quote`](#quote-1)
23+
* [`Raw`](#raw)
2324
* [Types](#types)
2425
* [Compatibility](#compatibility)
2526
* [Security](#security)
@@ -181,11 +182,47 @@ XML quotes for attribute values (TypeScript type).
181182
type Quote = '"' | "'"
182183
```
183184
185+
### `Raw`
186+
187+
Raw (TypeScript type).
188+
189+
###### Type
190+
191+
```ts
192+
import type {Data, Literal} from 'xast'
193+
194+
interface Raw extends Literal {
195+
type: 'raw'
196+
data?: RawData | undefined
197+
}
198+
199+
export interface RawData extends Data {}
200+
```
201+
184202
## Types
185203

186204
This package is fully typed with [TypeScript][].
187-
It exports the additional types [`Options`][api-options] and
188-
[`Quote`][api-quote].
205+
It exports the additional types [`Options`][api-options], [`Quote`][api-quote],
206+
and [`Raw`][api-raw].
207+
208+
It also registers the node type with `@types/xast`.
209+
If you’re working with the syntax tree, make sure to import this utility
210+
somewhere in your types, as that registers the new node types in the tree.
211+
212+
```js
213+
/**
214+
* @typedef {import('xast-util-to-xml')}
215+
*/
216+
217+
import {visit} from 'unist-util-visit'
218+
219+
/** @type {import('xast').Root} */
220+
const tree = getXastNodeSomeHow()
221+
222+
visit(tree, function (node) {
223+
// `node` can now be a raw node.
224+
})
225+
```
189226

190227
## Compatibility
191228

@@ -287,3 +324,5 @@ abide by its terms.
287324
[api-options]: #options
288325

289326
[api-quote]: #quote-1
327+
328+
[api-raw]: #raw

test/raw.js

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {toXml} from '../index.js'
66
test('`raw`', async function (t) {
77
await t.test('should encode `raw`s', async function () {
88
assert.deepEqual(
9-
// @ts-expect-error: check how the runtime handles `raw` nodes.
109
toXml(u('raw', '<script>alert("XSS!")</script>')),
1110
'&#x3C;script>alert("XSS!")&#x3C;/script>'
1211
)
@@ -16,7 +15,6 @@ test('`raw`', async function (t) {
1615
'should not encode `raw`s in `allowDangerousXml` mode',
1716
async function () {
1817
assert.deepEqual(
19-
// @ts-expect-error: check how the runtime handles `raw` nodes.
2018
toXml(u('raw', '<script>alert("XSS!")</script>'), {
2119
allowDangerousXml: true
2220
}),

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2020"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js"]
14+
"include": ["**/*.js", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)