Skip to content

Commit

Permalink
Fix Condition when Text Preview should be shown (#396)
Browse files Browse the repository at this point in the history
* Update FormatTextPreview.php

* chore: refactor & fix logic with blank lines

* style: formatting

---------

Co-authored-by: Davide Iadeluca <[email protected]>
  • Loading branch information
edgeinfinity1 and DavideIadeluca authored Nov 4, 2024
1 parent 3cd6cdc commit 6710366
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Formatter/TextPreview/FormatTextPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ public function __invoke(Renderer $renderer, $context, string $xml)

if ($file) {
$file_contents = file_get_contents($this->paths->public.'/assets/files/'.$file->path);
$lines = preg_split('/\R/', $file_contents);
$lines = array_filter($lines, 'trim');

$file_contents_normalised = str_replace(["\r\n", "\r", "\n"], "\n", $file_contents);

// automatically normalises line endings
$lines = explode("\n", $file_contents_normalised);
$first_five_lines = array_slice($lines, 0, 5);
$snippet = implode("\n", $first_five_lines);

if ($snippet !== $file_contents_normalised) {
if (count($lines) <= 5) {
$attributes['has_snippet'] = 'false';
}

$snippet = implode("\n", array_slice($lines, 0, 5));
}

$attributes['snippet'] = $snippet;
Expand Down

0 comments on commit 6710366

Please sign in to comment.