Skip to content

Commit

Permalink
build: use deno and deno2node
Browse files Browse the repository at this point in the history
Co-authored-by: Dunkan <70066170+dcdunkan@users.noreply.github.com>
Co-authored-by: Wojciech Pawlik <woj.pawlik@gmail.com>
  • Loading branch information
3 people committed Nov 13, 2023
1 parent ceb7d19 commit c1c3bbf
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 235 deletions.
14 changes: 0 additions & 14 deletions .github/dependabot.yml

This file was deleted.

38 changes: 27 additions & 11 deletions .github/workflows/nodejs.yml → .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
name: Node.js
name: Deno

on:
push:
pull_request:
schedule:
# Check if it works with current dependencies
# Check if it works with current versions
- 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:
name: Node.js
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
- uses: denoland/setup-deno@v1
with:
node-version: 'lts/*'
deno-version: v1.x
- uses: actions/checkout@v4
- run: npm install
- run: npm test
- run: npm pack

publish:
- run: deno cache source/*.ts
- run: deno check source/*.ts
- run: deno test

npm:
name: Publish on NPM
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
needs: test
needs: [denofmt-and-lint, test]
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm pack
- run: npm publish
if: startsWith(github.ref, 'refs/tags/v')
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_CONFIG_ACCESS: public
NPM_CONFIG_PROVENANCE: true
- name: Create GitHub release
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v1
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*-*.*.*.tgz
coverage
dist
node_modules
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
53 changes: 34 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,73 @@

[![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';
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))
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.
`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.
`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: 10 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"lock": false,
"fmt": {
"useTabs": true
},
"exclude": [
"./dist/",
"./node_modules/"
]
}
39 changes: 6 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,17 @@
"url": "https://edjopato.de"
},
"scripts": {
"build": "del-cli dist && tsc",
"prepack": "npm run build",
"test": "tsc --sourceMap && xo && c8 --all ava"
"prepare": "deno2node",
"test": "echo use deno test && exit 1"
},
"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"
"deno2node": "1.10.1"
},
"files": [
"dist",
"!*.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"
}
}
]
}
"files": ["dist"],
"main": "./dist/mod.js",
"types": "./dist/mod.d.ts"
}
71 changes: 37 additions & 34 deletions source/html.test.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,70 @@
import test from 'ava';
import {html as format} from './html.js';
import { assertEquals } from "https://deno.land/std@0.206.0/assert/mod.ts";
import { html as format } from "./html.ts";

test('bold', t => {
t.is(format.bold('bold'), '<b>bold</b>');
Deno.test("bold", () => {
assertEquals(format.bold("bold"), "<b>bold</b>");
});

test('italic', t => {
t.is(format.italic('italic'), '<i>italic</i>');
Deno.test("italic", () => {
assertEquals(format.italic("italic"), "<i>italic</i>");
});

test('strikethrough', t => {
t.is(format.strikethrough('strikethrough'), '<s>strikethrough</s>');
Deno.test("strikethrough", () => {
assertEquals(format.strikethrough("strikethrough"), "<s>strikethrough</s>");
});

test('underline', t => {
t.is(format.underline('underline'), '<u>underline</u>');
Deno.test("underline", () => {
assertEquals(format.underline("underline"), "<u>underline</u>");
});

test('spoiler', t => {
t.is(format.spoiler('spoiler'), '<span class="tg-spoiler">spoiler</span>');
Deno.test("spoiler", () => {
assertEquals(
format.spoiler("spoiler"),
'<span class="tg-spoiler">spoiler</span>',
);
});

test('url', t => {
t.is(
format.url('me', 'https://edjopato.de'),
Deno.test("url", () => {
assertEquals(
format.url("me", "https://edjopato.de"),
'<a href="https://edjopato.de">me</a>',
);
});

test('escape', t => {
t.is(format.escape('h<e>y'), 'h&lt;e&gt;y');
Deno.test("escape", () => {
assertEquals(format.escape("h<e>y"), "h&lt;e&gt;y");
});

test('bold italic', t => {
t.is(format.bold(format.italic('green')), '<b><i>green</i></b>');
Deno.test("bold italic", () => {
assertEquals(format.bold(format.italic("green")), "<b><i>green</i></b>");
});

test('user mention', t => {
t.is(
format.userMention('inline mention of a user', 123_456_789),
Deno.test("user mention", () => {
assertEquals(
format.userMention("inline mention of a user", 123_456_789),
'<a href="tg://user?id=123456789">inline mention of a user</a>',
);
});

test('monospace', t => {
t.is(
format.monospace('inline fixed-width code'),
'<code>inline fixed-width code</code>',
Deno.test("monospace", () => {
assertEquals(
format.monospace("inline fixed-width code"),
"<code>inline fixed-width code</code>",
);
});

test('monospaceBlock w/o language', t => {
t.is(
format.monospaceBlock('pre-formatted fixed-width code block'),
'<pre>pre-formatted fixed-width code block</pre>',
Deno.test("monospaceBlock w/o language", () => {
assertEquals(
format.monospaceBlock("pre-formatted fixed-width code block"),
"<pre>pre-formatted fixed-width code block</pre>",
);
});

test('monospaceBlock w/ language', t => {
t.is(
Deno.test("monospaceBlock w/ language", () => {
assertEquals(
format.monospaceBlock(
'pre-formatted fixed-width code block written in the Python programming language',
'python',
"pre-formatted fixed-width code block written in the Python programming language",
"python",
),
'<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>',
);
Expand Down
14 changes: 8 additions & 6 deletions source/html.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {Formatter} from './types.js';
import type { Formatter } from "./types.ts";

function escape(text: string): string {
return text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}

function bold(text: string): string {
Expand Down Expand Up @@ -33,7 +33,9 @@ function monospace(text: string): string {

function monospaceBlock(text: string, programmingLanguage?: string): string {
if (programmingLanguage) {
return `<pre><code class="language-${programmingLanguage}">${escape(text)}</code></pre>`;
return `<pre><code class="language-${programmingLanguage}">${
escape(text)
}</code></pre>`;
}

return `<pre>${escape(text)}</pre>`;
Expand All @@ -49,7 +51,7 @@ function userMention(label: string, userId: number): string {

/** https://core.telegram.org/bots/api#html-style */
export const html = {
parse_mode: 'HTML',
parse_mode: "HTML",
escape,
bold,
italic,
Expand Down
5 changes: 0 additions & 5 deletions source/index.ts

This file was deleted.

Loading

0 comments on commit c1c3bbf

Please sign in to comment.