|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Html2Text do |
| 6 | + describe 'the wrap_links option' do |
| 7 | + it 'defaults to wrapping links in [text](href) format' do |
| 8 | + text = Html2Text.convert('<a href="https://example.com">Click Here!</a>') |
| 9 | + expect(text).to eq('[Click Here!](https://example.com)') |
| 10 | + end |
| 11 | + |
| 12 | + it 'renders links as "text: href" with wrap_links: false' do |
| 13 | + text = Html2Text.convert('<a href="https://example.com">Click Here!</a>', wrap_links: false) |
| 14 | + expect(text).to eq('Click Here!: https://example.com') |
| 15 | + end |
| 16 | + |
| 17 | + it 'is also accepted by the initializer' do |
| 18 | + doc = Nokogiri::HTML('<a href="https://example.com">Click Here!</a>') |
| 19 | + expect(Html2Text.new(doc, wrap_links: false).convert).to eq('Click Here!: https://example.com') |
| 20 | + end |
| 21 | + |
| 22 | + it 'unwraps linked images' do |
| 23 | + text = Html2Text.convert('<a href="http://x.com"><img src="i.png" alt="two"></a>', wrap_links: false) |
| 24 | + expect(text).to eq('two: http://x.com') |
| 25 | + end |
| 26 | + |
| 27 | + it 'still renders links with no text as the bare href' do |
| 28 | + text = Html2Text.convert('<a href="http://x.com"></a>', wrap_links: false) |
| 29 | + expect(text).to eq('http://x.com') |
| 30 | + end |
| 31 | + |
| 32 | + it 'still renders links whose text equals the href as just the text' do |
| 33 | + text = Html2Text.convert('<a href="http://example.com">http://example.com</a>', wrap_links: false) |
| 34 | + expect(text).to eq('http://example.com') |
| 35 | + end |
| 36 | + |
| 37 | + it 'still renders mailto links matching their text as just the text' do |
| 38 | + text = Html2Text.convert('<a href="mailto:a@b.com">a@b.com</a>', wrap_links: false) |
| 39 | + expect(text).to eq('a@b.com') |
| 40 | + end |
| 41 | + end |
| 42 | +end |
0 commit comments