Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit e7ed58a

Browse files
committed
Collect-ld move towards using the library interface for accessing the resolvers
1 parent 3ef712d commit e7ed58a

5 files changed

Lines changed: 41 additions & 39 deletions

File tree

src/content/content.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {clearPageLinks, collectPageLinks, getClosestID, getOldid} from './content__collect-page-links.js'
2-
import {findMatchSuggestions, resolve} from '../resolver'
2+
import {getMatchSuggestions, resolve} from '../resolver'
33
import {getElementLanguage} from './content__collect-strings.js'
44
import {makeLanguageValid} from '../get-valid-string-languages.js'
55
import {findTitles} from './pagedata__title.js'
@@ -25,7 +25,7 @@ async function findDirectMatch(location) {
2525
}
2626

2727
async function detectPotentialMatches(location) {
28-
let matchSuggestions = await findMatchSuggestions(location)
28+
let matchSuggestions = await getMatchSuggestions(location)
2929
if (matchSuggestions.length === 0) return
3030

3131
let linkedData = findLinkedData(document)
@@ -50,7 +50,7 @@ async function detectPotentialMatches(location) {
5050
}
5151

5252
async function findApplicables(location) {
53-
if (await findDirectMatch(location )) return
53+
if (await findDirectMatch(location)) return
5454

5555
await detectPotentialMatches(location)
5656
}

src/content/content__collect-ld.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
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

314
async 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 };

src/content/content__collect-meta.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ async function enrichMetaData(tags, lang, url) {
116116
let link = document.createElement('a')
117117
link.href = tags[key][delta]
118118

119-
const entities = await resolveAll(link)
120-
entities.forEach(resolution => {
119+
const resolutions = await resolveAll(link)
120+
resolutions.forEach(resolution => {
121121
enriched[newKey] = {
122122
verb: type.prop,
123123
object: {

src/resolver/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,20 @@ export const resolveAll = async (location: LocationLike): Promise<Resolution[]>
4747
* Obvious candidate for being part of the resolver class if we go that way
4848
*/
4949
async function checkApplicableAndResolve(resolver: Resolver, location: LocationLike): Promise<Resolution | null> {
50-
if (!await resolver.applicable(location)) return
50+
const matchSuggestions = await resolver.applicable(location)
51+
if (!matchSuggestions) return
52+
5153
const entityId = await resolver.getEntityId(location)
5254

5355
if (entityId) return {
5456
entityId,
57+
matchSuggestions: matchSuggestions === true ? [] : matchSuggestions,
5558
doNotCache: resolver.noCache,
5659
}
5760
}
5861

5962
// todo better interface vs nested arrays
60-
export const findMatchSuggestions = async (location: LocationLike)
63+
export const getMatchSuggestions = async (location: LocationLike)
6164
: Promise<Array<Array<MatchSuggestion>>> => {
6265
const suggestions = await Promise.all(
6366
resolvers.map(resolver => resolver.applicable(location)),

src/resolver/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Resolver {
2020

2121
export interface Resolution {
2222
entityId: string
23+
matchSuggestions: Array<MatchSuggestion>
2324
doNotCache?: boolean
2425
}
2526

0 commit comments

Comments
 (0)