-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separated client side and server side for fixing dependency issues
- Loading branch information
Showing
8 changed files
with
503 additions
and
459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,14 @@ | |
"version": "3.0.0", | ||
"description": "A simple HTML to JSON and JSON to HTML converter", | ||
"main": "dist/index.js", | ||
"client": "dist/client.js", | ||
"exports" : { | ||
".": { | ||
"client" : "./dist/client.js", | ||
"server" : "./dist/index.js", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"dev": "tsx watch src/index.ts", | ||
"start": "node ./dist/index.js", | ||
|
@@ -22,7 +30,13 @@ | |
"typescript", | ||
"html", | ||
"json", | ||
"converter" | ||
"converter", | ||
"html-to-json", | ||
"html to json", | ||
"json-to-html", | ||
"json to html", | ||
"html to json converter", | ||
"json to html converter" | ||
], | ||
"author": "Techlism <[email protected]> (https://github.com/techlism)", | ||
"bugs": { | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { BaseHTMLJSONConverter, type ConverterConfig } from "./base.js"; | ||
// Client-side implementation | ||
export class ClientHTMLJSONConverter extends BaseHTMLJSONConverter { | ||
protected parseHTML(html: string): Element { | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(html, "text/html"); | ||
const { documentElement, body, head } = doc; | ||
|
||
const isFullDocument = BaseHTMLJSONConverter.DOCUMENT_INDICATORS.some((indicator) => | ||
html.toLowerCase().startsWith(indicator.toLowerCase()) | ||
); | ||
|
||
if (isFullDocument && documentElement?.tagName.toLowerCase() === "html") { | ||
return documentElement; | ||
} | ||
|
||
// Modified logic for head-only elements and empty structure | ||
const headElement = head.firstElementChild; | ||
const bodyElement = body.firstElementChild; | ||
|
||
if (!headElement && !bodyElement) { | ||
throw new Error("No HTML element found"); | ||
} | ||
|
||
// Check head first, then body | ||
if (html.toLowerCase().startsWith("<head")) { | ||
return head; | ||
} | ||
|
||
return bodyElement || headElement as Element; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.