-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
761597e
commit 7422fcd
Showing
5 changed files
with
134 additions
and
3 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
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,47 @@ | ||
// Minimum TypeScript Version: 3.4 | ||
|
||
import {Transformer} from 'unified' | ||
import {Prefix, CreateElementLike} from 'hast-to-hyperscript' | ||
|
||
declare namespace rehypeReact { | ||
type FragmentLike<T> = (props: any) => T | null | ||
|
||
type ComponentLike<T> = (props: {[prop: string]: unknown}) => T | ||
|
||
interface Options<H extends CreateElementLike> { | ||
/** | ||
* How to create elements or components. | ||
* You should typically pass `React.createElement` | ||
*/ | ||
createElement: H | ||
|
||
/** | ||
* Create fragments instead of an outer `<div>` if available | ||
* You should typically pass `React.Fragment` | ||
*/ | ||
Fragment?: FragmentLike<ReturnType<H>> | ||
|
||
/** | ||
* Override default elements (such as `<a>`, `<p>`, etcetera) by passing an object mapping tag names to components | ||
*/ | ||
components?: { | ||
[element: string]: ComponentLike<ReturnType<H>> | ||
} | ||
|
||
/** | ||
* React key prefix | ||
* | ||
* @defaultValue 'h-' | ||
*/ | ||
prefix?: Prefix | ||
} | ||
} | ||
|
||
/** | ||
* Rehype plugin to transform to React | ||
*/ | ||
declare function rehypeReact<H extends CreateElementLike>( | ||
options: rehypeReact.Options<H> | ||
): Transformer | ||
|
||
export = rehypeReact |
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,55 @@ | ||
import * as unified from 'unified' | ||
import * as rehypeToReact from 'rehype-react' | ||
import * as React from 'react' | ||
import {h as virtualDomCreateElement} from 'virtual-dom' | ||
import * as hyperscriptCreateElement from 'hyperscript' | ||
import Vue from 'vue' | ||
|
||
// Create element must be provided | ||
unified().use(rehypeToReact) // $ExpectError | ||
unified().use(rehypeToReact, {}) // $ExpectError | ||
|
||
// multiple frameworks providing createElement can be used | ||
unified().use(rehypeToReact, {createElement: React.createElement}) | ||
unified().use(rehypeToReact, {createElement: virtualDomCreateElement}) | ||
unified().use(rehypeToReact, {createElement: hyperscriptCreateElement}) | ||
unified().use(rehypeToReact, {createElement: new Vue().$createElement}) | ||
unified().use(rehypeToReact, { | ||
createElement: (name: number) => name // $ExpectError | ||
}) | ||
|
||
// Prefix, fragment, and components are optional | ||
unified().use(rehypeToReact, { | ||
createElement: React.createElement, | ||
prefix: 'h' | ||
}) | ||
|
||
unified().use(rehypeToReact, { | ||
createElement: React.createElement, | ||
Fragment: React.Fragment | ||
}) | ||
|
||
unified().use(rehypeToReact, { | ||
createElement: React.createElement, | ||
components: { | ||
a: () => <a>example</a> | ||
} | ||
}) | ||
|
||
unified().use(rehypeToReact, { | ||
createElement: React.createElement, | ||
components: { | ||
div: () => <>example</> | ||
} | ||
}) | ||
|
||
// Mismatch between framework of createElement and components or Fragment should fail | ||
unified().use(rehypeToReact, { | ||
createElement: virtualDomCreateElement, | ||
// prettier-ignore typescript versions can disagree where error happens, squish statement so they agree | ||
components: {a: () => <a>example</a>} // $ExpectError | ||
}) | ||
unified().use(rehypeToReact, { | ||
createElement: virtualDomCreateElement, | ||
Fragment: () => <a>example</a> // $ExpectError | ||
}) |
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,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2015", "DOM"], | ||
"strict": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"rehype-react": ["index.d.ts"] | ||
}, | ||
"jsx": "react" | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"semicolon": false, | ||
"whitespace": false, | ||
"no-unnecessary-generics": false | ||
} | ||
} |