From 654955dfdcc3d27eff03863fcca03ae7bb9476a1 Mon Sep 17 00:00:00 2001 From: Gustavo Rubio Date: Tue, 24 Feb 2026 12:58:17 -0800 Subject: [PATCH 1/3] Support option to not wrap links in markdown style syntax and instead just use pure text --- lib/html2text.rb | 10 +++++++--- spec/html2text_spec.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/html2text.rb b/lib/html2text.rb index a5db0e3..a7e3650 100644 --- a/lib/html2text.rb +++ b/lib/html2text.rb @@ -4,12 +4,14 @@ class Html2Text attr_reader :doc + attr_writer :wrap_links - def initialize(doc) + def initialize(doc, wrap_links: true) @doc = doc + @wrap_links = wrap_links end - def self.convert(html) + def self.convert(html, wrap_links: true) html = html.to_s if office_document?(html) @@ -27,7 +29,7 @@ def self.convert(html) html = fix_newlines(replace_entities(html)) doc = Nokogiri::HTML(html) - new(doc).convert + new(doc, wrap_links: wrap_links).convert end def self.fix_newlines(text) @@ -202,6 +204,8 @@ def wrap_link(node, output) href != "http://#{output}" && href != "https://#{output}" output = if output.empty? href + elsif !output.empty? && !@wrap_links + "#{output}: #{href}" else "[#{output}](#{href})" end diff --git a/spec/html2text_spec.rb b/spec/html2text_spec.rb index 516d64d..61bb586 100644 --- a/spec/html2text_spec.rb +++ b/spec/html2text_spec.rb @@ -58,3 +58,14 @@ end end end + +describe 'Wrapped links' do + describe 'when using the wrap_links: false option' do + let(:link_text) { 'Click Here!' } + let(:expected_text) { 'Click Here!: https://example.com' } + + it 'returns an unwrapped link' do + expect(Html2Text.convert(link_text, wrap_links: false)).to eq(expected_text) + end + end +end From 115a23788cec54745c9807792c226df4c8c828f2 Mon Sep 17 00:00:00 2001 From: Gustavo Rubio Date: Tue, 24 Feb 2026 13:02:51 -0800 Subject: [PATCH 2/3] Bump up version since I made some changes --- lib/html2text/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/html2text/version.rb b/lib/html2text/version.rb index 8324de9..095f286 100644 --- a/lib/html2text/version.rb +++ b/lib/html2text/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Html2Text - VERSION = '0.4.0' + VERSION = '0.4.1' end From d2275bb6b09a3719db610847ca05b98ef3c96ac3 Mon Sep 17 00:00:00 2001 From: Michael Scrivo Date: Wed, 8 Jul 2026 15:17:16 -0400 Subject: [PATCH 3/3] Clean up wrap_links option for merge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review fixes on top of the original contribution: - simplify the redundant `!output.empty?` check (already guaranteed by the surrounding if-branch) - drop the write-only `attr_writer :wrap_links`; the value is consumed once during convert and `doc` only has a reader - revert the version bump — versioning happens at release time, and a new option is a minor bump, not a patch - add a CHANGELOG entry documenting the option and its scope (only `` links; images and named anchors keep their brackets) - move the spec out of the top of html2text_spec.rb's scope into its own spec/wrap_links_spec.rb, and cover the default format, the initializer path, linked images, empty link text, and href-equals- text/mailto cases alongside the original example Co-Authored-By: Claude Fable 5 --- CHANGELOG.md | 3 +++ lib/html2text.rb | 5 ++--- lib/html2text/version.rb | 2 +- spec/html2text_spec.rb | 11 ----------- spec/wrap_links_spec.rb | 42 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 spec/wrap_links_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index d78f4b2..b1cb57b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/soundasleep/html2text_ruby/compare/v0.4.0...master) +### Added +- 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 `` links — images and named anchors keep their `[bracketed]` format ([#40](https://github.com/soundasleep/html2text_ruby/pull/40)) + ### Changed - Require Ruby 3.3+; CI now tests Ruby 3.3, 3.4 and 4.0 ([#42](https://github.com/soundasleep/html2text_ruby/pull/42)) - 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)) diff --git a/lib/html2text.rb b/lib/html2text.rb index a7e3650..0a562c5 100644 --- a/lib/html2text.rb +++ b/lib/html2text.rb @@ -4,7 +4,6 @@ class Html2Text attr_reader :doc - attr_writer :wrap_links def initialize(doc, wrap_links: true) @doc = doc @@ -177,7 +176,7 @@ def suffix_whitespace(node) end # rubocop:enable Lint/DuplicateBranch - # links are returned in [text](link) format + # links are returned in [text](link) format, or "text: link" with wrap_links: false def wrap_link(node, output) href = node.attribute('href') name = node.attribute('name') @@ -204,7 +203,7 @@ def wrap_link(node, output) href != "http://#{output}" && href != "https://#{output}" output = if output.empty? href - elsif !output.empty? && !@wrap_links + elsif !@wrap_links "#{output}: #{href}" else "[#{output}](#{href})" diff --git a/lib/html2text/version.rb b/lib/html2text/version.rb index 095f286..8324de9 100644 --- a/lib/html2text/version.rb +++ b/lib/html2text/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Html2Text - VERSION = '0.4.1' + VERSION = '0.4.0' end diff --git a/spec/html2text_spec.rb b/spec/html2text_spec.rb index 61bb586..516d64d 100644 --- a/spec/html2text_spec.rb +++ b/spec/html2text_spec.rb @@ -58,14 +58,3 @@ end end end - -describe 'Wrapped links' do - describe 'when using the wrap_links: false option' do - let(:link_text) { 'Click Here!' } - let(:expected_text) { 'Click Here!: https://example.com' } - - it 'returns an unwrapped link' do - expect(Html2Text.convert(link_text, wrap_links: false)).to eq(expected_text) - end - end -end diff --git a/spec/wrap_links_spec.rb b/spec/wrap_links_spec.rb new file mode 100644 index 0000000..6869e7e --- /dev/null +++ b/spec/wrap_links_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Html2Text do + describe 'the wrap_links option' do + it 'defaults to wrapping links in [text](href) format' do + text = Html2Text.convert('Click Here!') + expect(text).to eq('[Click Here!](https://example.com)') + end + + it 'renders links as "text: href" with wrap_links: false' do + text = Html2Text.convert('Click Here!', wrap_links: false) + expect(text).to eq('Click Here!: https://example.com') + end + + it 'is also accepted by the initializer' do + doc = Nokogiri::HTML('Click Here!') + expect(Html2Text.new(doc, wrap_links: false).convert).to eq('Click Here!: https://example.com') + end + + it 'unwraps linked images' do + text = Html2Text.convert('two', wrap_links: false) + expect(text).to eq('two: http://x.com') + end + + it 'still renders links with no text as the bare href' do + text = Html2Text.convert('', wrap_links: false) + expect(text).to eq('http://x.com') + end + + it 'still renders links whose text equals the href as just the text' do + text = Html2Text.convert('http://example.com', wrap_links: false) + expect(text).to eq('http://example.com') + end + + it 'still renders mailto links matching their text as just the text' do + text = Html2Text.convert('a@b.com', wrap_links: false) + expect(text).to eq('a@b.com') + end + end +end