-
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.
- Loading branch information
Showing
23 changed files
with
1,021 additions
and
291 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
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,16 @@ | ||
// npx ts-node ./src/babel-generator/sample.tsx | ||
const { parse } = require('@babel/parser'); | ||
const generate = require('@babel/generator').default; | ||
|
||
const code = 'class Example {}'; | ||
const ast = parse(code); | ||
|
||
const output = generate( | ||
ast, | ||
{ | ||
/* options */ | ||
}, | ||
code, | ||
); | ||
|
||
console.log(output); // { code: 'class Example {}', map: null, rawMappings: null } |
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,33 @@ | ||
import * as ReactDOM from 'react-dom/server'; | ||
import { converter, mdxToHtml } from '../mdx-sample'; | ||
|
||
const templateText = ` | ||
# Hello World | ||
body message | ||
## h2 title | ||
highlight | ||
`; | ||
|
||
const resultTest = '<div><h1>Hello World</h1><p>body message</p><h2>h2 title</h2><p>highlight</p></div>'; | ||
|
||
// tslint:disable:max-line-length | ||
const expectValue = ` | ||
<MDXTag name="wrapper" components={components}><MDXTag name="h1" components={components}>{\`Hello World\`}</MDXTag> | ||
<MDXTag name="p" components={components}>{\`body message\`}</MDXTag> | ||
<MDXTag name="h2" components={components}>{\`h2 title\`}</MDXTag> | ||
<MDXTag name="p" components={components}>{\`highlight\`}</MDXTag></MDXTag>`; | ||
|
||
test('hello', async () => { | ||
const result = await converter(templateText); | ||
expect(result).toBe(expectValue); | ||
}); | ||
|
||
test('hello2', async () => { | ||
const component = await mdxToHtml(templateText); | ||
const renderResult = ReactDOM.renderToStaticMarkup(component); | ||
expect(renderResult).toEqual(resultTest); | ||
}); |
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 @@ | ||
import * as ReactDOM from 'react-dom/server'; | ||
import { makeComponent } from '../sample'; | ||
|
||
test('renderToStaticMarkup2', () => { | ||
const component = makeComponent('h1', 'hoge', {}); | ||
const result = ReactDOM.renderToStaticMarkup(component); | ||
expect(result).toEqual('<h2 style="color:tomato">hoge</h2>'); | ||
}); |
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,42 @@ | ||
// @ts-ignore | ||
import * as objectRestSpread from '@babel/plugin-proposal-object-rest-spread'; | ||
// @ts-ignore | ||
import * as transformJSX from '@babel/plugin-transform-react-jsx'; | ||
// @ts-ignore | ||
import * as babelStandAlone from '@babel/standalone'; | ||
import * as mdx from '@mdx-js/mdx'; | ||
import { MDXTag } from '@mdx-js/tag'; | ||
import * as React from 'react'; | ||
|
||
process.on('unhandledRejection', console.dir); | ||
|
||
const parse2 = (raw: string): string | null => | ||
babelStandAlone.transform(raw, { | ||
plugins: [transformJSX, objectRestSpread], | ||
}).code; | ||
|
||
export const converter = async (content: string) => { | ||
return await mdx.sync(content, { | ||
skipExport: true, | ||
}); | ||
}; | ||
|
||
export const mdxToHtml = async (content: string) => { | ||
const tmp1 = await converter(content); | ||
const code = parse2(tmp1); | ||
const scope = {}; | ||
const components = {}; | ||
const props = {}; | ||
|
||
const fullScope = { | ||
MDXTag, | ||
components, | ||
props, | ||
...scope, | ||
}; | ||
const keys = Object.keys(fullScope); | ||
const values = keys.map(key => fullScope[key]); | ||
const fn = new Function('React', ...keys, `return ${code}`); | ||
const resultComponent = fn(React, ...values); | ||
return resultComponent; | ||
}; |
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,17 @@ | ||
import { MDXTag } from '@mdx-js/tag'; | ||
import * as React from 'react'; | ||
|
||
const H2 = (props: JSX.IntrinsicElements['h2']) => <h2 style={{ color: 'tomato' }} {...props} />; | ||
|
||
export const makeComponent = <T extends keyof JSX.IntrinsicElements>(tag: T, body: string, props?: JSX.IntrinsicElements[T]) => { | ||
return ( | ||
<MDXTag | ||
name={tag} | ||
props={props} | ||
components={{ | ||
h1: H2, | ||
}} | ||
children={body} | ||
/> | ||
); | ||
}; |
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
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,21 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOMServer from 'react-dom/server'; | ||
import * as renderer from 'react-test-renderer'; | ||
import { convert, TestApp } from '../sample'; | ||
|
||
test('pluginを対象とした単体テスト', () => { | ||
const component = convert('# Hello world'); | ||
expect(component).toEqual('<h1>Hello world</h1>\n'); | ||
}); | ||
|
||
test('Reactに組み込んだ場合のテスト', () => { | ||
const component = renderer.create(<TestApp body="# h1を書き換える" />); | ||
const componentJSON = component.toJSON()!; | ||
const result = componentJSON.props.dangerouslySetInnerHTML.__html; | ||
expect(result).toEqual('<h1>h1を書き換える</h1>\n'); | ||
}); | ||
|
||
test('renderToStaticMarkup', () => { | ||
const result = ReactDOMServer.renderToStaticMarkup(<TestApp body="# h1を書き換える" />); | ||
expect(result).toEqual('<div><h1>h1を書き換える</h1>\n</div>'); | ||
}); |
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,98 @@ | ||
const toHAST = require('mdast-util-to-hast'); | ||
const toSanitize = require('hast-util-sanitize'); | ||
const toH = require('hast-to-hyperscript'); | ||
const tableCellStyle = require('@mapbox/hast-util-table-cell-style'); | ||
import * as React from 'react'; | ||
|
||
const own = {}.hasOwnProperty; | ||
|
||
const TABLE_ELEMENTS = ['table', 'thead', 'tbody', 'tfoot', 'tr']; | ||
|
||
export interface Options { | ||
prefix?: string; | ||
processor?: any; | ||
sanitize?: boolean; | ||
createElement: typeof React.createElement; | ||
toHast?: typeof toHAST; | ||
remarkReactComponents?: { [key: string]: <T>(props: T) => React.ReactElement<T> }; | ||
} | ||
|
||
/** | ||
* Attach a react compiler. | ||
* | ||
* @param {Unified} processor - Instance. | ||
* @param {Object?} [options] | ||
* @param {Object?} [options.sanitize] | ||
* - Sanitation schema. | ||
* @param {Object?} [options.remarkReactComponents] | ||
* - Components. | ||
* @param {string?} [options.prefix] | ||
* - Key prefix. | ||
* @param {Function?} [options.createElement] | ||
* - `h()`. | ||
*/ | ||
export function remarkReact({ | ||
createElement = React.createElement, | ||
sanitize, | ||
toHast = {}, | ||
remarkReactComponents = {}, | ||
prefix = '', | ||
}: Options): any { | ||
const clean = sanitize !== false; | ||
const scheme = sanitize ? sanitize : null; | ||
const toHastOptions = toHast; | ||
|
||
/** | ||
* Wrapper around `createElement` to pass | ||
* components in. | ||
* | ||
* @param {string} name - Element name. | ||
* @param {Object} props - Attributes. | ||
* @return {ReactElement} - React element. | ||
*/ | ||
// @ts-ignore a | ||
function h(name: string, props: object, children: React.ReactNode[]) { | ||
const component = own.call(remarkReactComponents, name) ? remarkReactComponents[name] : name; | ||
|
||
/* | ||
* Currently, a warning is triggered by react for | ||
* *any* white-space in tables. So we remove the | ||
* pretty lines for now: | ||
* https://github.com/facebook/react/pull/7081 | ||
*/ | ||
if (children && typeof component === 'string' && TABLE_ELEMENTS.indexOf(component) !== -1) { | ||
children = children.filter(child => { | ||
return child !== '\n'; | ||
}); | ||
} | ||
|
||
return createElement(component, props, children); | ||
} | ||
|
||
/** | ||
* Compile MDAST to React. | ||
* | ||
* @param {Node} node - MDAST node. | ||
* @return {ReactElement} - React element. | ||
*/ | ||
function compile(node: any) { | ||
console.log(node); | ||
let hast = { | ||
type: 'element', | ||
tagName: 'div', | ||
properties: {}, | ||
children: toHAST(node, toHastOptions).children, | ||
}; | ||
|
||
if (clean) { | ||
hast = toSanitize(hast, scheme); | ||
} | ||
|
||
hast = tableCellStyle(hast); | ||
|
||
return toH(React.createElement, hast, prefix); | ||
} | ||
|
||
// @ts-ignore | ||
this.Compiler = compile; | ||
} |
Oops, something went wrong.