SamyPesse / draft-js-prism

Code highlighting for DraftJS using Prism
http://samypesse.github.io/draft-js-prism/
Apache License 2.0
275 stars 36 forks source link

In the local demo the code is not highlighted #20

Open zanonnicola opened 7 years ago

zanonnicola commented 7 years ago

Hi,

first of all thanks for this project!

I've tried to run the demo locally but the code is not highlighted. Just plain black text. There are no errors in the console either.

Am I missing something?

Thanks, Nicola

calummoore commented 7 years ago

I can't get this to work either.

calummoore commented 7 years ago

Ok, so looking at this a bit more. It looks like if there is no code present in the original state, then it will not set the decorator.

You can get around this by making sure it is set during the onChange handler:

onChange = (editorState) => {
  const content = editorState.getCurrentContent()
  this.setState({
    editorState: EditorState.set(editorState, { decorator }),
  })
}

You also need to set the syntax, I'm not exactly sure how it determines the syntax it will use, but you can set the default:

const decorator = new PrismDecorator({
  // Provide your own instance of PrismJS
  prism: Prism,
  defaultSyntax: 'javascript',
})

I think two changes would be helpful:

  1. Update the readme.md to explain how the syntax detection works
  2. Add the decorator to any code blocks, even if no tokens are originally detected.
mxstbr commented 7 years ago

Yeah, this is a super annoying design choice by DraftJS—imo decorators should be bound per-editor not per-state. You'll have to make sure to always set the decorator on any new editor state, PRs to fix the demo would be appreciated.

sebalvarez07 commented 5 years ago

Does anyone know how to change the language syntax aside from the defaultSyntax property?

LuzLabLLC commented 4 years ago

bump, I need some help on adding multiple languages or auto detecting them as you type. Thanks!