Sha256: c17b5135014a596eb73057a65fb398872721657bd460ec74b1da665b1ab8bffe

Contents?: true

Size: 1.04 KB

Versions: 7

Compression:

Stored size: 1.04 KB

Contents

module NotionOrbit
  module NotionObjects
    class RichText
      def initialize(raw_rich_text)
        @raw_rich_text = raw_rich_text
        @type = raw_rich_text.type
        @annotations = raw_rich_text.annotations
        @text = raw_rich_text.text
      end

      def to_markdown
        return "" unless @type == 'text'
        markdown = @text.content
        markdown = apply_link(markdown) unless @text.link.nil?
        markdown = apply_annotations(markdown)
        markdown
      end

      def apply_link(content)
        @text.link.nil? ? content : "[#{content}](#{@text.link.url})"
      end

      def apply_annotations(content)
        annotation_symbols = []
        annotation_symbols << '**' if @annotations.bold
        annotation_symbols << '_' if @annotations.italic
        annotation_symbols << '`' if @annotations.code
        wrap_with(content, annotation_symbols)
      end

      def wrap_with(string, wrappers)
        "#{wrappers.join}#{string}#{wrappers.reverse.join}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
notion_orbit-0.0.7 lib/notion_orbit/notion_objects/rich_text.rb
notion_orbit-0.0.6 lib/notion_orbit/notion_objects/rich_text.rb
notion_orbit-0.0.5 lib/notion_orbit/notion_objects/rich_text.rb
notion_orbit-0.0.4 lib/notion_orbit/notion_objects/rich_text.rb
notion_orbit-0.0.3 lib/notion_orbit/notion_objects/rich_text.rb
notion_orbit-0.0.2 lib/notion_orbit/notion_objects/rich_text.rb
notion_orbit-0.0.1 lib/notion_orbit/notion_objects/rich_text.rb