Skip to content

Commit

Permalink
Добавлено извлечение текста из markdown в Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
erickskrauch committed Apr 15, 2024
1 parent 40092f5 commit 4a03550
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@discordjs/formatters": "^0.3",
"discord-markdown-parser": "^1",
"discord.js": "^14",
"dotenv": "^16",
"node-graceful": "^3",
Expand Down
3 changes: 2 additions & 1 deletion src/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChannelType, Client as DiscordClient, Events, GatewayIntentBits, Partia
import { escapeMarkdown } from '@discordjs/formatters';
import { BotLauncher, MessageHandler, ShutdownFunction } from './Engine';
import UnconfiguredException from './UnconfiguredException';
import { markdownToTxt } from './markdown';

export function getLauncherFromEnv(): BotLauncher {
const token = process.env.DISCORD_BOT_TOKEN;
Expand Down Expand Up @@ -41,7 +42,7 @@ export function launch(token: string, handler: MessageHandler): Promise<Shutdown
}

const response = handler({
text,
text: markdownToTxt(text),
chat: message.channelId,
sender: message.author.id,
sentAt: message.createdAt,
Expand Down
13 changes: 13 additions & 0 deletions src/markdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { markdownToTxt } from './markdown';

describe('markdownToTxt', () => {
describe('underline', () => {
it('inline with space', () => expect(markdownToTxt('д __а__')).toBe('д а'));
it('inline no space', () => expect(markdownToTxt('д__а__')).toBe('да'));
it('underline space', () => expect(markdownToTxt('д __ __ а')).toBe('д а'));
});

describe('quote', () => {
it('simple quote', () => expect(markdownToTxt('> да')).toBe('да'));
});
});
19 changes: 19 additions & 0 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { parse as parseMarkdown } from 'discord-markdown-parser';

export function markdownToTxt(str: string): string {
return parseMarkdown(str).map(recursiveFindText).join('');
}

// I can't introduce valid typings since they're seems to be invalid in first place.
// Use tests to know the possible node types
function recursiveFindText(node: any): string {
if (typeof node.content === 'string') {
return node.content;
}

if (node.content !== undefined) {
return recursiveFindText(node.content);
}

return node.map(recursiveFindText).join('');
}
32 changes: 32 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,19 @@
dependencies:
undici-types "~5.26.4"

"@types/prop-types@*":
version "15.7.12"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.12.tgz#12bb1e2be27293c1406acb6af1c3f3a1481d98c6"
integrity sha512-5zvhXYtRNRluoE/jAp4GVsSduVUzNWKkOZrCDBWYtE7biZywwdC2AcEzg+cSMLFRfVgeAFqpfNabiPjxFddV1Q==

"@types/react@>=16.0.0":
version "18.2.78"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.78.tgz#94aec453d0ccca909998a2b4b2fd78af15a7d2fe"
integrity sha512-qOwdPnnitQY4xKlKayt42q5W5UQrSHjgoXNVEtxeqdITJ99k4VXJOP3vt8Rkm9HmgJpH50UNU+rlqfkfWOqp0A==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"

"@types/request@*":
version "2.48.12"
resolved "https://registry.yarnpkg.com/@types/request/-/request-2.48.12.tgz#0f590f615a10f87da18e9790ac94c29ec4c5ef30"
Expand Down Expand Up @@ -1540,6 +1553,11 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

csstype@^3.0.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.3.tgz#d80ff294d114fb0e6ac500fbf85b60137d7eff81"
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==

dashdash@^1.12.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"
Expand Down Expand Up @@ -1648,6 +1666,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.37.61.tgz#9dd8e58c624237e6f1b23be2d29579af268b8c5b"
integrity sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw==

discord-markdown-parser@^1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/discord-markdown-parser/-/discord-markdown-parser-1.1.0.tgz#bfc5c3c2963690a6b21fdedb49039e96b0ca91cd"
integrity sha512-o2+iFgt5qer6UYY5hVTPGq2mGzleKRGYKcvymg67FdKg4AMJ061KbebKunCERWKjx79dmNHMDnGV2F0DRGCNkw==
dependencies:
simple-markdown "^0.7.3"

discord.js@^14:
version "14.14.1"
resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-14.14.1.tgz#9a2bea23bba13819705ab87612837610abce9ee3"
Expand Down Expand Up @@ -3405,6 +3430,13 @@ signal-exit@^4.0.1:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-4.1.0.tgz#952188c1cbd546070e2dd20d0f41c0ae0530cb04"
integrity sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==

simple-markdown@^0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/simple-markdown/-/simple-markdown-0.7.3.tgz#e32150b2ec6f8287197d09869fd928747a9c5640"
integrity sha512-uGXIc13NGpqfPeFJIt/7SHHxd6HekEJYtsdoCM06mEBPL9fQH/pSD7LRM6PZ7CKchpSvxKL4tvwMamqAaNDAyg==
dependencies:
"@types/react" ">=16.0.0"

sisteransi@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
Expand Down

0 comments on commit 4a03550

Please sign in to comment.