1
0
mirror of https://github.com/til-schneider/slim-wiki.git synced 2025-08-06 16:46:31 +02:00

Added option to open external links in new browser tab (by adding target="_blank")

This commit is contained in:
til-schneider
2016-03-24 14:00:32 +01:00
parent 59b84c680c
commit cb9ef0e4c9
3 changed files with 22 additions and 13 deletions

View File

@@ -9,6 +9,9 @@ $config['lang'] = 'en'; // 'de' or 'en'
//$config['theme'] = 'slim'; //$config['theme'] = 'slim';
// Open external links in new browser tab? (adds `target="_blank"` to external links)
//$config['openExternalLinksInNewTab'] = false;
// Hide directories having no 'index.md' in breadcrumbs // Hide directories having no 'index.md' in breadcrumbs
//$config['showCompleteBreadcrumbs'] = false; //$config['showCompleteBreadcrumbs'] = false;

View File

@@ -31,6 +31,7 @@ class Context {
'lang' => 'en', 'lang' => 'en',
'theme' => 'slim', 'theme' => 'slim',
'demoMode' => false, 'demoMode' => false,
'openExternalLinksInNewTab' => true,
'showCompleteBreadcrumbs' => true 'showCompleteBreadcrumbs' => true
); );

View File

@@ -14,25 +14,30 @@ class RenderService {
} }
public function renderMarkdown($markdownText, $isEditMode) { public function renderMarkdown($markdownText, $isEditMode) {
$config = $this->context->getConfig();
require_once __DIR__ . '/../lib/parsedown/Parsedown.php'; require_once __DIR__ . '/../lib/parsedown/Parsedown.php';
$html = Parsedown::instance()->text($markdownText); $html = Parsedown::instance()->text($markdownText);
// Support `TODO` and `FIXME` // Support `TODO` and `FIXME`
$html = preg_replace('/(^|\\W)(TODO|FIXME):?(\\W|$)/', '$1<span class="todo">$2</span>$3', $html); $html = preg_replace('/(^|\\W)(TODO|FIXME):?(\\W|$)/', '$1<span class="todo">$2</span>$3', $html);
if ($isEditMode) { // Enhance links
// Append `?edit` to local links (in order to stay in edit mode) $openExternalLinksInNewTab = $config['openExternalLinksInNewTab'];
$html = preg_replace_callback('|(<a href="([^"]+))"|', $html = preg_replace_callback('|(<a href="([^"]+))"|',
function($match) { function($match) use($isEditMode, $openExternalLinksInNewTab) {
$url = $match[2]; $url = $match[2];
$isLocalLink = ! strpos($url, '//'); $isLocalLink = ! strpos($url, '//');
if ($isLocalLink) { if ($isLocalLink && $isEditMode) {
// Append `?edit` to local links (in order to stay in edit mode)
return $match[1] . '?edit"'; return $match[1] . '?edit"';
} else if (!$isLocalLink && $openExternalLinksInNewTab) {
// Add `target="_blank"` to external links
return $match[0] . ' target="_blank"';
} else { } else {
return $match[0]; return $match[0];
} }
}, $html); }, $html);
}
return $html; return $html;
} }