diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0d0579b5..abf8d16f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5697,7 +5697,7 @@ packages: /@types/lodash-es@4.17.10: resolution: {integrity: sha512-YJP+w/2khSBwbUSFdGsSqmDvmnN3cCKoPOL7Zjle6s30ZtemkkqhjVfFqGwPN7ASil5VyjE2GtyU/yqYY6mC0A==} dependencies: - '@types/lodash': 4.14.200 + '@types/lodash': 4.14.192 dev: false /@types/lodash.defaultsdeep@4.6.9: @@ -5720,7 +5720,6 @@ packages: /@types/lodash@4.14.192: resolution: {integrity: sha512-km+Vyn3BYm5ytMO13k9KTp27O75rbQ0NFw+U//g+PX7VZyjCioXaRFisqSIJRECljcTv73G3i6BpglNGHgUQ5A==} - dev: true /@types/lodash@4.14.200: resolution: {integrity: sha512-YI/M/4HRImtNf3pJgbF+W6FrXovqj+T+/HpENLTooK9PnkacBsDpeP3IpHab40CClUfhNmdM2WTNP2sa2dni5Q==} diff --git a/src/tools/index.ts b/src/tools/index.ts index 6c4f0ca1..c8ad5bed 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -134,6 +134,7 @@ import { tool as portNumbers } from './port-numbers'; import { tool as rsaEncryption } from './rsa-encryption'; import { tool as urlCleaner } from './url-cleaner'; import { tool as urlFanger } from './url-fanger'; +import { tool as urlTextFragmentMaker } from './url-text-fragment-maker'; import { tool as pdfSignatureChecker } from './pdf-signature-checker'; import { tool as numeronymGenerator } from './numeronym-generator'; import { tool as macAddressGenerator } from './mac-address-generator'; @@ -340,6 +341,7 @@ export const toolsByCategory: ToolCategory[] = [ urlParser, urlCleaner, urlFanger, + urlTextFragmentMaker, deviceInformation, basicAuthGenerator, htpasswdGenerator, diff --git a/src/tools/url-text-fragment-maker/index.ts b/src/tools/url-text-fragment-maker/index.ts new file mode 100644 index 00000000..8370bfa9 --- /dev/null +++ b/src/tools/url-text-fragment-maker/index.ts @@ -0,0 +1,12 @@ +import { FileSearch } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Url Text Search Fragment Maker', + path: '/url-text-fragment-maker', + description: 'Create url that allows linking directly to a specific portion of text in a web document', + keywords: ['url', 'text', 'fragment'], + component: () => import('./url-text-fragment-maker.vue'), + icon: FileSearch, + createdAt: new Date('2024-01-17'), +}); diff --git a/src/tools/url-text-fragment-maker/url-text-fragment-maker.service.test.ts b/src/tools/url-text-fragment-maker/url-text-fragment-maker.service.test.ts new file mode 100644 index 00000000..48780474 --- /dev/null +++ b/src/tools/url-text-fragment-maker/url-text-fragment-maker.service.test.ts @@ -0,0 +1,76 @@ +import { describe, expect, it } from 'vitest'; +import { getUrlWithTextFragment } from './url-text-fragment-maker.service'; + +describe('url-text-fragment-maker.service', () => { + describe('getUrlWithTextFragment', () => { + describe('compute url with text fragment', () => { + it('throws on invalid url', () => { + expect(() => getUrlWithTextFragment({ + url: 'example', + textStartSearch: 'for', + })).toThrow('Invalid url'); + expect(() => getUrlWithTextFragment({ + url: 'htt://example', + textStartSearch: 'for', + })).toThrow('Url must have http:// or https:// prefix'); + expect(() => getUrlWithTextFragment({ + url: 'http:/example', + textStartSearch: 'for', + })).toThrow('Url must have http:// or https:// prefix'); + }); + + it('should handle basic cases', () => { + expect(getUrlWithTextFragment({ + url: 'https://example.com', + textStartSearch: 'for', + })) + .toBe('https://example.com#:~:text=for'); + expect(getUrlWithTextFragment({ + url: 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a', + textStartSearch: 'human', + })) + .toBe('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#:~:text=human'); + }); + + it('should be url encoded', () => { + expect(getUrlWithTextFragment({ + url: 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a', + textStartSearch: 'linked URL', + suffixSearch: '\'s format', + })) + .toBe('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#:~:text=linked%20URL,-\'s%20format'); + expect(getUrlWithTextFragment({ + url: 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a', + textStartSearch: 'The Referer', + textStopSearch: 'be sent', + prefixSearch: 'downgrade:', + suffixSearch: 'to origins', + })) + .toBe('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#:~:text=downgrade%3A-,The%20Referer,be%20sent,-to%20origins'); + }); + + it('should handle multiple comma separated and encoded', () => { + expect( + getUrlWithTextFragment({ + url: 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a', + textStartSearch: 'Causes,linked', + })) + .toBe('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#:~:text=Causes&text=linked'); + + expect( + getUrlWithTextFragment({ + url: 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a', + textStartSearch: 'Causes 1,linked 1', + })) + .toBe('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#:~:text=Causes%201&text=linked%201'); + + expect( + getUrlWithTextFragment({ + url: 'https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a', + textStartSearch: 'Causes , linked', + })) + .toBe('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#:~:text=Causes&text=linked'); + }); + }); + }); +}); diff --git a/src/tools/url-text-fragment-maker/url-text-fragment-maker.service.ts b/src/tools/url-text-fragment-maker/url-text-fragment-maker.service.ts new file mode 100644 index 00000000..48b75a2f --- /dev/null +++ b/src/tools/url-text-fragment-maker/url-text-fragment-maker.service.ts @@ -0,0 +1,35 @@ +export function getUrlWithTextFragment( + { url, textStartSearch, textStopSearch, prefixSearch, suffixSearch }: + { url: string + textStartSearch: string + textStopSearch?: string + prefixSearch?: string + suffixSearch?: string + }, +) { + const isValidUrl = (urlString: string) => { + try { + return Boolean(new URL(urlString)); + } + catch (e) { + return false; + } + }; + if (!isValidUrl(url)) { + throw new Error('Invalid url'); + } + + if (!url.match(/^https?:\/\//)) { + throw new Error('Url must have http:// or https:// prefix'); + } + + const [textStartSearchFirstText, ...textStartSearchOtherTexts] = textStartSearch.split(','); + const text = `${encodeURIComponent(prefixSearch ?? '')}-,${encodeURIComponent(textStartSearchFirstText.trim())},${encodeURIComponent(textStopSearch ?? '')},-${encodeURIComponent(suffixSearch ?? '')}` + .replace(/^-,|,(?=,)|,-$/g, '') + .replace(/,+/g, ','); + let textStartSearchOtherTextEncoded = textStartSearchOtherTexts.map(t => `text=${encodeURIComponent(t.trim())}`).join('&'); + if (textStartSearchOtherTextEncoded.length) { + textStartSearchOtherTextEncoded = `&${textStartSearchOtherTextEncoded}`; + } + return `${url.trim()}#:~:text=${text}${textStartSearchOtherTextEncoded}`; +} diff --git a/src/tools/url-text-fragment-maker/url-text-fragment-maker.vue b/src/tools/url-text-fragment-maker/url-text-fragment-maker.vue new file mode 100644 index 00000000..537fb106 --- /dev/null +++ b/src/tools/url-text-fragment-maker/url-text-fragment-maker.vue @@ -0,0 +1,94 @@ + + + + + + Url with Text Fragments allows to make link to content that has no anchor or @id. + + See MDN for more info + + + + + + + + + + + + + + + + + + + + + + + + Test Searchable Url + + + +