html-json-extractor 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -1
- package/dist/index.cjs +24 -0
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +23 -1
- package/package.json +10 -3
package/README.md
CHANGED
|
@@ -15,7 +15,12 @@ npm install html-json-extractor
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
extractJsonLd,
|
|
20
|
+
extractJsonLdStrings,
|
|
21
|
+
getJsonLdItems,
|
|
22
|
+
getJsonLdRecords
|
|
23
|
+
} from 'html-json-extractor';
|
|
19
24
|
|
|
20
25
|
const html = `
|
|
21
26
|
<script type="application/ld+json">{"@type":"WebSite","name":"Example"}</script>
|
|
@@ -28,6 +33,12 @@ const raw = extractJsonLdStrings(html);
|
|
|
28
33
|
|
|
29
34
|
const parsed = extractJsonLd(html);
|
|
30
35
|
// [{ '@type': 'WebSite', name: 'Example' }, [{ '@type': 'Person', name: 'Ada' }]]
|
|
36
|
+
|
|
37
|
+
const items = parsed.flatMap(getJsonLdItems);
|
|
38
|
+
// [{ '@type': 'WebSite', name: 'Example' }, { '@type': 'Person', name: 'Ada' }]
|
|
39
|
+
|
|
40
|
+
const records = parsed.flatMap(getJsonLdRecords);
|
|
41
|
+
// [{ '@type': 'WebSite', name: 'Example' }, { '@type': 'Person', name: 'Ada' }]
|
|
31
42
|
```
|
|
32
43
|
|
|
33
44
|
## API
|
|
@@ -40,6 +51,14 @@ Returns normalized JSON-LD script contents as strings.
|
|
|
40
51
|
|
|
41
52
|
Parses the extracted strings with `JSON.parse`. Entries that fail to parse are skipped.
|
|
42
53
|
|
|
54
|
+
### `getJsonLdItems(value: unknown): unknown[]`
|
|
55
|
+
|
|
56
|
+
Normalizes a single parsed entry or parsed array entry into a flat list of items.
|
|
57
|
+
|
|
58
|
+
### `getJsonLdRecords(value: unknown): Record<string, unknown>[]`
|
|
59
|
+
|
|
60
|
+
Returns record-shaped items and follows nested `@graph` content.
|
|
61
|
+
|
|
43
62
|
## License
|
|
44
63
|
|
|
45
64
|
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -315,5 +315,29 @@ function extractJsonLd(html) {
|
|
|
315
315
|
return results;
|
|
316
316
|
}
|
|
317
317
|
//#endregion
|
|
318
|
+
//#region src/helpers.ts
|
|
319
|
+
const isRecord = (value) => {
|
|
320
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
321
|
+
};
|
|
322
|
+
const asRecord = (value) => {
|
|
323
|
+
return isRecord(value) ? value : void 0;
|
|
324
|
+
};
|
|
325
|
+
const asArray = (value) => {
|
|
326
|
+
return Array.isArray(value) ? value : void 0;
|
|
327
|
+
};
|
|
328
|
+
function getJsonLdRecords(value) {
|
|
329
|
+
const items = asArray(value);
|
|
330
|
+
if (items) return items.flatMap(getJsonLdRecords);
|
|
331
|
+
const record = asRecord(value);
|
|
332
|
+
if (!record) return [];
|
|
333
|
+
return [record, ...getJsonLdRecords(record["@graph"])];
|
|
334
|
+
}
|
|
335
|
+
function getJsonLdItems(value) {
|
|
336
|
+
if (value === void 0) return [];
|
|
337
|
+
return asArray(value) ?? [value];
|
|
338
|
+
}
|
|
339
|
+
//#endregion
|
|
318
340
|
exports.extractJsonLd = extractJsonLd;
|
|
319
341
|
exports.extractJsonLdStrings = extractJsonLdStrings;
|
|
342
|
+
exports.getJsonLdItems = getJsonLdItems;
|
|
343
|
+
exports.getJsonLdRecords = getJsonLdRecords;
|
package/dist/index.d.cts
CHANGED
|
@@ -12,4 +12,8 @@ declare function extractJsonLd<T extends JsonValue = JsonValue>(html: string): T
|
|
|
12
12
|
//#region src/scan.d.ts
|
|
13
13
|
declare function extractJsonLdStrings(html: string): string[];
|
|
14
14
|
//#endregion
|
|
15
|
-
|
|
15
|
+
//#region src/helpers.d.ts
|
|
16
|
+
declare function getJsonLdRecords(value: unknown): Record<string, unknown>[];
|
|
17
|
+
declare function getJsonLdItems(value: unknown): unknown[];
|
|
18
|
+
//#endregion
|
|
19
|
+
export { type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, extractJsonLd, extractJsonLdStrings, getJsonLdItems, getJsonLdRecords };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,4 +12,8 @@ declare function extractJsonLd<T extends JsonValue = JsonValue>(html: string): T
|
|
|
12
12
|
//#region src/scan.d.ts
|
|
13
13
|
declare function extractJsonLdStrings(html: string): string[];
|
|
14
14
|
//#endregion
|
|
15
|
-
|
|
15
|
+
//#region src/helpers.d.ts
|
|
16
|
+
declare function getJsonLdRecords(value: unknown): Record<string, unknown>[];
|
|
17
|
+
declare function getJsonLdItems(value: unknown): unknown[];
|
|
18
|
+
//#endregion
|
|
19
|
+
export { type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, extractJsonLd, extractJsonLdStrings, getJsonLdItems, getJsonLdRecords };
|
package/dist/index.js
CHANGED
|
@@ -314,4 +314,26 @@ function extractJsonLd(html) {
|
|
|
314
314
|
return results;
|
|
315
315
|
}
|
|
316
316
|
//#endregion
|
|
317
|
-
|
|
317
|
+
//#region src/helpers.ts
|
|
318
|
+
const isRecord = (value) => {
|
|
319
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
320
|
+
};
|
|
321
|
+
const asRecord = (value) => {
|
|
322
|
+
return isRecord(value) ? value : void 0;
|
|
323
|
+
};
|
|
324
|
+
const asArray = (value) => {
|
|
325
|
+
return Array.isArray(value) ? value : void 0;
|
|
326
|
+
};
|
|
327
|
+
function getJsonLdRecords(value) {
|
|
328
|
+
const items = asArray(value);
|
|
329
|
+
if (items) return items.flatMap(getJsonLdRecords);
|
|
330
|
+
const record = asRecord(value);
|
|
331
|
+
if (!record) return [];
|
|
332
|
+
return [record, ...getJsonLdRecords(record["@graph"])];
|
|
333
|
+
}
|
|
334
|
+
function getJsonLdItems(value) {
|
|
335
|
+
if (value === void 0) return [];
|
|
336
|
+
return asArray(value) ?? [value];
|
|
337
|
+
}
|
|
338
|
+
//#endregion
|
|
339
|
+
export { extractJsonLd, extractJsonLdStrings, getJsonLdItems, getJsonLdRecords };
|
package/package.json
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-json-extractor",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Fast, forgiving extraction of application/ld+json script blocks from HTML strings.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "VastBlast",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/VastBlast/html-json-extractor.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/VastBlast/html-json-extractor/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/VastBlast/html-json-extractor#readme",
|
|
7
15
|
"type": "module",
|
|
8
16
|
"sideEffects": false,
|
|
9
17
|
"files": [
|
|
@@ -42,10 +50,9 @@
|
|
|
42
50
|
},
|
|
43
51
|
"main": "./dist/index.cjs",
|
|
44
52
|
"module": "./dist/index.js",
|
|
45
|
-
"types": "./dist/index.d.
|
|
53
|
+
"types": "./dist/index.d.cts",
|
|
46
54
|
"exports": {
|
|
47
55
|
".": {
|
|
48
|
-
"types": "./dist/index.d.ts",
|
|
49
56
|
"import": "./dist/index.js",
|
|
50
57
|
"require": "./dist/index.cjs"
|
|
51
58
|
},
|