From 2d2c9599253a1efc372391bac447ad07c6dcc818 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Sat, 30 Dec 2023 13:26:30 +0700 Subject: [PATCH] added links trimmer --- src/service/template/index.ts | 22 ++++++++++++++-------- src/service/vk/handlers/PostNewHandler.ts | 11 +++++------ src/utils/transformMDLinks.ts | 10 ++++++++++ 3 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 src/utils/transformMDLinks.ts diff --git a/src/service/template/index.ts b/src/service/template/index.ts index 5c84c78..c1e5e46 100644 --- a/src/service/template/index.ts +++ b/src/service/template/index.ts @@ -9,6 +9,7 @@ import path from "path"; import hb from "handlebars"; import strip from "strip-markdown"; import { VFileCompatible } from "vfile"; +import transformMDLinks from "../../utils/transformMDLinks"; const removeFrontmatter = () => (tree) => { 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) => { const processor = unified() @@ -81,13 +83,17 @@ export class Template< }); } - public static cleanText(text: string) { - return unified() - .use(stringify) - .use(parser) - .use(strip) - .processSync(text) - .contents.toString(); + /** Cleans text from markdown, but transforms links to MD if needed */ + public static cleanText(text: string, markdown?: boolean) { + const processor = unified().use(stringify).use(parser).use(strip); + + const output = processor.processSync(text).contents.toString(); + + if (markdown) { + return transformMDLinks(output); + } + + return output; } } diff --git a/src/service/vk/handlers/PostNewHandler.ts b/src/service/vk/handlers/PostNewHandler.ts index 8bb9d39..4bef70f 100644 --- a/src/service/vk/handlers/PostNewHandler.ts +++ b/src/service/vk/handlers/PostNewHandler.ts @@ -363,17 +363,16 @@ export class PostNewHandler extends VkEventHandler { type?: string, user?: UsersUserFull, markdown?: boolean - ): string => { - return this.template.theme( + ) => + this.template.theme( { user, group: this.group, type, - text: Template.cleanText(text), + text: Template.cleanText(text, markdown), }, markdown ); - }; /** * Calculates, how much should we cut off the text to match photo caption limitations @@ -384,7 +383,7 @@ export class PostNewHandler extends VkEventHandler { type?: string, user?: UsersUserFull, markdown?: boolean - ): string => { + ) => { const withText = this.themeText(text, type, user, markdown); const limit = this.template.fields.char_limit ? Math.min(this.template.fields.char_limit, maxChars) @@ -410,7 +409,7 @@ export class PostNewHandler extends VkEventHandler { /** * Returns fullname from user of update callback */ - getNameFromContext = (from: User): string => + getNameFromContext = (from: User) => [from?.first_name, from?.last_name, from.username && `(@${from.username})`] .filter((el) => el) .join(" ") diff --git a/src/utils/transformMDLinks.ts b/src/utils/transformMDLinks.ts new file mode 100644 index 0000000..2b036bc --- /dev/null +++ b/src/utils/transformMDLinks.ts @@ -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})`; + });