Skip to content

Commit e7af7bd

Browse files
committed
Add static method Entity::parseMarkdownV2 to handle MarkdownV2.
1 parent 183094b commit e7af7bd

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
99
### Added
1010
- Replaced 'generic' and 'genericmessage' strings with Telegram::GENERIC_COMMAND and Telegram::GENERIC_MESSAGE_COMMAND constants (@1int)
1111
- Bot API 4.8 (Extra Poll and Dice features).
12+
- New static method `Entity::parseMarkdownV2` for MarkdownV2.
1213
### Changed
14+
- Made `Entity::parseMarkdown` static, to not require an `Entity` object.
1315
### Deprecated
1416
### Removed
1517
### Fixed

src/Entities/Entity.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,15 @@ protected function makePrettyObjectArray($class, $property)
193193
}
194194

195195
/**
196-
* Escape markdown special characters
196+
* Escape markdown (v1) special characters
197+
*
198+
* @see https://core.telegram.org/bots/api#markdown-style
197199
*
198200
* @param string $string
199201
*
200202
* @return string
201203
*/
202-
public function escapeMarkdown($string)
204+
public static function escapeMarkdown($string)
203205
{
204206
return str_replace(
205207
['[', '`', '*', '_',],
@@ -208,12 +210,32 @@ public function escapeMarkdown($string)
208210
);
209211
}
210212

213+
/**
214+
* Escape markdown (v2) special characters
215+
*
216+
* @see https://core.telegram.org/bots/api#markdownv2-style
217+
*
218+
* @param string $string
219+
*
220+
* @return string
221+
*/
222+
public static function escapeMarkdownV2($string)
223+
{
224+
return str_replace(
225+
['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'],
226+
['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'],
227+
$string
228+
);
229+
}
230+
211231
/**
212232
* Try to mention the user
213233
*
214234
* Mention the user with the username otherwise print first and last name
215235
* if the $escape_markdown argument is true special characters are escaped from the output
216236
*
237+
* @todo What about MarkdownV2?
238+
*
217239
* @param bool $escape_markdown
218240
*
219241
* @return string|null
@@ -239,7 +261,7 @@ public function tryMention($escape_markdown = false)
239261
}
240262

241263
if ($escape_markdown) {
242-
$name = $this->escapeMarkdown($name);
264+
$name = self::escapeMarkdown($name);
243265
}
244266

245267
return ($is_username ? '@' : '') . $name;

tests/unit/Entities/EntityTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Longman\TelegramBot\Tests\Unit;
13+
14+
use Longman\TelegramBot\Entities\Entity;
15+
16+
/**
17+
* @link https://github.com/php-telegram-bot/core
18+
* @author Baev Nikolay <[email protected]>
19+
* @copyright Avtandil Kikabidze <[email protected]>
20+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
21+
* @package TelegramTest
22+
*/
23+
class EntityTest extends TestCase
24+
{
25+
public function testEscapeMarkdown()
26+
{
27+
// Make sure all characters that need escaping are escaped.
28+
29+
// Markdown V1
30+
self::assertEquals('\[\`\*\_', Entity::escapeMarkdown('[`*_'));
31+
self::assertEquals('\*mark\*\_down\_~test~', Entity::escapeMarkdown('*mark*_down_~test~'));
32+
33+
// Markdown V2
34+
self::assertEquals('\_\*\[\]\(\)\~\`\>\#\+\-\=\|\{\}\.\!', Entity::escapeMarkdownV2('_*[]()~`>#+-=|{}.!'));
35+
self::assertEquals('\*mark\*\_down\_\~test\~', Entity::escapeMarkdownV2('*mark*_down_~test~'));
36+
}
37+
}

tests/unit/Entities/UserTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public function testEscapeMarkdown()
6565
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']);
6666
self::assertEquals('John `Taylor`', $user->tryMention());
6767
self::assertEquals('John \`Taylor\`', $user->tryMention(true));
68-
69-
// Plain escapeMarkdown functionality.
70-
self::assertEquals('a\`b\[c\*d\_e', $user->escapeMarkdown('a`b[c*d_e'));
7168
}
7269

7370
public function testGetProperties()

0 commit comments

Comments
 (0)