diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f2812f7 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,191 @@ +declare module "prism-react-renderer" { + import * as React from 'react'; + + type Language = + 'markup' | + 'bash' | + 'clike' | + 'c' | + 'cpp' | + 'css' | + 'javascript' | + 'jsx' | + 'coffeescript' | + 'actionscript' | + 'css-extr' | + 'diff' | + 'docker' | + 'elixir' | + 'erlang' | + 'git' | + 'go' | + 'graphql' | + 'handlebars' | + 'haskell' | + 'java' | + 'json' | + 'latex' | + 'less' | + 'makefile' | + 'markdown' | + 'objectivec' | + 'ocaml' | + 'php' | + 'php-extr' | + 'python' | + 'reason' | + 'ruby' | + 'rust' | + 'sass' | + 'scss' | + 'sql' | + 'stylus' | + 'swift' | + 'typescript' | + 'vim' | + 'yaml'; + + type PrismGrammar = { + [key: string]: any, + } + + type LanguageDict = { + [lang in Language]: PrismGrammar + } + + type PrismLib = { + languages: LanguageDict, + tokenize: (code: string, grammar: PrismGrammar, language: Language) => PrismToken[]|string[], + highlight: (code: string, grammar: PrismGrammar, language: Language) => string, + } + + type PrismThemeEntry = { + color?: string, + backgroundColor?: string, + fontStyle?: "normal" | "italic", + fontWeight?: + "normal" | + "bold" | + "100" | + "200" | + "300" | + "400" | + "500" | + "600" | + "700" | + "800" | + "900", + textDecorationLine?: + "none" | + "underline" | + "line-through" | + "underline line-through", + opacity?: number, + [styleKey: string]: string | number | void, + } + + type PrismTheme = { + plain: PrismThemeEntry, + styles: Array<{ + types: string[], + style: PrismThemeEntry, + languages?: Language[], + }>, + } + + type ThemeDict = { + root: StyleObj, + plain: StyleObj, + [type: string]: StyleObj, + } + + type Token = { + types: string[], + content: string, + empty?: boolean, + } + + type PrismToken = { + type: string, + content: Array | string + } + + type StyleObj = { + [key: string]: string | number | null + } + + type LineInputProps = { + key?: React.Key, + style?: StyleObj, + className?: string, + line: Token[], + [otherProp: string]: any + } + + type LineOutputProps = { + key?: React.Key, + style?: StyleObj, + className: string, + [otherProps: string]: any + } + + type TokenInputProps = { + key?: React.Key, + style?: StyleObj, + className?: string, + token: Token, + [otherProp: string]: any + } + + type TokenOutputProps = { + key?: React.Key, + style?: StyleObj, + className: string, + children: string, + [otherProp: string]: any + } + + type RenderProps = { + tokens: Token[][], + className: string, + style: StyleObj, + getLineProps: (input: LineInputProps) => LineOutputProps, + getTokenProps: (input: TokenInputProps) => TokenOutputProps, + } + + type DefaultProps = { + Prism: PrismLib, + theme: PrismTheme, + } + + interface HighlightProps { + Prism: PrismLib, + theme?: PrismTheme, + language: Language, + code: string, + children: (props: RenderProps) => React.ReactNode, + } + + export default class Highlight extends React.Component { + themeDict: ThemeDict; + getLineProps: (lineInputProps: LineInputProps) => LineOutputProps; + getStyleForToken: (token: Token) => { [inlineStyle: string]: string }; + getTokenProps: (tokenInputPropsL: TokenInputProps) => TokenOutputProps; + } + + export const defaultProps: DefaultProps + + export const Prism: PrismLib; + + export { + Language, + DefaultProps, + PrismTheme, + } +} + +declare module "prism-react-renderer/themes/*" { + import { PrismTheme } from 'prism-react-renderer'; + const theme: PrismTheme; + export default theme; +} diff --git a/package.json b/package.json index ee93aea..eaeec0f 100755 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Renders highlighted Prism output using React", "main": "lib/index.js", "module": "es/index.js", + "types": "./index.d.ts", "license": "MIT", "repository": "git@github.com:FormidableLabs/prism-react-renderer.git", "files": [