webforai
Version:
A library that provides a web interface for AI
47 lines (46 loc) • 1.22 kB
JavaScript
// src/utils/hast-utils.ts
import { select, selectAll } from "hast-util-select";
var getLangFromHast = (node) => {
const html = select("html", node);
if (html && typeof html.properties.lang === "string") {
return html.properties.lang;
}
if (node.type !== "element") {
return;
}
const element = node;
if (element.tagName !== "html") {
return;
}
const langAttr = element.properties.lang || element.properties["xml:lang"];
if (langAttr) {
return langAttr;
}
return void 0;
};
var getLangFromStr = (str) => {
const match = str.match(/lang=["']([^"']+)["']/);
if (match) {
return match[1];
}
return void 0;
};
var getUrlFromHast = (node) => {
if (node.type !== "element") {
return void 0;
}
const metaTagAttributes = ["og:url", "twitter:url"];
const metaTags = selectAll("meta", node);
for (const meta of metaTags) {
const property = meta.properties.property || meta.properties.name;
if (typeof property === "string" && metaTagAttributes.includes(property)) {
return typeof meta.properties.content === "string" ? meta.properties.content : void 0;
}
}
return void 0;
};
export {
getLangFromHast,
getLangFromStr,
getUrlFromHast
};