Compare commits

..
Author SHA1 Message Date
sharevb b779beb695 feat(new tool): URL Text Fragment Generator
Allows to generate an URL that make highlight in web pages (see https://developer.mozilla.org/en-US/docs/Web/Text_fragments)
2024-03-03 09:57:21 +01:00
9 changed files with 224 additions and 638 deletions

No files matched your search

-3
View File
@@ -79,7 +79,6 @@
"plausible-tracker": "^0.3.8",
"qrcode": "^1.5.1",
"sql-formatter": "^13.0.0",
"sshpk": "^1.18.0",
"ua-parser-js": "^1.0.35",
"ulid": "^2.3.0",
"unicode-emoji-json": "^0.4.0",
@@ -109,7 +108,6 @@
"@types/node": "^18.15.11",
"@types/node-forge": "^1.3.2",
"@types/qrcode": "^1.5.0",
"@types/sshpk": "^1.17.4",
"@types/ua-parser-js": "^0.7.36",
"@types/uuid": "^9.0.0",
"@unocss/eslint-config": "^0.57.0",
@@ -131,7 +129,6 @@
"unplugin-icons": "^0.17.0",
"unplugin-vue-components": "^0.25.0",
"vite": "^4.4.9",
"vite-plugin-node-polyfills": "^0.21.0",
"vite-plugin-pwa": "^0.16.0",
"vite-plugin-vue-markdown": "^0.23.5",
"vite-svg-loader": "^4.0.0",
+4 -631
View File
File diff suppressed because it is too large. Load diff
+1 -2
View File
@@ -16,7 +16,6 @@ const props = withDefaults(
language?: string
copyPlacement?: 'top-right' | 'bottom-right' | 'outside' | 'none'
copyMessage?: string
wordWrap?: boolean
}>(),
{
followHeightOf: null,
@@ -48,7 +47,7 @@ const tooltipText = computed(() => isJustCopied.value ? 'Copied!' : copyMessage.
:style="height ? `min-height: ${height - 40 /* card padding */ + 10 /* negative margin compensation */}px` : ''"
>
<n-config-provider :hljs="hljs">
<n-code :code="value" :language="language" :word-wrap="wordWrap" :trim="false" data-test-id="area-content" />
<n-code :code="value" :language="language" :trim="false" data-test-id="area-content" />
</n-config-provider>
</n-scrollbar>
<div absolute right-10px top-10px>
+2
View File
@@ -2,6 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as textToUnicode } from './text-to-unicode';
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';
@@ -111,6 +112,7 @@ export const toolsByCategory: ToolCategory[] = [
urlEncoder,
htmlEntities,
urlParser,
urlTextFragmentMaker,
deviceInformation,
basicAuthGenerator,
metaTagGenerator,
@@ -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'),
});
@@ -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');
});
});
});
});
@@ -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}`;
}
@@ -0,0 +1,94 @@
<script setup lang="ts">
import { getUrlWithTextFragment } from './url-text-fragment-maker.service';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
const url = ref('');
const prefixSearch = ref('');
const textStartSearch = ref('');
const textStopSearch = ref('');
const suffixSearch = ref('');
const searchableUrl = computed(() => {
try {
return getUrlWithTextFragment({
url: url.value,
textStartSearch: textStartSearch.value,
textStopSearch: textStopSearch.value,
prefixSearch: prefixSearch.value,
suffixSearch: suffixSearch.value,
});
}
catch (e: any) {
return e.toString();
}
});
</script>
<template>
<div>
<n-p>
Url with Text Fragments allows to make link to content that has no anchor or @id.
<n-a href="https://developer.mozilla.org/en-US/docs/Web/Text_fragments" target="blank" rel="noopener">
See MDN for more info
</n-a>
</n-p>
<div>
<c-input-text
v-model:value="url"
label="Base url:"
placeholder="Base url..."
type="url"
clearable raw-text mb-5
/>
</div>
<div flex justify-center gap-2>
<c-input-text
v-model:value="textStartSearch"
label="Start Search(es) (comma separated)"
placeholder="Start Search(es) (comma separated)..."
clearable
raw-text
mb-2
/>
<c-input-text
v-model:value="textStopSearch"
label="Stop Search"
placeholder="Stop Search text..."
clearable
raw-text
mb-2
/>
</div>
<div flex justify-center gap-2>
<c-input-text
v-model:value="prefixSearch"
label="Prefix"
placeholder="Prefix search"
clearable
raw-text
mb-2
/>
<c-input-text
v-model:value="suffixSearch"
label="Suffix"
placeholder="Suffix search"
clearable
raw-text
mb-2
/>
</div>
<n-divider />
<n-form-item label="Searchable Url:">
<TextareaCopyable :value="searchableUrl" />
</n-form-item>
<div flex justify-center>
<n-a :href="searchableUrl" target="blank" rel="noopener">
Test Searchable Url
</n-a>
</div>
</div>
</template>
-2
View File
@@ -1,6 +1,5 @@
import { resolve } from 'node:path';
import { URL, fileURLToPath } from 'node:url';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import VueI18n from '@intlify/unplugin-vue-i18n/vite';
import vue from '@vitejs/plugin-vue';
@@ -98,7 +97,6 @@ export default defineConfig({
resolvers: [NaiveUiResolver(), IconsResolver({ prefix: 'icon' })],
}),
Unocss(),
nodePolyfills(),
],
base: baseUrl,
resolve: {