function testGetRelativePath() { $page = new DocumentationPage('test.md', new DocumentationEntity('mymodule', null, BASE_PATH . '/sapphiredocs/tests/docs/')); $this->assertEquals('test.md', $page->getRelativePath()); $page = new DocumentationPage('subfolder/subpage.md', new DocumentationEntity('mymodule', null, BASE_PATH . '/sapphiredocs/tests/docs/')); $this->assertEquals('subfolder/subpage.md', $page->getRelativePath()); }
/** * Resolves all relative links within markdown. * * @param String $md Markdown content * @param DocumentationPage $page * * @return String Markdown */ public static function rewrite_relative_links($md, $page) { $baselink = $page->getEntity()->Link(); $re = '/ ([^\\!]?) # exclude image format \\[ (.*?) # link title (non greedy) \\] \\( (.*?) # link url (non greedy) \\) /x'; preg_match_all($re, $md, $matches); // relative path (relative to module base folder), without the filename. // For "sapphire/en/current/topics/templates", this would be "templates" $relativePath = dirname($page->getRelativePath()); if (strpos($page->getRelativePath(), 'index.md')) { $relativeLink = $page->getRelativeLink(); } else { $relativeLink = dirname($page->getRelativeLink()); } if ($relativePath == '.') { $relativePath = ''; } if ($relativeLink == ".") { $relativeLink = ''; } // file base link $fileBaseLink = Director::makeRelative(dirname($page->getPath())); if ($matches) { foreach ($matches[0] as $i => $match) { $title = $matches[2][$i]; $url = $matches[3][$i]; // Don't process API links if (preg_match('/^api:/', $url)) { continue; } // Don't process absolute links (based on protocol detection) $urlParts = parse_url($url); if ($urlParts && isset($urlParts['scheme'])) { continue; } // for images we need to use the file base path if (preg_match('/_images/', $url)) { $relativeUrl = Controller::join_links(Director::absoluteBaseURL(), $fileBaseLink, $url); } else { // Rewrite public URL if (preg_match('/^\\//', $url)) { // Absolute: Only path to module base $relativeUrl = Controller::join_links($baselink, $url, '/'); } else { // Relative: Include path to module base and any folders $relativeUrl = Controller::join_links($baselink, $relativeLink, $url, '/'); } } // Resolve relative paths while (strpos($relativeUrl, '..') !== FALSE) { $relativeUrl = preg_replace('/[-\\w]+\\/\\.\\.\\//', '', $relativeUrl); } // Replace any double slashes (apart from protocol) $relativeUrl = preg_replace('/([^:])\\/{2,}/', '$1/', $relativeUrl); // Replace in original content $md = str_replace($match, sprintf('%s[%s](%s)', $matches[1][$i], $title, $relativeUrl), $md); } } return $md; }
/** * Resolves all relative links within markdown. * * @param String $md Markdown content * @param DocumentationPage $page * @param String $baselink * @return String Markdown */ static function rewrite_relative_links($md, $page, $baselink) { $re = '/ ([^\\!]?) # exclude image format \\[ (.*?) # link title (non greedy) \\] \\( (.*?) # link url (non greedy) \\) /x'; preg_match_all($re, $md, $matches); // relative path (to module base folder), without the filename $relativePath = dirname($page->getRelativePath()); if ($relativePath == '.') { $relativePath = ''; } if ($matches) { foreach ($matches[0] as $i => $match) { $title = $matches[2][$i]; $url = $matches[3][$i]; // Don't process API links if (preg_match('/^api:/', $url)) { continue; } // Don't process absolute links (based on protocol detection) $urlParts = parse_url($url); if ($urlParts && isset($urlParts['scheme'])) { continue; } // Rewrite URL (relative or absolute) if (preg_match('/^\\//', $url)) { $relativeUrl = $baselink . $url; } else { $relativeUrl = $baselink . '/' . $relativePath . '/' . $url; } // Resolve relative paths while (strpos($relativeUrl, '..') !== FALSE) { $relativeUrl = preg_replace('/\\w+\\/\\.\\.\\//', '', $relativeUrl); } // Replace any double slashes (apart from protocol) $relativeUrl = preg_replace('/([^:])\\/{2,}/', '$1/', $relativeUrl); // Replace in original content $md = str_replace($match, sprintf('%s[%s](%s)', $matches[1][$i], $title, $relativeUrl), $md); } } return $md; }