@wroud/vite-plugin-ssg
Version:
A Vite plugin for static site generation (SSG) with React. Renders React applications to static HTML for faster load times and improved SEO.
26 lines • 1.12 kB
JavaScript
import { unescapeHtml } from "./utils/unescapeHtml.js";
// Main regex to match tag, attributes, and content
const mainRegex = /<(\w+)(\s+[^>]*?)?>([\s\S]*?)<\/\1>|<(\w+)(\s+[^>]*?)?\/?>/g;
// Secondary regex to capture each attribute within the attributes string
const attributeRegex = /(\w+)(?:="([^"]*)")?/g;
export function parseHtmlTagsFromHtml(html) {
const matches = [...html.matchAll(mainRegex)];
const tags = matches.map((match) => {
const tagName = match[1] || match[4]; // Tag name
const attributesString = match[2] || match[5] || ""; // Attributes as string
const content = match[3] || ""; // Content
// Extract individual attributes as an array of { name, value } pairs
const attrs = [...attributesString.matchAll(attributeRegex)].reduce((acc, [, name, value]) => ({
...acc,
[name]: unescapeHtml(value),
}), {});
return {
tag: tagName,
attrs,
children: content,
injectTo: "head-prepend",
};
});
return tags;
}
//# sourceMappingURL=parseHtmlTagsFromHtml.js.map