Skip to content

Commit

Permalink
Merge pull request #4 from taylornetwork/fix-check-existing-formatting
Browse files Browse the repository at this point in the history
Fix check existing formatting
  • Loading branch information
itssamtaylor authored May 10, 2019
2 parents d13ac8a + afc20a3 commit d1c30f9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
composer.lock
vendor/
58 changes: 44 additions & 14 deletions src/Linkify.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,8 @@ public function setConfig(string $key, $value)
*/
public function markdown(string $caption, string $url)
{
if ($this->config['checkForExistingFormatting']) {
$pos = strpos($this->text, $url);

if ($this->text[$pos - 2] === ']' && $this->text[$pos - 1] === '(' && $this->text[$pos + strlen($url)] === ')') {
return $url;
}
if ($this->config['checkForExistingFormatting'] && $this->hasExistingFormatting($url)) {
return $url;
}

return '['.$caption.']('.$url.')';
Expand All @@ -122,12 +118,8 @@ public function markdown(string $caption, string $url)
*/
public function HTML(string $caption, string $url)
{
if ($this->config['checkForExistingFormatting']) {
$pos = strpos($this->text, $url);

if ($this->text[$pos - 1] === '>' && substr($this->text, $pos + strlen($url), 4) === '</a>') {
return $url;
}
if ($this->config['checkForExistingFormatting'] && $this->hasExistingFormatting($url)) {
return $url;
}

$attributes = [''];
Expand Down Expand Up @@ -157,16 +149,54 @@ public function custom(string $caption, string $url)
return $url;
}

/**
* Check for existing formatting.
*
* @param string $url
*
* @return bool
*/
public function hasExistingFormatting(string $url)
{
$pos = strpos($this->text, $url);

if ($this->text[$pos - 2] === ']' && $this->text[$pos - 1] === '(' && $this->text[$pos + strlen($url)] === ')') {
return true;
}

if (substr($this->text, $pos - 6, 5) === 'href=' && $this->text[$pos + strlen($url)] === '"') {
return true;
}

return false;
}

/**
* Set text.
*
* @param string $text
*
* @return $this
*/
public function setText(string $text)
{
$this->text = $text;

return $this;
}

/**
* Parse text.
*
* @param string $text
*
* @return string
*/
public function parse(string $text)
public function parse(string $text = null)
{
$this->text = $text;
if (!is_null($text)) {
$this->text = $text;
}

$callback = function ($urlMatch) {
$url = $urlMatch[0];
Expand Down
47 changes: 33 additions & 14 deletions tests/LinkifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public function testRegularCall()
$linkify = new Linkify();

$this->assertEquals(
$linkify->parse('Google Link: https://www.google.com!'),
'Google Link: [google.com](https://www.google.com)!'
'Google Link: [google.com](https://www.google.com)!',
$linkify->parse('Google Link: https://www.google.com!')
);
}

public function testStaticCall()
{
$this->assertEquals(
Linkify::instance()->parse('Google Link: https://www.google.com!'),
'Google Link: [google.com](https://www.google.com)!'
'Google Link: [google.com](https://www.google.com)!',
Linkify::instance()->parse('Google Link: https://www.google.com!')
);
}

Expand All @@ -42,8 +42,8 @@ public function testConvertHTML()
$linkify->setConfig('convertTo', Linkify::ConvertHTML);

$this->assertEquals(
$linkify->parse('Google Link: https://www.google.com!'),
'Google Link: <a href="https://www.google.com">google.com</a>!'
'Google Link: <a href="https://www.google.com">google.com</a>!',
$linkify->parse('Google Link: https://www.google.com!')
);
}

Expand All @@ -58,8 +58,8 @@ public function testConvertHTMLWithAttributes()
]);

$this->assertEquals(
$linkify->parse('Google Link: https://www.google.com!'),
'Google Link: <a class="btn-link" target="_blank" href="https://www.google.com">google.com</a>!'
'Google Link: <a class="btn-link" target="_blank" href="https://www.google.com">google.com</a>!',
$linkify->parse('Google Link: https://www.google.com!')
);
}

Expand All @@ -68,8 +68,8 @@ public function testMultipleMarkdown()
$linkify = new Linkify();

$this->assertEquals(
$linkify->parse('Google Link: https://www.google.com! And another: https://github.com/taylornetwork/linkify'),
'Google Link: [google.com](https://www.google.com)! And another: [github.com](https://github.com/taylornetwork/linkify)'
'Google Link: [google.com](https://www.google.com)! And another: [github.com](https://github.com/taylornetwork/linkify)',
$linkify->parse('Google Link: https://www.google.com! And another: https://github.com/taylornetwork/linkify')
);
}

Expand All @@ -82,8 +82,8 @@ public function testCustomCallback()
});

$this->assertEquals(
$linkify->parse('Google Link: https://www.google.com!'),
'Google Link: {google.com}**https://www.google.com**!'
'Google Link: {google.com}**https://www.google.com**!',
$linkify->parse('Google Link: https://www.google.com!')
);
}

Expand All @@ -92,8 +92,27 @@ public function testTraitWithCustom()
$testClass = new TestingClass();

$this->assertEquals(
$testClass->linkify('Google Link: https://www.google.com!'),
'Google Link: ^https://www.google.com^$google.com$!'
'Google Link: ^https://www.google.com^$google.com$!',
$testClass->linkify('Google Link: https://www.google.com!')
);
}

public function testHasExisting()
{
$linkify = new Linkify();
$this->assertEquals(
'Google Link: [google.com](https://www.google.com)!',
$linkify->parse('Google Link: [google.com](https://www.google.com)!')
);
}

public function testHasExistingOtherFormat()
{
$linkify = new Linkify();
$linkify->setConfig('convertTo', Linkify::ConvertMarkdown);
$this->assertEquals(
'Google Link: <a href="https://www.google.com">google.com</a>!',
$linkify->parse('Google Link: <a href="https://www.google.com">google.com</a>!')
);
}
}

0 comments on commit d1c30f9

Please sign in to comment.