diff --git a/perlite/content.php b/perlite/content.php index 6cb850e..db1c9a1 100644 --- a/perlite/content.php +++ b/perlite/content.php @@ -86,6 +86,20 @@ function parseContent($requestFile) $charCount = strlen($content); $content = $Parsedown->text($content); + // protect code blocks to avoid parsing of internal links + // we use a unique identifier to avoid conflicts with real content + $uniqueId = uniqid(); + $protectedBlocks = []; + $content = preg_replace_callback( + '/]*>.*?<\/code>/s', + function ($matches) use (&$protectedBlocks, $uniqueId) { + $placeholder = ''; + $protectedBlocks[] = $matches[0]; + return $placeholder; + }, + $content + ); + // Relative or absolute pathes if ($relPathes) { @@ -197,6 +211,17 @@ function parseContent($requestFile) $content = translateLink($pattern, $content, $mdpath, true); + // restore code blocks + if (!empty($protectedBlocks)) { + $content = preg_replace_callback( + '//', + function ($matches) use ($protectedBlocks) { + return $protectedBlocks[$matches[1]] ?? $matches[0]; + }, + $content + ); + } + // add some meta data $content = '