Skip to content

Commit

Permalink
TypeScript integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic REY committed Dec 19, 2018
1 parent 6f367ef commit ab734bb
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
191 changes: 191 additions & 0 deletions 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<PrismToken | string> | 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<HighlightProps> {
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;
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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": [
Expand Down

0 comments on commit ab734bb

Please sign in to comment.