mirror of
https://github.com/til-schneider/slim-wiki.git
synced 2025-08-06 08:37:31 +02:00
Added option to open external links in new browser tab (by adding target="_blank"
)
This commit is contained in:
@@ -9,6 +9,9 @@ $config['lang'] = 'en'; // 'de' or 'en'
|
||||
|
||||
//$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
|
||||
//$config['showCompleteBreadcrumbs'] = false;
|
||||
|
||||
|
@@ -31,6 +31,7 @@ class Context {
|
||||
'lang' => 'en',
|
||||
'theme' => 'slim',
|
||||
'demoMode' => false,
|
||||
'openExternalLinksInNewTab' => true,
|
||||
'showCompleteBreadcrumbs' => true
|
||||
);
|
||||
|
||||
|
@@ -14,25 +14,30 @@ class RenderService {
|
||||
}
|
||||
|
||||
public function renderMarkdown($markdownText, $isEditMode) {
|
||||
$config = $this->context->getConfig();
|
||||
|
||||
require_once __DIR__ . '/../lib/parsedown/Parsedown.php';
|
||||
$html = Parsedown::instance()->text($markdownText);
|
||||
|
||||
// Support `TODO` and `FIXME`
|
||||
$html = preg_replace('/(^|\\W)(TODO|FIXME):?(\\W|$)/', '$1<span class="todo">$2</span>$3', $html);
|
||||
|
||||
if ($isEditMode) {
|
||||
// Append `?edit` to local links (in order to stay in edit mode)
|
||||
// Enhance links
|
||||
$openExternalLinksInNewTab = $config['openExternalLinksInNewTab'];
|
||||
$html = preg_replace_callback('|(<a href="([^"]+))"|',
|
||||
function($match) {
|
||||
function($match) use($isEditMode, $openExternalLinksInNewTab) {
|
||||
$url = $match[2];
|
||||
$isLocalLink = ! strpos($url, '//');
|
||||
if ($isLocalLink) {
|
||||
if ($isLocalLink && $isEditMode) {
|
||||
// Append `?edit` to local links (in order to stay in edit mode)
|
||||
return $match[1] . '?edit"';
|
||||
} else if (!$isLocalLink && $openExternalLinksInNewTab) {
|
||||
// Add `target="_blank"` to external links
|
||||
return $match[0] . ' target="_blank"';
|
||||
} else {
|
||||
return $match[0];
|
||||
}
|
||||
}, $html);
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user