From 0f7e4ccc07f05e4881dcde9290f7bb5e6781a0a3 Mon Sep 17 00:00:00 2001 From: Edwin Noorlander Date: Wed, 19 Nov 2025 14:29:00 +0100 Subject: [PATCH] Fix auto-linking order and add dash-dash underline styling - Moved auto-linking before markdown link processing to avoid conflicts - Added dash-dash (dashed) underline for auto-links instead of dotted - Improved pattern matching to avoid linking inside existing markdown links - Enhanced detection of existing HTML tags and links - Auto-links now have 2px dashed underline with hover effect --- index.php | 32 +++++++++++++++++++++++--------- templates/layout.html | 4 +++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/index.php b/index.php index f9cd2a7..8f26139 100644 --- a/index.php +++ b/index.php @@ -212,6 +212,9 @@ class CodePressCMS { $body = preg_replace('/\*\*(.+?)\*\*/', '$1', $body); $body = preg_replace('/\*(.+?)\*/', '$1', $body); + // Auto-link page titles to existing content pages (before markdown link processing) + $body = $this->autoLinkPageTitles($body); + // Convert Markdown links to HTML links $body = preg_replace('/\[([^\]]+)\]\(([^)]+)\)/', '$1', $body); @@ -219,9 +222,6 @@ class CodePressCMS { $body = preg_replace('/href="\/blog\/([^"]+)"/', 'href="?page=blog/$1"', $body); $body = preg_replace('/href="\/([^"]+)"/', 'href="?page=$1"', $body); - // Auto-link page titles to existing content pages - $body = $this->autoLinkPageTitles($body); - $body = preg_replace('/\n\n/', '

', $body); $body = '

' . $body . '

'; $body = preg_replace('/

<\/p>/', '', $body); @@ -234,22 +234,36 @@ class CodePressCMS { ]; } - private function autoLinkPageTitles($content) { +private function autoLinkPageTitles($content) { // Get all available pages with their titles $pages = $this->getAllPageTitles(); foreach ($pages as $pagePath => $pageTitle) { // Create a pattern that matches the exact page title (case-insensitive) + // Use word boundaries to avoid partial matches $pattern = '/\b' . preg_quote($pageTitle, '/') . '\b/i'; - // Replace with link, but avoid linking inside existing links or headings + // Replace with link, but avoid linking inside existing links, headings, or markdown $replacement = function($matches) use ($pageTitle, $pagePath) { - // Check if we're inside an HTML tag or link - $before = substr($matches[0], 0, 50); - if (preg_match('/<[^>]*$/', $before) || preg_match('/href=/', $before)) { - return $matches[0]; // Don't link inside HTML tags + $text = $matches[0]; + + // Check if we're inside an existing link or markdown syntax + if (preg_match('/\[.*?\]\(.*?\)/', $text) || + preg_match('/\[.*?\]:/', $text) || + preg_match('/]*>/', $text) || + preg_match('/href=/', $text)) { + return $text; // Don't link existing links } + return '' . $text . ''; + }; + + $content = preg_replace_callback($pattern, $replacement, $content); + } + + return $content; + } + return '' . $matches[0] . ''; }; diff --git a/templates/layout.html b/templates/layout.html index 5e99457..6a24deb 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -165,13 +165,15 @@ .auto-link { color: #0d6efd; text-decoration: none; - border-bottom: 1px dotted #0d6efd; + border-bottom: 2px dashed #0d6efd; font-weight: 500; + transition: all 0.2s ease; } .auto-link:hover { color: #0a58ca; text-decoration: none; border-bottom-style: solid; + border-bottom-color: #0a58ca; } .search-form { max-width: 300px;