|
1 | | -import $rdf from '@zazuko/env/web.js' |
| 1 | +import $rdf from '@zazuko/env/web' |
| 2 | +import isUrl from 'is-url' |
2 | 3 | import formats from '@rdfjs/formats' |
3 | 4 | import { Readable } from 'readable-stream' |
4 | | -import type { DatasetCore, Quad } from '@rdfjs/types' |
5 | | -import '../src/custom-elements.d.ts' |
| 5 | +import type { DatasetCore } from '@rdfjs/types' |
6 | 6 |
|
7 | 7 | $rdf.formats.import(formats) |
8 | 8 |
|
9 | | -declare module '@rdfjs/types' { |
10 | | - interface Stream extends AsyncGenerator<Quad> {} |
11 | | -} |
12 | | - |
13 | | -const scriptGraphs = new WeakMap<HTMLScriptElement, DatasetCore>() |
14 | | - |
15 | | -async function parseGraphs() { |
16 | | - const graphs: Record<string, DatasetCore> = {} |
| 9 | +const graphCache = new Map<string, DatasetCore>() |
17 | 10 |
|
18 | | - for (const script of document.querySelectorAll<HTMLScriptElement>('script[data-graph]')) { |
19 | | - const graphName = script.getAttribute('data-graph') |
20 | | - const mediaType = script.getAttribute('type') |
21 | | - |
22 | | - if (mediaType && graphName) { |
23 | | - let graph = scriptGraphs.get(script) |
24 | | - if (!graph) { |
25 | | - graph = await parseOrFetch(script, mediaType) |
26 | | - scriptGraphs.set(script, graph) |
27 | | - } |
28 | | - |
29 | | - if (graph) { |
30 | | - graphs[graphName] = graph |
31 | | - } |
32 | | - } |
| 11 | +export async function loadGraphData(contentOrUrl: string | URL): Promise<DatasetCore> { |
| 12 | + const key = typeof contentOrUrl === 'string' ? contentOrUrl : contentOrUrl.toString() |
| 13 | + const cached = graphCache.get(key) |
| 14 | + if (cached) { |
| 15 | + return cached |
33 | 16 | } |
34 | 17 |
|
35 | | - return graphs |
36 | | -} |
37 | | - |
38 | | -async function parseOrFetch(script: HTMLScriptElement, mediaType: string) { |
39 | 18 | const dataset = $rdf.dataset() |
40 | | - let content: string | null | undefined |
41 | | - |
42 | | - if (script.src) { |
43 | | - const response = await fetch(script.src) |
| 19 | + let content = key |
| 20 | + let mediaType = 'text/turtle' |
| 21 | + |
| 22 | + if (isUrl(content) || content.startsWith('http://') || content.startsWith('https://')) { |
| 23 | + const url = content |
| 24 | + const response = await fetch(url) |
| 25 | + const contentType = response.headers.get('content-type') |
| 26 | + if (contentType) { |
| 27 | + mediaType = contentType.split(';')[0] |
| 28 | + } |
| 29 | + if (url.endsWith('.nq') || url.endsWith('.nquads')) { |
| 30 | + mediaType = 'application/n-quads' |
| 31 | + } |
| 32 | + else if (url.endsWith('.nt')) { |
| 33 | + mediaType = 'application/n-triples' |
| 34 | + } |
| 35 | + else if (url.endsWith('.json') || url.endsWith('.jsonld')) { |
| 36 | + mediaType = 'application/ld+json' |
| 37 | + } |
| 38 | + else if (url.endsWith('.trig')) { |
| 39 | + mediaType = 'application/trig' |
| 40 | + } |
| 41 | + else if (url.endsWith('.ttl')) { |
| 42 | + mediaType = 'text/turtle' |
| 43 | + } |
44 | 44 | content = await response.text() |
45 | 45 | } |
46 | | - else { |
47 | | - content = script.textContent |
48 | | - } |
49 | 46 |
|
50 | 47 | const stream = $rdf.formats.parsers.import(mediaType, Readable.from(content)) |
51 | 48 | if (stream) { |
52 | 49 | for await (const quad of stream) { |
53 | 50 | dataset.add(quad) |
54 | 51 | } |
55 | 52 | } |
| 53 | + |
| 54 | + graphCache.set(key, dataset) |
56 | 55 | return dataset |
57 | 56 | } |
58 | | - |
59 | | -const mutationObserver = new MutationObserver(async () => { |
60 | | - const graphs = await parseGraphs() |
61 | | - |
62 | | - const targets = document.querySelectorAll('rdf-graph') |
63 | | - |
64 | | - for (const target of targets) { |
65 | | - const graphName = target.id |
66 | | - if (!graphName) { |
67 | | - continue |
68 | | - } |
69 | | - if (graphs[graphName]) { |
70 | | - target.value = graphs[graphName] |
71 | | - } |
72 | | - } |
73 | | -}) |
74 | | - |
75 | | -mutationObserver.observe(document.body, { childList: true, subtree: true }) |
0 commit comments