Skip to content

Commit 427f823

Browse files
committed
fix: fix server-side rendering compatibility
1 parent 96b9ba9 commit 427f823

19 files changed

+131
-790
lines changed

README.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ Let the React code in Markdown be rendered to the page, and support online editi
44

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

7-
## Principle of implementation
8-
9-
- Parse Markdown documents through `markdown-loader` and `html-loader`.
10-
- Extract the code that needs to be rendered in the document and pass it to the `codemirror` editor.
11-
- Compile the code in `codemirror` through swc, and then render the component to the specified container through `ReactDOM.render`.
12-
137
## Install
148

159
```
@@ -25,18 +19,46 @@ export default {
2519
rules: [
2620
{
2721
test: /\.md$/,
28-
use: [
29-
{
30-
loader: 'react-code-view/webpack-md-loader',
31-
parseLanguages: ['javascript', 'bash', 'xml', 'css', 'markdown', 'less', 'typescript'],
32-
htmlOptions: {
33-
// HTML Loader options
34-
// See https://github.com/webpack-contrib/html-loader#options
35-
},
36-
markedOptions: {
37-
// Pass options to marked
38-
// See https://marked.js.org/using_advanced#options
39-
}
22+
loader: 'react-code-view/webpack-md-loader'
23+
}
24+
]
25+
}
26+
};
27+
```
28+
29+
#### Options
30+
31+
```json
32+
{
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+
}
46+
}
47+
```
48+
49+
**webpack.config.js**
50+
51+
```js
52+
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']
4062
}
4163
]
4264
}
@@ -51,7 +73,7 @@ export default {
5173
import CodeView from 'react-code-view';
5274
import { Button } from 'rsuite';
5375

54-
import 'react-code-view/styles/index.less'; // or 'react-code-view/styles/react-code-view.css'
76+
import 'react-code-view/styles/react-code-view.css';
5577

5678
return (
5779
<CodeView

docs/example.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ npm install react-code-view
1212

1313
## Usage
1414

15-
```js
15+
```jsx
1616
import CodeView from 'react-code-view';
1717
import { Button } from 'rsuite';
1818

19-
import 'react-code-view/styles/styles.less'; // or 'react-code-view/styles/react-code-view.css'
19+
import 'react-code-view/styles/react-code-view.css';
2020

2121
return <CodeView dependencies={{ Button }}>{require('./example.md')}</CodeView>;
2222
```

docs/gh-pages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var ghpages = require('gh-pages');
22
var path = require('path');
33

4-
ghpages.publish(path.join(__dirname, '../assets'), function (err) {
4+
ghpages.publish(path.join(__dirname, './assets'), function (err) {
55
console.log(err);
66
});

docs/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import CodeView from '../src';
55

66
import './styles/index.less';
77

8+
const example = require('./example.md');
9+
810
const App = () => {
911
return (
1012
<Grid>
@@ -16,7 +18,7 @@ const App = () => {
1618
return code.replace(/import\ [\w\,\{\}\ ]+\ from\ "[\w-]+";/gi, '');
1719
}}
1820
>
19-
{require('./example.md')}
21+
{example}
2022
</CodeView>
2123
</Grid>
2224
);

docs/styles/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@import '~codemirror/lib/codemirror.css';
33
@import '~codemirror/theme/base16-light.css';
44
@import '~codemirror/theme/base16-dark.css';
5-
@import '~highlight.js/styles/github.css';
5+
@import '~highlight.js/styles/base16/tomorrow.css';
66

77
@import '../../src/less/styles.less';
88
@import './markdown.less';

docs/webpack.config.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,7 @@ module.exports = {
5252
},
5353
{
5454
test: /\.md$/,
55-
use: [
56-
{
57-
loader: path.resolve('./webpack-md-loader'),
58-
options: {
59-
parseLanguages: ['javascript', 'bash', 'xml', 'css', 'markdown', 'less', 'typescript']
60-
}
61-
}
62-
]
55+
loader: path.resolve('./webpack-md-loader')
6356
}
6457
]
6558
},

gulpfile.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const writeFile = util.promisify(fs.writeFile);
1313

1414
function buildLess() {
1515
return gulp
16-
.src([`${STYLE_SOURCE_DIR}/*.less`])
16+
.src([`${STYLE_SOURCE_DIR}/index.less`])
1717
.pipe(sourcemaps.init())
1818
.pipe(less({ javascriptEnabled: true, paths: ['*.css', '*.less'] }))
1919
.pipe(postcss([require('autoprefixer')]))
@@ -32,10 +32,6 @@ function buildCSS() {
3232
.pipe(gulp.dest(`${STYLE_DIST_DIR}`));
3333
}
3434

35-
function copyLessFiles() {
36-
return gulp.src([`${STYLE_SOURCE_DIR}/*.less`]).pipe(gulp.dest(STYLE_DIST_DIR));
37-
}
38-
3935
function copyDocs() {
4036
return gulp
4137
.src(['./README.md', './CHANGELOG.md', './LICENSE', 'package.json'])
@@ -58,11 +54,4 @@ function createPkgFile(done) {
5854
});
5955
}
6056

61-
exports.build = gulp.series(
62-
buildLess,
63-
buildCSS,
64-
copyLessFiles,
65-
copyDocs,
66-
copyLoader,
67-
createPkgFile
68-
);
57+
exports.build = gulp.series(buildLess, buildCSS, copyDocs, copyLoader, createPkgFile);

package.json

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{
22
"name": "react-code-view",
3-
"version": "2.0.0-beta.1",
3+
"version": "2.0.0-beta.17",
44
"description": "Code view for React",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",
77
"typings": "esm/index.d.ts",
88
"scripts": {
9-
"build:umd": "rollup -c",
109
"build:esm": "NODE_ENV=esm npx babel --extensions .ts,.tsx src --out-dir dist/esm",
1110
"build:cjs": "NODE_ENV=commonjs npx babel --extensions .ts,.tsx src --out-dir dist/cjs",
1211
"build:types": "npx tsc --emitDeclarationOnly --outDir dist/cjs && npx tsc --emitDeclarationOnly --outDir dist/esm",
1312
"build:gulp": "gulp build",
14-
"build": "npm run clean && npm run build:umd && npm run build:esm && npm run build:cjs && npm run build:types && npm run build:gulp",
13+
"build": "npm run clean && npm run build:esm && npm run build:cjs && npm run build:types && npm run build:gulp",
1514
"clean": "rimraf dist",
1615
"dev": "webpack serve --mode development --port 3100 --host 0.0.0.0 --progress --config ./docs/webpack.config.js",
1716
"docs": "rm -rf assets && webpack --mode production --config ./docs/webpack.config.js",
@@ -40,6 +39,7 @@
4039
"classnames": "^2.2.5",
4140
"codemirror": "5.65.6",
4241
"highlight.js": "^11.5.1",
42+
"html-loader": "^3.1.2",
4343
"marked": "^4.0.17"
4444
},
4545
"peerDependencies": {
@@ -57,9 +57,7 @@
5757
"@babel/preset-env": "^7.7.7",
5858
"@babel/preset-react": "^7.7.4",
5959
"@babel/preset-typescript": "^7.12.7",
60-
"@rollup/plugin-commonjs": "^22.0.1",
61-
"@rollup/plugin-node-resolve": "^13.3.0",
62-
"@rollup/plugin-typescript": "^8.3.3",
60+
"@types/node": "^18.0.3",
6361
"@types/react": "^17.0.0",
6462
"@types/react-dom": "^17.0.0",
6563
"autoprefixer": "^10.4.7",
@@ -78,7 +76,6 @@
7876
"gulp-postcss": "^9.0.1",
7977
"gulp-rename": "^2.0.0",
8078
"gulp-sourcemaps": "^3.0.0",
81-
"html-loader": "^3.1.2",
8279
"html-webpack-plugin": "^5.5.0",
8380
"less": "^4.1.3",
8481
"less-loader": "^11.0.0",
@@ -88,10 +85,6 @@
8885
"prettier": "^2.4.1",
8986
"react": "^17.0.2",
9087
"react-dom": "^17.0.2",
91-
"rollup": "^2.75.7",
92-
"rollup-plugin-babel": "^4.4.0",
93-
"rollup-plugin-peer-deps-external": "^2.2.4",
94-
"rollup-plugin-terser": "^7.0.2",
9588
"rsuite": "^5.15.2",
9689
"typescript": "^4.6.4",
9790
"webpack": "^5.73.0",

0 commit comments

Comments
 (0)