markdownEditor.gif

编辑Markdown

编辑使用的是for-editor库,安装npm install for-editor

  1. import React from 'react';
  2. import Editor from 'for-editor'
  3. export default class App extends React.Component{
  4. constructor(props){
  5. super(props)
  6. this.state = {
  7. editorValue:'',
  8. imageUrl:''
  9. }
  10. }
  11. handleChange(value){
  12. this.setState({
  13. editorValue:value
  14. });
  15. }
  16. uploadHandler(params){
  17. }
  18. render(){
  19. return(
  20. <Editor className="my-editor"
  21. subfield = {true}
  22. preview = {true}
  23. addImg = {(file) => this.uploadHandler(file)}
  24. value={this.state.editorValue} onChange={(value) => this.handleChange(value)} />
  25. )
  26. }
  27. }

渲染Markdown

效果显示如下:
renderMarkdown.jpg

渲染使用的是react-markdown

  1. import React from 'react';
  2. import ReactMarkdown from 'react-markdown';
  3. const markValue =
  4. `
  5. # Live demo
  6. Changes are automatically rendered as you type.
  7. ## Table of Contents
  8. * Implements [GitHub Flavored Markdown](https://github.github.com/gfm/)
  9. * Renders actual, "native" React DOM elements
  10. * Allows you to escape or skip HTML (try toggling the checkboxes above)
  11. * If you escape or skip the HTML, no \`dangerouslySetInnerHTML\` is used! Yay!
  12. ## HTML block below
  13. <blockquote>
  14. This blockquote will change based on the HTML settings above.
  15. </blockquote>
  16. ## How about some code?
  17. \`\`\`js
  18. var React = require('react');
  19. var Markdown = require('react-markdown');
  20. React.render(
  21. <Markdown source="# Your markdown here" />,
  22. document.getElementById('content')
  23. );
  24. \`\`\`
  25. Pretty neat, eh?
  26. ## Tables?
  27. | Feature | Support |
  28. | --------- | ------- |
  29. | tables | |
  30. | alignment | |
  31. | wewt | |
  32. ## More info?
  33. Read usage information and more on [GitHub](//github.com/rexxars/react-markdown)
  34. ---------------
  35. A component by [Espen Hovlandsdal](https://espen.codes/)
  36. `;
  37. export default class App extends React.Component {
  38. constructor(props) {
  39. super(props)
  40. this.state = {
  41. articleDetail: {
  42. article_content: markValue
  43. },
  44. }
  45. }
  46. render() {
  47. return (
  48. <ReactMarkdown source={this.state.articleDetail.article_content}
  49. escapeHtml={true}></ReactMarkdown>
  50. )
  51. }
  52. }

添加代码高亮

效果图如下:
renderMarkdown1.jpg

  1. // CodeBlock.jsx
  2. import React, { PureComponent } from "react";
  3. import PropTypes from "prop-types";
  4. import { PrismLight as SyntaxHighlighter } from "react-syntax-highlighter";
  5. // 设置高亮样式
  6. import { xonokai } from "react-syntax-highlighter/dist/esm/styles/prism";
  7. // 设置高亮的语言
  8. import { jsx, javascript, sass, scss, python } from "react-syntax-highlighter/dist/esm/languages/prism";
  9. class CodeBlock extends PureComponent {
  10. static propTypes = {
  11. value: PropTypes.string.isRequired,
  12. language: PropTypes.string
  13. };
  14. static defaultProps = {
  15. language: null
  16. };
  17. componentWillMount() {
  18. // 注册要高亮的语法,
  19. // 注意:如果不设置打包后供第三方使用是不起作用的
  20. SyntaxHighlighter.registerLanguage("jsx", jsx);
  21. SyntaxHighlighter.registerLanguage("javascript", javascript);
  22. SyntaxHighlighter.registerLanguage("js", javascript);
  23. SyntaxHighlighter.registerLanguage("sass", sass);
  24. SyntaxHighlighter.registerLanguage("scss", scss);
  25. SyntaxHighlighter.registerLanguage("python", python);
  26. }
  27. render() {
  28. const { language, value } = this.props;
  29. return (
  30. <figure className="highlight">
  31. <SyntaxHighlighter language={language} style={xonokai}>
  32. {value}
  33. </SyntaxHighlighter>
  34. </figure>
  35. );
  36. }
  37. }
  38. export default CodeBlock;
  39. // App.jsx
  40. import React from 'react';
  41. import ReactMarkdown from 'react-markdown';
  42. import CodeBlock from './CodeBlock';
  43. const markValue =
  44. `
  45. # Live demo
  46. Changes are automatically rendered as you type.
  47. ## Table of Contents
  48. * Implements [GitHub Flavored Markdown](https://github.github.com/gfm/)
  49. * Renders actual, "native" React DOM elements
  50. * Allows you to escape or skip HTML (try toggling the checkboxes above)
  51. * If you escape or skip the HTML, no \`dangerouslySetInnerHTML\` is used! Yay!
  52. ## HTML block below
  53. <blockquote>
  54. This blockquote will change based on the HTML settings above.
  55. </blockquote>
  56. ## How about some code?
  57. \`\`\`js
  58. var React = require('react');
  59. var Markdown = require('react-markdown');
  60. React.render(
  61. <Markdown source="# Your markdown here" />,
  62. document.getElementById('content')
  63. );
  64. \`\`\`
  65. Pretty neat, eh?
  66. ## Tables?
  67. | Feature | Support |
  68. | --------- | ------- |
  69. | tables | |
  70. | alignment | |
  71. | wewt | |
  72. ## More info?
  73. Read usage information and more on [GitHub](//github.com/rexxars/react-markdown)
  74. ---------------
  75. A component by [Espen Hovlandsdal](https://espen.codes/)
  76. `;
  77. export default class App extends React.Component {
  78. constructor(props) {
  79. super(props)
  80. this.state = {
  81. articleDetail: {
  82. article_content: markValue
  83. },
  84. }
  85. }
  86. render() {
  87. return (
  88. <ReactMarkdown source={this.state.articleDetail.article_content}
  89. renderers={{
  90. code: CodeBlock
  91. }}
  92. escapeHtml={true}></ReactMarkdown>
  93. )
  94. }
  95. }