/** @var Image[] */
protected array $images = [];
- /** @var array<string, Model> */
+ /**
+ * Mapping keyed by "type:old-reference-id" with values being the new imported equivalent model.
+ * @var array<string, Model>
+ */
protected array $referenceMap = [];
/** @var array<int, ZipExportPage> */
return null;
}
+ protected function replaceDrawingIdReferences(string $content): string
+ {
+ $referenceRegex = '/\sdrawio-diagram=[\'"](\d+)[\'"]/';
+
+ $result = preg_replace_callback($referenceRegex, function ($matches) {
+ $key = 'image:' . $matches[1];
+ $model = $this->referenceMap[$key] ?? null;
+ if ($model instanceof Image && $model->type === 'drawio') {
+ return ' drawio-diagram="' . $model->id . '"';
+ }
+ return $matches[0];
+ }, $content);
+
+ return $result ?: $content;
+ }
+
public function replaceReferences(): void
{
foreach ($this->books as $book) {
$exportPage = $this->zipExportPageMap[$page->id];
$contentType = $exportPage->markdown ? 'markdown' : 'html';
$content = $exportPage->markdown ?: ($exportPage->html ?: '');
+
$parsed = $this->parser->parseReferences($content, $this->handleReference(...));
+ $parsed = $this->replaceDrawingIdReferences($parsed);
$this->pageRepo->setContentFromInput($page, [
$contentType => $parsed,
ZipTestHelper::deleteZipForImport($import);
}
+
+ public function test_drawing_references_are_updated_within_content()
+ {
+ $testImagePath = $this->files->testFilePath('test-image.png');
+ $parent = $this->entities->chapter();
+
+ $import = ZipTestHelper::importFromData([], [
+ 'page' => [
+ 'name' => 'Page A',
+ 'html' => '<div drawio-diagram="1125"><img src="[[bsexport:image:1125]]"></div>',
+ 'images' => [
+ [
+ 'id' => 1125,
+ 'name' => 'Cat',
+ 'type' => 'drawio',
+ 'file' => 'my_drawing'
+ ]
+ ],
+ ],
+ ], [
+ 'my_drawing' => $testImagePath,
+ ]);
+
+ $this->asAdmin();
+ /** @var Page $page */
+ $page = $this->runner->run($import, $parent);
+
+ $pageImages = Image::where('uploaded_to', '=', $page->id)->whereIn('type', ['gallery', 'drawio'])->get();
+ $this->assertCount(1, $pageImages);
+ $this->assertEquals('drawio', $pageImages[0]->type);
+
+ $drawingId = $pageImages[0]->id;
+ $this->assertStringContainsString("drawio-diagram=\"{$drawingId}\"", $page->html);
+ $this->assertStringNotContainsString('[[bsexport:image:1125]]', $page->html);
+ $this->assertStringNotContainsString('drawio-diagram="1125"', $page->html);
+
+ ZipTestHelper::deleteZipForImport($import);
+ }
}