Skip to content

Commit

Permalink
fmt single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix committed May 24, 2024
1 parent 9994718 commit 068afb2
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 53 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@
## Installing

```ts
import { posts, postsFlavor } from "@example/telegraph";
import { posts, postsFlavor } from '@example/telegraph';

type MyContext = Context & PostsFlavor;

const bot = new Bot<MyContext>("BOT_TOKEN");
const bot = new Bot<MyContext>('BOT_TOKEN');
bot.use(posts());
```

It's strongly recommend to install using a fixed access token to prevent creating a publisher on every action, you can create a publisher as specified [here](https://telegra.ph/api#createAccount):

```ts
bot.use(posts({ accessToken: "myAccessToken", short_name: "Me" }));
bot.use(posts({ accessToken: 'myAccessToken', short_name: 'Me' }));
```

## Usage

Default parse method is Markdown. A `mediaUpload` helper function it's provided for embedding images on the post

```ts
bot.command("example1", async (c) => {
bot.command('example1', async (c) => {
await c.replyWithPost(veryLongMarkdown);
});

bot.command("example2", async (c) => {
bot.command('example2', async (c) => {
await c.replyWithPost(veryLongHTML, {
pageParseMode: "HTML",
pageTitle: "My Title",
pageParseMode: 'HTML',
pageTitle: 'My Title',
});
});

bot.command("example3", async (c) => {
bot.command('example3', async (c) => {
const superLongMarkdown = `# My Super Long Message
![My Image](${await mediaUpload("./file.jpg")})`;
![My Image](${await mediaUpload('./file.jpg')})`;
await c.replyWithPost(msg);
});

