Skip to content

Commit

Permalink
types: add typescript typings for @mdx-js/mdx, @mdx-js/react, @mdx-js…
Browse files Browse the repository at this point in the history
…/runtime, and @mdx-js/vue (#1079)
  • Loading branch information
ChristianMurphy authored May 20, 2020
1 parent 08c809a commit 4bf052c
Show file tree
Hide file tree
Showing 30 changed files with 804 additions and 59 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"publish": "lerna publish --force-publish=\"*\"",
"publish-ci": "lerna publish -y --canary --preid ci --pre-dist-tag ci",
"publish-next": "lerna publish --force-publish=\"*\" --dist-tag next --preid rc",
"test": "jest --runInBand --detectOpenHandles && nyc --reporter lcov tape packages/remark-mdx/test/index.js"
"test": "yarn test-jest && yarn test-types",
"test-jest": "jest --runInBand --detectOpenHandles && nyc --reporter lcov tape packages/remark-mdx/test/index.js",
"test-types": "lerna run test-types"
},
"devDependencies": {
"@babel/core": "7.9.6",
Expand All @@ -50,6 +52,7 @@
"babel-plugin-macros": "2.8.0",
"babel-plugin-remove-export-keywords": "^1.6.2",
"babel-plugin-transform-vue-jsx": "4.0.1",
"dtslint": "3.6.3",
"eslint": "7.0.0",
"eslint-config-prettier": "6.11.0",
"eslint-config-xo": "0.29.1",
Expand Down Expand Up @@ -118,7 +121,7 @@
"*.{js,jsx,md,mdx}": [
"eslint --cache --fix -f friendly"
],
"*.{css,html,json,yml}": [
"*.{css,html,json,ts,tsx,yml}": [
"prettier --write"
]
},
Expand Down
10 changes: 8 additions & 2 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
"Tim Neutkens <[email protected]>",
"Matija Marohnić <[email protected]>",
"Titus Wormer <[email protected]> (https://wooorm.com)",
"JounQin <[email protected]> (https://www.1stg.me)"
"JounQin <[email protected]> (https://www.1stg.me)",
"Christian Murphy <[email protected]>"
],
"license": "MIT",
"types": "types/index.d.ts",
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"keywords": [
"mdx",
Expand All @@ -31,6 +34,9 @@
"webpack",
"loader"
],
"scripts": {
"test-types": "dtslint types"
},
"dependencies": {
"@mdx-js/mdx": "^2.0.0-next.0",
"@mdx-js/react": "^2.0.0-next.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/loader/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// TypeScript Version: 3.4

import {ComponentType} from 'react'

declare module '*.mdx' {
const MDXComponent: ComponentType
export default MDXComponent
}
13 changes: 13 additions & 0 deletions packages/loader/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["dom", "es6"],
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"jsx": "react",
"paths": {
"@mdx-js/react": ["index.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions packages/loader/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false
}
}
8 changes: 7 additions & 1 deletion packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
"Tim Neutkens <[email protected]>",
"Matija Marohnić <[email protected]>",
"Titus Wormer <[email protected]> (https://wooorm.com)",
"JounQin <[email protected]> (https://www.1stg.me)"
"JounQin <[email protected]> (https://www.1stg.me)",
"Christian Murphy <[email protected]>"
],
"license": "MIT",
"types": "types/index.d.ts",
"files": [
"index.js",
"mdx-ast-to-mdx-hast.js",
"mdx-hast-to-jsx.js",
"types/index.d.ts",
"util.js"
],
"keywords": [
Expand All @@ -32,6 +35,9 @@
"remark",
"mdxast"
],
"scripts": {
"test-types": "dtslint types"
},
"dependencies": {
"@babel/core": "7.9.6",
"@babel/plugin-syntax-jsx": "7.8.3",
Expand Down
63 changes: 63 additions & 0 deletions packages/mdx/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// TypeScript Version: 3.4

import {Plugin, Compiler, Processor} from 'unified'

declare namespace mdx {
interface Options {
/**
* support footnotes
*
* @default true
*/
footnotes?: boolean

/**
* remark plugins to transform markdown content
*
* @default []
*/
remarkPlugins?: Plugin[]

/**
* rehype plugins html content
*
* @default []
*/
rehypePlugins?: Plugin[]

/**
* compilers to customize output
*
* @default []
*/
compilers?: Compiler[]
}

/**
* compile mdx text to jsx text asynchronously
*
* @param mdx content as a text
* @param options transform and compiler options
* @returns jsx text
*/
function sync(mdx: string, options?: Options): string

/**
* Generated an MDX compiler
*
* @param options transform and compiler options
* @returns Unified Processor for MDX
*/
function createMdxAstCompiler(options?: Options): Processor
}

/**
* compile mdx text to jsx text asynchronously
*
* @param mdx content as a text
* @param options transform and compiler options
* @returns jsx text
*/
declare function mdx(mdx: string, options?: mdx.Options): Promise<string>

export = mdx
22 changes: 22 additions & 0 deletions packages/mdx/types/mdx-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as mdx from '@mdx-js/mdx'

mdx('# title') // $ExpectType Promise<string>
mdx('# title', {}) // $ExpectType Promise<string>
mdx('# title', {footnotes: false}) // $ExpectType Promise<string>
mdx('# title', {rehypePlugins: [() => () => ({type: 'test'})]}) // $ExpectType Promise<string>
mdx('# title', {remarkPlugins: [() => () => ({type: 'test'})]}) // $ExpectType Promise<string>
mdx('# title', {compilers: []}) // $ExpectType Promise<string>

mdx.sync('# title') // $ExpectType string
mdx.sync('# title', {}) // $ExpectType string
mdx.sync('# title', {footnotes: false}) // $ExpectType string
mdx.sync('# title', {rehypePlugins: [() => () => ({type: 'test'})]}) // $ExpectType string
mdx.sync('# title', {remarkPlugins: [() => () => ({type: 'test'})]}) // $ExpectType string
mdx.sync('# title', {compilers: []}) // $ExpectType string

mdx.createMdxAstCompiler() // $ExpectType Processor<Settings>
mdx.createMdxAstCompiler({}) // $ExpectType Processor<Settings>
mdx.createMdxAstCompiler({footnotes: false}) // $ExpectType Processor<Settings>
mdx.createMdxAstCompiler({rehypePlugins: [() => () => ({type: 'test'})]}) // $ExpectType Processor<Settings>
mdx.createMdxAstCompiler({remarkPlugins: [() => () => ({type: 'test'})]}) // $ExpectType Processor<Settings>
mdx.createMdxAstCompiler({compilers: []}) // $ExpectType Processor<Settings>
13 changes: 13 additions & 0 deletions packages/mdx/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["dom", "es6"],
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"jsx": "react",
"paths": {
"@mdx-js/mdx": ["index.d.ts"]
}
}
}
8 changes: 8 additions & 0 deletions packages/mdx/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false,
"no-redundant-jsdoc": false
}
}
10 changes: 8 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
"Tim Neutkens <[email protected]>",
"Matija Marohnić <[email protected]>",
"Titus Wormer <[email protected]> (https://wooorm.com)",
"JounQin <[email protected]> (https://www.1stg.me)"
"JounQin <[email protected]> (https://www.1stg.me)",
"Christian Murphy <[email protected]>"
],
"license": "MIT",
"main": "dist/cjs.js",
"module": "dist/esm.js",
"types": "types/index.d.ts",
"files": [
"dist"
"dist",
"types/index.d.ts"
],
"keywords": [
"mdx",
Expand All @@ -31,6 +34,9 @@
"remark",
"mdxast"
],
"scripts": {
"test-types": "dtslint types"
},
"peerDependencies": {
"react": "^16.9.0"
}
Expand Down
71 changes: 71 additions & 0 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// TypeScript Version: 3.4

