Skip to content

Advanced Usage

The setups are a nice way to get an editor up and running with minimal configuration, but they have multiple downsides. If you want to fully customize which extensions are added and which styles are imported, this page will show you how.

All the setups are an abstraction over createEditor() which can be used directly for more control.

index.js

The 3rd argument onward to createEditor() are the extensions you want the editor to be initialized with, but you can also add more extensions later with the editor.addExtensions() method. The example shown uses a dynamic import() to load most extensions. This enables code splitting for even faster load times.

extensions.js

The PrismEditor object returned by createEditor() has some useful properties and methods, but to keep the core as light as possible, many functions that could’ve been methods on PrismEditor are external instead. This includes insertText(), setSelection(), getLanguage(), and getClosestToken()

createEditor() will append the editor to your container. If you want more control over where your editor ends up, you can use editorFromPlaceholder() instead, which will replace the placeholder with the new editor. Additionally, the textContent of the placeholder will be used as the editor’s value unless options.value is defined. The placeholder doesn’t have to be an element; it can also be a Text or Comment node.

Adding editors to your page can cause layout shifts. If you’re using editorFromPlaceholder() with placeholders that are the same size as the editor, layout shifts can be avoided. Working example. If you’re using a static-site generator, a better solution can be found in the Server-side rendering guide.