Skip to content

Commit c7273e8

Browse files
committed
Better output mechanisms for URL Type
1 parent dca2922 commit c7273e8

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

_test/Type_Url.test.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ public function validateSuccessProvider() {
5050
);
5151
}
5252

53+
/**
54+
* Provide data to test autoshortening feature
55+
*
56+
* @return array
57+
*/
58+
public function generateAutoTitleProvider() {
59+
return [
60+
['https://foobar.com', 'foobar.com'],
61+
['https://foobar.com/', 'foobar.com'],
62+
['https://www.foobar.com/', 'foobar.com'],
63+
['https://www.foobar.com/test', 'foobar.com/…'],
64+
['https://www.foobar.com/?test', 'foobar.com/…'],
65+
['https://www.foobar.com/#hash', 'foobar.com/…'],
66+
];
67+
}
68+
5369
/**
5470
* @expectedException \dokuwiki\plugin\struct\meta\ValidationException
5571
* @dataProvider validateFailProvider
@@ -67,4 +83,26 @@ public function test_validate_success($value, $prefix, $postfix, $autoscheme) {
6783
$url->validate($value);
6884
$this->assertTrue(true); // we simply check that no exceptions are thrown
6985
}
86+
87+
/**
88+
* @dataProvider generateAutoTitleProvider
89+
*/
90+
public function test_generateAutoTitle($input, $title) {
91+
$url = new Url(['autoshorten' => true]);
92+
$result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
93+
$this->assertSame($title, $result);
94+
95+
$url = new Url(['autoshorten' => false]);
96+
$result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
97+
$this->assertSame($input, $result);
98+
}
99+
100+
public function test_generateFixedTitle() {
101+
$input = 'https://www.foobar.com/long';
102+
$title = 'oink';
103+
104+
$url = new Url(['fixedtitle' => $title]);
105+
$result = $this->callInaccessibleMethod($url, 'generateTitle', [$input]);
106+
$this->assertSame($title, $result);
107+
}
70108
}

types/Url.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Url extends Text
1111
'autoscheme' => 'https',
1212
'prefix' => '',
1313
'postfix' => '',
14+
'fixedtitle' => '',
15+
'autoshorten' => true,
1416
);
1517

1618
/**
@@ -43,10 +45,38 @@ public function validate($rawvalue)
4345
public function renderValue($value, \Doku_Renderer $R, $mode)
4446
{
4547
$url = $this->buildURL($value);
46-
$R->externallink($url);
48+
$title = $this->generateTitle($url);
49+
$R->externallink($url, $title);
4750
return true;
4851
}
4952

53+
/**
54+
* Make a label for the link
55+
*
56+
* @param $url
57+
* @return string
58+
*/
59+
protected function generateTitle($url) {
60+
if($this->config['fixedtitle']) return $this->config['fixedtitle'];
61+
if(!$this->config['autoshorten']) return $url;
62+
63+
$parsed = parse_url($url);
64+
65+
$title = $parsed['host'];
66+
$title = preg_replace('/^www\./i', '', $title);
67+
if(isset($parsed['path']) && $parsed['path'] === '/') {
68+
unset($parsed['path']);
69+
}
70+
if(
71+
isset($parsed['path']) ||
72+
isset($parsed['query']) ||
73+
isset($parsed['fragment'])
74+
) {
75+
$title .= '/…';
76+
}
77+
return $title;
78+
}
79+
5080
/**
5181
* Creates the full URL and applies the autoscheme if needed
5282
*

0 commit comments

Comments
 (0)