Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new-tool):-text-extractor-form-html #139

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ declare module '@vue/runtime-core' {
Encryption: typeof import('./src/tools/encryption/encryption.vue')['default']
EnergyComputer: typeof import('./src/tools/energy-computer/energy-computer.vue')['default']
EtaCalculator: typeof import('./src/tools/eta-calculator/eta-calculator.vue')['default']
ExtractTextFromHtml: typeof import('./src/tools/extract-text-from-html/extract-text-from-html.vue')['default']
FavoriteButton: typeof import('./src/components/FavoriteButton.vue')['default']
FormatTransformer: typeof import('./src/components/FormatTransformer.vue')['default']
GitMemo: typeof import('./src/tools/git-memo/git-memo.vue')['default']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from '@playwright/test';

test.describe('Tool - Extract text from html', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/extract-text-from-html');
});

test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('GoDev.Run - Extract text from HTML');
});

test('Extract text from HTML', async ({ page }) => {
await page.getByTestId('input').fill('<p>Paste your HTML in the input form on the left</p>');
const extractedText = await page.getByTestId('area-content').innerText();
expect(extractedText.trim()).toEqual('Paste your HTML in the input form on the left'.trim());
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';
import { getTextFromHtml, validateHtml } from './extract-text-from-html.service';

describe('extract-text-from-html service', () => {
describe('validateHtml', () => {
it('check if the value is valid html', () => {
expect(validateHtml('<p>Paste your HTML in the input form on the left</p>')).toBeTruthy();
expect(validateHtml('<div>Paste your HTML in the input form on the left</div>')).toBeTruthy();
expect(validateHtml('<div><p>Paste your HTML in the input form on the left</p></div>')).toBeTruthy();
expect(validateHtml('<body><div><p>Paste your HTML in the input form on the left</p></div></body>')).toBeTruthy();
expect(validateHtml('<p>Paste your HTML in the input form on the left</p>')).toBeTruthy();
});

it('check if the value is an html invlid', () => {
expect(validateHtml('<p>Paste your HTML in the input form on the left<p>')).toBeFalsy();
expect(validateHtml('Paste your HTML in the input form on the left<p>')).toBeFalsy();
expect(validateHtml('<p>Paste your HTML in the input form on the left')).toBeFalsy();
expect(validateHtml('<p>Paste your HTML in the input form on the left<>')).toBeFalsy();
expect(validateHtml('<>Paste your HTML in the input form on the left<>')).toBeFalsy();
expect(validateHtml('<p>Paste your HTML in the input form on the left</a>')).toBeFalsy();
expect(validateHtml('<div><p>Paste your HTML in the input form on the left</p>')).toBeTruthy();
});
});

describe('getTextFromHtml', () => {
it('must be return a string', () => {
expect(getTextFromHtml('<p>Paste your HTML in the input form on the left</p>')).toString();
});

it('must be return text from html', () => {
expect(getTextFromHtml('<p>Paste your HTML in the input form on the left</p>')).toStrictEqual(
'Paste your HTML in the input form on the left',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function validateHtml(value: string) {
try {
new DOMParser().parseFromString(value, 'text/html');
}
catch (error) {
return false;
}

const regex = /<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>|<([a-z][a-z0-9]*)\b[^\/]*\/>/gi;
const matches = value.match(regex);

return Boolean(matches !== null && matches.length);
}

function getTextFromHtml(value: string) {
const element = document.createElement('div');
element.innerHTML = value;
const text = element?.innerText || element?.textContent || '';
return text.replace(/\s+/g, ' ');
}

export { validateHtml, getTextFromHtml };
31 changes: 31 additions & 0 deletions src/tools/extract-text-from-html/extract-text-from-html.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup lang="ts">
import { getTextFromHtml, validateHtml } from './extract-text-from-html.service';
import { withDefaultOnError } from '@/utils/defaults';
import type { UseValidationRule } from '@/composable/validation';

function transformer(value: string) {
return withDefaultOnError(() => {
if (value === '') {
return '';
}
return getTextFromHtml(value);
}, '');
}

const rules: UseValidationRule<string>[] = [
{
validator: (value: string) => value === '' || validateHtml(value),
message: 'Provided HTML is not valid.',
},
];
</script>

<template>
<format-transformer
input-label="Your raw HTML"
input-placeholder="Paste your raw HTML here..."
output-label="Text from your HTML"
:input-validation-rules="rules"
:transformer="transformer"
/>
</template>
13 changes: 13 additions & 0 deletions src/tools/extract-text-from-html/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CursorText } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Extract text from HTML',
path: '/extract-text-from-html',
description:
'Paste your HTML in the input form on the left and you will get text instantly. Occasionally, you may need to extract plain text from an HTML page where CSS properties (like user-select: none;) prevent text selection. The typical workaround involves using the DevTools (F12) to select "Copy → outer HTML". The proposed tool would simplify this process by extracting the "inner Text" directly from the copied HTML.',
keywords: ['extract', 'text', 'from', 'html'],
component: () => import('./extract-text-from-html.vue'),
icon: CursorText,
createdAt: new Date('2024-05-10'),
});
2 changes: 2 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { tool as energyComputer } from './energy-computer';
import { tool as peerShare } from './peer-share';
import { tool as macAddressConverter } from './mac-address-converter';
import { tool as jsUnobfuscator } from './js-unobfuscator';
import { tool as extractTextFromHtml } from './extract-text-from-html';

import { tool as asciiTextDrawer } from './ascii-text-drawer';
import { tool as textToUnicode } from './text-to-unicode';
Expand Down Expand Up @@ -213,6 +214,7 @@ export const toolsByCategory: ToolCategory[] = [
emailNormalizer,
regexTester,
regexMemo,
extractTextFromHtml,
],
},
{
Expand Down
Loading