1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-24 22:46:41 +07:00

added links trimmer

This commit is contained in:
Fedor Katurov 2023-12-30 13:26:30 +07:00
parent 31af4e11b1
commit 2d2c959925
3 changed files with 29 additions and 14 deletions

View file

@ -9,6 +9,7 @@ import path from "path";
import hb from "handlebars"; import hb from "handlebars";
import strip from "strip-markdown"; import strip from "strip-markdown";
import { VFileCompatible } from "vfile"; import { VFileCompatible } from "vfile";
import transformMDLinks from "../../utils/transformMDLinks";
const removeFrontmatter = () => (tree) => { const removeFrontmatter = () => (tree) => {
tree.children = tree.children.filter((item) => item.type !== "yaml"); tree.children = tree.children.filter((item) => item.type !== "yaml");
@ -50,7 +51,8 @@ export class Template<
} }
/** /**
* Themes the template with values * Themes the template with values, removes markdown from template.
* NOTE: text, that we'll insert into template, won't be used here
*/ */
public theme = (values: V, markdown?: boolean) => { public theme = (values: V, markdown?: boolean) => {
const processor = unified() const processor = unified()
@ -81,13 +83,17 @@ export class Template<
}); });
} }
public static cleanText(text: string) { /** Cleans text from markdown, but transforms links to MD if needed */
return unified() public static cleanText(text: string, markdown?: boolean) {
.use(stringify) const processor = unified().use(stringify).use(parser).use(strip);
.use(parser)
.use(strip) const output = processor.processSync(text).contents.toString();
.processSync(text)
.contents.toString(); if (markdown) {
return transformMDLinks(output);
}
return output;
} }
} }

View file

@ -363,17 +363,16 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
type?: string, type?: string,
user?: UsersUserFull, user?: UsersUserFull,
markdown?: boolean markdown?: boolean
): string => { ) =>
return this.template.theme( this.template.theme(
{ {
user, user,
group: this.group, group: this.group,
type, type,
text: Template.cleanText(text), text: Template.cleanText(text, markdown),
}, },
markdown markdown
); );
};
/** /**
* Calculates, how much should we cut off the text to match photo caption limitations * Calculates, how much should we cut off the text to match photo caption limitations
@ -384,7 +383,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
type?: string, type?: string,
user?: UsersUserFull, user?: UsersUserFull,
markdown?: boolean markdown?: boolean
): string => { ) => {
const withText = this.themeText(text, type, user, markdown); const withText = this.themeText(text, type, user, markdown);
const limit = this.template.fields.char_limit const limit = this.template.fields.char_limit
? Math.min(this.template.fields.char_limit, maxChars) ? Math.min(this.template.fields.char_limit, maxChars)
@ -410,7 +409,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
/** /**
* Returns fullname from user of update callback * Returns fullname from user of update callback
*/ */
getNameFromContext = (from: User): string => getNameFromContext = (from: User) =>
[from?.first_name, from?.last_name, from.username && `(@${from.username})`] [from?.first_name, from?.last_name, from.username && `(@${from.username})`]
.filter((el) => el) .filter((el) => el)
.join(" ") .join(" ")

View file

@ -0,0 +1,10 @@
const urlRegex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/g;
const trimTo = (val: string, maxLength: number) =>
val.length > maxLength ? val.substring(0, maxLength - 1).concat("…") : val;
/** Formatting all links in markdown output, trimming them to reasonable length */
export default (value: string) =>
value.replace(urlRegex, (val) => {
return `[${trimTo(val, 20)}](${val})`;
});