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

fix md links again
All checks were successful
Build & Publish / Build & Publish (push) Successful in 1m22s

This commit is contained in:
Fedor Katurov 2025-02-24 21:39:52 +07:00
parent 7dd72aa4e3
commit 5d83a2d29b
4 changed files with 24 additions and 5 deletions

View file

@ -90,6 +90,8 @@ export class Template<
const output = processor.processSync(text).contents.toString();
if (markdown) {
console.log("HERE--->", output, transformMDLinks(output));
return transformMDLinks(output);
}

View file

@ -28,6 +28,15 @@ describe("extractURLs", () => {
expect(result[0].href).toBe("https://map.vault48.org/test");
});
it("works with that weird new VK urls without scheme (with backslash)", () => {
const result = extractURLs(
"Trying out links: \\[#alias|map.vault48.org/test|map.vault48.org/test]"
);
expect(result.length).toBe(1);
expect(result[0].href).toBe("https://map.vault48.org/test");
});
it("deduplicates matching urls", () => {
const result = extractURLs(
`Trying out links: [#alias|map.vault48.org/test|map.vault48.org/test] map.vault48.org/test https://map.vault48.org/test map.vault48.org/test2 https://map.vault48.org/test3

View file

@ -25,5 +25,13 @@ describe("transformMDLinks", () => {
).toBe(
"Trying out links [1234567890123456789…](https://map.vault48.org/test_abc_def_ghi) 123"
);
expect(
transformMDLinks(
"Trying out links \\[#alias|12345678901234567890123|map.vault48.org/test_abc_def_ghi] 123"
)
).toBe(
"Trying out links [1234567890123456789…](https://map.vault48.org/test_abc_def_ghi) 123"
);
});
});

View file

@ -3,7 +3,7 @@ import { URL } from "url";
const simpleUrlRegex = /(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,})/gim;
/** Yep, that's how VK posts it's links */
const weirdLongUrlRegex = /\[\#alias\|([^\|]+)\|([^\]]+)\]/gim;
const weirdLongUrlRegex = /(\\?)\[\#alias\|([^\|]+)\|([^\]]+)\]/gim;
const fixUrl = (url: string) =>
url.startsWith("http") || !url ? url : `https://${url}`;
@ -15,7 +15,7 @@ export const extractURLs = (text: string): URL[] => {
text
.match(weirdLongUrlRegex)
?.forEach((match) =>
urls.add(fixUrl(match.replace(weirdLongUrlRegex, "$1")))
urls.add(fixUrl(match.replace(weirdLongUrlRegex, "$2")))
);
text.match(simpleUrlRegex)?.forEach((match) => urls.add(match));
@ -39,12 +39,12 @@ const trimTo = (val: string, maxLength: number) =>
export const transformMDLinks = (value: string) =>
value
.replace(weirdLongUrlRegex, (val, ...args) => {
if (args.length < 2) {
if (args.length < 3) {
return val;
}
const title = trimTo(args[0] ?? args[1], 20);
const url = fixUrl(args[1]);
const title = trimTo(args[1] ?? args[2], 20);
const url = fixUrl(args[2]);
return `[${title}](${url})`;
})