A lightweight Java library that transforms common Markdown into Telegram's MarkdownV2 format for safe and styled message rendering
- Headings are converted to bold text
- Numbered lists are preserved (
• item) - Unordered lists (
*,+,-) are converted to Telegram bullets (- item) - Emphasis (
*text*or_text_) is converted to italic - Strong emphasis (
**text**or__text__) is converted to bold - Inline and block code is supported, including language annotation for code blocks
- Block quotes are supported
- Links and images are converted to Telegram MarkdownV2 format
- All special characters are escaped for Telegram MarkdownV2
- Spaces and parentheses in links are URL-encoded
- Thematic breaks (---, ***, ___) are supported
- Safe conversion for Telegram message rendering
Note: You need a GitHub personal access token (PAT) with at least
read:packagespermission to download packages from GitHub. Set it asgpr.keyand your GitHub username asgpr.userin yourgradle.properties.
In build.gradle:
repositories {
maven {
name = "markdown2tg"
url = uri("https://maven.pkg.github.com/webcane/markdown2tg")
credentials {
username = project.findProperty("gpr.user")
password = project.findProperty("gpr.key")
}
}
}dependencies {
implementation "cane.brothers:markdown2tg:${version}"
}import cane.brothers.tg.md.convert.MarkdownToTelegramConverter;
MarkdownToTelegramConverter c = new MarkdownToTelegramConverter();
String out = c.convert("# Title\n\n*Hello* **world**. [link](https://example.com)");- CommonMark Specification: https://commonmark.org/
- CommonMark Java library: https://github.com/commonmark/commonmark-java
- Telegram MarkdownV2: https://core.telegram.org/bots/api#markdownv2-style