Skip to content

Commit dbdaf2d

Browse files
committed
feat: add rehypePlugins props (#60)
1 parent c6d38af commit dbdaf2d

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,16 @@ interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HTMLTextA
123123
* Optional padding for code. Default: `10`.
124124
*/
125125
padding?: number;
126+
/**
127+
* rehypePlugins (Array.<Plugin>, default: `[[rehypePrism, { ignoreMissing: true }]]`)
128+
* List of [rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins) to use. See the next section for examples on how to pass options
129+
*/
130+
rehypePlugins?: PluggableList;
126131
/**
127132
* The minimum height of the editor. Default: `16`.
128133
*/
129134
minHeight?: number;
135+
onKeyDown?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void | boolean;
130136
}
131137
```
132138
List of supported languages can be found [here](https://github.com/wooorm/refractor#syntaxes)

src/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
2+
import { PluggableList } from 'unified';
23
import { processHtml, htmlEncode } from './utils';
34
import shortcuts from './shortcuts';
45
import * as styles from './styles';
@@ -16,6 +17,11 @@ export interface TextareaCodeEditorProps extends React.TextareaHTMLAttributes<HT
1617
* Optional padding for code. Default: `10`.
1718
*/
1819
padding?: number;
20+
/**
21+
* rehypePlugins (Array.<Plugin>, default: `[[rehypePrism, { ignoreMissing: true }]]`)
22+
* List of [rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins) to use. See the next section for examples on how to pass options
23+
*/
24+
rehypePlugins?: PluggableList;
1925
/**
2026
* The minimum height of the editor. Default: `16`.
2127
*/
@@ -33,6 +39,7 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
3339
language,
3440
className,
3541
style,
42+
rehypePlugins,
3643
onChange,
3744
...other
3845
} = props;
@@ -55,8 +62,9 @@ export default React.forwardRef<HTMLTextAreaElement, TextareaCodeEditorProps>((p
5562
`<pre aria-hidden=true><code ${language ? `class="language-${language}"` : ''} >${htmlEncode(
5663
String(value || ''),
5764
)}</code><br /></pre>`,
65+
rehypePlugins,
5866
),
59-
[value, language],
67+
[value, language, rehypePlugins],
6068
);
6169
const preView = useMemo(
6270
() => (

src/utils.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1+
import { PluggableList } from 'unified';
12
import { rehype } from 'rehype';
23
// @ts-ignore
34
import rehypePrism from '@mapbox/rehype-prism';
45

5-
export const processHtml = (html: string) => {
6+
export const processHtml = (html: string, plugins: PluggableList = [[rehypePrism, { ignoreMissing: true }]]) => {
67
return rehype()
78
.data('settings', { fragment: true })
8-
.use(rehypePrism, { ignoreMissing: true })
9+
.use([...plugins])
910
.processSync(`${html}`)
1011
.toString();
1112
};
1213

13-
// function htmlEncode(html: string) {
14-
// const temp = document.createElement ('div');
15-
// (temp.textContent !== undefined) ? (temp.textContent = html) : (temp.innerText = html);
16-
// const output = temp.innerHTML;
17-
// return output;
18-
// }
19-
2014
export function htmlEncode(sHtml: string) {
2115
return sHtml
2216
.replace(/```(tsx?|jsx?|html|xml)(.*)\s+([\s\S]*?)(\s.+)?```/g, (str: string) => {

0 commit comments

Comments
 (0)