Removed cheerio parameter
parent
91f809f249
commit
a3c2eb81b8
|
@ -10,22 +10,21 @@ import { selectors as f95Selector } from "./constants/css-selector.js";
|
||||||
/**
|
/**
|
||||||
* Represents information contained in a JSON+LD tag.
|
* Represents information contained in a JSON+LD tag.
|
||||||
*/
|
*/
|
||||||
export type JSONLD = { [s: string]: string | JSONLD }
|
export type TJsonLD = { [s: string]: string | TJsonLD }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts and processes the JSON-LD values of the page.
|
* Extracts and processes the JSON-LD values of the page.
|
||||||
* @param {cheerio.Root} $ Cheerio root of document.
|
|
||||||
* @param {cheerio.Cheerio} body Page `body` selector
|
* @param {cheerio.Cheerio} body Page `body` selector
|
||||||
* @returns {JSONLD[]} List of data obtained from the page
|
* @returns {TJsonLD[]} List of data obtained from the page
|
||||||
*/
|
*/
|
||||||
export function getJSONLD($: cheerio.Root, body: cheerio.Cheerio): JSONLD {
|
export function getJSONLD(body: cheerio.Cheerio): TJsonLD {
|
||||||
shared.logger.trace("Extracting JSON-LD data...");
|
shared.logger.trace("Extracting JSON-LD data...");
|
||||||
|
|
||||||
// Fetch the JSON-LD data
|
// Fetch the JSON-LD data
|
||||||
const structuredDataElements = body.find(f95Selector.GT_JSONLD);
|
const structuredDataElements = body.find(f95Selector.GT_JSONLD);
|
||||||
|
|
||||||
// Parse the data
|
// Parse the data
|
||||||
const values = structuredDataElements.map((idx, el) => parseJSONLD($(el))).get();
|
const values = structuredDataElements.map((idx, el) => parseJSONLD(el)).get();
|
||||||
|
|
||||||
// Merge the data and return a single value
|
// Merge the data and return a single value
|
||||||
return mergeJSONLD(values);
|
return mergeJSONLD(values);
|
||||||
|
@ -36,9 +35,9 @@ export function getJSONLD($: cheerio.Root, body: cheerio.Cheerio): JSONLD {
|
||||||
* Merges multiple JSON+LD tags into one object.
|
* Merges multiple JSON+LD tags into one object.
|
||||||
* @param data List of JSON+LD tags
|
* @param data List of JSON+LD tags
|
||||||
*/
|
*/
|
||||||
function mergeJSONLD(data: JSONLD[]): JSONLD {
|
function mergeJSONLD(data: TJsonLD[]): TJsonLD {
|
||||||
// Local variables
|
// Local variables
|
||||||
let merged: JSONLD = {};
|
let merged: TJsonLD = {};
|
||||||
|
|
||||||
for (const value of data) {
|
for (const value of data) {
|
||||||
merged = Object.assign(merged, value);
|
merged = Object.assign(merged, value);
|
||||||
|
@ -50,9 +49,9 @@ function mergeJSONLD(data: JSONLD[]): JSONLD {
|
||||||
/**
|
/**
|
||||||
* Parse a JSON-LD element source code.
|
* Parse a JSON-LD element source code.
|
||||||
*/
|
*/
|
||||||
function parseJSONLD(element: cheerio.Cheerio): JSONLD {
|
function parseJSONLD(element: cheerio.Element): TJsonLD {
|
||||||
// Get the element HTML
|
// Get the element HTML
|
||||||
const html = element.html().trim();
|
const html = cheerio(element).html().trim();
|
||||||
|
|
||||||
// Obtain the JSON-LD
|
// Obtain the JSON-LD
|
||||||
const data = html
|
const data = html
|
||||||
|
|
Loading…
Reference in New Issue