From 74f96458fa294db897674f88149af49e95d84770 Mon Sep 17 00:00:00 2001 From: ben519 Date: Fri, 22 Sep 2023 17:50:04 -0500 Subject: [PATCH 1/4] reference the default schema and provide example for overriding --- readme.md | 125 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 110 insertions(+), 15 deletions(-) diff --git a/readme.md b/readme.md index c3a7d44..84108f2 100644 --- a/readme.md +++ b/readme.md @@ -12,20 +12,36 @@ ## Contents -* [What is this?](#what-is-this) -* [When should I use this?](#when-should-i-use-this) -* [Install](#install) -* [Use](#use) -* [API](#api) - * [`defaultSchema`](#defaultschema) - * [`sanitize(tree[, options])`](#sanitizetree-options) - * [`Schema`](#schema) -* [Types](#types) -* [Compatibility](#compatibility) -* [Security](#security) -* [Related](#related) -* [Contribute](#contribute) -* [License](#license) +- [hast-util-sanitize](#hast-util-sanitize) + - [Contents](#contents) + - [What is this?](#what-is-this) + - [When should I use this?](#when-should-i-use-this) + - [Install](#install) + - [Use](#use) + - [API](#api) + - [`defaultSchema`](#defaultschema) + - [Overriding the default schema](#overriding-the-default-schema) + - [`sanitize(tree[, options])`](#sanitizetree-options) + - [Parameters](#parameters) + - [Returns](#returns) + - [`Schema`](#schema) + - [Fields](#fields) + - [`allowComments`](#allowcomments) + - [`allowDoctypes`](#allowdoctypes) + - [`ancestors`](#ancestors) + - [`attributes`](#attributes) + - [`clobber`](#clobber) + - [`clobberPrefix`](#clobberprefix) + - [`protocols`](#protocols) + - [`required`](#required) + - [`strip`](#strip) + - [`tagNames`](#tagnames) + - [Types](#types) + - [Compatibility](#compatibility) + - [Security](#security) + - [Related](#related) + - [Contribute](#contribute) + - [License](#license) ## What is this? @@ -124,7 +140,86 @@ There is no default export. Default schema ([`Schema`][api-schema]). -Follows [GitHub][] style sanitation. +Follows [GitHub][] style sanitation. + +#### Overriding the default schema + +In some cases, it may be necessary to override the default schema. In the example below, notice how the `id` attribute of the `

` is removed, while the `

` tag is unaltered. + +```js +import rehypeParse from "rehype-parse" +import rehypeSanitize from "rehype-sanitize" +import rehypeStringify from "rehype-stringify" +import { unified } from "unified" + +const file = await unified() + .use(rehypeParse) + .use(rehypeSanitize) + .use(rehypeStringify) + .process( + `

I'm just a poor boy

+

From a poor family

` + ) + +console.log(String(file)) + +// === Output ======== +//

I'm just a poor boy

+//

From a poor family

+``` + +This happens because the [default schema](https://github.com/syntax-tree/hast-util-sanitize/blob/main/lib/schema.js) only allows `id` equal to `'footnote-label'` for `

` tags. + +```js +// lib/schema.js + +export const defaultSchema = { + ancestors: {...}, + attributes: { + ... + h2: [ + ['id', 'footnote-label'], + ['className', 'sr-only'], + ], + ... + } +} +``` + +To get around this, override the default schema. + +```js +import { defaultSchema } from "hast-util-sanitize" +import rehypeParse from "rehype-parse" +import rehypeSanitize from "rehype-sanitize" +import rehypeStringify from "rehype-stringify" +import { unified } from "unified" + +// Copy the default schema +const schema = { ...defaultSchema } + +// Give

tags permission to have an id named "rhapsody" +schema["attributes"]["h2"] = ["id", "rhapsody"] + +// Inpsect the final schema +console.log(schema) + +const file = await unified() + .use(rehypeParse) + .use(rehypeSanitize, schema) + .use(rehypeStringify) + .process( + `

I'm just a poor boy

+

From a poor family

` + ) + +console.log(String(file)) + +// === Output ======== +//

I'm just a poor boy

+//

From a poor family

+``` + ### `sanitize(tree[, options])` From c37cb99d6a66a53daa50d09268720b6785611d5c Mon Sep 17 00:00:00 2001 From: ben519 Date: Fri, 22 Sep 2023 17:52:06 -0500 Subject: [PATCH 2/4] tweak wording --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 84108f2..8f884b9 100644 --- a/readme.md +++ b/readme.md @@ -144,7 +144,7 @@ Follows [GitHub][] style sanitation. #### Overriding the default schema -In some cases, it may be necessary to override the default schema. In the example below, notice how the `id` attribute of the `

` is removed, while the `

` tag is unaltered. +In some cases, it may be necessary to override the default schema. In the example below, notice how the `id` attribute of the `

` tag is removed, while the `

` tag is unaltered. ```js import rehypeParse from "rehype-parse" From 2afa8e9bd5ce0cc3176edd01500dc4c3ce0d9beb Mon Sep 17 00:00:00 2001 From: ben519 Date: Fri, 22 Sep 2023 18:16:56 -0500 Subject: [PATCH 3/4] fix format errors --- readme.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 8f884b9..51e224f 100644 --- a/readme.md +++ b/readme.md @@ -144,7 +144,8 @@ Follows [GitHub][] style sanitation. #### Overriding the default schema -In some cases, it may be necessary to override the default schema. In the example below, notice how the `id` attribute of the `

` tag is removed, while the `

` tag is unaltered. +In some cases, it may be necessary to override the default schema. In the example below, +notice how the `id` attribute of the `

` tag is removed, while the `

` tag is unaltered. ```js import rehypeParse from "rehype-parse" @@ -168,7 +169,9 @@ console.log(String(file)) //

From a poor family

``` -This happens because the [default schema](https://github.com/syntax-tree/hast-util-sanitize/blob/main/lib/schema.js) only allows `id` equal to `'footnote-label'` for `

` tags. +This happens because the +[default schema](https://github.com/syntax-tree/hast-util-sanitize/blob/main/lib/schema.js) +only allows `id` equal to `'footnote-label'` for `

` tags. ```js // lib/schema.js @@ -220,7 +223,6 @@ console.log(String(file)) //

From a poor family

``` - ### `sanitize(tree[, options])` Sanitize a tree. From c2cee7a688f265fe0faac0c4b65cb808ed960871 Mon Sep 17 00:00:00 2001 From: ben519 Date: Fri, 22 Sep 2023 18:25:27 -0500 Subject: [PATCH 4/4] fix format errors (attempt 2) --- readme.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 51e224f..cf80efe 100644 --- a/readme.md +++ b/readme.md @@ -144,8 +144,9 @@ Follows [GitHub][] style sanitation. #### Overriding the default schema -In some cases, it may be necessary to override the default schema. In the example below, -notice how the `id` attribute of the `

` tag is removed, while the `

` tag is unaltered. +In some cases, it may be necessary to override the default schema. +In the example below, the `id` attribute of the `

` tag is removed +while the `

` tag is unaltered. ```js import rehypeParse from "rehype-parse"