New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Line highlight plugin highlights the line on last rendered code block only on a page with multiple code blocks. #3762
Comments
Odd, maybe I'm implementing it wrong somehow. |
All I can suggest is make sure they've got different IDs |
protected function includeCodeInResponse($exception)
{
$codeSnippets = [];
foreach ($exception->getTrace() as $traceItem) {
if (isset($traceItem['file'])) {
$codeSnippets[] = [
'class' => $traceItem['class'] ?? null,
'function' => $traceItem['function'] ?? null,
'file' => $traceItem['file'],
'line' => $traceItem['line'],
'snippet' => $this->getCodeSnippet($traceItem['file'], $traceItem['line']),
];
}
}
return $codeSnippets;
}
protected function getCodeSnippet($file, $line, $linesBefore = 10, $linesAfter = 10)
{
$id = preg_replace('/[^a-zA-Z0-9]/', '', crypt(basename($file) . $line, md5(rand(1, 99999999))));
$code = "<pre id=\"{$id}\" data-line=\"{$line}\" class=\"line-numbers brad-3\" data-start=\"".($line - $linesBefore)."\"><code id=\"{$id}2\" class=\"language-php\">";
if (is_readable($file)) {
$lines = file($file);
$startLine = max(1, $line - $linesBefore);
$endLine = min(count($lines), $line + $linesAfter);
for ($i = $startLine; $i <= $endLine; $i++) {
$code .= htmlspecialchars($lines[$i - 1]);
}
}
$code .= "\n</code>\n</pre>\n";
return $code;
} I tried to ensure that the IDs are fairly guaranteed to be unique, but regardless of how many times I try, only the last code block has the line highlighted. |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/assets/prism/themes/prism-tomorrow.css" data-noprefix />
<link rel="stylesheet" href="/assets/prism/plugins/line-highlight/prism-line-highlight.css" data-noprefix />
<link rel="stylesheet" href="/assets/prism/plugins/line-numbers/prism-line-numbers.css" data-noprefix />
<title>Errors Encountered</title>
</head>
<body>
<main class="row flex-center g-1 stretch-y">
<section class="col-10 body brad-3 double-border drop-shadow">
<h1>Error Occurred</h1>
<p>Errors encountered while trying to fulfill the request.</p>
<div style="margin-inline: 10%;">
<h2>Error Details:</h2>
<pre>
<strong>Message:</strong>call_user_func_array(): Argument #1 ($callback) must be a valid callback, class App\Controllers\Auth\AuthController does not have a method "register"<br>
<strong>Code:</strong>0<br>
<strong>File:</strong>C:\laragon\www\mysite\Core\Router.php<br>
<strong>Line:</strong>149<br>
</pre>
<pre id="bfU8rQEXBpF4c" data-line="149" class="brad-3 line-numbers" data-start="139"><code id="bfU8rQEXBpF4c2" class="language-php"> return new Response($pipeline->handle($request, function ($request) use ($class, $method, $matches) {
$params = array_filter($matches, function ($key) {
return !is_int($key);
}, ARRAY_FILTER_USE_KEY);
if (!empty($request->request)) {
$params['request'] = $request;
}
$classInstance = new $class();
return call_user_func_array([$classInstance, $method], $params);
}));
}
}
return (new Response(''))->statusCode(404)->abort(404);
}
}
</code>
</pre>
</div>
<!-- Trace section -->
<h2>Trace:</h2>
<div class="trace-item border brad-3">
<p>
<strong>Step 1:</strong>
<span class="trace-function">call_user_func_array</span>
</p>
<!-- Include the code snippet for each trace item -->
<p class="trace-file">File: C:\laragon\www\mysite\Core\Router.php</p>
<p>Line: 149</p>
<pre id="a6QZTGpGDEcv" data-line="149" class="brad-3 line-numbers" data-start="139"><code id="a6QZTGpGDEcv2" class="language-php"> return new Response($pipeline->handle($request, function ($request) use ($class, $method, $matches) {
$params = array_filter($matches, function ($key) {
return !is_int($key);
}, ARRAY_FILTER_USE_KEY);
if (!empty($request->request)) {
$params['request'] = $request;
}
$classInstance = new $class();
return call_user_func_array([$classInstance, $method], $params);
}));
}
}
return (new Response(''))->statusCode(404)->abort(404);
}
}
</code>
</pre>
</div>
<div class="trace-item border brad-3">
<p>
<strong>Step 2:</strong>
Core\Router-> <span class="trace-function">Core\{closure}</span>
</p>
<!-- Include the code snippet for each trace item -->
<p class="trace-file">File: C:\laragon\www\mysite\Core\MiddlewarePipeline.php</p>
<p>Line: 30</p>
<pre id="f0e9UPtQ4ZArI" data-line="30" class="brad-3 line-numbers" data-start="20"><code id="f0e9UPtQ4ZArI2" class="language-php"> array_reverse($this->middlewares),
function ($next, $middleware) {
return function ($request) use ($middleware, $next) {
return $middleware->handle($request, $next);
};
},
$controllerClosure // Pass the controller closure as the initial "next"
);
// Execute the pipeline starting with the initial request
return $pipeline($request);
}
}
</code>
</pre>
</div>
<div class="trace-item border brad-3">
<p>
<strong>Step 3:</strong>
Core\MiddlewarePipeline-> <span class="trace-function">handle</span>
</p>
<!-- Include the code snippet for each trace item -->
<p class="trace-file">File: C:\laragon\www\mysite\Core\Router.php</p>
<p>Line: 139</p>
<pre id="4bI6qqXduwOyg" data-line="139" class="brad-3 line-numbers" data-start="129"><code id="4bI6qqXduwOyg2" class="language-php"> $class = "App\Controllers\\{$handler[0]}";
$method = $handler[1];
$pipeline = new MiddlewarePipeline();
foreach ($route['middleware'] as $middlewareClass) {
$pipeline->addMiddleware(new $middlewareClass());
}
$request = new Request();
return new Response($pipeline->handle($request, function ($request) use ($class, $method, $matches) {
$params = array_filter($matches, function ($key) {
return !is_int($key);
}, ARRAY_FILTER_USE_KEY);
if (!empty($request->request)) {
$params['request'] = $request;
}
$classInstance = new $class();
return call_user_func_array([$classInstance, $method], $params);
</code>
</pre>
</div>
<div class="trace-item border brad-3">
<p>
<strong>Step 4:</strong>
Core\Router-> <span class="trace-function">dispatch</span>
</p>
<!-- Include the code snippet for each trace item -->
<p class="trace-file">File: C:\laragon\www\mysite\public\index.php</p>
<p>Line: 15</p>
<pre id="77A1QppD1v88c" data-line="15" class="brad-3 line-numbers" data-start="5"><code id="77A1QppD1v88c2" class="language-php">
try {
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../core/helpers/helper.php';
require_once baseDir('routes/web.php');
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_POST['_method'] ?? $_SERVER['REQUEST_METHOD'];
echo Router::dispatch($method, $uri)->prepare()->getContent();
} catch (\Throwable $exception) {
ob_end_clean();
$errorHandler = new ErrorHandler(true); // Set true for debug mode
$response = $errorHandler->handleException($exception);
echo $response->statusCode(500)->prepare()->getContent();
}
?>
</code>
</pre>
</div>
</section>
</main>
<script src="/assets/prism/prism.js"></script>
<script src="/assets/prism/plugins/line-highlight/prism-line-highlight.js"></script>
<script src="/assets/prism/plugins/line-numbers/prism-line-numbers.js"></script>
<script src="/assets/prism/plugins/autoloader/prism-autoloader.js" data-autoloader-path="/assets/prism/components/"></script>
</body>
</html> This is the generated HTML page... according to the docs, everything seems to be correct. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Information:
Description
I'm using the latest JavaScript and CSS for Prism I got off the website after selecting the theme I wanted (prism-tomorrow) and selected all the languages I wanted to include with prism. I've encountered some problems along the way specifically with the line-highlight plugin and I don't think it's the error with how I'm implementing it. (It's a custom error page where it shows portions of my code for debug).
On one page I have multiple code blocks that gets the same configuration for their highlighting but for some reason only the last code block gets any of it's line highlighted.
Why don't the other code blocks get their lines highlighted?
Example
All the other code blocks (that are not last) don't get any love from the line-highlighting plug-in
No lines highlighted in any other code block
The last code block is the only block that gets highlighted
The last code block
This is the code that generates the code block that does the highlighting
The text was updated successfully, but these errors were encountered: