Skip to content

Commit 36eb69e

Browse files
add typescript support
1 parent 7940f10 commit 36eb69e

File tree

7 files changed

+369
-835
lines changed

7 files changed

+369
-835
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [3.0.0] - 2021-05-18
6+
7+
### Added
8+
9+
- Typescript support.
10+
11+
### 🔥💥 Breaking Changes
12+
13+
- There is no need to import `defaults` to extend/change default theme anymore. `chakra-ui-markdown-renderer` will merge default and your theme together automatically. If you have `...defaults` in your theme, you may delete it safely.
14+
15+
```jsx
16+
import ChakraUIRenderer from 'chakra-ui-markdown-renderer';
17+
18+
const newTheme = {
19+
p: props => {
20+
const { children } = props;
21+
return <Text mb={2} fontSize={'12px'}>{children}</Text>;
22+
},
23+
}
24+
25+
<ReactMarkdown
26+
components={ChakraUIRenderer(newTheme)}
27+
children={markdown}
28+
escapeHtml={false}
29+
/>;
30+
```
31+
532
## [2.0.0] - 2021-05-14
633

734
### 🔥💥 Breaking Changes

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<img src="https://img.shields.io/npm/v/chakra-ui-markdown-renderer?color=green&style=flat-square" />
55
</a>
66

