MAJ gestion des pages services
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
const hasDomParser =
|
||||
(typeof window !== "undefined" && typeof window.DOMParser !== "undefined") ||
|
||||
typeof DOMParser !== "undefined";
|
||||
|
||||
const createDocument = (html) => {
|
||||
if (!hasDomParser || !html) return null;
|
||||
const ParserConstructor =
|
||||
typeof DOMParser !== "undefined"
|
||||
? DOMParser
|
||||
: typeof window !== "undefined" && window.DOMParser
|
||||
? window.DOMParser
|
||||
: null;
|
||||
|
||||
if (!ParserConstructor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parser = new ParserConstructor();
|
||||
return parser.parseFromString(html, "text/html");
|
||||
};
|
||||
|
||||
export const extractFirstImageSrc = (html) => {
|
||||
if (!html) return "";
|
||||
const doc = createDocument(html);
|
||||
if (doc) {
|
||||
const img = doc.querySelector("img");
|
||||
if (img) {
|
||||
return img.getAttribute("src") || "";
|
||||
}
|
||||
}
|
||||
|
||||
const match = html.match(/<img[^>]+src=["']([^"']+)["']/i);
|
||||
return match?.[1] || "";
|
||||
};
|
||||
|
||||
export const removeImageTags = (html) => {
|
||||
if (!html) return "";
|
||||
return html.replace(/<img\b[^>]*>/gi, "");
|
||||
};
|
||||
|
||||
export const stripHtml = (html) => {
|
||||
if (!html) return "";
|
||||
const doc = createDocument(html);
|
||||
if (doc) {
|
||||
return doc.body.textContent || "";
|
||||
}
|
||||
return html.replace(/<[^>]*>/g, "");
|
||||
};
|
||||
Reference in New Issue
Block a user