Skip to content

Commit a860299

Browse files
authored
Merge pull request #40 from gusrub/gusrub/optional-link-wrapping
feat: add wrap_links option to render links as plain "text: href"
2 parents f681562 + d2275bb commit a860299

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased](https://github.com/soundasleep/html2text_ruby/compare/v0.4.0...master)
8+
### Added
9+
- Add a `wrap_links:` option to `Html2Text.convert` and `Html2Text.new` (default `true`); when `false`, hyperlinks render as `text: href` instead of markdown-style `[text](href)`. Only affects `<a href>` links — images and named anchors keep their `[bracketed]` format ([#40](https://github.com/soundasleep/html2text_ruby/pull/40))
10+
811
### Changed
912
- Require Ruby 3.3+; CI now tests Ruby 3.3, 3.4 and 4.0 ([#42](https://github.com/soundasleep/html2text_ruby/pull/42))
1013
- Update development dependencies (rubocop 1.88, nokogiri 1.19 in the lockfile), removing the transitive `rexml` dependency ([#42](https://github.com/soundasleep/html2text_ruby/pull/42))

‎lib/html2text.rb‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
class Html2Text
66
attr_reader :doc
77

8-
def initialize(doc)
8+
def initialize(doc, wrap_links: true)
99
@doc = doc
10+
@wrap_links = wrap_links
1011
end
1112

12-
def self.convert(html)
13+
def self.convert(html, wrap_links: true)
1314
html = html.to_s
1415

1516
if office_document?(html)
@@ -27,7 +28,7 @@ def self.convert(html)
2728
html = fix_newlines(replace_entities(html))
2829
doc = Nokogiri::HTML(html)
2930

30-
new(doc).convert
31+
new(doc, wrap_links: wrap_links).convert
3132
end
3233

3334
def self.fix_newlines(text)
@@ -162,7 +163,7 @@ def suffix_whitespace(node)
162163
end
163164
end
164165

165-
# links are returned in [text](link) format
166+
# links are returned in [text](link) format, or "text: link" with wrap_links: false
166167
def wrap_link(node, output)
167168
href = node.attribute('href')
168169
name = node.attribute('name')
@@ -189,6 +190,8 @@ def wrap_link(node, output)
189190
href != "http://#{output}" && href != "https://#{output}"
190191
output = if output.empty?
191192
href
193+
elsif !@wrap_links
194+
"#{output}: #{href}"
192195
else
193196
"[#{output}](#{href})"
194197
end

‎spec/wrap_links_spec.rb‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)