Class: Inkpen::Extensions::CodeBlockSyntax

Inherits:
Base
  • Object
show all
Defined in:
lib/inkpen/extensions/code_block_syntax.rb

Overview

Code Block with Syntax Highlighting extension.

Enhances the standard code block with syntax highlighting using lowlight (highlight.js). Supports multiple programming languages with automatic language detection.

Examples:

Basic usage

extension = Inkpen::Extensions::CodeBlockSyntax.new

With custom languages

extension = Inkpen::Extensions::CodeBlockSyntax.new(
  languages: [:ruby, :javascript, :python, :sql],
  default_language: :ruby
)

See Also:

Author:

  • Inkpen Team

Since:

  • 0.2.0

Constant Summary collapse

AVAILABLE_LANGUAGES =

All available programming languages for syntax highlighting. These are loaded from highlight.js via lowlight.

Since:

  • 0.2.0

%i[
  javascript
  typescript
  ruby
  python
  css
  xml
  html
  json
  bash
  shell
  sql
  markdown
  yaml
  go
  rust
  java
  kotlin
  swift
  php
  c
  cpp
  csharp
  elixir
  erlang
  haskell
  scala
  r
  matlab
  dockerfile
  nginx
  apache
  graphql
].freeze
DEFAULT_LANGUAGES =

Default languages to load (for performance).

Since:

  • 0.2.0

%i[
  javascript
  typescript
  ruby
  python
  css
  xml
  json
  bash
  sql
  markdown
].freeze

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#enabled?, #initialize, #to_h, #to_json

Constructor Details

This class inherits a constructor from Inkpen::Extensions::Base

Instance Method Details

#copy_button?Boolean

Whether to enable copy-to-clipboard button.

Returns:

  • (Boolean)

    true to show copy button

Since:

  • 0.2.0



132
133
134
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 132

def copy_button?
  options.fetch(:copy_button, true)
end

#default_languageSymbol?

Default language when none is specified.

Returns:

  • (Symbol, nil)

    default language or nil for auto-detect

Since:

  • 0.2.0



105
106
107
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 105

def default_language
  options[:default_language]
end

#languagesArray<Symbol>

Languages to enable for syntax highlighting.

Only these languages will be loaded from highlight.js to reduce bundle size.

Returns:

  • (Array<Symbol>)

    enabled language identifiers

Since:

  • 0.2.0



96
97
98
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 96

def languages
  options.fetch(:languages, DEFAULT_LANGUAGES)
end

#line_numbers?Boolean

Whether to show line numbers in code blocks.

Returns:

  • (Boolean)

    true to show line numbers

Since:

  • 0.2.0



114
115
116
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 114

def line_numbers?
  options.fetch(:line_numbers, false)
end

#nameSymbol

The unique name of this extension.

Returns:

  • (Symbol)

    :code_block_syntax

Since:

  • 0.2.0



84
85
86
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 84

def name
  :code_block_syntax
end

#show_language_selector?Boolean

Whether to show a language selector dropdown.

Returns:

  • (Boolean)

    true to show language selector

Since:

  • 0.2.0



123
124
125
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 123

def show_language_selector?
  options.fetch(:language_selector, true)
end

#themeString

CSS theme for syntax highlighting.

Returns:

  • (String)

    theme name (e.g., “github”, “monokai”, “dracula”)

Since:

  • 0.2.0



141
142
143
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 141

def theme
  options.fetch(:theme, "github")
end

#to_configHash

Convert to configuration hash for JavaScript.

Returns:

  • (Hash)

    configuration for the TipTap extension

Since:

  • 0.2.0



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/inkpen/extensions/code_block_syntax.rb', line 150

def to_config
  {
    languages: languages,
    defaultLanguage: default_language,
    lineNumbers: line_numbers?,
    languageSelector: show_language_selector?,
    copyButton: copy_button?,
    theme: theme,
    lowlight: {
      languages: languages
    }
  }
end