ParsedownTest.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Page\Markdown\Excerpts;
  5. use Grav\Common\Uri;
  6. use Grav\Common\Config\Config;
  7. use Grav\Common\Page\Pages;
  8. use Grav\Common\Markdown\Parsedown;
  9. use Grav\Common\Language\Language;
  10. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  11. /**
  12. * Class ParsedownTest
  13. */
  14. class ParsedownTest extends \Codeception\TestCase\Test
  15. {
  16. /** @var Parsedown $parsedown */
  17. protected $parsedown;
  18. /** @var Grav $grav */
  19. protected $grav;
  20. /** @var Pages $pages */
  21. protected $pages;
  22. /** @var Config $config */
  23. protected $config;
  24. /** @var Uri $uri */
  25. protected $uri;
  26. /** @var Language $language */
  27. protected $language;
  28. protected $old_home;
  29. protected function _before(): void
  30. {
  31. $grav = Fixtures::get('grav');
  32. $this->grav = $grav();
  33. $this->pages = $this->grav['pages'];
  34. $this->config = $this->grav['config'];
  35. $this->uri = $this->grav['uri'];
  36. $this->language = $this->grav['language'];
  37. $this->old_home = $this->config->get('system.home.alias');
  38. $this->config->set('system.home.alias', '/item1');
  39. $this->config->set('system.absolute_urls', false);
  40. $this->config->set('system.languages.supported', []);
  41. unset($this->grav['language']);
  42. $this->grav['language'] = new Language($this->grav);
  43. /** @var UniformResourceLocator $locator */
  44. $locator = $this->grav['locator'];
  45. $locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false);
  46. $this->pages->init();
  47. $defaults = [
  48. 'markdown' => [
  49. 'extra' => false,
  50. 'auto_line_breaks' => false,
  51. 'auto_url_links' => false,
  52. 'escape_markup' => false,
  53. 'special_chars' => ['>' => 'gt', '<' => 'lt'],
  54. ],
  55. 'images' => $this->config->get('system.images', [])
  56. ];
  57. $page = $this->pages->find('/item2/item2-2');
  58. $excerpts = new Excerpts($page, $defaults);
  59. $this->parsedown = new Parsedown($excerpts);
  60. }
  61. protected function _after(): void
  62. {
  63. $this->config->set('system.home.alias', $this->old_home);
  64. }
  65. public function testImages(): void
  66. {
  67. $this->config->set('system.languages.supported', ['fr','en']);
  68. unset($this->grav['language']);
  69. $this->grav['language'] = new Language($this->grav);
  70. $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
  71. self::assertSame(
  72. '<p><img alt="" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  73. $this->parsedown->text('![](sample-image.jpg)')
  74. );
  75. self::assertRegexp(
  76. '|<p><img alt="" src="\/images\/.*-cache-image.jpe?g\?foo=1" \/><\/p>|',
  77. $this->parsedown->text('![](cache-image.jpg?cropResize=200,200&foo)')
  78. );
  79. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  80. self::assertSame(
  81. '<p><img alt="" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  82. $this->parsedown->text('![](sample-image.jpg)')
  83. );
  84. self::assertRegexp(
  85. '|<p><img alt="" src="\/images\/.*-cache-image.jpe?g\?foo=1" \/><\/p>|',
  86. $this->parsedown->text('![](cache-image.jpg?cropResize=200,200&foo)')
  87. );
  88. self::assertRegexp(
  89. '|<p><img alt="" src="\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
  90. $this->parsedown->text('![](/home-cache-image.jpg?cache)')
  91. );
  92. self::assertSame(
  93. '<p><img src="/item2/item2-2/missing-image.jpg" alt="" /></p>',
  94. $this->parsedown->text('![](missing-image.jpg)')
  95. );
  96. self::assertSame(
  97. '<p><img src="/home-missing-image.jpg" alt="" /></p>',
  98. $this->parsedown->text('![](/home-missing-image.jpg)')
  99. );
  100. self::assertSame(
  101. '<p><img src="/home-missing-image.jpg" alt="" /></p>',
  102. $this->parsedown->text('![](/home-missing-image.jpg)')
  103. );
  104. self::assertSame(
  105. '<p><img src="https://getgrav-grav.netdna-ssl.com/user/pages/media/grav-logo.svg" alt="" /></p>',
  106. $this->parsedown->text('![](https://getgrav-grav.netdna-ssl.com/user/pages/media/grav-logo.svg)')
  107. );
  108. }
  109. public function testImagesSubDir(): void
  110. {
  111. $this->config->set('system.images.cache_all', false);
  112. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  113. self::assertRegexp(
  114. '|<p><img alt="" src="\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
  115. $this->parsedown->text('![](/home-cache-image.jpg?cache)')
  116. );
  117. self::assertSame(
  118. '<p><img alt="" src="/subdir/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  119. $this->parsedown->text('![](sample-image.jpg)')
  120. );
  121. self::assertRegexp(
  122. '|<p><img alt="" src="\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',
  123. $this->parsedown->text('![](cache-image.jpg?cache)')
  124. );
  125. self::assertSame(
  126. '<p><img src="/subdir/item2/item2-2/missing-image.jpg" alt="" /></p>',
  127. $this->parsedown->text('![](missing-image.jpg)')
  128. );
  129. self::assertSame(
  130. '<p><img src="/subdir/home-missing-image.jpg" alt="" /></p>',
  131. $this->parsedown->text('![](/home-missing-image.jpg)')
  132. );
  133. }
  134. public function testImagesAbsoluteUrls(): void
  135. {
  136. $this->config->set('system.absolute_urls', true);
  137. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  138. self::assertSame(
  139. '<p><img alt="" src="http://testing.dev/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  140. $this->parsedown->text('![](sample-image.jpg)')
  141. );
  142. self::assertRegexp(
  143. '|<p><img alt="" src="http:\/\/testing.dev\/images\/.*-cache-image.jpe?g" \/><\/p>|',
  144. $this->parsedown->text('![](cache-image.jpg?cache)')
  145. );
  146. self::assertRegexp(
  147. '|<p><img alt="" src="http:\/\/testing.dev\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
  148. $this->parsedown->text('![](/home-cache-image.jpg?cache)')
  149. );
  150. self::assertSame(
  151. '<p><img src="http://testing.dev/item2/item2-2/missing-image.jpg" alt="" /></p>',
  152. $this->parsedown->text('![](missing-image.jpg)')
  153. );
  154. self::assertSame(
  155. '<p><img src="http://testing.dev/home-missing-image.jpg" alt="" /></p>',
  156. $this->parsedown->text('![](/home-missing-image.jpg)')
  157. );
  158. }
  159. public function testImagesSubDirAbsoluteUrls(): void
  160. {
  161. $this->config->set('system.absolute_urls', true);
  162. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  163. self::assertSame(
  164. '<p><img alt="" src="http://testing.dev/subdir/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  165. $this->parsedown->text('![](sample-image.jpg)')
  166. );
  167. self::assertRegexp(
  168. '|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',
  169. $this->parsedown->text('![](cache-image.jpg?cache)')
  170. );
  171. self::assertRegexp(
  172. '|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
  173. $this->parsedown->text('![](/home-cache-image.jpg?cropResize=200,200)')
  174. );
  175. self::assertSame(
  176. '<p><img src="http://testing.dev/subdir/item2/item2-2/missing-image.jpg" alt="" /></p>',
  177. $this->parsedown->text('![](missing-image.jpg)')
  178. );
  179. self::assertSame(
  180. '<p><img src="http://testing.dev/subdir/home-missing-image.jpg" alt="" /></p>',
  181. $this->parsedown->text('![](/home-missing-image.jpg)')
  182. );
  183. }
  184. public function testImagesAttributes(): void
  185. {
  186. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  187. self::assertSame(
  188. '<p><img title="My Title" alt="" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  189. $this->parsedown->text('![](sample-image.jpg "My Title")')
  190. );
  191. self::assertSame(
  192. '<p><img alt="" class="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  193. $this->parsedown->text('![](sample-image.jpg?classes=foo)')
  194. );
  195. self::assertSame(
  196. '<p><img alt="" class="foo bar" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  197. $this->parsedown->text('![](sample-image.jpg?classes=foo,bar)')
  198. );
  199. self::assertSame(
  200. '<p><img alt="" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  201. $this->parsedown->text('![](sample-image.jpg?id=foo)')
  202. );
  203. self::assertSame(
  204. '<p><img alt="Alt Text" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  205. $this->parsedown->text('![Alt Text](sample-image.jpg?id=foo)')
  206. );
  207. self::assertSame(
  208. '<p><img alt="Alt Text" class="bar" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  209. $this->parsedown->text('![Alt Text](sample-image.jpg?class=bar&id=foo)')
  210. );
  211. self::assertSame(
  212. '<p><img title="My Title" alt="Alt Text" class="bar" id="foo" src="/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  213. $this->parsedown->text('![Alt Text](sample-image.jpg?class=bar&id=foo "My Title")')
  214. );
  215. }
  216. public function testRootImages(): void
  217. {
  218. $this->uri->initializeWithURL('http://testing.dev/')->init();
  219. $defaults = [
  220. 'markdown' => [
  221. 'extra' => false,
  222. 'auto_line_breaks' => false,
  223. 'auto_url_links' => false,
  224. 'escape_markup' => false,
  225. 'special_chars' => ['>' => 'gt', '<' => 'lt'],
  226. ],
  227. 'images' => $this->config->get('system.images', [])
  228. ];
  229. $page = $this->pages->find('/');
  230. $excerpts = new Excerpts($page, $defaults);
  231. $this->parsedown = new Parsedown($excerpts);
  232. self::assertSame(
  233. '<p><img alt="" src="/tests/fake/nested-site/user/pages/01.item1/home-sample-image.jpg" /></p>',
  234. $this->parsedown->text('![](home-sample-image.jpg)')
  235. );
  236. self::assertRegexp(
  237. '|<p><img alt="" src="\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
  238. $this->parsedown->text('![](home-cache-image.jpg?cache)')
  239. );
  240. self::assertRegexp(
  241. '|<p><img alt="" src="\/images\/.*-home-cache-image.jpe?g\?foo=1" \/><\/p>|',
  242. $this->parsedown->text('![](home-cache-image.jpg?cropResize=200,200&foo)')
  243. );
  244. self::assertSame(
  245. '<p><img src="/home-missing-image.jpg" alt="" /></p>',
  246. $this->parsedown->text('![](/home-missing-image.jpg)')
  247. );
  248. $this->config->set('system.languages.supported', ['fr','en']);
  249. unset($this->grav['language']);
  250. $this->grav['language'] = new Language($this->grav);
  251. $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
  252. self::assertSame(
  253. '<p><img alt="" src="/tests/fake/nested-site/user/pages/01.item1/home-sample-image.jpg" /></p>',
  254. $this->parsedown->text('![](home-sample-image.jpg)')
  255. );
  256. }
  257. public function testRootImagesSubDirAbsoluteUrls(): void
  258. {
  259. $this->config->set('system.absolute_urls', true);
  260. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  261. self::assertSame(
  262. '<p><img alt="" src="http://testing.dev/subdir/tests/fake/nested-site/user/pages/02.item2/02.item2-2/sample-image.jpg" /></p>',
  263. $this->parsedown->text('![](sample-image.jpg)')
  264. );
  265. self::assertRegexp(
  266. '|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-cache-image.jpe?g" \/><\/p>|',
  267. $this->parsedown->text('![](cache-image.jpg?cache)')
  268. );
  269. self::assertRegexp(
  270. '|<p><img alt="" src="http:\/\/testing.dev\/subdir\/images\/.*-home-cache-image.jpe?g" \/><\/p>|',
  271. $this->parsedown->text('![](/home-cache-image.jpg?cropResize=200,200)')
  272. );
  273. self::assertSame(
  274. '<p><img src="http://testing.dev/subdir/item2/item2-2/missing-image.jpg" alt="" /></p>',
  275. $this->parsedown->text('![](missing-image.jpg)')
  276. );
  277. self::assertSame(
  278. '<p><img src="http://testing.dev/subdir/home-missing-image.jpg" alt="" /></p>',
  279. $this->parsedown->text('![](/home-missing-image.jpg)')
  280. );
  281. }
  282. public function testRootAbsoluteLinks(): void
  283. {
  284. $this->uri->initializeWithURL('http://testing.dev/')->init();
  285. $defaults = [
  286. 'markdown' => [
  287. 'extra' => false,
  288. 'auto_line_breaks' => false,
  289. 'auto_url_links' => false,
  290. 'escape_markup' => false,
  291. 'special_chars' => ['>' => 'gt', '<' => 'lt'],
  292. ],
  293. 'images' => $this->config->get('system.images', [])
  294. ];
  295. $page = $this->pages->find('/');
  296. $excerpts = new Excerpts($page, $defaults);
  297. $this->parsedown = new Parsedown($excerpts);
  298. self::assertSame(
  299. '<p><a href="/item1/item1-3">Down a Level</a></p>',
  300. $this->parsedown->text('[Down a Level](item1-3)')
  301. );
  302. self::assertSame(
  303. '<p><a href="/item2">Peer Page</a></p>',
  304. $this->parsedown->text('[Peer Page](../item2)')
  305. );
  306. self::assertSame(
  307. '<p><a href="/?foo=bar">With Query</a></p>',
  308. $this->parsedown->text('[With Query](?foo=bar)')
  309. );
  310. self::assertSame(
  311. '<p><a href="/foo:bar">With Param</a></p>',
  312. $this->parsedown->text('[With Param](/foo:bar)')
  313. );
  314. self::assertSame(
  315. '<p><a href="#foo">With Anchor</a></p>',
  316. $this->parsedown->text('[With Anchor](#foo)')
  317. );
  318. $this->config->set('system.languages.supported', ['fr','en']);
  319. unset($this->grav['language']);
  320. $this->grav['language'] = new Language($this->grav);
  321. $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
  322. self::assertSame(
  323. '<p><a href="/fr/item2">Peer Page</a></p>',
  324. $this->parsedown->text('[Peer Page](../item2)')
  325. );
  326. self::assertSame(
  327. '<p><a href="/fr/item1/item1-3">Down a Level</a></p>',
  328. $this->parsedown->text('[Down a Level](item1-3)')
  329. );
  330. self::assertSame(
  331. '<p><a href="/fr/?foo=bar">With Query</a></p>',
  332. $this->parsedown->text('[With Query](?foo=bar)')
  333. );
  334. self::assertSame(
  335. '<p><a href="/fr/foo:bar">With Param</a></p>',
  336. $this->parsedown->text('[With Param](/foo:bar)')
  337. );
  338. self::assertSame(
  339. '<p><a href="#foo">With Anchor</a></p>',
  340. $this->parsedown->text('[With Anchor](#foo)')
  341. );
  342. }
  343. public function testAnchorLinksLangRelativeUrls(): void
  344. {
  345. $this->config->set('system.languages.supported', ['fr','en']);
  346. unset($this->grav['language']);
  347. $this->grav['language'] = new Language($this->grav);
  348. $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
  349. self::assertSame(
  350. '<p><a href="#foo">Current Anchor</a></p>',
  351. $this->parsedown->text('[Current Anchor](#foo)')
  352. );
  353. self::assertSame(
  354. '<p><a href="/fr/#foo">Root Anchor</a></p>',
  355. $this->parsedown->text('[Root Anchor](/#foo)')
  356. );
  357. self::assertSame(
  358. '<p><a href="/fr/item2/item2-1#foo">Peer Anchor</a></p>',
  359. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  360. );
  361. self::assertSame(
  362. '<p><a href="/fr/item2/item2-1#foo">Peer Anchor 2</a></p>',
  363. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  364. );
  365. }
  366. public function testAnchorLinksLangAbsoluteUrls(): void
  367. {
  368. $this->config->set('system.absolute_urls', true);
  369. $this->config->set('system.languages.supported', ['fr','en']);
  370. unset($this->grav['language']);
  371. $this->grav['language'] = new Language($this->grav);
  372. $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init();
  373. self::assertSame(
  374. '<p><a href="#foo">Current Anchor</a></p>',
  375. $this->parsedown->text('[Current Anchor](#foo)')
  376. );
  377. self::assertSame(
  378. '<p><a href="http://testing.dev/fr/item2/item2-1#foo">Peer Anchor</a></p>',
  379. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  380. );
  381. self::assertSame(
  382. '<p><a href="http://testing.dev/fr/item2/item2-1#foo">Peer Anchor 2</a></p>',
  383. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  384. );
  385. self::assertSame(
  386. '<p><a href="http://testing.dev/fr/#foo">Root Anchor</a></p>',
  387. $this->parsedown->text('[Root Anchor](/#foo)')
  388. );
  389. }
  390. public function testExternalLinks(): void
  391. {
  392. self::assertSame(
  393. '<p><a href="http://www.cnn.com">cnn.com</a></p>',
  394. $this->parsedown->text('[cnn.com](http://www.cnn.com)')
  395. );
  396. self::assertSame(
  397. '<p><a href="https://www.google.com">google.com</a></p>',
  398. $this->parsedown->text('[google.com](https://www.google.com)')
  399. );
  400. self::assertSame(
  401. '<p><a href="https://github.com/getgrav/grav/issues/new?title=%5Badd-resource%5D%20New%20Plugin%2FTheme&amp;body=Hello%20%2A%2AThere%2A%2A">complex url</a></p>',
  402. $this->parsedown->text('[complex url](https://github.com/getgrav/grav/issues/new?title=[add-resource]%20New%20Plugin/Theme&body=Hello%20**There**)')
  403. );
  404. }
  405. public function testExternalLinksSubDir(): void
  406. {
  407. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  408. self::assertSame(
  409. '<p><a href="http://www.cnn.com">cnn.com</a></p>',
  410. $this->parsedown->text('[cnn.com](http://www.cnn.com)')
  411. );
  412. self::assertSame(
  413. '<p><a href="https://www.google.com">google.com</a></p>',
  414. $this->parsedown->text('[google.com](https://www.google.com)')
  415. );
  416. }
  417. public function testExternalLinksSubDirAbsoluteUrls(): void
  418. {
  419. $this->config->set('system.absolute_urls', true);
  420. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  421. self::assertSame(
  422. '<p><a href="http://www.cnn.com">cnn.com</a></p>',
  423. $this->parsedown->text('[cnn.com](http://www.cnn.com)')
  424. );
  425. self::assertSame(
  426. '<p><a href="https://www.google.com">google.com</a></p>',
  427. $this->parsedown->text('[google.com](https://www.google.com)')
  428. );
  429. }
  430. public function testAnchorLinksRelativeUrls(): void
  431. {
  432. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  433. self::assertSame(
  434. '<p><a href="#foo">Current Anchor</a></p>',
  435. $this->parsedown->text('[Current Anchor](#foo)')
  436. );
  437. self::assertSame(
  438. '<p><a href="/#foo">Root Anchor</a></p>',
  439. $this->parsedown->text('[Root Anchor](/#foo)')
  440. );
  441. self::assertSame(
  442. '<p><a href="/item2/item2-1#foo">Peer Anchor</a></p>',
  443. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  444. );
  445. self::assertSame(
  446. '<p><a href="/item2/item2-1#foo">Peer Anchor 2</a></p>',
  447. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  448. );
  449. }
  450. public function testAnchorLinksAbsoluteUrls(): void
  451. {
  452. $this->config->set('system.absolute_urls', true);
  453. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  454. self::assertSame(
  455. '<p><a href="#foo">Current Anchor</a></p>',
  456. $this->parsedown->text('[Current Anchor](#foo)')
  457. );
  458. self::assertSame(
  459. '<p><a href="http://testing.dev/item2/item2-1#foo">Peer Anchor</a></p>',
  460. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  461. );
  462. self::assertSame(
  463. '<p><a href="http://testing.dev/item2/item2-1#foo">Peer Anchor 2</a></p>',
  464. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  465. );
  466. self::assertSame(
  467. '<p><a href="http://testing.dev/#foo">Root Anchor</a></p>',
  468. $this->parsedown->text('[Root Anchor](/#foo)')
  469. );
  470. }
  471. public function testAnchorLinksWithPortAbsoluteUrls(): void
  472. {
  473. $this->config->set('system.absolute_urls', true);
  474. $this->uri->initializeWithURL('http://testing.dev:8080/item2/item2-2')->init();
  475. self::assertSame(
  476. '<p><a href="http://testing.dev:8080/item2/item2-1#foo">Peer Anchor</a></p>',
  477. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  478. );
  479. self::assertSame(
  480. '<p><a href="http://testing.dev:8080/item2/item2-1#foo">Peer Anchor 2</a></p>',
  481. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  482. );
  483. self::assertSame(
  484. '<p><a href="#foo">Current Anchor</a></p>',
  485. $this->parsedown->text('[Current Anchor](#foo)')
  486. );
  487. self::assertSame(
  488. '<p><a href="http://testing.dev:8080/#foo">Root Anchor</a></p>',
  489. $this->parsedown->text('[Root Anchor](/#foo)')
  490. );
  491. }
  492. public function testAnchorLinksSubDirRelativeUrls(): void
  493. {
  494. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  495. self::assertSame(
  496. '<p><a href="/subdir/item2/item2-1#foo">Peer Anchor</a></p>',
  497. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  498. );
  499. self::assertSame(
  500. '<p><a href="/subdir/item2/item2-1#foo">Peer Anchor 2</a></p>',
  501. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  502. );
  503. self::assertSame(
  504. '<p><a href="#foo">Current Anchor</a></p>',
  505. $this->parsedown->text('[Current Anchor](#foo)')
  506. );
  507. self::assertSame(
  508. '<p><a href="/subdir/#foo">Root Anchor</a></p>',
  509. $this->parsedown->text('[Root Anchor](/#foo)')
  510. );
  511. }
  512. public function testAnchorLinksSubDirAbsoluteUrls(): void
  513. {
  514. $this->config->set('system.absolute_urls', true);
  515. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  516. self::assertSame(
  517. '<p><a href="http://testing.dev/subdir/item2/item2-1#foo">Peer Anchor</a></p>',
  518. $this->parsedown->text('[Peer Anchor](../item2-1#foo)')
  519. );
  520. self::assertSame(
  521. '<p><a href="http://testing.dev/subdir/item2/item2-1#foo">Peer Anchor 2</a></p>',
  522. $this->parsedown->text('[Peer Anchor 2](../item2-1/#foo)')
  523. );
  524. self::assertSame(
  525. '<p><a href="#foo">Current Anchor</a></p>',
  526. $this->parsedown->text('[Current Anchor](#foo)')
  527. );
  528. self::assertSame(
  529. '<p><a href="http://testing.dev/subdir/#foo">Root Anchor</a></p>',
  530. $this->parsedown->text('[Root Anchor](/#foo)')
  531. );
  532. }
  533. public function testSlugRelativeLinks(): void
  534. {
  535. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  536. self::assertSame(
  537. '<p><a href="/">Up to Root Level</a></p>',
  538. $this->parsedown->text('[Up to Root Level](../..)')
  539. );
  540. self::assertSame(
  541. '<p><a href="/item2/item2-1">Peer Page</a></p>',
  542. $this->parsedown->text('[Peer Page](../item2-1)')
  543. );
  544. self::assertSame(
  545. '<p><a href="/item2/item2-2/item2-2-1">Down a Level</a></p>',
  546. $this->parsedown->text('[Down a Level](item2-2-1)')
  547. );
  548. self::assertSame(
  549. '<p><a href="/item2">Up a Level</a></p>',
  550. $this->parsedown->text('[Up a Level](..)')
  551. );
  552. self::assertSame(
  553. '<p><a href="/item3/item3-3">Up and Down</a></p>',
  554. $this->parsedown->text('[Up and Down](../../item3/item3-3)')
  555. );
  556. self::assertSame(
  557. '<p><a href="/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',
  558. $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')
  559. );
  560. self::assertSame(
  561. '<p><a href="/item2?foo=bar">Up a Level with Query</a></p>',
  562. $this->parsedown->text('[Up a Level with Query](../?foo=bar)')
  563. );
  564. self::assertSame(
  565. '<p><a href="/item3/item3-3?foo=bar">Up and Down with Query</a></p>',
  566. $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')
  567. );
  568. self::assertSame(
  569. '<p><a href="/item3/item3-3/foo:bar">Up and Down with Param</a></p>',
  570. $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')
  571. );
  572. self::assertSame(
  573. '<p><a href="/item3/item3-3#foo">Up and Down with Anchor</a></p>',
  574. $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')
  575. );
  576. }
  577. public function testSlugRelativeLinksAbsoluteUrls(): void
  578. {
  579. $this->config->set('system.absolute_urls', true);
  580. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  581. self::assertSame(
  582. '<p><a href="http://testing.dev/item2/item2-1">Peer Page</a></p>',
  583. $this->parsedown->text('[Peer Page](../item2-1)')
  584. );
  585. self::assertSame(
  586. '<p><a href="http://testing.dev/item2/item2-2/item2-2-1">Down a Level</a></p>',
  587. $this->parsedown->text('[Down a Level](item2-2-1)')
  588. );
  589. self::assertSame(
  590. '<p><a href="http://testing.dev/item2">Up a Level</a></p>',
  591. $this->parsedown->text('[Up a Level](..)')
  592. );
  593. self::assertSame(
  594. '<p><a href="http://testing.dev/">Up to Root Level</a></p>',
  595. $this->parsedown->text('[Up to Root Level](../..)')
  596. );
  597. self::assertSame(
  598. '<p><a href="http://testing.dev/item3/item3-3">Up and Down</a></p>',
  599. $this->parsedown->text('[Up and Down](../../item3/item3-3)')
  600. );
  601. self::assertSame(
  602. '<p><a href="http://testing.dev/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',
  603. $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')
  604. );
  605. self::assertSame(
  606. '<p><a href="http://testing.dev/item2?foo=bar">Up a Level with Query</a></p>',
  607. $this->parsedown->text('[Up a Level with Query](../?foo=bar)')
  608. );
  609. self::assertSame(
  610. '<p><a href="http://testing.dev/item3/item3-3?foo=bar">Up and Down with Query</a></p>',
  611. $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')
  612. );
  613. self::assertSame(
  614. '<p><a href="http://testing.dev/item3/item3-3/foo:bar">Up and Down with Param</a></p>',
  615. $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')
  616. );
  617. self::assertSame(
  618. '<p><a href="http://testing.dev/item3/item3-3#foo">Up and Down with Anchor</a></p>',
  619. $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')
  620. );
  621. }
  622. public function testSlugRelativeLinksSubDir(): void
  623. {
  624. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  625. self::assertSame(
  626. '<p><a href="/subdir/item2/item2-1">Peer Page</a></p>',
  627. $this->parsedown->text('[Peer Page](../item2-1)')
  628. );
  629. self::assertSame(
  630. '<p><a href="/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',
  631. $this->parsedown->text('[Down a Level](item2-2-1)')
  632. );
  633. self::assertSame(
  634. '<p><a href="/subdir/item2">Up a Level</a></p>',
  635. $this->parsedown->text('[Up a Level](..)')
  636. );
  637. self::assertSame(
  638. '<p><a href="/subdir">Up to Root Level</a></p>',
  639. $this->parsedown->text('[Up to Root Level](../..)')
  640. );
  641. self::assertSame(
  642. '<p><a href="/subdir/item3/item3-3">Up and Down</a></p>',
  643. $this->parsedown->text('[Up and Down](../../item3/item3-3)')
  644. );
  645. self::assertSame(
  646. '<p><a href="/subdir/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',
  647. $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')
  648. );
  649. self::assertSame(
  650. '<p><a href="/subdir/item2?foo=bar">Up a Level with Query</a></p>',
  651. $this->parsedown->text('[Up a Level with Query](../?foo=bar)')
  652. );
  653. self::assertSame(
  654. '<p><a href="/subdir/item3/item3-3?foo=bar">Up and Down with Query</a></p>',
  655. $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')
  656. );
  657. self::assertSame(
  658. '<p><a href="/subdir/item3/item3-3/foo:bar">Up and Down with Param</a></p>',
  659. $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')
  660. );
  661. self::assertSame(
  662. '<p><a href="/subdir/item3/item3-3#foo">Up and Down with Anchor</a></p>',
  663. $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')
  664. );
  665. }
  666. public function testSlugRelativeLinksSubDirAbsoluteUrls(): void
  667. {
  668. $this->config->set('system.absolute_urls', true);
  669. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  670. self::assertSame(
  671. '<p><a href="http://testing.dev/subdir/item2/item2-1">Peer Page</a></p>',
  672. $this->parsedown->text('[Peer Page](../item2-1)')
  673. );
  674. self::assertSame(
  675. '<p><a href="http://testing.dev/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',
  676. $this->parsedown->text('[Down a Level](item2-2-1)')
  677. );
  678. self::assertSame(
  679. '<p><a href="http://testing.dev/subdir/item2">Up a Level</a></p>',
  680. $this->parsedown->text('[Up a Level](..)')
  681. );
  682. self::assertSame(
  683. '<p><a href="http://testing.dev/subdir">Up to Root Level</a></p>',
  684. $this->parsedown->text('[Up to Root Level](../..)')
  685. );
  686. self::assertSame(
  687. '<p><a href="http://testing.dev/subdir/item3/item3-3">Up and Down</a></p>',
  688. $this->parsedown->text('[Up and Down](../../item3/item3-3)')
  689. );
  690. self::assertSame(
  691. '<p><a href="http://testing.dev/subdir/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',
  692. $this->parsedown->text('[Down a Level with Query](item2-2-1?foo=bar)')
  693. );
  694. self::assertSame(
  695. '<p><a href="http://testing.dev/subdir/item2?foo=bar">Up a Level with Query</a></p>',
  696. $this->parsedown->text('[Up a Level with Query](../?foo=bar)')
  697. );
  698. self::assertSame(
  699. '<p><a href="http://testing.dev/subdir/item3/item3-3?foo=bar">Up and Down with Query</a></p>',
  700. $this->parsedown->text('[Up and Down with Query](../../item3/item3-3?foo=bar)')
  701. );
  702. self::assertSame(
  703. '<p><a href="http://testing.dev/subdir/item3/item3-3/foo:bar">Up and Down with Param</a></p>',
  704. $this->parsedown->text('[Up and Down with Param](../../item3/item3-3/foo:bar)')
  705. );
  706. self::assertSame(
  707. '<p><a href="http://testing.dev/subdir/item3/item3-3#foo">Up and Down with Anchor</a></p>',
  708. $this->parsedown->text('[Up and Down with Anchor](../../item3/item3-3#foo)')
  709. );
  710. }
  711. public function testDirectoryRelativeLinks(): void
  712. {
  713. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  714. self::assertSame(
  715. '<p><a href="/item3/item3-3/foo:bar">Up and Down with Param</a></p>',
  716. $this->parsedown->text('[Up and Down with Param](../../03.item3/03.item3-3/foo:bar)')
  717. );
  718. self::assertSame(
  719. '<p><a href="/item2/item2-1">Peer Page</a></p>',
  720. $this->parsedown->text('[Peer Page](../01.item2-1)')
  721. );
  722. self::assertSame(
  723. '<p><a href="/item2/item2-2/item2-2-1">Down a Level</a></p>',
  724. $this->parsedown->text('[Down a Level](01.item2-2-1)')
  725. );
  726. self::assertSame(
  727. '<p><a href="/item3/item3-3">Up and Down</a></p>',
  728. $this->parsedown->text('[Up and Down](../../03.item3/03.item3-3)')
  729. );
  730. self::assertSame(
  731. '<p><a href="/item2/item2-2/item2-2-1?foo=bar">Down a Level with Query</a></p>',
  732. $this->parsedown->text('[Down a Level with Query](01.item2-2-1?foo=bar)')
  733. );
  734. self::assertSame(
  735. '<p><a href="/item3/item3-3?foo=bar">Up and Down with Query</a></p>',
  736. $this->parsedown->text('[Up and Down with Query](../../03.item3/03.item3-3?foo=bar)')
  737. );
  738. self::assertSame(
  739. '<p><a href="/item3/item3-3#foo">Up and Down with Anchor</a></p>',
  740. $this->parsedown->text('[Up and Down with Anchor](../../03.item3/03.item3-3#foo)')
  741. );
  742. }
  743. public function testAbsoluteLinks(): void
  744. {
  745. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  746. self::assertSame(
  747. '<p><a href="/">Root</a></p>',
  748. $this->parsedown->text('[Root](/)')
  749. );
  750. self::assertSame(
  751. '<p><a href="/item2/item2-1">Peer Page</a></p>',
  752. $this->parsedown->text('[Peer Page](/item2/item2-1)')
  753. );
  754. self::assertSame(
  755. '<p><a href="/item2/item2-2/item2-2-1">Down a Level</a></p>',
  756. $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)')
  757. );
  758. self::assertSame(
  759. '<p><a href="/item2">Up a Level</a></p>',
  760. $this->parsedown->text('[Up a Level](/item2)')
  761. );
  762. self::assertSame(
  763. '<p><a href="/item2?foo=bar">With Query</a></p>',
  764. $this->parsedown->text('[With Query](/item2?foo=bar)')
  765. );
  766. self::assertSame(
  767. '<p><a href="/item2/foo:bar">With Param</a></p>',
  768. $this->parsedown->text('[With Param](/item2/foo:bar)')
  769. );
  770. self::assertSame(
  771. '<p><a href="/item2#foo">With Anchor</a></p>',
  772. $this->parsedown->text('[With Anchor](/item2#foo)')
  773. );
  774. }
  775. public function testDirectoryAbsoluteLinksSubDir(): void
  776. {
  777. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  778. self::assertSame(
  779. '<p><a href="/subdir/">Root</a></p>',
  780. $this->parsedown->text('[Root](/)')
  781. );
  782. self::assertSame(
  783. '<p><a href="/subdir/item2/item2-1">Peer Page</a></p>',
  784. $this->parsedown->text('[Peer Page](/item2/item2-1)')
  785. );
  786. self::assertSame(
  787. '<p><a href="/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',
  788. $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)')
  789. );
  790. self::assertSame(
  791. '<p><a href="/subdir/item2">Up a Level</a></p>',
  792. $this->parsedown->text('[Up a Level](/item2)')
  793. );
  794. self::assertSame(
  795. '<p><a href="/subdir/item2?foo=bar">With Query</a></p>',
  796. $this->parsedown->text('[With Query](/item2?foo=bar)')
  797. );
  798. self::assertSame(
  799. '<p><a href="/subdir/item2/foo:bar">With Param</a></p>',
  800. $this->parsedown->text('[With Param](/item2/foo:bar)')
  801. );
  802. self::assertSame(
  803. '<p><a href="/subdir/item2#foo">With Anchor</a></p>',
  804. $this->parsedown->text('[With Anchor](/item2#foo)')
  805. );
  806. }
  807. public function testDirectoryAbsoluteLinksSubDirAbsoluteUrl(): void
  808. {
  809. $this->config->set('system.absolute_urls', true);
  810. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  811. self::assertSame(
  812. '<p><a href="http://testing.dev/subdir/">Root</a></p>',
  813. $this->parsedown->text('[Root](/)')
  814. );
  815. self::assertSame(
  816. '<p><a href="http://testing.dev/subdir/item2/item2-1">Peer Page</a></p>',
  817. $this->parsedown->text('[Peer Page](/item2/item2-1)')
  818. );
  819. self::assertSame(
  820. '<p><a href="http://testing.dev/subdir/item2/item2-2/item2-2-1">Down a Level</a></p>',
  821. $this->parsedown->text('[Down a Level](/item2/item2-2/item2-2-1)')
  822. );
  823. self::assertSame(
  824. '<p><a href="http://testing.dev/subdir/item2">Up a Level</a></p>',
  825. $this->parsedown->text('[Up a Level](/item2)')
  826. );
  827. self::assertSame(
  828. '<p><a href="http://testing.dev/subdir/item2?foo=bar">With Query</a></p>',
  829. $this->parsedown->text('[With Query](/item2?foo=bar)')
  830. );
  831. self::assertSame(
  832. '<p><a href="http://testing.dev/subdir/item2/foo:bar">With Param</a></p>',
  833. $this->parsedown->text('[With Param](/item2/foo:bar)')
  834. );
  835. self::assertSame(
  836. '<p><a href="http://testing.dev/subdir/item2#foo">With Anchor</a></p>',
  837. $this->parsedown->text('[With Anchor](/item2#foo)')
  838. );
  839. }
  840. public function testSpecialProtocols(): void
  841. {
  842. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  843. self::assertSame(
  844. '<p><a href="mailto:user@domain.com">mailto</a></p>',
  845. $this->parsedown->text('[mailto](mailto:user@domain.com)')
  846. );
  847. self::assertSame(
  848. '<p><a href="xmpp:xyx@domain.com">xmpp</a></p>',
  849. $this->parsedown->text('[xmpp](xmpp:xyx@domain.com)')
  850. );
  851. self::assertSame(
  852. '<p><a href="tel:123-555-12345">tel</a></p>',
  853. $this->parsedown->text('[tel](tel:123-555-12345)')
  854. );
  855. self::assertSame(
  856. '<p><a href="sms:123-555-12345">sms</a></p>',
  857. $this->parsedown->text('[sms](sms:123-555-12345)')
  858. );
  859. self::assertSame(
  860. '<p><a href="rdp://ts.example.com">ts.example.com</a></p>',
  861. $this->parsedown->text('[ts.example.com](rdp://ts.example.com)')
  862. );
  863. }
  864. public function testSpecialProtocolsSubDir(): void
  865. {
  866. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  867. self::assertSame(
  868. '<p><a href="mailto:user@domain.com">mailto</a></p>',
  869. $this->parsedown->text('[mailto](mailto:user@domain.com)')
  870. );
  871. self::assertSame(
  872. '<p><a href="xmpp:xyx@domain.com">xmpp</a></p>',
  873. $this->parsedown->text('[xmpp](xmpp:xyx@domain.com)')
  874. );
  875. self::assertSame(
  876. '<p><a href="tel:123-555-12345">tel</a></p>',
  877. $this->parsedown->text('[tel](tel:123-555-12345)')
  878. );
  879. self::assertSame(
  880. '<p><a href="sms:123-555-12345">sms</a></p>',
  881. $this->parsedown->text('[sms](sms:123-555-12345)')
  882. );
  883. self::assertSame(
  884. '<p><a href="rdp://ts.example.com">ts.example.com</a></p>',
  885. $this->parsedown->text('[ts.example.com](rdp://ts.example.com)')
  886. );
  887. }
  888. public function testSpecialProtocolsSubDirAbsoluteUrl(): void
  889. {
  890. $this->config->set('system.absolute_urls', true);
  891. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  892. self::assertSame(
  893. '<p><a href="mailto:user@domain.com">mailto</a></p>',
  894. $this->parsedown->text('[mailto](mailto:user@domain.com)')
  895. );
  896. self::assertSame(
  897. '<p><a href="xmpp:xyx@domain.com">xmpp</a></p>',
  898. $this->parsedown->text('[xmpp](xmpp:xyx@domain.com)')
  899. );
  900. self::assertSame(
  901. '<p><a href="tel:123-555-12345">tel</a></p>',
  902. $this->parsedown->text('[tel](tel:123-555-12345)')
  903. );
  904. self::assertSame(
  905. '<p><a href="sms:123-555-12345">sms</a></p>',
  906. $this->parsedown->text('[sms](sms:123-555-12345)')
  907. );
  908. self::assertSame(
  909. '<p><a href="rdp://ts.example.com">ts.example.com</a></p>',
  910. $this->parsedown->text('[ts.example.com](rdp://ts.example.com)')
  911. );
  912. }
  913. public function testReferenceLinks(): void
  914. {
  915. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  916. $sample = '[relative link][r_relative]
  917. [r_relative]: ../item2-3#blah';
  918. self::assertSame(
  919. '<p><a href="/item2/item2-3#blah">relative link</a></p>',
  920. $this->parsedown->text($sample)
  921. );
  922. $sample = '[absolute link][r_absolute]
  923. [r_absolute]: /item3#blah';
  924. self::assertSame(
  925. '<p><a href="/item3#blah">absolute link</a></p>',
  926. $this->parsedown->text($sample)
  927. );
  928. $sample = '[external link][r_external]
  929. [r_external]: http://www.cnn.com';
  930. self::assertSame(
  931. '<p><a href="http://www.cnn.com">external link</a></p>',
  932. $this->parsedown->text($sample)
  933. );
  934. }
  935. public function testAttributeLinks(): void
  936. {
  937. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  938. self::assertSame(
  939. '<p><a href="#something" class="button">Anchor Class</a></p>',
  940. $this->parsedown->text('[Anchor Class](?classes=button#something)')
  941. );
  942. self::assertSame(
  943. '<p><a href="/item2/item2-3" class="button">Relative Class</a></p>',
  944. $this->parsedown->text('[Relative Class](../item2-3?classes=button)')
  945. );
  946. self::assertSame(
  947. '<p><a href="/item2/item2-3" id="unique">Relative ID</a></p>',
  948. $this->parsedown->text('[Relative ID](../item2-3?id=unique)')
  949. );
  950. self::assertSame(
  951. '<p><a href="https://github.com/getgrav/grav" class="button big">External</a></p>',
  952. $this->parsedown->text('[External](https://github.com/getgrav/grav?classes=button,big)')
  953. );
  954. self::assertSame(
  955. '<p><a href="/item2/item2-3?id=unique">Relative Noprocess</a></p>',
  956. $this->parsedown->text('[Relative Noprocess](../item2-3?id=unique&noprocess)')
  957. );
  958. self::assertSame(
  959. '<p><a href="/item2/item2-3" target="_blank">Relative Target</a></p>',
  960. $this->parsedown->text('[Relative Target](../item2-3?target=_blank)')
  961. );
  962. self::assertSame(
  963. '<p><a href="/item2/item2-3" rel="nofollow">Relative Rel</a></p>',
  964. $this->parsedown->text('[Relative Rel](../item2-3?rel=nofollow)')
  965. );
  966. self::assertSame(
  967. '<p><a href="/item2/item2-3?foo=bar&amp;baz=qux" rel="nofollow" class="button">Relative Mixed</a></p>',
  968. $this->parsedown->text('[Relative Mixed](../item2-3?foo=bar&baz=qux&rel=nofollow&class=button)')
  969. );
  970. }
  971. public function testInvalidLinks(): void
  972. {
  973. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  974. self::assertSame(
  975. '<p><a href="/item2/item2-2/no-page">Non Existent Page</a></p>',
  976. $this->parsedown->text('[Non Existent Page](no-page)')
  977. );
  978. self::assertSame(
  979. '<p><a href="/item2/item2-2/existing-file.zip">Existent File</a></p>',
  980. $this->parsedown->text('[Existent File](existing-file.zip)')
  981. );
  982. self::assertSame(
  983. '<p><a href="/item2/item2-2/missing-file.zip">Non Existent File</a></p>',
  984. $this->parsedown->text('[Non Existent File](missing-file.zip)')
  985. );
  986. }
  987. public function testInvalidLinksSubDir(): void
  988. {
  989. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  990. self::assertSame(
  991. '<p><a href="/subdir/item2/item2-2/no-page">Non Existent Page</a></p>',
  992. $this->parsedown->text('[Non Existent Page](no-page)')
  993. );
  994. self::assertSame(
  995. '<p><a href="/subdir/item2/item2-2/existing-file.zip">Existent File</a></p>',
  996. $this->parsedown->text('[Existent File](existing-file.zip)')
  997. );
  998. self::assertSame(
  999. '<p><a href="/subdir/item2/item2-2/missing-file.zip">Non Existent File</a></p>',
  1000. $this->parsedown->text('[Non Existent File](missing-file.zip)')
  1001. );
  1002. }
  1003. public function testInvalidLinksSubDirAbsoluteUrl(): void
  1004. {
  1005. $this->config->set('system.absolute_urls', true);
  1006. $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/item2/item2-2', '/subdir')->init();
  1007. self::assertSame(
  1008. '<p><a href="http://testing.dev/subdir/item2/item2-2/no-page">Non Existent Page</a></p>',
  1009. $this->parsedown->text('[Non Existent Page](no-page)')
  1010. );
  1011. self::assertSame(
  1012. '<p><a href="http://testing.dev/subdir/item2/item2-2/existing-file.zip">Existent File</a></p>',
  1013. $this->parsedown->text('[Existent File](existing-file.zip)')
  1014. );
  1015. self::assertSame(
  1016. '<p><a href="http://testing.dev/subdir/item2/item2-2/missing-file.zip">Non Existent File</a></p>',
  1017. $this->parsedown->text('[Non Existent File](missing-file.zip)')
  1018. );
  1019. }
  1020. /**
  1021. * @param $string
  1022. *
  1023. * @return mixed
  1024. */
  1025. private function stripLeadingWhitespace($string)
  1026. {
  1027. return preg_replace('/^\s*(.*)/', '', $string);
  1028. }
  1029. }