Skip to content

Commit

Permalink
feat: allow for function nesting
Browse files Browse the repository at this point in the history
format.bold(format.italic('green'))

BREAKING CHANGE: html functions do not escape on their own anymore.
use format.bold(format.escape(input)) instead of format.bold(input) whenever you need
  • Loading branch information
EdJoPaTo committed Mar 3, 2020
1 parent 23cef46 commit 756ba07
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
29 changes: 26 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ $ npm install telegram-format

```js
const {html: format} = require('telegram-format');
const {markdown: format} = require('telegram-format');
const {markdownv2: format} = require('telegram-format');
import {html as format} from 'telegram-format';
import {markdown as format} from 'telegram-format';
import {markdownv2 as format} from 'telegram-format';

format.bold('hey');
Expand All @@ -33,11 +31,36 @@ format.bold('hey');
format.italic('you');
//=> '_you_'

format.bold(format.italic('they'))
//=> '*_they_*'

format.url('me', 'https://edjopato.de');
//=> '[me](https://edjopato.de)'

// Legacy but still works
const {markdown: format} = require('telegram-format');
import {markdown as format} from 'telegram-format';
```

When using format as an alias its easy to switch between Markdown and HTML fast
When using `format` as an alias its easy to switch between Markdown and HTML fast.

### Escaping Input

When you have something that might be unescaped you need to use `format.escape` before formatting it.

```js
const username = 'master_yoda'
format.italic(format.escape(username))
```

`format.monospace` and `format.monospaceBlock` will escape on their own as they only need to escape specific characters.
You do not need to escape the input in this cases.

`MarkdownV2` and `HTML` are fairly similar in escaping inputs but `Markdown` is not.
`Markdown` is still supported by this library and by Telegram for legacy reasons but it behaves a bit differently.
`Markdown` still escapes inputs and does not need `format.escape` before.
Also nested formatting is not supported by telegram itself.
Consider switching to `MarkdownV2` or `HTML` for simplicity reasons.

## API

Expand Down
10 changes: 5 additions & 5 deletions source/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ function escape(text: string): string {
}

function bold(text: string): string {
return `<b>${escape(text)}</b>`
return `<b>${text}</b>`
}

function italic(text: string): string {
return `<i>${escape(text)}</i>`
return `<i>${text}</i>`
}

function strikethrough(text: string): string {
return `<s>${escape(text)}</s>`
return `<s>${text}</s>`
}

function underline(text: string): string {
return `<u>${escape(text)}</u>`
return `<u>${text}</u>`
}

function monospace(text: string): string {
Expand All @@ -38,7 +38,7 @@ function monospaceBlock(text: string, programmingLanguage?: string): string {
}

function url(label: string, url: string): string {
return `<a href="${url}">${escape(label)}</a>`
return `<a href="${url}">${label}</a>`
}

function userMention(label: string, userId: number): string {
Expand Down
10 changes: 5 additions & 5 deletions source/markdownv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ function escape(text: string): string {
}

function bold(text: string): string {
return `*${escape(text)}*`
return `*${text}*`
}

function italic(text: string): string {
return `_${escape(text)}_`
return `_${text}_`
}

function strikethrough(text: string): string {
return `~${escape(text)}~`
return `~${text}~`
}

function underline(text: string): string {
return `__${escape(text)}__`
return `__${text}__`
}

function monospace(text: string): string {
Expand All @@ -46,7 +46,7 @@ function monospaceBlock(text: string, programmingLanguage?: string): string {
}

function url(label: string, url: string): string {
return `[${escapeInteral(label, '\\]')}](${escapeInteral(url, ')')})`
return `[${label}](${url})`
}

function userMention(label: string, userId: number): string {
Expand Down
4 changes: 2 additions & 2 deletions test/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ test('escape', t => {
t.is(format.escape('h<e>y'), 'h&lt;e&gt;y')
})

test('bold malicious', t => {
t.is(format.bold('bo<ld'), '<b>bo&lt;ld</b>')
test('bold italic', t => {
t.is(format.bold(format.italic('green')), '<b><i>green</i></b>')
})

test('user mention', t => {
Expand Down
8 changes: 2 additions & 6 deletions test/markdownv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ 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('bold italic', t => {
t.is(format.bold(format.italic('green')), '*_green_*')
})

test('user mention', t => {
Expand Down

0 comments on commit 756ba07

Please sign in to comment.