Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: use Deno and deno2node #42

Merged
merged 10 commits into from
Nov 28, 2023
Merged
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
14 changes: 0 additions & 14 deletions .github/dependabot.yml

This file was deleted.

33 changes: 33 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deno

on:
push:
EdJoPaTo marked this conversation as resolved.
Show resolved Hide resolved
pull_request:
schedule:
# Check if it works with current deno version
- cron: '42 2 * * 6' # weekly on Saturday 2:42 UTC

jobs:
denofmt-and-lint:
runs-on: ubuntu-latest
steps:
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/checkout@v4

- run: deno lint
- run: deno fmt --check

test:
runs-on: ubuntu-latest
steps:
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/checkout@v4

- run: deno cache source/*.ts
- run: deno check source/*.ts
- run: deno test --coverage=coverage/
- run: deno coverage coverage/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vscode
/*-*.*.*.tgz
coverage
dist
docs
node_modules
151 changes: 36 additions & 115 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,149 +2,70 @@

[![NPM Version](https://img.shields.io/npm/v/telegram-format.svg)](https://www.npmjs.com/package/telegram-format)
[![node](https://img.shields.io/node/v/telegram-format.svg)](https://www.npmjs.com/package/telegram-format)
[![deno module](https://shield.deno.dev/x/telegram_format)](https://deno.land/x/telegram_format)

> Format Telegram message texts with Markdown or HTML

This library abstracts the [formatting options](https://core.telegram.org/bots/api#formatting-options) for you.
This library abstracts the
[formatting options](https://core.telegram.org/bots/api#formatting-options) for
you.

## Install

Node.js:

```bash
npm install telegram-format
```

Deno:

```ts
import {/* ... */} from "https://deno.land/x/telegram_format/mod.ts";
```

## Usage

```js
import {html as format} from 'telegram-format';
import {markdownv2 as format} from 'telegram-format';
```ts
import { html as format } from "telegram-format";
import { markdownv2 as format } from "telegram-format";

format.bold('hey');
format.bold("hey");
//=> '*hey*'

format.italic('you');
format.italic("you");
//=> '_you_'

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

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

// Legacy but still works
import {markdown as format} from '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.
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))
```ts
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

### bold(text)

#### text

Type: `string`

Text to be formatted bold

### italic(text)

#### text

Type: `string`

Text to be formatted italic

### strikethrough(text)

#### text

Type: `string`

Text to be striked through

### underline(text)

#### text

Type: `string`

Text to be underlined

### monospace(text)

#### text

Type: `string`

Text to be displayed as inline monospace

### monospaceBlock(text, [programmingLanguage])

#### text

Type: `string`

Text to be displayed as monospace block

#### programmingLanguage

Type: `string` (optional)

Can be used to specify the programming language of the code

### url(label, url)

#### label

Type: `string`

Label of the url field

#### url

Type: `string`

Url which is called when the user taps on the label

### userMention(label, userId)

#### label

Type: `string`

Label of the user mention. Might be used with the first name for example.

#### userID

Type: `number`

User id to which is linked

### escape(text)

#### text

Type: `string`

Text which should be escaped so it does not interfer with formatting.
`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.

User generated Text should always be escapted to prevent errors.
`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.
44 changes: 44 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
// https://deno.land/manual/getting_started/configuration_file
"lock": false,
// https://deno.land/manual/advanced/typescript/configuration
// https://www.typescriptlang.org/tsconfig/
"compilerOptions": {
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"useUnknownInCatchVariables": true
},
EdJoPaTo marked this conversation as resolved.
Show resolved Hide resolved
"exclude": [
"./coverage/",
"./dist/",
"./docs/",
"./node_modules/"
],
"fmt": {
"useTabs": true
},
"lint": {
"rules": {
// https://lint.deno.land/?all=on
"include": [
"default-param-last",
"eqeqeq",
"explicit-module-boundary-types",
"no-await-in-loop",
"no-eval",
"no-non-null-asserted-optional-chain",
"no-non-null-assertion",
"no-sparse-arrays"
]
}
},
"tasks": {
"coverage": "deno test --coverage=coverage/ && deno coverage coverage/",
"doc": "deno doc --html --name=telegram_format source/mod.ts"
}
}
46 changes: 14 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,33 @@
"markdownv2"
],
"license": "MIT",
"repository": "EdJoPaTo/telegram-format",
"repository": {
"type": "git",
"url": "https://github.com/EdJoPaTo/telegram-format.git"
},
"author": {
"name": "EdJoPaTo",
"email": "telegram-format-npm-package@edjopato.de",
"email": "telegram-format-typescript-package@edjopato.de",
"url": "https://edjopato.de"
},
"scripts": {
"build": "del-cli dist && tsc",
"prepack": "npm run build",
"test": "tsc --sourceMap && xo && c8 --all ava"
"prepare": "deno2node",
"pretest": "deno2node",
"test": "node --test"
},
"type": "module",
"engines": {
"node": ">=14"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
"ava": "^5.0.1",
"c8": "^8.0.1",
"del-cli": "^5.0.0",
"typescript": "^5.0.2",
"xo": "^0.56.0"
"@types/node": "^16.18.64",
"deno2node": "1.10.1"
},
"files": [
"dist",
"!*.test.*"
"!*.test.*",
"!test.*"
],
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"xo": {
"rules": {
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/prefer-readonly-parameter-types": "error",
"unicorn/prefer-string-replace-all": "off",
"ava/no-ignored-test-files": "off"
},
"overrides": [
{
"files": [
"**/*.test.*"
],
"rules": {
"@typescript-eslint/prefer-readonly-parameter-types": "off"
}
}
]
}
"main": "./dist/mod.js",
"types": "./dist/mod.d.ts"
}
4 changes: 4 additions & 0 deletions source/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# telegram_format

See the [README on GitHub](https://github.com/EdJoPaTo/telegram-format#readme)
or the [documentation on deno.land](https://deno.land/x/telegram-format?doc).
Loading