Class: NotionOrbit::NotionObjects::RichText

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_orbit/notion_objects/rich_text.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_rich_text) ⇒ RichText

Returns a new instance of RichText.



4
5
6
7
8
9
# File 'lib/notion_orbit/notion_objects/rich_text.rb', line 4

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

Instance Method Details

#apply_annotations(content) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/notion_orbit/notion_objects/rich_text.rb', line 23

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


19
20
21
# File 'lib/notion_orbit/notion_objects/rich_text.rb', line 19

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

#to_markdownObject



11
12
13
14
15
16
17
# File 'lib/notion_orbit/notion_objects/rich_text.rb', line 11

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

#wrap_with(string, wrappers) ⇒ Object



31
32
33
# File 'lib/notion_orbit/notion_objects/rich_text.rb', line 31

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