Sha256: 0ea784dc7cb3ee1a4ebfe7500cea85ecc6a48cc61ff004dfe2a37fdffea493c1

Contents?: true

Size: 724 Bytes

Versions: 11

Compression:

Stored size: 724 Bytes

Contents

require "dtext/dtext"
require "nokogiri"

module DTextRagel
  class Error < StandardError; end

  def self.parse_inline(str)
    parse(str, :inline => true)
  end

  def self.parse_strip(str)
    parse(str, :strip => true)
  end

  def self.parse(str, strip: false, inline: false, disable_mentions: false, base_url: nil)
    html = c_parse(str, strip, inline, disable_mentions)
    html = resolve_relative_urls(html, base_url) if base_url
    html
  end

  private

  def self.resolve_relative_urls(html, base_url)
    nodes = Nokogiri::HTML.fragment(html)
    nodes.traverse do |node|
      if node[:href]&.start_with?("/")
        node[:href] = base_url.chomp("/") + node[:href]
      end
    end
    nodes.to_s
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dtext_rb-1.9.2 lib/dtext.rb
dtext_rb-1.9.1 lib/dtext.rb
dtext_rb-1.9.0 lib/dtext.rb
dtext_rb-1.8.0 lib/dtext.rb
dtext_rb-1.7.0 lib/dtext.rb
dtext_rb-1.6.1 lib/dtext.rb
dtext_rb-1.6.0 lib/dtext.rb
dtext_rb-1.5.1 lib/dtext.rb
dtext_rb-1.5.0 lib/dtext.rb
dtext_rb-1.4.4 lib/dtext.rb
dtext_rb-1.4.3 lib/dtext.rb