7-
This package is created for people who using [Chakra-UI's](https://chakra-ui.com/) and [react-markdown](https://github.com/rexxars/react-markdown) together.
7+
This package is created for people who using [Chakra-UI's](https://chakra-ui.com/) and [react-markdown](https://github.com/remarkjs/react-markdown) together.
88

99
### The Problem
1010

@@ -38,11 +38,12 @@ import ChakraUIRenderer from 'chakra-ui-markdown-renderer';
3838

3939
### Extending Defaults
4040

41+
`chakra-ui-markdown-renderer` is merging standart theme with your theme by default.
42+
4143
```jsx
42-
import ChakraUIRenderer, { defaults } from 'chakra-ui-markdown-renderer';
44+
import ChakraUIRenderer from 'chakra-ui-markdown-renderer';
4345

4446
const newTheme = {
45-
...defaults,
4647
p: props => {
4748
const { children } = props;
4849
return <Text mb={2} fontSize={'12px'}>{children}</Text>;

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"name": "chakra-ui-markdown-renderer",
33
"main": "dist/index.cjs.js",
44
"module": "dist/index.esm.js",
5-
"source": "src/index.js",
6-
"version": "2.0.0",
5+
"source": "src/index.tsx",
6+
"version": "3.0.0",
77
"private": false,
8+
"types": "./dist/index.d.ts",
89
"license": "MIT",
910
"files": [
1011
"dist",
@@ -22,19 +23,26 @@
2223
"emotion-theming": "^10.0.27",
2324
"framer-motion": "^2.9.5",
2425
"react": ">= 16.8",
25-
"react-dom": ">= 16.8"
26+
"react-dom": ">= 16.8",
27+
"react-markdown": "^6.0.2"
2628
},
2729
"devDependencies": {
2830
"@babel/cli": "^7.13.16",
2931
"@babel/core": "^7.14.2",
3032
"@babel/preset-env": "^7.14.2",
3133
"@babel/preset-react": "^7.13.13",
3234
"@rollup/plugin-babel": "^5.3.0",
35+
"@types/deepmerge": "^2.2.0",
36+
"@types/react": "^17.0.5",
3337
"cross-env": "^7.0.0",
38+
"install-peers-cli": "^2.2.0",
3439
"rollup": "^2.47.0",
40+
"rollup-plugin-copy": "^3.4.0",
3541
"rollup-plugin-delete": "^2.0.0",
3642
"rollup-plugin-peer-deps-external": "^2.2.4",
37-
"rollup-plugin-terser": "^7.0.2"
43+
"rollup-plugin-terser": "^7.0.2",
44+
"rollup-plugin-typescript2": "^0.30.0",
45+
"typescript": "^4.2.4"
3846
},
3947
"scripts": {
4048
"build": "rollup -c",

rollup.config.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@ import external from 'rollup-plugin-peer-deps-external';
33
import del from 'rollup-plugin-delete';
44
import { terser } from 'rollup-plugin-terser';
55
import pkg from './package.json';
6+
import typescript from 'rollup-plugin-typescript2';
67

7-
export default {
8-
input: pkg.source,
9-
output: [
10-
{ file: pkg.main, format: 'cjs' },
11-
{ file: pkg.module, format: 'esm' },
12-
],
13-
plugins: [
14-
external(),
15-
terser(),
16-
babel({
17-
exclude: 'node_modules/**',
18-
}),
19-
del({ targets: ['dist/*'] }),
20-
],
21-
external: Object.keys(pkg.peerDependencies || {}),
22-
};
8+
export default [
9+
{
10+
input: pkg.source,
11+
output: [
12+
{ file: pkg.main, format: 'cjs', exports: 'named' },
13+
{ file: pkg.module, format: 'esm', exports: 'named' },
14+
],
15+
plugins: [
16+
del({ targets: ['dist/*'] }),
17+
terser(),
18+
external(),
19+
babel({
20+
exclude: 'node_modules/**',
21+
}),
22+
typescript({
23+
rollupCommonJSResolveHack: false,
24+
clean: true,
25+
}),
26+
],
27+
external: Object.keys(pkg.peerDependencies || {}),
28+
},
29+
];

src/index.js renamed to src/index.tsx

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import * as React from 'react';
22
import {
33
Text,
44
Code,
@@ -17,14 +17,25 @@ import {
1717
Th,
1818
Td,
1919
} from '@chakra-ui/react';
20+
import deepmerge from 'deepmerge';
21+
import { Components } from 'react-markdown/src/ast-to-react';
2022

21-
function getCoreProps(props) {
23+
type GetCoreProps = {
24+
children?: React.ReactNode;
25+
'data-sourcepos'?: any;
26+
};
27+
28+
function getCoreProps(props: GetCoreProps): any {
2229
return props['data-sourcepos']
2330
? { 'data-sourcepos': props['data-sourcepos'] }
2431
: {};
2532
}
2633

27-
export const defaults = {
34+
interface Defaults extends Components {
35+
heading?: Components['h1'];
36+
}
37+
38+
export const defaults: Defaults = {
2839
p: props => {
2940
const { children } = props;
3041
return <Text mb={2}>{children}</Text>;
@@ -48,7 +59,16 @@ export const defaults = {
4859
return <Code p={2} children={children} />;
4960
}
5061

51-
return <Code className={className} whiteSpace="break-spaces" d="block" w="full" p={2} children={children} />;
62+
return (
63+
<Code
64+
className={className}
65+
whiteSpace="break-spaces"
66+
d="block"
67+
w="full"
68+
p={2}
69+
children={children}
70+
/>
71+
);
5272
},
5373
del: props => {
5474
const { children } = props;
@@ -59,18 +79,13 @@ export const defaults = {
5979
},
6080
a: Link,
6181
img: Image,
62-
linkReference: Link,
63-
imageReference: Image,
6482
text: props => {
6583
const { children } = props;
6684
return <Text as="span">{children}</Text>;
6785
},
6886
ul: props => {
69-
const { start, ordered, children, depth } = props;
87+
const { ordered, children, depth } = props;
7088
const attrs = getCoreProps(props);
71-
if (start !== null && start !== 1 && start !== undefined) {
72-
attrs.start = start.toString();
73-
}
7489
let Element = UnorderedList;
7590
let styleType = 'disc';
7691
if (ordered) {
@@ -91,11 +106,8 @@ export const defaults = {
91106
);
92107
},
93108
ol: props => {
94-
const { start, ordered, children, depth } = props;
109+
const { ordered, children, depth } = props;
95110
const attrs = getCoreProps(props);
96-
if (start !== null && start !== 1 && start !== undefined) {
97-
attrs.start = start.toString();
98-
}
99111
let Element = UnorderedList;
100112
let styleType = 'disc';
101113
if (ordered) {
@@ -160,34 +172,40 @@ export const defaults = {
160172
th: Th,
161173
};
162174

163-
function ChakraUIRenderer(theme = defaults) {
164-
return {
165-
p: theme.p,
166-
em: theme.em,
167-
blockquote: theme.blockquote,
168-
code: theme.code,
169-
del: theme.del,
170-
hr: theme.hr,
171-
a: theme.a,
172-
img: theme.img,
173-
text: theme.text,
174-
ul: theme.ul,
175-
ol: theme.ol,
176-
li: theme.li,
177-
h1: theme.heading,
178-
h2: theme.heading,
179-
h3: theme.heading,
180-
h4: theme.heading,
181-
h5: theme.heading,
182-
h6: theme.heading,
183-
pre: theme.pre,
184-
table: theme.table,
185-
thead: theme.thead,
186-
tbody: theme.tbody,
187-
tr: theme.tr,
188-
td: theme.td,
189-
th: theme.th,
175+
function ChakraUIRenderer(theme?: Defaults, merge = true): Components {
176+
const elements = {
177+
p: defaults.p,
178+
em: defaults.em,
179+
blockquote: defaults.blockquote,
180+
code: defaults.code,
181+
del: defaults.del,
182+
hr: defaults.hr,
183+
a: defaults.a,
184+
img: defaults.img,
185+
text: defaults.text,
186+
ul: defaults.ul,
187+
ol: defaults.ol,
188+
li: defaults.li,
189+
h1: defaults.heading,
190+
h2: defaults.heading,
191+
h3: defaults.heading,
192+
h4: defaults.heading,
193+
h5: defaults.heading,
194+
h6: defaults.heading,
195+
pre: defaults.pre,
196+
table: defaults.table,
197+
thead: defaults.thead,
198+
tbody: defaults.tbody,
199+
tr: defaults.tr,
200+
td: defaults.td,
201+
th: defaults.th,
190202
};
203+
204+
if (theme && merge) {
205+
return deepmerge(elements, theme);
206+
}
207+
208+
return elements;
191209
}
192210

193211
export default ChakraUIRenderer;

tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"target": "es5",
5+
"lib": ["dom", "dom.iterable", "esnext"],
6+
"allowJs": true,
7+
"skipLibCheck": true,
8+
"esModuleInterop": true,
9+
"allowSyntheticDefaultImports": true,
10+
"strict": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"noFallthroughCasesInSwitch": true,
13+
"module": "esnext",
14+
"moduleResolution": "node",
15+
"resolveJsonModule": true,
16+
"isolatedModules": true,
17+
"noEmit": true,
18+
"jsx": "react-jsx",
19+
"suppressImplicitAnyIndexErrors": true
20+
},
21+
"exclude": ["node_modules", "./**/*.js"],
22+
"include": ["src"]
23+
}

0 commit comments

Comments
 (0)