bot.command("example4", async (c) => {
bot.command('example4', async (c) => {
const superLongHTML = `<h1>¡YES!</h1><br>
<img src="${await mediaUpload("./no.jpg")}">`;
<img src="${await mediaUpload('./no.jpg')}">`;
await c.replyWithPost(msg);
});
```
Expand Down
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"fmt": {
"indentWidth": 4,
"proseWrap": "preserve",
"singleQuote": true,
"exclude": [
"./node_modules/",
"./out/",
Expand Down
16 changes: 8 additions & 8 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseHtml, parseMarkdown, Telegraph } from "./deps.deno.ts";
import { Context, Message, NextFunction } from "./deps.deno.ts";
import { postsOpts, TelegraphOpts } from "./types.d.ts";
import { parseHtml, parseMarkdown, Telegraph } from './deps.deno.ts';
import { Context, Message, NextFunction } from './deps.deno.ts';
import { postsOpts, TelegraphOpts } from './types.d.ts';

export interface PostsFlavor<C extends Context = Context> extends Context {
/**
Expand Down Expand Up @@ -29,7 +29,7 @@ export interface PostsFlavor<C extends Context = Context> extends Context {
export function posts<C extends Context>(
publisher?: TelegraphOpts,
) {
publisher = { short_name: "Anonymous", ...publisher };
publisher = { short_name: 'Anonymous', ...publisher };
const tph = new Telegraph(publisher);

return (ctx: PostsFlavor<C>, next: NextFunction) => {
Expand All @@ -39,19 +39,19 @@ export function posts<C extends Context>(
) => {
if (!ctx.chat) {
throw new Error(
"Cannot reply if the `chat` property does not exist on the update",
'Cannot reply if the `chat` property does not exist on the update',
);
}

const pageParseMode = opts?.postParseMode || "Markdown";
const pageParseMode = opts?.postParseMode || 'Markdown';

const content = pageParseMode === "Markdown"
const content = pageParseMode === 'Markdown'
? parseMarkdown(text)
: parseHtml(text);

if (!content) {
throw new Error(
"Content should not be undefined",
'Content should not be undefined',
);
}
if (!tph.token.length) {
Expand Down
11 changes: 7 additions & 4 deletions src/deps.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ export {
parseMarkdown,
Telegraph,
upload,
} from "https://deno.land/x/telegraph/mod.ts";
export type { CreatePageOptions, TelegraphOptions } from "https://deno.land/x/telegraph/mod.ts";
} from 'https://deno.land/x/telegraph/mod.ts';
export type {
CreatePageOptions,
TelegraphOptions,
} from 'https://deno.land/x/telegraph/mod.ts';

export {
Context,
type NextFunction,
} from "https://lib.deno.dev/x/grammy@1/mod.ts";
export type { Chat, Message } from "https://lib.deno.dev/x/grammy@1/types.ts";
} from 'https://lib.deno.dev/x/grammy@1/mod.ts';
export type { Chat, Message } from 'https://lib.deno.dev/x/grammy@1/types.ts';
4 changes: 2 additions & 2 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { upload as mediaUpload } from "./deps.deno.ts";
export * from './context.ts'
export { upload as mediaUpload } from './deps.deno.ts';
export * from './context.ts';
16 changes: 8 additions & 8 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Context } from "./deps.deno.ts";
import { CreatePageOptions, TelegraphOptions } from "./deps.deno.ts";
import { Context } from './deps.deno.ts';
import { CreatePageOptions, TelegraphOptions } from './deps.deno.ts';

export interface postsOpts {
postParseMode?: "HTML" | "Markdown";
postTitle?: CreatePageOptions["title"];
postParseMode?: 'HTML' | 'Markdown';
postTitle?: CreatePageOptions['title'];
message?: {
text: Parameters<Context["reply"]>[0];
other: Parameters<Context["reply"]>[1];
text: Parameters<Context['reply']>[0];
other: Parameters<Context['reply']>[1];
};
}

export type TelegraphOpts =
& Required<Pick<TelegraphOptions, "short_name">>
& Omit<TelegraphOptions, "apiRoot">;
& Required<Pick<TelegraphOptions, 'short_name'>>
& Omit<TelegraphOptions, 'apiRoot'>;
2 changes: 1 addition & 1 deletion test/assets/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ magnorum. Poscimus querellas cultum.
Quas summorum medioque pererrat sedit, nec modo smaragdis fasque miseranda.
Vultu per venabula. Ater incurvae, iungi amictae Sigeia pulsabantque magis
primisque *vobis*.
`
`;
6 changes: 3 additions & 3 deletions test/deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ export {
assertRejects,
assertStringIncludes,
assertThrows,
} from "https://deno.land/std@0.203.0/assert/mod.ts";
} from 'https://deno.land/std@0.203.0/assert/mod.ts';
export {
afterEach,
beforeEach,
describe,
it,
} from "https://deno.land/std@0.203.0/testing/bdd.ts";
} from 'https://deno.land/std@0.203.0/testing/bdd.ts';
export {
type Spy,
spy,
type Stub,
stub,
} from "https://deno.land/std@0.203.0/testing/mock.ts";
} from 'https://deno.land/std@0.203.0/testing/mock.ts';
32 changes: 16 additions & 16 deletions test/telegraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { assertExists } from "https://deno.land/std@0.203.0/assert/assert_exists.ts";
import { parseMarkdown, Telegraph } from "../src/deps.deno.ts";
import { TelegraphOpts } from "../src/types.d.ts";
import { assertObjectMatch } from "./deps.test.ts";
import { describe, it } from "./deps.test.ts";
import { dummyMarkdownText } from "./assets/text.ts";
import { assertExists } from 'https://deno.land/std@0.203.0/assert/assert_exists.ts';
import { parseMarkdown, Telegraph } from '../src/deps.deno.ts';
import { TelegraphOpts } from '../src/types.d.ts';
import { assertObjectMatch } from './deps.test.ts';
import { describe, it } from './deps.test.ts';
import { dummyMarkdownText } from './assets/text.ts';

function getPublisher(publisher?: TelegraphOpts): TelegraphOpts {
return { short_name: "Anonymous", ...publisher };
return { short_name: 'Anonymous', ...publisher };
}

describe("Publisher opts tests", () => {
it("Should assign a publisher short name if no opts", () => {
describe('Publisher opts tests', () => {
it('Should assign a publisher short name if no opts', () => {
let publisher = undefined;
publisher = getPublisher(publisher);
assertObjectMatch(publisher, { short_name: "Anonymous" });
assertObjectMatch(publisher, { short_name: 'Anonymous' });
});
it("If publisher.author_name exist, should keep it", () => {
let publisher = { short_name: "Papito" };
it('If publisher.author_name exist, should keep it', () => {
let publisher = { short_name: 'Papito' };
publisher = getPublisher(publisher);
assertObjectMatch(publisher, { short_name: "Papito" });
assertObjectMatch(publisher, { short_name: 'Papito' });
});
});

describe("Publish", () => {
it("Creating an article should work with no access token provided", async () => {
const publisher = getPublisher({ short_name: "papito" });
describe('Publish', () => {
it('Creating an article should work with no access token provided', async () => {
const publisher = getPublisher({ short_name: 'papito' });
const tph = new Telegraph(publisher);
if (!tph.token.length) {
tph.token = (await tph.createAccount(publisher)).access_token;
Expand Down

0 comments on commit 068afb2

Please sign in to comment.