mirror of
https://github.com/acid-info/logos-press-engine.git
synced 2025-02-23 22:58:08 +00:00
refactor: enhance html attribute extractor util
This commit is contained in:
parent
61a229d015
commit
3c5467a7f4
@ -1,6 +1,4 @@
|
|||||||
const regexForInnerHtml = /<[^>]*>/
|
const attributeRegex = (name: string) => new RegExp(`^<[^>]*${name}="([^"]*)"`)
|
||||||
const regexForId = /id="([^"]*)"/
|
|
||||||
const regexForClass = /class="([^"]*)"/
|
|
||||||
|
|
||||||
export function extractInnerHtml(htmlString: string) {
|
export function extractInnerHtml(htmlString: string) {
|
||||||
var regex = /^<[^>]+>([\s\S]*)<\/[^>]+>$/
|
var regex = /^<[^>]+>([\s\S]*)<\/[^>]+>$/
|
||||||
@ -13,14 +11,31 @@ export function extractInnerHtml(htmlString: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const extractIdFromFirstTag = (html: string) => {
|
export const extractIdFromFirstTag = (html: string) =>
|
||||||
const match = html.match(regexForId)
|
extractAttributeFromHTML(html, 'id')
|
||||||
return match ? match[1] : null
|
|
||||||
}
|
|
||||||
|
|
||||||
export const extractClassFromFirstTag = (html: string) => {
|
export const extractClassFromFirstTag = (html: string) =>
|
||||||
const match = html.match(regexForClass)
|
extractAttributeFromHTML(html, 'class')
|
||||||
return match ? match[1] : null
|
|
||||||
|
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) => {
|
export const isAuthorsParagraph = (html: string) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user