import {
Context,
Consumer,
ComponentType,
FunctionComponent,
ReactElement,
createElement
} from 'react'

/**
* Mapping of names for JSX components to React components
*/
interface ComponentDictionary {
[name: string]: ComponentType<any>
}

/**
* Prop type that includes a component dictionary
*/
interface ComponentsProp {
/**
* Mapping of names for JSX components to React components
*/
components: ComponentDictionary
}

/**
* Direct access to the MDX React Context
*/
declare const MDXContext: Context<ComponentsProp>

/**
* Provider for MDX context
*/
declare const MDXProvider: FunctionComponent<ComponentsProp>

/**
* Gets components from the MDX Context
*
* @param components additional components to include
* @returns all components from context with overrides from components parameter
*/
declare function useMDXComponents(
components: ComponentDictionary | (() => ComponentDictionary)
): ComponentDictionary

/**
* High order component that passes components prop to child
*
* @param child Component being wrapped
*/
declare function withMDXComponents(
child: ComponentType<ComponentsProp>
): ReactElement | null

/**
* React createElement function wrapped with handler for MDX content
*/
declare const mdx: typeof createElement

export {
ComponentDictionary,
ComponentsProp,
MDXContext,
MDXProvider,
useMDXComponents,
withMDXComponents,
mdx
}
35 changes: 35 additions & 0 deletions packages/react/types/mdx-react-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react'
import {
MDXProvider,
useMDXComponents,
withMDXComponents,
ComponentsProp,
MDXContext,
mdx
} from '@mdx-js/react'

const H1 = ({children}: {children: React.ComponentType}) => <h1>{children}</h1>

const MDXProvideExample = () => (
<MDXProvider components={{h1: H1}}>
<h1>Hello, world!</h1>
</MDXProvider>
)

const WithMDXComponentsExample = () =>
withMDXComponents(({components}: ComponentsProp) => {
components // $ExpectType ComponentDictionary
return <div />
})

const UseMDXComponentsExample = () => {
useMDXComponents({h1: H1}) // $ExpectType ComponentDictionary
useMDXComponents(() => ({h1: H1})) // $ExpectType ComponentDictionary
}

const UseMDXContextExample = () => {
const {components} = React.useContext(MDXContext)
components // $ExpectType ComponentDictionary
}

const MDXCreateElementExample = () => mdx('mdx', {title: 'example'}, [])
13 changes: 13 additions & 0 deletions packages/react/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["dom", "es6"],
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"jsx": "react",
"paths": {
"@mdx-js/react": ["index.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions packages/react/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false
}
}
Loading

0 comments on commit 4bf052c

Please sign in to comment.