Skip to content

Commit 6a36560

Browse files
authored
Merge pull request #33 from simonguo/next
Refactor via typescirpt
2 parents 1703322 + 50405ff commit 6a36560

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+9355
-13125
lines changed

.babelrc

-10
This file was deleted.

.editorconfig

-11
This file was deleted.

.eslintrc.js

+25-57
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,39 @@
1-
'use strict';
2-
31
const OFF = 0;
42
const WARNING = 1;
53
const ERROR = 2;
64

75
module.exports = {
8-
extends: 'airbnb',
96
env: {
107
browser: true,
11-
node: true,
12-
es6: true,
13-
mocha: true
14-
},
15-
globals: {
16-
assert: true
17-
},
18-
parser: 'babel-eslint',
19-
plugins: ['react', 'babel'],
20-
parserOptions: {
21-
ecmaVersion: 6,
22-
sourceType: 'module',
23-
ecmaFeatures: {
24-
jsx: true,
25-
experimentalObjectRestSpread: true
26-
}
8+
es6: true
279
},
10+
parser: '@typescript-eslint/parser',
11+
extends: [
12+
'plugin:@typescript-eslint/recommended',
13+
'plugin:react/recommended',
14+
'plugin:react-hooks/recommended',
15+
'prettier'
16+
],
17+
parserOptions: {},
18+
plugins: ['@typescript-eslint', 'react'],
2819
rules: {
29-
indent: [ERROR, 2, { SwitchCase: 1 }], //规定代码的缩进方式:2个空格
30-
camelcase: ERROR, //强制驼峰法命名
31-
curly: ERROR, //必须使用 if(){} 中的{}
32-
eqeqeq: ERROR, //必须使用全等
33-
'brace-style': [ERROR, '1tbs'], //大括号风格
34-
quotes: [ERROR, 'single'], //引号类型
35-
semi: [ERROR, 'always'], //语句强制分号结尾
36-
'space-infix-ops': ERROR, //中缀操作符周围要不要有空格
37-
'no-param-reassign': OFF, //不允许对函数的形参进行赋值
38-
'prefer-spread': ERROR, //首选展开运算
39-
'comma-dangle': OFF, //不允许或强制在对象字面量或者数组属性的结尾使用逗号
40-
'padded-blocks': OFF, //规定代码块前后是否要加空行
41-
'prefer-const': OFF,
20+
semi: [ERROR, 'always'],
21+
'space-infix-ops': ERROR,
22+
'prefer-spread': ERROR,
4223
'no-multi-spaces': ERROR,
43-
'no-var': OFF,
44-
'one-var': OFF,
4524
'class-methods-use-this': WARNING,
46-
'no-unused-expressions': [ERROR, { allowShortCircuit: true }],
4725
'arrow-parens': [ERROR, 'as-needed'],
48-
'no-mixed-operators': OFF,
49-
/**
50-
* https://github.com/airbnb/javascript/tree/master/react
51-
*/
52-
'react/prefer-es6-class': [WARNING, 'always'], //使用 class extends React.Component
53-
'react/jsx-pascal-case': ERROR, //骆驼式命名
54-
'react/jsx-closing-bracket-location': ERROR, //JSX语法缩进/格式
55-
'react/jsx-curly-spacing': ERROR, //JSX {} 引用括号里两边加空格
56-
'react/jsx-boolean-value': [OFF, 'always'], //如果属性值为 true, 可以直接省略
57-
'jsx-quotes': [ERROR, 'prefer-double'], //JSX属性值总是使用双引号(")
58-
'react/no-string-refs': ERROR, //Refs里使用回调函数
59-
'react/jsx-wrap-multilines': ERROR, //多行的JSX标签写在 ()里
60-
'react/self-closing-comp': ERROR, //没有子元素的标签来说总是自己关闭标签
61-
'react/jsx-no-bind': ERROR, //当在 render() 里使用事件处理方法时,提前在构造函数里把 this 绑定上去
62-
'react/no-is-mounted': ERROR, //不要再使用 isMounted
63-
'react/prop-types': [ERROR, { ignore: ['children', 'className', 'style'] }],
64-
'jsx-a11y/href-no-hash': OFF,
65-
'jsx-a11y/label-has-for': OFF,
66-
'react/jsx-filename-extension': OFF,
67-
'react/prefer-stateless-function': OFF,
68-
'react/require-default-props': OFF,
69-
'react/forbid-prop-types': OFF
26+
'@typescript-eslint/no-unused-vars': ERROR,
27+
'@typescript-eslint/no-explicit-any': OFF,
28+
'@typescript-eslint/explicit-function-return-type': OFF,
29+
'@typescript-eslint/explicit-member-accessibility': OFF,
30+
'@typescript-eslint/no-namespace': OFF,
31+
'@typescript-eslint/explicit-module-boundary-types': OFF,
32+
'react/display-name': OFF
33+
},
34+
settings: {
35+
react: {
36+
version: 'detect'
37+
}
7038
}
7139
};

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ node_modules
3838
build
3939
lib
4040

