CodeSnippet

I was looking for a way to post code samples, and found the CKEditor CodeSnippet module. It adds a button to CKEditor that let's you create a formatted code snippet with syntax highlighting for many languages.

There are a ton of highlight colors to choose from.

<?php

/**
 * @file
 * Provides integration with the CKEditor WYSIWYG editor.
 */

use Drupal\editor\Entity\Editor;

/**
 * Implements hook_ckeditor_css_alter().
 *
 * Injects our selected CSS sheet anytime CKEditor has loaded.
 *
 * @param array $css
 * @param Drupal\editor\Entity\Editor $editor
 */
function codesnippet_ckeditor_css_alter(array &$css, Editor $editor) {
  if (!$editor->hasAssociatedFilterFormat()) {
    return;
  }

  $settings = $editor->getSettings();

  if (!empty($settings['plugins']['codesnippet']['highlight_style'])) {
    $css[] = base_path() . 'libraries/codesnippet/lib/highlight/styles/' . $settings['plugins']['codesnippet']['highlight_style'] . '.css';
  }
}

 

Unfortunately, the color choices are set on the ckeditor config, and can't easily be selected by users.

 

This patch is needed for the background to fill completely on mobile: https://www.drupal.org/project/codesnippet/issues/2914784#comment-12403…

Resources