andy-stack_vaultkeeper-ai/Services/HTMLService.ts
Andrew Beal 167a2b13a5 refactor: standardize error handling with Exception helper and improve return types
Add Exception helper class for consistent error handling and logging. Replace throw statements and console.error calls with Exception methods. Update service methods to return Error | T instead of mixed success/failure objects. Improve type safety in Claude.extractContents with explicit return type.

Add WikiLinks helper to VaultCacheService for managing wiki link references.

Update unit tests.
2025-11-17 19:02:15 +00:00

36 lines
No EOL
1.2 KiB
TypeScript

export class HTMLService {
public clearElement(element: HTMLElement) {
element.empty();
}
public setHTMLContent(container: HTMLElement, htmlString: string) {
this.clearElement(container);
const fragment = this.parseHTMLString(htmlString);
container.appendChild(fragment);
}
public parseHTMLString(htmlString: string): DocumentFragment {
const parser = new DOMParser();
const fragment = document.createDocumentFragment();
const doc = parser.parseFromString(htmlString, "text/html");
// Transfer all nodes from the parsed body to the fragment
while (doc.body.firstChild) {
fragment.appendChild(doc.body.firstChild);
}
return fragment;
}
// Creates a temporary container, parses HTML, and returns the container.
// Useful for parsing HTML when you need to traverse the resulting DOM structure.
public parseHTMLToContainer(htmlString: string): HTMLDivElement {
const container = document.createElement("div");
const fragment = this.parseHTMLString(htmlString);
container.appendChild(fragment);
return container;
}
}