Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cheerio options fragment mode support #82

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
v0.22.0

- DX Improved.
- CheerioOptions and Fragment Mode support added.

```ts
parse(data, config, options?: CheerioOptions, isDocument?: boolean)
```

v0.21.0

- Dependencies updated.
Expand All @@ -15,13 +24,7 @@ v0.19.0

```ts
{
fill: null;
}
{
fill: false;
}
{
fill: '';
fill: null | false | '';
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muninn",
"version": "0.21.2",
"version": "0.22.0",
"description": "It parses the html and collects the requested data as desired.",
"main": "build/index.js",
"scripts": {
Expand Down
22 changes: 19 additions & 3 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { load, CheerioAPI } from 'cheerio';
import { load, CheerioAPI, CheerioOptions } from 'cheerio';
import { Config } from '../config/types';
import { getRawConfig } from './getRawConfig';
import getValue from './getValue';

/**
* Create a JSON output using the provided schema and markup.
*
* Note that similar to web browser contexts, this operation may introduce
* `<html>`, `<head>`, and `<body>` elements; set `isDocument` to `false` to
* switch to fragment mode and disable this.
*
* @param content - Markup to be loaded or a cheerio instance.
* @param config - Your parse config including your schema
* @param options - Cheerio options for the created instance.
* @param isDocument - Allows cheerio parser to be switched to fragment mode.
* @returns Your JSON output parsed according to your schema.
* @see {@link https://wopehq.gitbook.io/muninn} for additional usage information.
*/
function parse<Initial = unknown>(
data: string | CheerioAPI,
config: Config<Initial>
config: Config<Initial>,
options?: CheerioOptions,
isDocument?: boolean
): Record<string, unknown> {
let $;

if (typeof data === 'string') {
$ = load(data);
$ = load(data, options, isDocument);
} else if (typeof data === 'function') {
$ = data;
}
Expand Down
Loading