1- import { resolvers } from "../resolver"
1+ import { getMatchSuggestions , resolveAll } from '../resolver'
2+
3+ function createLink ( thing , url ) {
4+ let link = document . createElement ( "a" )
5+ if ( thing ?. [ "url" ] ?. startsWith ( "/" ) || thing ?. [ "url" ] ?. startsWith ( "." ) ) {
6+ link . setAttribute ( "href" , url )
7+ link . pathname = thing [ "url" ]
8+ } else {
9+ link . setAttribute ( "href" , thing [ "url" ] )
10+ }
11+ return link
12+ }
213
314async function parse ( thing , ids , url ) {
4- if (
5- thing . hasOwnProperty ( "@type" ) &&
15+ if ( thing . hasOwnProperty ( "@type" ) &&
616 [ "BreadcrumbList" ] . includes ( thing [ "@type" ] )
717 ) {
8- return null ;
18+ return null
919 }
1020
1121 if ( thing . hasOwnProperty ( "url" ) ) {
12- let link = document . createElement ( "a" ) ;
13- if ( thing ?. [ "url" ] ?. startsWith ( "/" ) || thing ?. [ "url" ] ?. startsWith ( "." ) ) {
14- link . setAttribute ( "href" , url ) ;
15- link . pathname = thing [ "url" ] ;
16- } else {
17- link . setAttribute ( "href" , thing [ "url" ] ) ;
18- }
22+ let link = createLink ( thing , url )
1923
20- for ( let resolver of resolvers ) {
21- let isApplicable = await resolver . applicable ( link )
22- if ( isApplicable ) {
23- let entityId = await resolver . getEntityId ( link )
24- if ( entityId ) {
25- let wdUrl = `https://www.wikidata.org/wiki/${ entityId } ` ;
26- if ( Array . isArray ( thing [ "sameAs" ] ) ) {
27- thing [ "sameAs" ] . push ( wdUrl ) ;
28- } else {
29- thing [ "sameAs" ] = wdUrl ;
30- }
31- } else {
32- if ( JSON . stringify ( isApplicable ) === JSON . stringify ( ids ) ) {
33- thing . isNeedle = true ;
34- }
35- }
24+ const resolutions = await resolveAll ( link )
25+ resolutions . forEach ( it => {
26+ let wdUrl = `https://www.wikidata.org/wiki/${ it . entityId } `
27+ if ( Array . isArray ( thing [ 'sameAs' ] ) ) {
28+ thing [ 'sameAs' ] . push ( wdUrl )
29+ } else {
30+ thing [ 'sameAs' ] = wdUrl
3631 }
37- }
32+ } )
33+
34+ // Todo: with the new interface this requires us to iterate over resolvers twice.
35+ // Check perf impact
36+ const matchSuggestions = await getMatchSuggestions ( link )
37+ thing . isNeedle = Boolean ( matchSuggestions . find ( it => JSON . stringify ( it ) === JSON . stringify ( ids ) ) )
3838 }
3939 for ( let prop in thing ) {
4040 if ( Array . isArray ( thing [ prop ] ) ) {
@@ -61,7 +61,7 @@ function jsonParse(i) {
6161 return JSON . parse ( i . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, "" ) ) ;
6262}
6363
64- async function enrichLinkedData ( snippeds , ids , url ) {
64+ export async function enrichLinkedData ( snippeds , ids , url ) {
6565 let parsed = [ ] ;
6666
6767 for ( let snipped of snippeds ) {
@@ -82,7 +82,7 @@ async function enrichLinkedData(snippeds, ids, url) {
8282 return parsed ;
8383}
8484
85- function findLinkedData ( document ) {
85+ export function findLinkedData ( document ) {
8686 const snippeds = document . querySelectorAll (
8787 'script[type="application/ld+json"]'
8888 ) ;
@@ -93,5 +93,3 @@ function findLinkedData(document) {
9393
9494 return snippeds ;
9595}
96-
97- export { findLinkedData , enrichLinkedData } ;
0 commit comments