Class: TipTap::Nodes::Text

Inherits:
TipTap::Node show all
Defined in:
lib/tip_tap/nodes/text.rb

Instance Attribute Summary collapse

Attributes included from HtmlRenderable

#output_buffer

Attributes included from HasContent

#attrs, #content

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonRenderable

#include_empty_content_in_json?

Methods included from HtmlRenderable

#html_attributes, #html_class_name, #html_tag, included, #inline_styles

Methods included from HasContent

#add_content, #blank?, #each, #find_node, included, #size

Methods included from Registerable

included, #type_name

Constructor Details

#initialize(content, **attributes) {|_self| ... } ⇒ Text

Returns a new instance of Text.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
# File 'lib/tip_tap/nodes/text.rb', line 14

def initialize(content, **attributes)
  @text = content
  @marks = Array(attributes[:marks]).map(&:deep_stringify_keys)
  yield self if block_given?
end

Instance Attribute Details

#marksObject (readonly)

Returns the value of attribute marks.



10
11
12
# File 'lib/tip_tap/nodes/text.rb', line 10

def marks
  @marks
end

#textObject

Allow the text to be set and accessed directly



9
10
11
# File 'lib/tip_tap/nodes/text.rb', line 9

def text
  @text
end

Class Method Details

.from_json(json) ⇒ Object



20
21
22
23
24
# File 'lib/tip_tap/nodes/text.rb', line 20

def self.from_json(json)
  json.deep_stringify_keys!

  new(json["text"], marks: Array(json["marks"]))
end

Instance Method Details

#bold?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/tip_tap/nodes/text.rb', line 76

def bold?
  has_mark_with_type?("bold")
end

#code?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/tip_tap/nodes/text.rb', line 92

def code?
  has_mark_with_type?("code")
end

#highlight?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/tip_tap/nodes/text.rb', line 104

def highlight?
  has_mark_with_type?("highlight")
end

#italic?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/tip_tap/nodes/text.rb', line 72

def italic?
  has_mark_with_type?("italic")
end

#link?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/tip_tap/nodes/text.rb', line 84

def link?
  has_mark_with_type?("link")
end

#strike?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/tip_tap/nodes/text.rb', line 88

def strike?
  has_mark_with_type?("strike")
end

#subscript?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/tip_tap/nodes/text.rb', line 100

def subscript?
  has_mark_with_type?("subscript")
end

#superscript?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/tip_tap/nodes/text.rb', line 96

def superscript?
  has_mark_with_type?("superscript")
end

#text_style?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/tip_tap/nodes/text.rb', line 108

def text_style?
  has_mark_with_type?("textStyle")
end

#to_hObject



26
27
28
29
30
# File 'lib/tip_tap/nodes/text.rb', line 26

def to_h
  data = {type: type_name, text: text || ""}
  data[:marks] = marks.map(&:deep_symbolize_keys) unless marks.empty?
  data
end

#to_htmlObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tip_tap/nodes/text.rb', line 32

def to_html
  value = text
  value = (:sup, value) if superscript?
  value = (:sub, value) if subscript?
  value = highlight_tag(value) if highlight?
  value = (:code, value) if code?
  value = (:u, value) if underline?
  value = (:em, value) if italic?
  value = (:strong, value) if bold?
  value = (:s, value) if strike?
  value = (:a, value, href: link_href, target: link_target) if link?
  value = (:span, value, style: inline_style_content(text_styles)) if text_style?
  value
end

#to_markdown(context = Markdown::Context.root) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tip_tap/nodes/text.rb', line 51

def to_markdown(context = Markdown::Context.root)
  value = text.to_s
  return "" if value.empty?

  return value if context.within_code_block?

  return wrap_with_backticks(value) if code?

  value = escape_markdown(value)
  value = apply_bold(value) if bold?
  value = apply_italic(value) if italic?
  value = apply_strike(value) if strike?
  value = wrap_with_html_tag("u", value) if underline?
  value = apply_highlight(value) if highlight?
  value = apply_text_style(value) if text_style?
  value = wrap_with_html_tag("sup", value) if superscript?
  value = wrap_with_html_tag("sub", value) if subscript?
  value = wrap_with_link(value) if link?
  value
end

#to_plain_text(separator: " ") ⇒ Object



47
48
49
# File 'lib/tip_tap/nodes/text.rb', line 47

def to_plain_text(separator: " ")
  text
end

#underline?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/tip_tap/nodes/text.rb', line 80

def underline?
  has_mark_with_type?("underline")
end