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
All checks were successful
Build & Publish / Build & Publish (push) Successful in 1m22s
This commit is contained in:
parent
7dd72aa4e3
commit
62f080c739
3 changed files with 22 additions and 5 deletions
|
@ -28,6 +28,15 @@ describe("extractURLs", () => {
|
||||||
expect(result[0].href).toBe("https://map.vault48.org/test");
|
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", () => {
|
it("deduplicates matching urls", () => {
|
||||||
const result = extractURLs(
|
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
|
`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
|
||||||
|
|
|
@ -25,5 +25,13 @@ describe("transformMDLinks", () => {
|
||||||
).toBe(
|
).toBe(
|
||||||
"Trying out links [1234567890123456789…](https://map.vault48.org/test_abc_def_ghi) 123"
|
"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"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
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 */
|
/** Yep, that's how VK posts it's links */
|
||||||
const weirdLongUrlRegex = /\[\#alias\|([^\|]+)\|([^\]]+)\]/gim;
|
const weirdLongUrlRegex = /(\\?)\[\#alias\|([^\|]+)\|([^\]]+)\]/gim;
|
||||||
|
|
||||||
const fixUrl = (url: string) =>
|
const fixUrl = (url: string) =>
|
||||||
url.startsWith("http") || !url ? url : `https://${url}`;
|
url.startsWith("http") || !url ? url : `https://${url}`;
|
||||||
|
@ -15,7 +15,7 @@ export const extractURLs = (text: string): URL[] => {
|
||||||
text
|
text
|
||||||
.match(weirdLongUrlRegex)
|
.match(weirdLongUrlRegex)
|
||||||
?.forEach((match) =>
|
?.forEach((match) =>
|
||||||
urls.add(fixUrl(match.replace(weirdLongUrlRegex, "$1")))
|
urls.add(fixUrl(match.replace(weirdLongUrlRegex, "$2")))
|
||||||
);
|
);
|
||||||
|
|
||||||
text.match(simpleUrlRegex)?.forEach((match) => urls.add(match));
|
text.match(simpleUrlRegex)?.forEach((match) => urls.add(match));
|
||||||
|
@ -39,12 +39,12 @@ const trimTo = (val: string, maxLength: number) =>
|
||||||
export const transformMDLinks = (value: string) =>
|
export const transformMDLinks = (value: string) =>
|
||||||
value
|
value
|
||||||
.replace(weirdLongUrlRegex, (val, ...args) => {
|
.replace(weirdLongUrlRegex, (val, ...args) => {
|
||||||
if (args.length < 2) {
|
if (args.length < 3) {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
const title = trimTo(args[0] ?? args[1], 20);
|
const title = trimTo(args[1] ?? args[2], 20);
|
||||||
const url = fixUrl(args[1]);
|
const url = fixUrl(args[2]);
|
||||||
|
|
||||||
return `[${title}](${url})`;
|
return `[${title}](${url})`;
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue