Class: Markbridge::AST::Url

Inherits:
Element show all
Defined in:
lib/markbridge/ast/url.rb

Overview

Represents a hyperlink/URL element.

Examples:

URL with explicit href

url = AST::Url.new(href: "https://example.com")
url << AST::Text.new("Click here")

URL with text as href

url = AST::Url.new(href: "https://example.com")
url << AST::Text.new("https://example.com")

Instance Attribute Summary collapse

Attributes inherited from Element

#children

Instance Method Summary collapse

Methods inherited from Element

#<<, #descendants, #each_descendant, #replace_child

Constructor Details

#initialize(href: nil) ⇒ Url

Create a new URL element.

Parameters:

  • href (String, nil) (defaults to: nil)

    the URL/href for this link



21
22
23
24
# File 'lib/markbridge/ast/url.rb', line 21

def initialize(href: nil)
  super()
  @href = href
end

Instance Attribute Details

#hrefString? (readonly)

Returns the URL/href for this link.

Returns:

  • (String, nil)

    the URL/href for this link



16
17
18
# File 'lib/markbridge/ast/url.rb', line 16

def href
  @href
end

Instance Method Details

#bare?Boolean

Whether this link is "bare" — it has no link text of its own: either no children, or a single Text child whose content is exactly the href (how parsers model a URL that stands for itself). Consumers that record or rewrite links need this judgment too (a bare URL must stay bare to keep autolinking and oneboxing working), so it lives on the node rather than in the renderer.

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/markbridge/ast/url.rb', line 35

def bare?
  return true if children.empty?

  children.size == 1 && children.first.instance_of?(Text) && children.first.text == href
end