Skip to content

Commit d37d011

Browse files
committed
Fixed the handling of custom image prefix
1 parent 82a2fa3 commit d37d011

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

src/BuildConfig.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct()
3737
$this->theme = Configuration::THEME_DEFAULT;
3838
$this->symfonyVersion = '4.4';
3939
$this->excludedPaths = [];
40-
$this->imagesPublicPrefix = '/_images';
40+
$this->imagesPublicPrefix = '';
4141
}
4242

4343
public function createFileFinder(): Finder

src/Listener/CopyImagesListener.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public function preNodeRender(PreNodeRenderEvent $event)
5353
$newAbsoluteFilePath = $this->buildConfig->getImagesDir().'/'.$fileInfo->getFilename();
5454
$fs->copy($sourceImage, $newAbsoluteFilePath, true);
5555

56-
$newUrlPath = $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename();
57-
$node->setValue($node->getEnvironment()->relativeUrl($newUrlPath));
56+
if ('' === $this->buildConfig->getImagesPublicPrefix()) {
57+
$newUrlPath = $node->getEnvironment()->relativeUrl('_images/'.$fileInfo->getFilename());
58+
} else {
59+
$newUrlPath = $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename();
60+
}
61+
$node->setValue($newUrlPath);
5862
}
5963
}

tests/Command/BuildDocsCommandTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,30 @@ public function testBuildDocsForPdf()
9090
$this->assertStringContainsString('[OK] Build complete', $output);
9191
}
9292

93+
public function testBuildDocsWithCustomImagePrefix()
94+
{
95+
$buildConfig = $this->createBuildConfig();
96+
$buildConfig->setImagesPublicPrefix('/some/custom/prefix-for-images');
97+
$outputDir = __DIR__.'/../_output';
98+
99+
$filesystem = new Filesystem();
100+
$filesystem->remove($outputDir);
101+
$filesystem->mkdir($outputDir);
102+
103+
$output = $this->executeCommand(
104+
$buildConfig,
105+
[
106+
'source-dir' => __DIR__.'/../fixtures/source/main',
107+
'output-dir' => $outputDir,
108+
]
109+
);
110+
111+
$this->assertStringContainsString('[OK] Build complete', $output);
112+
113+
$generatedHtml = file_get_contents($outputDir.'/index.html');
114+
$this->assertStringContainsString('/some/custom/prefix-for-images/symfony-logo.png', $generatedHtml);
115+
}
116+
93117
private function executeCommand(BuildConfig $buildConfig, array $input): string
94118
{
95119
$input['--no-theme'] = true;

tests/fixtures/expected/main/form/form_type.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<head>
44
<meta charset="utf-8" />
55

6-
6+
77
</head>
88

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

1616
</body>
17-
</html>
17+
</html>

0 commit comments

Comments
 (0)