-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(markdownv2): add general markdownv2 support
only implements the existing interface right now
- Loading branch information
Showing
5 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './interface' | ||
|
||
export * from './html' | ||
export * from './markdown' | ||
export * from './markdownv2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// https://core.telegram.org/bots/api#markdownv2-style | ||
|
||
import {Formatter} from './interface' | ||
|
||
function escapeInteral(text: string, escapeChars: string): string { | ||
return text.replace(new RegExp(`[${escapeChars}\\\\]`, 'g'), '\\$&') | ||
} | ||
|
||
function escape(text: string): string { | ||
return text.replace(/[_*[\]()~`>#+-=|{}.!]/g, '\\$&') | ||
} | ||
|
||
function bold(text: string): string { | ||
return `*${escape(text)}*` | ||
} | ||
|
||
function italic(text: string): string { | ||
return `_${escape(text)}_` | ||
} | ||
|
||
function monospace(text: string): string { | ||
return '`' + escapeInteral(text, '`') + '`' | ||
} | ||
|
||
function monospaceBlock(text: string, programmingLanguage?: string): string { | ||
let result = '' | ||
result += '```' | ||
|
||
if (programmingLanguage) { | ||
result += programmingLanguage | ||
} | ||
|
||
result += '\n' | ||
result += escapeInteral(text, '`') | ||
result += '\n' | ||
result += '```' | ||
return result | ||
} | ||
|
||
function url(label: string, url: string): string { | ||
return `[${escapeInteral(label, '\\]')}](${escapeInteral(url, ')')})` | ||
} | ||
|
||
function userMention(label: string, userId: number): string { | ||
return url(label, `tg://user?id=${userId}`) | ||
} | ||
|
||
export const markdownv2: Formatter = { | ||
parse_mode: 'MarkdownV2', | ||
escape, | ||
bold, | ||
italic, | ||
monospace, | ||
monospaceBlock, | ||
url, | ||
userMention | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import test from 'ava' | ||
|
||
import {markdownv2 as format} from '../source' | ||
|
||
test('bold', t => { | ||
t.is(format.bold('bold'), '*bold*') | ||
}) | ||
|
||
test('italic', t => { | ||
t.is(format.italic('italic'), '_italic_') | ||
}) | ||
|
||
test('url', t => { | ||
t.is(format.url('me', 'https://edjopato.de'), '[me](https://edjopato.de)') | ||
}) | ||
|
||
test('escape', t => { | ||
t.is(format.escape('[h_]e(*y)`'), '\\[h\\_\\]e\\(\\*y\\)\\`') | ||
}) | ||
|
||
test('bold malicious', t => { | ||
t.is(format.bold('bo*ld'), '*bo\\*ld*') | ||
}) | ||
|
||
test('italic malicious', t => { | ||
t.is(format.italic('ita_lic'), '_ita\\_lic_') | ||
}) | ||
|
||
test('user mention', t => { | ||
t.is(format.userMention('inline mention of a user', 123456789), '[inline mention of a user](tg://user?id=123456789)') | ||
}) | ||
|
||
test('monospace', t => { | ||
t.is(format.monospace('inline fixed-width code'), '`inline fixed-width code`') | ||
}) | ||
|
||
test('monospaceBlock w/o language', t => { | ||
t.is(format.monospaceBlock('pre-formatted fixed-width code block'), '```\npre-formatted fixed-width code block\n```') | ||
}) | ||
|
||
test('monospaceBlock w/ language', t => { | ||
t.is(format.monospaceBlock('pre-formatted fixed-width code block written in the Python programming language', 'python'), '```python\npre-formatted fixed-width code block written in the Python programming language\n```') | ||
}) |