Skip to content

Docs schema #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 113 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please run npm test to format correctly


## What is this?

Expand Down Expand Up @@ -124,7 +140,89 @@ There is no default export.

Default schema ([`Schema`][api-schema]).

Follows [GitHub][] style sanitation.
Follows [GitHub][] style sanitation.

#### Overriding the default schema
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in a separate Example section, see for example https://github.com/rehypejs/rehype-sanitize?tab=readme-ov-file#example


In some cases, it may be necessary to override the default schema.
In the example below, the `id` attribute of the `<h2>` tag is removed
while the `<h1>` 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(
`<h1 id="bohemian">I'm just a poor boy</h1>
<h2 id="rhapsody">From a poor family</h2>`
)

console.log(String(file))

// === Output ========
// <h1 id="user-content-bohemian">I'm just a poor boy</h1>
// <h2>From a poor family</h2>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, if you don’t use this package directly here, perhaps it should go in rehype-sanitize instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this example doesn’t match project style. Save it in an example.js, run npm test, and you’d get it back formatted

```

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 `<h2>` tags.

```js
// lib/schema.js

export const defaultSchema = {
ancestors: {...},
attributes: {
...
h2: [
['id', 'footnote-label'],
['className', 'sr-only'],
],
...
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d like to actually fix this bug. So I’d prefer it if this example was unrelated to h2 and className.

```

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 <h2> 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(
`<h1 id="bohemian">I'm just a poor boy</h1>
<h2 id="rhapsody">From a poor family</h2>`
)

console.log(String(file))

// === Output ========
// <h1 id="user-content-bohemian">I'm just a poor boy</h1>
// <h2>From a poor family</h2>
```

### `sanitize(tree[, options])`

Expand Down