You are here

README.txt in CKEditor CodeSnippet 8

CodeSnippet

Installation
============

This module requires the core CKEditor module and the CodeSnippet plugin
from CKEditor.com.

1. Download the CodeSnippet plugin (AT LEAST version 4.5.11) from
   http://ckeditor.com/addon/codesnippet.
2. Place the plugin folder in the root libraries folder
   (/libraries/codesnippet).
3. Enable CodeSnippet in the Drupal admin.
4. Configure your WYSIWYG toolbar to include the buttons.

Basic Usage
===========

After completing the installation steps above, go to the filter format you
want to configure (must be using CKEditor).

CodeSnippet:

Drag the CodeSnippet icon into the active toolbar, and the config form will
appear below with a syntax highlighting style and supported languages
option.  By default, all are checked for you.  Uncheck ones you won't need,
it's optional.  This only controls the options in the dialog window of
CKEditor when inserting a code snippet.

Note that your filter format must support the use of pre and code tags under
allowed tags as well, if using anything other than Full HTML.  You also need
to configure the HTML filter (if Limit Allowed Tags is enabled) to allow the
class attribute like so:

  <code class=""> <pre class="">

Supporting Multiple Stylesheets
===============================

While this module allows each filter format to configure a stylesheet for
highlighting, the HLJS plugin does not yet support this feature.  See this
issue for more details, including a possible workaround to implementing it
in your own style:

https://github.com/isagalaev/highlight.js/issues/862

If you are using multiple filter formats on a page, note that the highest
weighted filter formats settings will be added to the page last and
therefore that style will override any of the other HLJS styles selected in
other formats.

For now, it is best to only configure one format for highlighting, or, use
the same style library for all formats.

CodeSnippet Supported Languages
===============================

To add new options to the supported languages option in the admin form, you
can use a form alter hook within your own custom module to add on:

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 * Add extra languages for CodeSnippet
 * @param $form
 * @param FormStateInterface $form_state
 * @param $form_id
 */
function MYMODULE_form_filter_format_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['editor']['settings']['subform']['plugins']['codesnippet'])) {
    $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['cpp'] = 'C++';
    $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['d'] = 'D';
    $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['rust'] = 'Rust';
    asort($form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']);
  }
}

This would add C++, D, and Rust to the list of languages to check off, which
will then make them available in the dialog of CKEditor CodeSnippet.

An important thing to note is that the key of the array item needs to match
the expected code class for HighlightJS for proper coloring.  If you are
unsure of the class name, refer to the HighlightJS live demo page and
inspect the codeblock of what you want, and check the class on the code HTML
element.

Additionally, you will need to add any new languages to HighlightJS by
customizing its build.  You can customize the build at
https://highlightjs.org/download/

1. Select all the languages you want to support with syntax highlighting
   and download it.
2. Overwrite /libraries/codesnippet/lib/highlight/highlight.pack.js with
   this new file.
3. Clear Drupal caches.

Note that code previews syntax highlighting may not look 100% right (in the
WYSIWYG), but typically will when viewed on the frontend.

Out of the box, the included version of highlightjs comes with these
languages (as defined in config/install/codesnippet.settings.yml):

languages:
  apache: 'Apache'
  bash: 'Bash'
  coffeescript: 'CoffeeScript'
  css: 'CSS'
  dart: 'Dart'
  dockerfile: 'Dockerfile'
  dust: 'Dust'
  gherkin: 'Gherkin'
  go: 'Go'
  haml: 'HAML'
  handlebars: 'Handlebars'
  ini: 'Ini'
  java: 'Java'
  javascript: 'JavaScript'
  json: 'JSON'
  less: 'Less'
  makefile: 'Makefile'
  markdown: 'Markdown'
  nginx: 'Nginx'
  php: 'PHP'
  perl: 'Perl'
  powershell: 'Powershell'
  puppet: 'Puppet'
  python: 'Python'
  ruby: 'Ruby'
  scss: 'SCSS'
  sql: 'SQL'
  twig: 'Twig'
  typescript: 'TypeScript'
  yaml: 'Yaml'
  xml: 'XML'

Note that if you want to highlight HTML code snippets, you need to use the
XML option.  It works for both.

File

