From 771db4abbc7e954577f4c5984dadea542a2b0245 Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Thu, 31 Aug 2023 00:57:22 +0330 Subject: [PATCH] refactor: enhance html attribute extractor util --- src/utils/html.utils.ts | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/utils/html.utils.ts b/src/utils/html.utils.ts index a0b83b3..7934e79 100644 --- a/src/utils/html.utils.ts +++ b/src/utils/html.utils.ts @@ -1,6 +1,4 @@ -const regexForInnerHtml = /<[^>]*>/ -const regexForId = /id="([^"]*)"/ -const regexForClass = /class="([^"]*)"/ +const attributeRegex = (name: string) => new RegExp(`^<[^>]*${name}="([^"]*)"`) export function extractInnerHtml(htmlString: string) { var regex = /^<[^>]+>([\s\S]*)<\/[^>]+>$/ @@ -13,14 +11,31 @@ export function extractInnerHtml(htmlString: string) { } } -export const extractIdFromFirstTag = (html: string) => { - const match = html.match(regexForId) - return match ? match[1] : null -} +export const extractIdFromFirstTag = (html: string) => + extractAttributeFromHTML(html, 'id') -export const extractClassFromFirstTag = (html: string) => { - const match = html.match(regexForClass) - return match ? match[1] : null +export const extractClassFromFirstTag = (html: string) => + extractAttributeFromHTML(html, 'class') + +export function extractAttributeFromHTML( + html: string, + attribute: string, +): string | null +export function extractAttributeFromHTML( + html: string, + attribute: string, + defaultValue: string, +): string +export function extractAttributeFromHTML( + html: string, + attribute: string, + defaultValue?: string, +): string | null { + return ( + (html.match(attributeRegex(attribute))?.[1] as string) || + defaultValue || + null + ) } export const isAuthorsParagraph = (html: string) => {