Class: RubyRich::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/markdown.rb

Defined Under Namespace

Classes: TerminalConverter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Markdown

Returns a new instance of Markdown.



404
405
406
# File 'lib/ruby_rich/markdown.rb', line 404

def initialize(options = {})
  @options = options
end

Class Method Details

.render(markdown_text, options = {}) ⇒ String

渲染 Markdown 文本为 ANSI 终端输出

Parameters:

  • markdown_text (String)

    输入的 Markdown 文本

  • options (Hash) (defaults to: {})

    渲染选项

Options Hash (options):

  • :width (Integer)

    终端宽度(默认 80)

  • :indent (String)

    缩进字符串(默认 ‘ ’)

  • :table_border_style (Symbol)

    表格边框样式 (:none, :simple, :full)

  • :kramdown (Hash)

    传递给 Kramdown::Document 的额外选项

Returns:

  • (String)

    ANSI 格式的终端输出



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/ruby_rich/markdown.rb', line 384

def self.render(markdown_text, options = {})
  converter_options = {
    width: options[:width] || 80,
    indent: options[:indent] || '  ',
    table_border_style: options[:table_border_style] || :simple
  }

  kramdown_opts = {
    input: 'GFM',                 # GitHub Flavored Markdown
    syntax_highlighter: nil,      # 自行处理语法高亮
    hard_wrap: false,
    html_to_native: true,
    line_width: converter_options[:width]
  }.merge(options[:kramdown] || {})

  doc = Kramdown::Document.new(markdown_text, kramdown_opts)
  result, _warnings = TerminalConverter.convert(doc.root, converter_options)
  result
end

Instance Method Details

#render(markdown_text) ⇒ Object



408
409
410
# File 'lib/ruby_rich/markdown.rb', line 408

def render(markdown_text)
  self.class.render(markdown_text, @options)
end