Sha256: 143f10f0f6fd0311bf74743d0b715db084b1230723e3da315d21e7ef0b7e62e5
Contents?: true
Size: 1.95 KB
Versions: 9
Compression:
Stored size: 1.95 KB
Contents
module SlateSerializer # Text de- and serializer class Plain class << self # Convert text to a Slate document # # @param text format [String] the text # return [Hash] Slate document def deserializer(text) text = '' if text.nil? lines = split_text_into_lines(text) { document: { object: 'document', nodes: convert_lines_into_nodes(lines) } } end # Convert a Slate Document to plain text # # @param value format [Hash] the Slate document # @param options format [Hash] options for the serializer, delimitter defaults to "\n" # @return [String] plain text version of the Slate documnent def serializer(value, options = {}) return '' unless value.key?(:document) options[:delimiter] = "\n" unless options.key?(:delimiter) serialize_node(value[:document], options) end private def split_text_into_lines(text) lines = text.strip.split("\n").map(&:strip) blocks = [] loop do index = lines.find_index('') if index.nil? blocks << lines.join("\n") break end blocks << lines[0...index].join("\n") lines.shift(index + 1) end blocks.length == 1 ? blocks : blocks.reject { |block| block == '' } end def convert_lines_into_nodes(lines) lines.map do |line| { object: 'block', type: 'paragraph', data: {}, nodes: [ object: 'text', text: line, marks: [] ] } end end def serialize_node(node, options) if node[:object] == 'document' || node[:object] == 'block' node[:nodes].map { |n| serialize_node(n, options) }.join(options[:delimiter]) else node[:text] end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems