Class: Markdownator::Converters::Json

Inherits:
Base
  • Object
show all
Defined in:
lib/markdownator/converters/json.rb

Overview

Renders JSON as a pretty-printed fenced code block (lossless).

Instance Method Summary collapse

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/markdownator/converters/json.rb', line 9

def accepts?(_io, stream_info)
  matches?(stream_info, extensions: %w[json], mimetypes: %w[application/json text/json])
end

#convert(io, stream_info, **_options) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/markdownator/converters/json.rb', line 13

def convert(io, stream_info, **_options)
  raw = read_all(io, stream_info)
  pretty = JSON.pretty_generate(JSON.parse(raw))
  Result.new(markdown: "```json\n#{pretty}\n```")
rescue JSON::ParserError => e
  raise FileConversionError, "Could not parse JSON: #{e.message}"
end