Skip to content

Fixed the handling of custom image prefix #86

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

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BuildConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct()
$this->theme = Configuration::THEME_DEFAULT;
$this->symfonyVersion = '4.4';
$this->excludedPaths = [];
$this->imagesPublicPrefix = '/_images';
$this->imagesPublicPrefix = '';
}

public function createFileFinder(): Finder
Expand Down
8 changes: 6 additions & 2 deletions src/Listener/CopyImagesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public function preNodeRender(PreNodeRenderEvent $event)
$newAbsoluteFilePath = $this->buildConfig->getImagesDir().'/'.$fileInfo->getFilename();
$fs->copy($sourceImage, $newAbsoluteFilePath, true);

$newUrlPath = $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename();
$node->setValue($node->getEnvironment()->relativeUrl($newUrlPath));
if ('' === $this->buildConfig->getImagesPublicPrefix()) {
$newUrlPath = $node->getEnvironment()->relativeUrl('_images/'.$fileInfo->getFilename());
} else {
$newUrlPath = $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hii @javiereguiluz

we're copying the image to $this->buildConfig->getImagesDir().'/'.$fileInfo->getFilename() but we're linking to $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename()

isn't there a problem if these two paths are uncorrelated?

in the test below, the img's src is /some/custom/prefix-for-images/symfony-logo.png but the image file path is ./_images/symfony-logo.png

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. However, this is intended.

The default behavior is unchanged (copy images in <contents/_images/ and link to them in that dir using a relative link). However, for advanced/custom setups, the images are copied to a separate dir (or even server/service like AWS). The missing piece that you mentioned is that in your own code using this Doc Builder you must take care of this and copy/move the images accordingly.

}
$node->setValue($newUrlPath);
}
}
24 changes: 24 additions & 0 deletions tests/Command/BuildDocsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ public function testBuildDocsForPdf()
$this->assertStringContainsString('[OK] Build complete', $output);
}

public function testBuildDocsWithCustomImagePrefix()
{
$buildConfig = $this->createBuildConfig();
$buildConfig->setImagesPublicPrefix('/some/custom/prefix-for-images');
$outputDir = __DIR__.'/../_output';

$filesystem = new Filesystem();
$filesystem->remove($outputDir);
$filesystem->mkdir($outputDir);

$output = $this->executeCommand(
$buildConfig,
[
'source-dir' => __DIR__.'/../fixtures/source/main',
'output-dir' => $outputDir,
]
);

$this->assertStringContainsString('[OK] Build complete', $output);

$generatedHtml = file_get_contents($outputDir.'/index.html');
$this->assertStringContainsString('/some/custom/prefix-for-images/symfony-logo.png', $generatedHtml);
}

private function executeCommand(BuildConfig $buildConfig, array $input): string
{
$input['--no-theme'] = true;
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/expected/main/form/form_type.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<head>
<meta charset="utf-8" />


</head>

<body>
<div class="section">
<h1 id="formtype-documentation"><a class="headerlink" href="#formtype-documentation" title="Permalink to this headline">FormType Documentation</a></h1>
<span id="internal-reference"></span>
<img src="../_images/symfony-logo.png" />
<img src="_images/symfony-logo.png" />
</div>

</body>
</html>
</html>