Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Can't get syntax highligth with draft-js-prism-plugin #2

@jtbonhomme

Description

@jtbonhomme

Hi,

I face a serious issue using the prism-plugin.
I can't get any syntax highlighting ...

Any suggestion would be very, very appreciated :)

Thank you in advance,

Here is my source code:

import React, { Component } from 'react';
import { RichUtils, EditorState, ContentState, convertToRaw, convertFromRaw, DefaultDraftBlockRenderMap } from 'draft-js';
import Editor, { createEditorStateWithText } from 'draft-js-plugins-editor';
import createToolbarPlugin from 'draft-js-static-toolbar-plugin';
import Prism from 'prismjs';
import 'prismjs/themes/prism-solarizedlight.css';

import createCodeEditorPlugin from 'draft-js-code-editor-plugin';
import createPrismPlugin from 'draft-js-prism-plugin';

import {
  CodeBlockButton,
} from 'draft-js-buttons';

const toolbarPlugin = createToolbarPlugin({
  structure: [
    CodeBlockButton,
  ]
});

const { Toolbar } = toolbarPlugin;
const plugins = [
  toolbarPlugin,
  createCodeEditorPlugin(),
  createPrismPlugin({
    prism: Prism
  }),
];
const text = 'var i = 1;';

class CourseEditor extends Component {

  constructor(props) {
    super(props);
    this.state = {
      editorState: createEditorStateWithText(text),
    };

    this.onChange = this.onChange.bind(this);
  }

  onChange(editorState) {
    this.setState({
      editorState: editorState,
    });
  };

  getBlockStyle(block) {
    switch (block.getType()) {
      case 'code-block': return 'language-javascript';
      default: return null;
    }
  }

  // Render
  render() {
    return (
      <div className="editor">
        <Editor
          editorState={this.state.editorState}
          onChange={this.onChange}
          plugins={plugins}
          blockStyleFn={this.getBlockStyle}
          customStyleMap={styleMap}
        />
        <Toolbar />
      </div>
    );
  }
}

const styleMap = {
  CODE: {
    backgroundColor: 'rgba(0, 0, 0, 0.05)',
    fontFamily: '"Inconsolata", "Menlo", "Consolas", monospace',
    fontSize: 16,
    padding: 2,
  },
};

export default CourseEditor;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions