rehype-element-properties
rehype plugin to replace element properties.
What is this?
This package is a unified (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:
npm install @ganta/rehype-element-properties
Install with Yarn:
yarn add @ganta/rehype-element-properties
Install with pnpm:
pnpm add @ganta/rehype-element-properties
Usage
This package is dual package (CommonJS / ES Modules).
Import as CommonJS:
const rehypeElementProperties = require("@ganta/rehype-element-properties");
Import as ES Modules:
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.
import rehypeElementProperties, { Setting } from "@ganta/rehype-element-properties";
Use as a rehype plugin as follows:
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:
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
.