Skip to content

Commit bf613dc

Browse files
authored
Merge pull request #77 from nicoschoenmaker/feature/link-display
Shorten output when link target and display match
2 parents c6490bd + 7c8a3c2 commit bf613dc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Html2Text.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,16 @@ protected function buildlinkList($link, $display, $linkOverride = null)
434434

435435
return $display . ' [' . ($index + 1) . ']';
436436
} elseif ($linkMethod == 'nextline') {
437+
if ($url === $display) {
438+
return $display;
439+
}
437440
return $display . "\n[" . $url . ']';
438441
} elseif ($linkMethod == 'bbcode') {
439442
return sprintf('[url=%s]%s[/url]', $url, $display);
440443
} else { // link_method defaults to inline
444+
if ($url === $display) {
445+
return $display;
446+
}
441447
return $display . ' [' . $url . ']';
442448
}
443449
}

test/LinkTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,18 @@ public function testDoLinksBBCode()
145145

146146
$this->assertEquals($expected, $html2text->getText());
147147
}
148+
149+
public function testDoLinksWhenTargetInText()
150+
{
151+
$html = '<a href="http://example.com">http://example.com</a>';
152+
$expected = 'http://example.com';
153+
154+
$html2text = new Html2Text($html, array('do_links' => 'inline'));
155+
156+
$this->assertEquals($expected, $html2text->getText());
157+
158+
$html2text = new Html2Text($html, array('do_links' => 'nextline'));
159+
160+
$this->assertEquals($expected, $html2text->getText());
161+
}
148162
}

0 commit comments

Comments
 (0)