41-
.tern-project
41+
dist
4242
assets
43-
yarn.lock

.prettierrc

-5
This file was deleted.

.prettierrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
printWidth: 100,
3+
tabWidth: 2,
4+
singleQuote: true,
5+
arrowParens: 'avoid',
6+
trailingComma: 'none'
7+
};

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Simon Guo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+76-79
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,110 @@
11
# react-code-view
22

3-
English | [中文版][readm-cn]
4-
5-
## Why use this component?
6-
7-
Let the code in Markdown run in real time, why is there such a need?
8-
9-
In our front-end team, technical-related documents are written in Markdown. There are often a lot of sample code in the documentation. We hope that when you read the documentation, you can run the sample code to see the rendering interface.
10-
11-
## What are the requirements?
12-
13-
- Let the code in Markdown run and preview.
14-
- The code can be edited online.
15-
- Does not affect the layout of the entire document stream.
16-
- Support React, support code highlighting.
17-
- Babel can be configured.
18-
19-
After understanding the requirements, I wrote a React component to satisfy these functions, [`react-code-view`](https://github.com/simonguo/react-code-view) , first look at the preview:
3+
Let the React code in Markdown be rendered to the page, and support online editing.
204

215
![preview](https://user-images.githubusercontent.com/1203827/44707274-a30c0f80-aad6-11e8-8cc5-9cf7daf4d9e2.gif)
226

23-
Preview: https://simonguo.github.io/react-code-view/
24-
25-
## Principle of implementation
26-
27-
- Parse the Markdown document with `markdown-loader` and `html-loader`.
28-
- Take the code out of the regular expression and give it to `codemirror`
29-
- Compile the code in `codemirror` through babel and render it to the specified container via `ReactDOM.render` .
30-
317
## Install
328

339
```
3410
npm install react-code-view
3511
```
3612

37-
### Configure webpack .
38-
39-
Support for `markdown-loader` needs to be added in webpack. Need to install:
40-
41-
```
42-
npm install html-loader --save-dev
43-
npm install [email protected] --save-dev
44-
npm install [email protected] --save-dev
45-
```
46-
47-
`webpack.config.js`
13+
### Configure Webpack
4814

4915
```js
16+
// webpack.config.js
17+
export default {
18+
module: {
19+
rules: [
20+
{
21+
test: /\.md$/,
22+
loader: 'react-code-view/webpack-md-loader'
23+
}
24+
]
25+
}
26+
};
27+
```
5028

51-
const markdownRenderer = require('react-markdown-reader').renderer;
29+
#### Options
5230

53-
...
31+
```json
5432
{
55-
test: /\.md$/,
56-
use: [{
57-
loader: 'html-loader'
58-
}, {
59-
loader: 'markdown-loader',
60-
options: {
61-
renderer: markdownRenderer(/**languages:Array<string>**/)
62-
}
63-
}]
33+
"parseLanguages": [
34+
// Supported languages for highlight.js
35+
// default: "javascript", "bash", "xml", "css", "markdown", "less", "typescript"
36+
// See https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
37+
],
38+
"htmlOptions": {
39+
// HTML Loader options
40+
// See https://github.com/webpack-contrib/html-loader#options
41+
},
42+
"markedOptions": {
43+
// Pass options to marked
44+
// See https://marked.js.org/using_advanced#options
45+
}
6446
}
6547
```
6648

67-
languages 默认值:`["javascript", "bash", "xml", "css", "markdown", "less"]`;
68-
69-
### Add Babel
49+
**webpack.config.js**
7050

71-
The sample code usually uses `ES2015+`, `React`, etc., and needs to compile the code in real time, so introduce `Babel` in `HTML`:
51+
```js
7252

73-
```html
74-
<script
75-
type="text/javascript"
76-
src="//cdn.staticfile.org/babel-standalone/6.24.0/babel.min.js"
77-
></script>
53+
export default {
54+
module: {
55+
rules: [
56+
{
57+
test: /\.md$/,
58+
use:[
59+
loader: 'react-code-view/webpack-md-loader',
60+
options:{
61+
parseLanguages: ['typescript','rust']
62+
}
63+
]
64+
}
65+
]
66+
}
67+
};
7868
```
7969

80-
> If `cdn.staticfile.org` is not well accessed in your area, you can change other CDN.
81-
82-
## Example
70+
## Usage
8371

8472
```js
8573
import CodeView from 'react-code-view';
86-
import 'react-code-view/lib/less/index.less';
87-
8874
import { Button } from 'rsuite';
8975

90-
<CodeView dependencies={{ Button }}>{require('./example.md')}</CodeView>;
76+
import 'react-code-view/styles/react-code-view.css';
77+
78+
return (
79+
<CodeView
80+
dependencies={{
81+
Button
82+
}}
83+
>
84+
{require('./example.md')}
85+
</CodeView>
86+
);
9187
```
9288

93-
The source code is uniformly written in markdown, reference:
94-
[example.md](https://raw.githubusercontent.com/simonguo/react-code-view/master/docs/example.md)
95-
96-
> The important thing to note here is that the code that needs to be run must be placed in `<!--start-code-->` and `<!--end-code-->` between.
97-
98-
## API
89+
The source code is written in markdown, refer to [example.md](https://raw.githubusercontent.com/simonguo/react-code-view/master/docs/example.md)
9990

100-
| Name | Type | Default value | Description |
101-
| --------------------- | -------- | ------------------------------------------- | ------------------------------------------------- |
102-
| babelTransformOptions | Object | { presets: ['stage-0', 'react', 'es2015'] } | Babel configuration parameters [options][babeljs] |
103-
| dependencies | Object | | Dependent components. |
104-
| renderToolbar | Function | | Custom toolbar. |
105-
| showCode | boolean | true | Display code. |
106-
| theme | string | 'light' | Theme, options `light` and `dark`. |
91+
> Note: The code to be rendered must be placed between `<!--start-code-->` and `<!--end-code-->`
10792
108-
## Who is using?
93+
## Props
10994

110-
- [React Suite](https://rsuitejs.com/)
95+
### `<CodeView>`
11196

112-
[babeljs]: https://babeljs.io/docs/usage/api/#options
113-
[readm-cn]: https://github.com/simonguo/react-code-view/blob/master/README_zh-CN.md
97+
| Name | Type | Default value | Description |
98+
| -------------- | --------------------------------- | ----------------------- | ------------------------------------------------------------------------- |
99+
| afterCompile | (code: string) => string | | Executed after compiling the code |
100+
| beforeCompile | (code: string) => string | | Executed before compiling the code |
101+
| children | any | | The code to be rendered is executed. Usually imported via markdown-loader |
102+
| compiler | (code: string) => string | | A compiler that transforms the code. Use swc.transformSync by default |
103+
| dependencies | object | | Dependent objects required by the executed code |
104+
| editable | boolean | false | Renders a code editor that can modify the source code |
105+
| editor | object | | Editor properties |
106+
| onChange | (code?: string) => void | | Callback triggered after code change |
107+
| renderToolbar | (buttons: ReactNode) => ReactNode | | Customize the rendering toolbar |
108+
| sourceCode | string | | The code to be rendered is executed |
109+
| theme | 'light' , 'dark' | 'light' | Code editor theme, applied to CodeMirror |
110+
| compileOptions | object | defaultTransformOptions | swc configuration https://swc.rs/docs/configuration/compilation |

0 commit comments

Comments
 (0)