README.txt
View source
  1. CodeSnippet
  2. Installation
  3. ============
  4. This module requires the core CKEditor module and the CodeSnippet plugin
  5. from CKEditor.com.
  6. 1. Download the CodeSnippet plugin (AT LEAST version 4.5.11) from
  7. http://ckeditor.com/addon/codesnippet.
  8. 2. Place the plugin folder in the root libraries folder
  9. (/libraries/codesnippet).
  10. 3. Enable CodeSnippet in the Drupal admin.
  11. 4. Configure your WYSIWYG toolbar to include the buttons.
  12. Basic Usage
  13. ===========
  14. After completing the installation steps above, go to the filter format you
  15. want to configure (must be using CKEditor).
  16. CodeSnippet:
  17. Drag the CodeSnippet icon into the active toolbar, and the config form will
  18. appear below with a syntax highlighting style and supported languages
  19. option. By default, all are checked for you. Uncheck ones you won't need,
  20. it's optional. This only controls the options in the dialog window of
  21. CKEditor when inserting a code snippet.
  22. Note that your filter format must support the use of pre and code tags under
  23. allowed tags as well, if using anything other than Full HTML. You also need
  24. to configure the HTML filter (if Limit Allowed Tags is enabled) to allow the
  25. class attribute like so:
  26. Supporting Multiple Stylesheets
  27. ===============================
  28. While this module allows each filter format to configure a stylesheet for
  29. highlighting, the HLJS plugin does not yet support this feature. See this
  30. issue for more details, including a possible workaround to implementing it
  31. in your own style:
  32. https://github.com/isagalaev/highlight.js/issues/862
  33. If you are using multiple filter formats on a page, note that the highest
  34. weighted filter formats settings will be added to the page last and
  35. therefore that style will override any of the other HLJS styles selected in
  36. other formats.
  37. For now, it is best to only configure one format for highlighting, or, use
  38. the same style library for all formats.
  39. CodeSnippet Supported Languages
  40. ===============================
  41. To add new options to the supported languages option in the admin form, you
  42. can use a form alter hook within your own custom module to add on:
  43. use Drupal\Core\Form\FormStateInterface;
  44. /**
  45. * Implements hook_form_FORM_ID_alter().
  46. * Add extra languages for CodeSnippet
  47. * @param $form
  48. * @param FormStateInterface $form_state
  49. * @param $form_id
  50. */
  51. function MYMODULE_form_filter_format_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  52. if (isset($form['editor']['settings']['subform']['plugins']['codesnippet'])) {
  53. $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['cpp'] = 'C++';
  54. $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['d'] = 'D';
  55. $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['rust'] = 'Rust';
  56. asort($form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']);
  57. }
  58. }
  59. This would add C++, D, and Rust to the list of languages to check off, which
  60. will then make them available in the dialog of CKEditor CodeSnippet.
  61. An important thing to note is that the key of the array item needs to match
  62. the expected code class for HighlightJS for proper coloring. If you are
  63. unsure of the class name, refer to the HighlightJS live demo page and
  64. inspect the codeblock of what you want, and check the class on the code HTML
  65. element.
  66. Additionally, you will need to add any new languages to HighlightJS by
  67. customizing its build. You can customize the build at
  68. https://highlightjs.org/download/
  69. 1. Select all the languages you want to support with syntax highlighting
  70. and download it.
  71. 2. Overwrite /libraries/codesnippet/lib/highlight/highlight.pack.js with
  72. this new file.
  73. 3. Clear Drupal caches.
  74. Note that code previews syntax highlighting may not look 100% right (in the
  75. WYSIWYG), but typically will when viewed on the frontend.
  76. Out of the box, the included version of highlightjs comes with these
  77. languages (as defined in config/install/codesnippet.settings.yml):
  78. languages:
  79. apache: 'Apache'
  80. bash: 'Bash'
  81. coffeescript: 'CoffeeScript'
  82. css: 'CSS'
  83. dart: 'Dart'
  84. dockerfile: 'Dockerfile'
  85. dust: 'Dust'
  86. gherkin: 'Gherkin'
  87. go: 'Go'
  88. haml: 'HAML'
  89. handlebars: 'Handlebars'
  90. ini: 'Ini'
  91. java: 'Java'
  92. javascript: 'JavaScript'
  93. json: 'JSON'
  94. less: 'Less'
  95. makefile: 'Makefile'
  96. markdown: 'Markdown'
  97. nginx: 'Nginx'
  98. php: 'PHP'
  99. perl: 'Perl'
  100. powershell: 'Powershell'
  101. puppet: 'Puppet'
  102. python: 'Python'
  103. ruby: 'Ruby'
  104. scss: 'SCSS'
  105. sql: 'SQL'
  106. twig: 'Twig'
  107. typescript: 'TypeScript'
  108. yaml: 'Yaml'
  109. xml: 'XML'
  110. Note that if you want to highlight HTML code snippets, you need to use the
  111. XML option. It works for both.