如何制作Reaction Stack代码段

wtzytmuj  于 2022-10-15  发布在  React
关注(0)|答案(1)|浏览(147)

我正在尝试使用Stack代码片段显示我的代码。谁能告诉我这个React片段出了什么问题?
这是正在工作的Codepen

  1. import { marked } from "https://cdn.skypack.dev/marked@4.0.16";
  2. //import DOMPurify from "https://cdn.skypack.dev/dompurify@2.3.8";
  3. const sampleText = `# Welcome to my React Markdown Previewer!
  4. ## This is a sub-heading...
  5. ### And here's some other cool stuff:
  6. Heres some code, \`<div></div>\`, between 2 backticks.
  7. \`\`\`
  8. // this is multi-line code:
  9. function anotherExample(firstLine, lastLine) {
  10. if (firstLine == '\`\`\`' && lastLine == '\`\`\`') {
  11. return multiLineCode;
  12. }
  13. }
  14. \`\`\`
  15. You can also make text**bold**... whoa!
  16. Or _italic_.
  17. Or... wait for it...**_both!_**
  18. And feel free to go crazy ~~crossing stuff out~~.
  19. There's also [links](https://www.freecodecamp.org), and
  20. > Block Quotes!
  21. And if you want to get really crazy, even tables:
  22. Wild Header | Crazy Header | Another Header?
  23. ------------ | ------------- | -------------
  24. Your content can | be here, and it | can be here....
  25. And here. | Okay. | I think we get it.
  26. - And of course there are lists.
  27. - Some are bulleted.
  28. - With different indentation levels.
  29. - That look like this.
  30. 1. And there are numbered lists too.
  31. 1. Use just 1s if you want!
  32. 1. And last but not least, let's not forget embedded images:
  33. ![freeCodeCamp Logo](https://cdn.freecodecamp.org/testable-projects-fcc/images/fcc_secondary.svg)
  34. `;
  35. class App extends React.Component {
  36. state = {
  37. text: sampleText,
  38. };
  39. handleChange = (event) => {
  40. const text = event.target.value;
  41. this.setState({ text });
  42. };
  43. //renderText = (text) => DOMPurify.sanitize(marked(text));
  44. renderText = (text) => marked(text);
  45. // On souhaite que les modifications soient enregistrée dans le LocalStorage
  46. // du navigateur. Ainsi si l'utilisateur rafréchit la page, ses modifications
  47. // seront sauvegarder.
  48. // On sauvegarde les modifications
  49. componentDidUpdate() {
  50. const { text } = this.state;
  51. localStorage.setItem('text', text);
  52. }
  53. // On réaffiche la sauvegarde lorsque l'App est relancée par le rafréchissement.
  54. // s'il y à eu du text sauvegardé, je rafiche ce text.
  55. // Si tout à été supprimé, je remets le sampleText de départ.
  56. componentDidMount() {
  57. const text = localStorage.getItem('text');
  58. if (text) {
  59. this.setState({ text });
  60. } else {
  61. this.setState({ text: sampleText });
  62. }
  63. }
  64. // Vidéo - Anthony Welc - Ch 03 - 05 Du Markdown avec Marked, time 03:44
  65. render() {
  66. return (
  67. <div className="container">
  68. <div className="row">
  69. <div className="col-sm-6">
  70. <textarea
  71. onChange={this.handleChange}
  72. className="form-control"
  73. rows="35"
  74. value={this.state.text}
  75. ></textarea>
  76. </div>
  77. <div className="col-sm-6">
  78. <div
  79. dangerouslySetInnerHTML={{
  80. __html: this.renderText(this.state.text),
  81. }}
  82. ></div>
  83. </div>
  84. </div>
  85. </div>
  86. );
  87. }
  88. }
  89. ReactDOM.render(<App />, document.getElementById('root'));
  1. .container {
  2. margin-top: 24px;
  3. }
  1. <script src="https://unpkg.com/react-bootstrap@next/dist/react-bootstrap.min.js"></script>
  2. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"/>
  3. <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
  6. <div id="root"></div>
3hvapo4f

3hvapo4f1#

以下是几个问题:

  • 本地存储在代码段沙箱中不起作用。
  • 模块要正确处理有点麻烦,所以我会使用UMD导入。
  • 标记的最新版本使用marked.parse(),而不是marked()
  1. const sampleText = `# Welcome to my React Markdown Previewer!
  2. ## This is a sub-heading...
  3. ### And here's some other cool stuff:
  4. Heres some code, \`<div></div>\`, between 2 backticks.
  5. \`\`\`
  6. // this is multi-line code:
  7. function anotherExample(firstLine, lastLine) {
  8. if (firstLine == '\`\`\`' && lastLine == '\`\`\`') {
  9. return multiLineCode;
  10. }
  11. }
  12. \`\`\`
  13. You can also make text**bold**... whoa!
  14. Or _italic_.
  15. Or... wait for it...**_both!_**
  16. And feel free to go crazy ~~crossing stuff out~~.
  17. There's also [links](https://www.freecodecamp.org), and
  18. > Block Quotes!
  19. And if you want to get really crazy, even tables:
  20. Wild Header | Crazy Header | Another Header?
  21. ------------ | ------------- | -------------
  22. Your content can | be here, and it | can be here....
  23. And here. | Okay. | I think we get it.
  24. - And of course there are lists.
  25. - Some are bulleted.
  26. - With different indentation levels.
  27. - That look like this.
  28. 1. And there are numbered lists too.
  29. 1. Use just 1s if you want!
  30. 1. And last but not least, let's not forget embedded images:
  31. ![freeCodeCamp Logo](https://cdn.freecodecamp.org/testable-projects-fcc/images/fcc_secondary.svg)
  32. `;
  33. class App extends React.Component {
  34. state = {
  35. text: sampleText,
  36. };
  37. handleChange = (event) => {
  38. const text = event.target.value;
  39. this.setState({ text });
  40. };
  41. renderText = (text) => DOMPurify.sanitize(marked.parse(text));
  42. // On souhaite que les modifications soient enregistrée dans le LocalStorage
  43. // du navigateur. Ainsi si l'utilisateur rafréchit la page, ses modifications
  44. // seront sauvegarder.
  45. // On sauvegarde les modifications
  46. componentDidUpdate() {
  47. const { text } = this.state;
  48. //localStorage.setItem('text', text);
  49. }
  50. // On réaffiche la sauvegarde lorsque l'App est relancée par le rafréchissement.
  51. // s'il y à eu du text sauvegardé, je rafiche ce text.
  52. // Si tout à été supprimé, je remets le sampleText de départ.
  53. componentDidMount() {
  54. const text = null;
  55. //const text = localStorage.getItem('text');
  56. if (text) {
  57. this.setState({ text });
  58. } else {
  59. this.setState({ text: sampleText });
  60. }
  61. }
  62. // Vidéo - Anthony Welc - Ch 03 - 05 Du Markdown avec Marked, time 03:44
  63. render() {
  64. return (
  65. <div className="container">
  66. <div className="row">
  67. <div className="col-sm-6">
  68. <textarea
  69. onChange={this.handleChange}
  70. className="form-control"
  71. rows="35"
  72. value={this.state.text}
  73. ></textarea>
  74. </div>
  75. <div className="col-sm-6">
  76. <div
  77. dangerouslySetInnerHTML={{
  78. __html: this.renderText(this.state.text),
  79. }}
  80. ></div>
  81. </div>
  82. </div>
  83. </div>
  84. );
  85. }
  86. }
  87. ReactDOM.render(<App />, document.getElementById('root'));
  1. .container {
  2. margin-top: 24px;
  3. }
  1. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"/>
  2. <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/0.7.1/purify.min.js"></script>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
  6. <div id="root"></div>

以下是Reaction 18的版本:

  1. const sampleText = `# Welcome to my React Markdown Previewer!
  2. ## This is a sub-heading...
  3. ### And here's some other cool stuff:
  4. Heres some code, \`<div></div>\`, between 2 backticks.
  5. \`\`\`
  6. // this is multi-line code:
  7. function anotherExample(firstLine, lastLine) {
  8. if (firstLine == '\`\`\`' && lastLine == '\`\`\`') {
  9. return multiLineCode;
  10. }
  11. }
  12. \`\`\`
  13. You can also make text**bold**... whoa!
  14. Or _italic_.
  15. Or... wait for it...**_both!_**
  16. And feel free to go crazy ~~crossing stuff out~~.
  17. There's also [links](https://www.freecodecamp.org), and
  18. > Block Quotes!
  19. And if you want to get really crazy, even tables:
  20. Wild Header | Crazy Header | Another Header?
  21. ------------ | ------------- | -------------
  22. Your content can | be here, and it | can be here....
  23. And here. | Okay. | I think we get it.
  24. - And of course there are lists.
  25. - Some are bulleted.
  26. - With different indentation levels.
  27. - That look like this.
  28. 1. And there are numbered lists too.
  29. 1. Use just 1s if you want!
  30. 1. And last but not least, let's not forget embedded images:
  31. ![freeCodeCamp Logo](https://cdn.freecodecamp.org/testable-projects-fcc/images/fcc_secondary.svg)
  32. `;
  33. class App extends React.Component {
  34. state = {
  35. text: sampleText,
  36. };
  37. handleChange = (event) => {
  38. const text = event.target.value;
  39. this.setState({ text });
  40. };
  41. renderText = (text) => DOMPurify.sanitize(marked.parse(text));
  42. // On souhaite que les modifications soient enregistrée dans le LocalStorage
  43. // du navigateur. Ainsi si l'utilisateur rafréchit la page, ses modifications
  44. // seront sauvegarder.
  45. // On sauvegarde les modifications
  46. componentDidUpdate() {
  47. const { text } = this.state;
  48. //localStorage.setItem('text', text);
  49. }
  50. // On réaffiche la sauvegarde lorsque l'App est relancée par le rafréchissement.
  51. // s'il y à eu du text sauvegardé, je rafiche ce text.
  52. // Si tout à été supprimé, je remets le sampleText de départ.
  53. componentDidMount() {
  54. const text = null;
  55. //const text = localStorage.getItem('text');
  56. if (text) {
  57. this.setState({ text });
  58. } else {
  59. this.setState({ text: sampleText });
  60. }
  61. }
  62. // Vidéo - Anthony Welc - Ch 03 - 05 Du Markdown avec Marked, time 03:44
  63. render() {
  64. return (
  65. <div className="container">
  66. <div className="row">
  67. <div className="col-sm-6">
  68. <textarea
  69. onChange={this.handleChange}
  70. className="form-control"
  71. rows="35"
  72. value={this.state.text}
  73. ></textarea>
  74. </div>
  75. <div className="col-sm-6">
  76. <div
  77. dangerouslySetInnerHTML={{
  78. __html: this.renderText(this.state.text),
  79. }}
  80. ></div>
  81. </div>
  82. </div>
  83. </div>
  84. );
  85. }
  86. }
  87. ReactDOM.createRoot(document.querySelector("#app"))
  88. .render(<App />);
  1. .container {
  2. margin-top: 24px;
  3. }
  1. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"/>
  2. <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/0.7.1/purify.min.js"></script>
  4. <script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
  5. <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  6. <div id="app"></div>

另请参阅How do I create a React Stack Snippet with JSX support?

展开查看全部

相关问题