Skip to content

Commit

Permalink
Fix local links to not included sections, fixes dokufreaks#108
Browse files Browse the repository at this point in the history
This transforms local links in an included page into internal links if
the page hasn't been fully included. This doesn't consider the case that
the target section (but not the full page) has been included at another
place in the page as this is considered to be rather unlikely.
  • Loading branch information
michitux committed Apr 19, 2013
1 parent 1473d1a commit 7026615
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions _test/locallink_conversion.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

if (!defined('DOKU_INC')) die();

/**
* Test the conversion of local links to internal links if the page hasn't been fully included
*/
class plugin_include_locallink_conversion_test extends DokuWikiTest {
/** @var helper_plugin_include $helper */
private $helper;

public function setUp() {
$this->pluginsEnabled[] = 'include';
parent::setUp();

$this->helper = plugin_load('helper', 'include');

saveWikiText('included', 'Example content with link [[#jump]]', 'Test setup');
idx_addPage('test:included');

saveWikiText('test:includefull', '{{page>..:included}}', 'Test setup');
idx_addPage('test:includefull');

saveWikiText('test:includefirst', '{{page>..:included&firstseconly}}', 'Test setup');
idx_addPage('test:includefirst');
}

public function testLocalConverted() {
$html = p_wiki_xhtml('test:includefirst');
$this->assertContains('href="'.wl('included').'#jump"', $html);
$this->assertNotContains('href="#jump"', $html);
}

public function testLocalExistsIfIncluded() {
$html = p_wiki_xhtml('test:includefull');
$this->assertContains('href="#jump"', $html);
}
}
7 changes: 7 additions & 0 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc
}
}
break;
case 'locallink':
/* Convert local links to internal links if the page hasn't been fully included */
if ($included_pages == null || !array_key_exists($page, $included_pages)) {
$ins[$i][0] = 'internallink';
$ins[$i][1][0] = ':'.$page.'#'.$ins[$i][1][0];
}
break;
case 'plugin':
// FIXME skip other plugins?
switch($ins[$i][1][0]) {
Expand Down

0 comments on commit 7026615

Please sign in to comment.