Fix cheerio error
							parent
							
								
									3c89da8ea5
								
							
						
					
					
						commit
						1cad5c277f
					
				| 
						 | 
					@ -4,8 +4,8 @@
 | 
				
			||||||
import cheerio from "cheerio";
 | 
					import cheerio from "cheerio";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Modules from file
 | 
					// Modules from file
 | 
				
			||||||
import shared from "./shared";
 | 
					import shared from "./shared.js";
 | 
				
			||||||
import { selectors as f95Selector } from "./constants/css-selector";
 | 
					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.
 | 
				
			||||||
| 
						 | 
					@ -14,17 +14,18 @@ export type JSONLD = { [s: string]: string | JSONLD }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 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 {JSONLD[]} List of data obtained from the page
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export function getJSONLD(body: cheerio.Cheerio): JSONLD {
 | 
					export function getJSONLD($: cheerio.Root, body: cheerio.Cheerio): JSONLD {
 | 
				
			||||||
    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);
 | 
				
			||||||
| 
						 | 
					@ -49,9 +50,9 @@ function mergeJSONLD(data: JSONLD[]): JSONLD {
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Parse a JSON-LD element source code.
 | 
					 * Parse a JSON-LD element source code.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function parseJSONLD(element: cheerio.Element): JSONLD {
 | 
					function parseJSONLD(element: cheerio.Cheerio): JSONLD {
 | 
				
			||||||
    // Get the element HTML
 | 
					    // Get the element HTML
 | 
				
			||||||
    const html = cheerio.load(element).html().trim();
 | 
					    const html = element.html().trim();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Obtain the JSON-LD
 | 
					    // Obtain the JSON-LD
 | 
				
			||||||
    const data = html
 | 
					    const data = html
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue