Skip to content

Commit

Permalink
Merge pull request #77 from nicoschoenmaker/feature/link-display
Browse files Browse the repository at this point in the history
Shorten output when link target and display match
  • Loading branch information
mtibben committed May 15, 2017
2 parents c6490bd + 7c8a3c2 commit bf613dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Html2Text.php
Expand Up @@ -434,10 +434,16 @@ protected function buildlinkList($link, $display, $linkOverride = null)

return $display . ' [' . ($index + 1) . ']';
} elseif ($linkMethod == 'nextline') {
if ($url === $display) {
return $display;
}
return $display . "\n[" . $url . ']';
} elseif ($linkMethod == 'bbcode') {
return sprintf('[url=%s]%s[/url]', $url, $display);
} else { // link_method defaults to inline
if ($url === $display) {
return $display;
}
return $display . ' [' . $url . ']';
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/LinkTest.php
Expand Up @@ -145,4 +145,18 @@ public function testDoLinksBBCode()

$this->assertEquals($expected, $html2text->getText());
}

public function testDoLinksWhenTargetInText()
{
$html = '<a href="http://example.com">http://example.com</a>';
$expected = 'http://example.com';

$html2text = new Html2Text($html, array('do_links' => 'inline'));

$this->assertEquals($expected, $html2text->getText());

$html2text = new Html2Text($html, array('do_links' => 'nextline'));

$this->assertEquals($expected, $html2text->getText());
}
}

0 comments on commit bf613dc

Please sign in to comment.