hd-html-parser
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -1,24 +0,1 @@ | ||
{ | ||
"name": "hd-html-parser", | ||
"version": "2.0.5", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hoangdaicntt/hd-html-parser.git" | ||
}, | ||
"keywords": [ | ||
"html-parser", | ||
"html dom", | ||
"html parser nodejs" | ||
], | ||
"author": "hoangdaicntt", | ||
"license": "ISC", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} | ||
{"name":"hd-html-parser","version":"2.0.6","description":"","main":"dist/index.js","types":"dist/index.d.ts","files":["dist"],"scripts":{},"repository":{"type":"git","url":"git+https://github.com/hoangdaicntt/hd-html-parser.git"},"keywords":["html-parser","html dom","html parser nodejs"],"author":"hoangdaicntt","license":"ISC","dependencies":{},"devDependencies":{}} |
121
README.MD
@@ -1,49 +0,76 @@ | ||
<div class="markdown prose w-full break-words dark:prose-invert light"><h1>HTML Parser</h1><p>This is a simple HTML parser for Node.js, created using <code>htmlparser2</code> package. It provides a <code>HtmlParser</code> class which can be used to query and manipulate HTML documents. The class also provides some useful methods like <code>getAttribute</code>, <code>getText</code>, <code>getParent</code>, <code>getChildren</code>, <code>getOuterHTML</code>, <code>getInnerHTML</code>, <code>getNext</code>, <code>getPrev</code>, <code>querySelector</code>, <code>querySelectorAll</code>, and <code>getListData</code>.</p><h2>Installation</h2><p>You can install this package using <code>npm</code>:</p><pre><div class="bg-black mb-4 rounded-md"><div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans"></div><div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-java">npm install <span class="hljs-meta">@hd</span>/html-parser | ||
</code></div></div></pre><h2>Usage</h2><p>Here's an example of how to use this package:</p><pre><div class="bg-black mb-4 rounded-md"><div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans"></div><div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">HDHtmlParser</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'@hd/html-parser'</span>; | ||
# HD HTML Parser | ||
<span class="hljs-keyword">const</span> html = <span class="hljs-string">`<html> | ||
<head> | ||
<title>My title</title> | ||
</head> | ||
<body> | ||
<h1>Heading 1</h1> | ||
<p>Paragraph 1</p> | ||
<p>Paragraph 2</p> | ||
</body> | ||
</html>`</span>; | ||
[](https://badge.fury.io/js/hd-html-parser) | ||
<span class="hljs-keyword">const</span> <span class="hljs-variable language_">document</span> | ||
= <span class="hljs-keyword">await</span> <span class="hljs-title class_">HDHtmlParser</span>(html); | ||
<span class="hljs-keyword">const</span> title = <span class="hljs-variable language_">document</span> | ||
.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">'title'</span>) | ||
.<span class="hljs-title function_">getText</span>(); | ||
<span class="hljs-keyword">const</span> paragraphs = <span class="hljs-variable language_">document</span> | ||
.<span class="hljs-title function_">querySelectorAll</span>(<span class="hljs-string">'p'</span>) | ||
.<span class="hljs-title function_">map</span>(<span class="hljs-function"><span class="hljs-params">p</span> | ||
=></span> p.<span class="hljs-title function_">getText</span>()); | ||
</code></div></div></pre><h2>API</h2><h3>HtmlParser</h3><p>The <code>HtmlParser</code> class provides the following | ||
methods:</p><h4><code>querySelector(selector: string): HtmlParser | null</code></h4><p>Returns a new <code> | ||
HtmlParser</code> object that matches the first element in the document that matches the specified <code>selector</code> | ||
, or <code>null</code> if no such element exists.</p><h4><code>querySelectorAll(selector: string): | ||
HtmlParser[]</code></h4><p>Returns an array of <code>HtmlParser</code> objects that match all elements in the document | ||
that match the specified <code>selector</code>. If no elements match, an empty array is returned.</p><h4><code> | ||
getHtml(): string | null</code></h4><p>Returns the HTML string of the document, or <code>null</code> if an error | ||
occurs.</p><h4><code>getAttribute(name: string): string | null | undefined</code></h4><p>Returns the value of the | ||
specified attribute on the current element, or <code>null</code> if the attribute does not exist or an error | ||
occurs.</p><h4><code>getText(): string | null</code></h4><p>Returns the text content of the current element, or <code> | ||
null</code> if an error occurs.</p><h4><code>getParent(): HtmlParser</code></h4><p>Returns a new <code>HtmlParser</code> | ||
object that represents the parent of the current element.</p><h4><code>getChildren(): HtmlParser[]</code></h4><p>Returns | ||
an array of <code>HtmlParser</code> objects that represent the children of the current element.</p><h4><code> | ||
getOuterHTML(): string | null</code></h4><p>Returns the outer HTML of the current element, or <code>null</code> if an | ||
error occurs.</p><h4><code>getInnerHTML(): string | null</code></h4><p>Returns the inner HTML of the current element, or | ||
an empty string if the element has no children.</p><h4><code>getNext(): HtmlParser | null</code></h4><p>Returns a | ||
new <code>HtmlParser</code> object that represents the next sibling of the current element, or <code>null</code> if no | ||
such element exists.</p><h4><code>getPrev(): HtmlParser | null</code></h4><p>Returns a new <code>HtmlParser</code> | ||
object that represents the previous sibling of the current element, or <code>null</code> if no such element | ||
exists.</p><h4><code>getListData(selector: string, itemsSelector: any, meta: any): object | null</code></h4><p>Returns | ||
an object that represents the data extracted from a list of elements that match the specified <code>selector</code>. | ||
The <code>itemsSelector</code> argument is an object that maps the keys of the output object to functions that extract | ||
the data from the corresponding elements. The <code>meta</code> argument is an object that can be used to pass | ||
additional information to the extractor functions. Returns <code>null</code> if an error occurs.</p><h3>HDHtmlParser( | ||
html: string): Promise<HtmlParser | null></h3><p>This is the main function of the package. It takes an HTML string | ||
as input and returns a <code>Promise</code> that resolves to</p></div> | ||
A fast and simple HTML parser that returns a Document object with various methods to manipulate and query the HTML elements. | ||
## Features | ||
- [x] React Native support | ||
- [x] All Browser support | ||
- [x] Node.js support | ||
## Installation | ||
To install the hd-html-parser package, run the following command: | ||
```bash | ||
npm i hd-html-parser | ||
``` | ||
## Usage | ||
To use the hd-html-parser package, you need to import it and call the HtmlParser function with a HTML string as an argument. It will return a Promise that resolves to a Document object or null if the HTML string is invalid. For example: | ||
```typescript | ||
import HDHtmlParser from "hd-html-parser"; | ||
// declare a HTML string | ||
const html = ` | ||
<html> | ||
<head> | ||
<title>Example</title> | ||
</head> | ||
<body> | ||
<h1>Hello, world!</h1> | ||
<p>This is a paragraph.</p> | ||
<ul> | ||
<li>Item 1</li> | ||
<li>Item 2</li> | ||
<li>Item 3</li> | ||
</ul> | ||
</body> | ||
</html> | ||
`; | ||
// parse the HTML string | ||
HDHtmlParser(html).then((document) => { | ||
// do something with the document object | ||
console.log(document.getHtml()); // prints the whole HTML string | ||
console.log(document.querySelector("h1").getText()); // prints "Hello, world!" | ||
console.log(document.querySelectorAll("li").length); // prints 3 | ||
}); | ||
``` | ||
## Document Methods | ||
The Document object returned by the hd-html-parser package has the following methods: | ||
- `querySelector(selector: string): Document | null` - Returns the first element that matches the given CSS selector, or null if none is found. | ||
- `querySelectorAll(selector: string): (Document | null)[]` - Returns an array of all elements that match the given CSS selector, or an empty array if none is found. | ||
- `getHtml(): string | null` - Returns the HTML string of the element, or null if the element is not valid. | ||
- `getAttribute(name: string): string | null | undefined` - Returns the value of the attribute with the given name, or null if the attribute does not exist, or undefined if the element is not valid. | ||
- `getText(): string | null` - Returns the text content of the element, or null if the element is not valid. | ||
- `getParent(): Document` - Returns the parent element of the element, or the element itself if it has no parent. | ||
- `getChildren(): Array<Document>` - Returns an array of the child elements of the element, or an empty array if the element has no children. | ||
- `getOuterHTML(): string | null` - Returns the HTML string of the element including its opening and closing tags, or null if the element is not valid. | ||
- `getInnerHTML(): string | null` - Returns the HTML string of the element excluding its opening and closing tags, or null if the element is not valid. | ||
- `getNext(): Document | null` - Returns the next sibling element of the element, or null if the element has no next sibling. | ||
- `getPrev(): Document | null` - Returns the previous sibling element of the element, or null if the element has no previous sibling. | ||
- `getListData(selector: string, itemsSelector: any, meta?: any): Array<object>` - Returns an array of objects that represent the data of the list elements that match the given selector. The itemsSelector parameter is an object that maps the keys of the data objects to the CSS selectors of the list items. The optional meta parameter is an object that maps the keys of the data objects to the values of the meta attributes of the list elements. For example: | ||
## License | ||
This project is licensed under the terms of the MIT license. See the [LICENSE](LICENSE) file for details. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
77
7
322389