Skip to content

Commit

Permalink
fix: inline query and table parser
Browse files Browse the repository at this point in the history
  • Loading branch information
svifty7 committed Feb 7, 2024
1 parent 2b41cd3 commit b90f1e5
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 74 deletions.
5 changes: 3 additions & 2 deletions src/inline-query/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InlineKeyboard, InlineQueryResultBuilder } from 'grammy';
import { debounce } from 'lodash-es';

import { useConfig } from '../utils/useConfig.js';
import { useHelpers } from '../utils/useHelpers.js';
Expand All @@ -20,7 +21,7 @@ export const useInlineQueries = () => {
}
};

const handler = async (ctx: InlineQueryContext<IContext>) => {
const handler = debounce(async (ctx: InlineQueryContext<IContext>) => {
const { query } = ctx.inlineQuery;

if (!query || query.length < 3) {
Expand Down Expand Up @@ -73,7 +74,7 @@ export const useInlineQueries = () => {
}

return ctx.answerInlineQuery(spells, answerConfig);
};
}, 500);

const registerInlineQueries = (bot: Bot<IContext>) => {
bot.on('inline_query', ctx => handler(ctx));
Expand Down
171 changes: 99 additions & 72 deletions src/utils/useMarkup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,95 +48,122 @@ const allowedTags = [
'dice-roller'
];

export const useMarkup = () => {
const turndownService = new TurndownService({
bulletListMarker: '-'
});
const turndownService = new TurndownService({
bulletListMarker: '-'
});

turndownService.use(gfm);

turndownService.use(gfm);
turndownService.addRule('paragraph', {
filter: 'p',
replacement: (content, node) => {
if (!node.parentNode?.nodeName) {
return `\n\n${content}\n\n`;
}

turndownService.addRule('paragraph', {
filter: 'p',
replacement: content => `\n\n${content}\n\n`
});
return content;
}
});

turndownService.addRule('superscript', {
filter: node => node.nodeName === 'SUP',
replacement: content => ` [${content}]`
});

turndownService.addRule('tableCell', {
filter: node => node.nodeName === 'TH' || node.nodeName === 'TD',
replacement: (content, node) => {
const isLastElement =
node.nextSibling?.nodeName !== 'TH' &&
node.nextSibling?.nodeName !== 'TD';

return `| ${content}${isLastElement ? ' |' : ''}`;
}
});

turndownService.addRule('tableRow', {
filter: node => node.nodeName === 'TR',
replacement: content => `\n${content}\n`
});

turndownService.addRule('table', {
filter: node => node.nodeName === 'TABLE',
replacement: content => `\n${content}\n`
});

turndownService.addRule('diceRoller', {
filter: node => node.nodeName === 'DICE-ROLLER',
replacement: (content, node) => {
let text = '';

if ('getAttribute' in node && node.getAttribute('formula')) {
text = `<b>${node.getAttribute('formula')}</b>`;
}

turndownService.addRule('superscript', {
filter: node => node.nodeName === 'SUP',
replacement: content => ` [${content}]`
});
if ('getAttribute' in node && node.getAttribute(':formula')) {
text = `<b>${node.getAttribute('formula')}</b>`;
}

turndownService.addRule('diceRoller', {
filter: node => node.nodeName === 'DICE-ROLLER',
replacement: (content, node) => {
let text = '';
if (content) {
text = `<b>${content}</b>`;
}

if ('getAttribute' in node && node.getAttribute('formula')) {
text = `<b>${node.getAttribute('formula')}</b>`;
}
return text;
}
});

if ('getAttribute' in node && node.getAttribute(':formula')) {
text = `<b>${node.getAttribute('formula')}</b>`;
}
turndownService.addRule('inlineLink', {
filter: (node, options) =>
options.linkStyle === 'inlined' &&
node.nodeName === 'A' &&
!!node.getAttribute('href'),

if (content) {
text = `<b>${content}</b>`;
replacement: (content, node) => {
const getUpdatedHref = (href: string) => {
if (href.startsWith('http')) {
return href;
}

return text;
}
});
return getUrl(href);
};

turndownService.addRule('inlineLink', {
filter: (node, options) =>
options.linkStyle === 'inlined' &&
node.nodeName === 'A' &&
!!node.getAttribute('href'),
let href: string | null = null;

replacement: (content, node) => {
const getUpdatedHref = (href: string) => {
if (href.startsWith('http')) {
return href;
}
if ('getAttribute' in node) {
href = node.getAttribute('href');
}

return getUrl(href);
};
if (href) {
href = getUpdatedHref(href);
}

let href: string | null = null;
return `<a href="${href}">${content}</a>`;
}
});

if ('getAttribute' in node) {
href = node.getAttribute('href');
}
turndownService.addRule('baseTags', {
filter: [
// Bold
'b',
'strong',

if (href) {
href = getUpdatedHref(href);
}
// Italic
'i',
'em',

return `<a href="${href}">${content}</a>`;
}
});

turndownService.addRule('baseTags', {
filter: [
// Bold
'b',
'strong',

// Italic
'i',
'em',

// Strikethrough
's',
'del',

// Underline
'u',
'ins'
],
replacement: (content, node) =>
`<${node.nodeName.toLowerCase()}>${content}</${node.nodeName.toLowerCase()}>`
});
// Strikethrough
's',
'del',

// Underline
'u',
'ins'
],
replacement: (content, node) =>
`<${node.nodeName.toLowerCase()}>${content}</${node.nodeName.toLowerCase()}>`
});

export const useMarkup = () => {
const getSanitized = (html: string) => sanitizeHtml(html, { allowedTags });

const getMarkup = (html: string) => {
Expand Down

0 comments on commit b90f1e5

Please sign in to comment.