-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ganta/add-readme
docs: Add README
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# rehype-element-properties | ||
|
||
[![CI](https://github.com/ganta/rehype-element-properties/actions/workflows/ci.yaml/badge.svg)](https://github.com/ganta/rehype-element-properties/actions/workflows/ci.yaml) | ||
|
||
[rehype](https://github.com/rehypejs/rehype) plugin to replace element properties. | ||
|
||
## What is this? | ||
|
||
This package is a [unified](https://github.com/unifiedjs/unified) ([rehype](https://github.com/rehypejs/rehype)) plugin to replace element properties. | ||
You can customize the attributes of HTML elements that cannot be expressed in Markdown notation, and add embedded styles. | ||
|
||
## Install | ||
|
||
Install with [npm](https://docs.npmjs.com/cli): | ||
|
||
```shell | ||
npm install @ganta/rehype-element-properties | ||
``` | ||
|
||
Install with [Yarn](https://yarnpkg.com/): | ||
|
||
```shell | ||
yarn add @ganta/rehype-element-properties | ||
``` | ||
|
||
Install with [pnpm](https://pnpm.io/): | ||
|
||
```shell | ||
pnpm add @ganta/rehype-element-properties | ||
``` | ||
|
||
## Usage | ||
|
||
This package is dual package (CommonJS / ES Modules). | ||
|
||
Import as CommonJS: | ||
|
||
```javascript | ||
const rehypeElementProperties = require("@ganta/rehype-element-properties"); | ||
``` | ||
|
||
Import as ES Modules: | ||
|
||
```javascript | ||
import rehypeElementProperties from "@ganta/rehype-element-properties"; | ||
``` | ||
|
||
This package is fully typed with TypeScript. | ||
It exports `Setting` type, which specifies the interface of the plugin setting. | ||
|
||
```typescript | ||
import rehypeElementProperties, { Setting } from "@ganta/rehype-element-properties"; | ||
``` | ||
|
||
Use as a rehype plugin as follows: | ||
|
||
```typescript | ||
const processor = unified() | ||
.use(parse, { fragment: true }) | ||
.use(rehypeElementProperties, { | ||
img: [ | ||
{ | ||
name: "alt", | ||
replacer: (value, properties) => value || properties?.src | ||
}, | ||
], | ||
}) | ||
``` | ||
|
||
## API | ||
|
||
This package exports no identifiers. | ||
The default export is `rehypeElementProperties`. | ||
|
||
### `unified().use(rehypeElementProperties, setting)` | ||
|
||
#### `setting` | ||
|
||
Plugin setting. | ||
|
||
##### `setting.<tagName>` | ||
|
||
Describes the setting for each element that replaces the property. | ||
The tag name of the element is specified as a key. | ||
|
||
###### `setting.<tagName>[].name` | ||
|
||
Specifies the name of the property to be replaced. | ||
|
||
###### `setting.<tagName>[].replacer` | ||
|
||
Specifies the function that returns the value of the replaced property. | ||
|
||
The value types of a property is as follows: | ||
|
||
```typescript | ||
boolean | number | string | null | undefined | Array<string | number> | ||
``` | ||
|
||
The function takes the value of the current property as an argument. | ||
The first argument is the value of the specified property. | ||
The second argument is the entire property of the specified element. | ||
|
||
If you want to remove a property, return `undefined`. | ||
|
||
## License | ||
|
||
[Apache License 2.0](LICENSE) |