Module: Asciidoctor::Html::Highlightjs

Defined in:
lib/asciidoctor/html/highlightjs.rb

Overview

Constants for the highlightjs syntax highlighting library

Constant Summary collapse

CDN_PATH =
"https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build"
INCLUDED_LANGS =
{
  "bash" => true,
  "c" => true,
  "cpp" => true,
  "csharp" => true,
  "css" => true,
  "diff" => true,
  "go" => true,
  "graphql" => true,
  "ini" => true,
  "java" => true,
  "javascript" => true,
  "json" => true,
  "kotlin" => true,
  "less" => true,
  "lua" => true,
  "makefile" => true,
  "markdown" => true,
  "objectivec" => true,
  "perl" => true,
  "php" => true,
  "php-template" => true,
  "plaintext" => true,
  "python" => true,
  "python-repl" => true,
  "r" => true,
  "ruby" => true,
  "rust" => true,
  "scss" => true,
  "shell" => true,
  "sql" => true,
  "swift" => true,
  "typescript" => true,
  "vbnet" => true,
  "wasm" => true,
  "xml" => true,
  "yaml" => true
}.freeze
PLUGIN =
"(function() {\n  const tooltipText = 'Copy to clipboard';\n  const canHover = matchMedia('(hover: hover)').matches &&\n                   matchMedia('(pointer: fine)').matches;\n  function toggleCopyIcon(copyIcon) {\n    copyIcon.classList.toggle('bi-clipboard');\n    copyIcon.classList.toggle('bi-clipboard-check');\n  }\n  hljs.addPlugin({\n    'after:highlightElement': function({ el, result, text }) {\n      let cbText = text\n      const wrapper = el.parentElement; // pre element\n      if(wrapper == null) { return; }\n\n      const overlay = document.createElement('div');\n      overlay.classList.add('copy-button');\n      overlay.textContent = result.language.toUpperCase() + ' ';\n\n      const copyButton = document.createElement('button');\n      copyButton.classList.add('btn');\n      copyButton.setAttribute('type', 'button');\n      const tooltip = new bootstrap.Tooltip(copyButton, {\n        title: tooltipText,\n        trigger: (canHover ? 'hover focus' : 'manual')\n      });\n      const copyIcon = document.createElement('i');\n      copyIcon.classList.add('bi', 'bi-clipboard');\n\n      copyButton.append(copyIcon);\n      overlay.append(copyButton);\n\n      copyButton.addEventListener('click', function() {\n        navigator.clipboard.writeText(cbText);\n        tooltip.setContent({ '.tooltip-inner': 'Copied!' });\n        if(!canHover) tooltip.show();\n        if(!copyIcon.classList.contains('bi-clipboard-check')) {\n          toggleCopyIcon(copyIcon);\n          setTimeout(() => {\n            toggleCopyIcon(copyIcon);\n            tooltip.setContent({ '.tooltip-inner': tooltipText });\n            tooltip.hide();\n          }, 1500);\n        }\n      });\n\n      // Append the copy button to the wrapper\n      wrapper.appendChild(overlay);\n\n      // Find and replace inline callouts\n      const rgx = /[\\u2460-\\u2468]/gu;\n      const cbRgx = /\\s*[\\u2460-\\u2468]/gu;\n      if(text.match(rgx)) {\n        cbText = text.replaceAll(cbRgx, '');\n        el.innerHTML = el.innerHTML.replaceAll(rgx, (match) => {\n          return '<i class=\"hljs-comment bi bi-' + (match.charCodeAt() - 9311) + '-circle\"></i>';\n        });\n      }\n    }\n  });\n  hljs.highlightAll();\n})();\n"