Class: TipTap::Nodes::Text
Instance Attribute Summary collapse
#output_buffer
Attributes included from HasContent
#attrs, #content
Class Method Summary
collapse
Instance Method Summary
collapse
#include_empty_content_in_json?
#html_attributes, #html_class_name, #html_tag, included, #inline_styles
Methods included from HasContent
#add_content, #blank?, #each, #find_node, included, #size
included, #type_name
Constructor Details
#initialize(content, **attributes) {|_self| ... } ⇒ Text
Returns a new instance of Text.
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
#marks ⇒ Object
Returns the value of attribute marks.
10
11
12
|
# File 'lib/tip_tap/nodes/text.rb', line 10
def marks
@marks
end
|
#text ⇒ Object
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
76
77
78
|
# File 'lib/tip_tap/nodes/text.rb', line 76
def bold?
has_mark_with_type?("bold")
end
|
#code? ⇒ Boolean
92
93
94
|
# File 'lib/tip_tap/nodes/text.rb', line 92
def code?
has_mark_with_type?("code")
end
|
#highlight? ⇒ Boolean
104
105
106
|
# File 'lib/tip_tap/nodes/text.rb', line 104
def highlight?
has_mark_with_type?("highlight")
end
|
#italic? ⇒ Boolean
72
73
74
|
# File 'lib/tip_tap/nodes/text.rb', line 72
def italic?
has_mark_with_type?("italic")
end
|
#link? ⇒ Boolean
84
85
86
|
# File 'lib/tip_tap/nodes/text.rb', line 84
def link?
has_mark_with_type?("link")
end
|
#strike? ⇒ Boolean
88
89
90
|
# File 'lib/tip_tap/nodes/text.rb', line 88
def strike?
has_mark_with_type?("strike")
end
|
#subscript? ⇒ Boolean
100
101
102
|
# File 'lib/tip_tap/nodes/text.rb', line 100
def subscript?
has_mark_with_type?("subscript")
end
|
#superscript? ⇒ Boolean
96
97
98
|
# File 'lib/tip_tap/nodes/text.rb', line 96
def superscript?
has_mark_with_type?("superscript")
end
|
#text_style? ⇒ Boolean
108
109
110
|
# File 'lib/tip_tap/nodes/text.rb', line 108
def text_style?
has_mark_with_type?("textStyle")
end
|
#to_h ⇒ Object
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_html ⇒ Object
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 = content_tag(:sup, value) if superscript?
value = content_tag(:sub, value) if subscript?
value = highlight_tag(value) if highlight?
value = content_tag(:code, value) if code?
value = content_tag(:u, value) if underline?
value = content_tag(:em, value) if italic?
value = content_tag(:strong, value) if bold?
value = content_tag(:s, value) if strike?
value = content_tag(:a, value, href: link_href, target: link_target) if link?
value = content_tag(: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
80
81
82
|
# File 'lib/tip_tap/nodes/text.rb', line 80
def underline?
has_mark_with_type?("underline")
end
|