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:
parent
31af4e11b1
commit
2d2c959925
3 changed files with 29 additions and 14 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -363,17 +363,16 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
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<Fields, Values> {
|
|||
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<Fields, Values> {
|
|||
/**
|
||||
* 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(" ")
|
||||
|
|
10
src/utils/transformMDLinks.ts
Normal file
10
src/utils/transformMDLinks.ts
Normal 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})`;
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue