Skip to content
Closed
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
23 changes: 17 additions & 6 deletions lib/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,29 @@ public function cleanupAttachments(int $fileId): int {
*/
public static function getAttachmentNamesFromContent(string $content): array {
$matches = [];
// get first level of parenthesis after ![.*]
$regex = '/!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\](\(((?>[^()]+)|(?-2))*\))/';

preg_match_all(
// simple version with .+ between the brackets
// '/\!\[.+\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/',
// complex version of php-markdown
'/\!\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[(?>[^\[\]]+|\[\])*\])*\])*\])*\])*\])*\]\(text:\/\/image\?[^)]*imageFileName=([^)&]+)\)/',
$regex,
$content,
$matches,
PREG_SET_ORDER
);
return array_map(static function (array $match) {
return urldecode($match[1]);

$linkTargets = array_map(static function (array $match) {
return trim(urldecode($match[1]), '()');
}, $matches);

$attachmentNames = array_map(static function (string $target) {
preg_match('/^text:\/\/image\?imageFileName=(.*)$/', $target, $targetMatches);
return $targetMatches[1] ?? '';
}, $linkTargets);

// remove empty values
$nonEmptyAttachmentNames = array_filter($attachmentNames);

return $nonEmptyAttachmentNames;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testGetAttachmentNamesFromContent() {
'a](a.png',
',;:!?.§-_a_',
'a`a`.png',
'aaa (2).png',
];
$content = "some content\n";
foreach ($contentNames as $name) {
Expand Down