mdld-parse 0.9.4 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,6 +62,9 @@ const state = {
62
62
  removeSet: new Set(), // External retractions
63
63
  origin: { quadIndex: new Map() }, // Lean origin tracking
64
64
  currentSubject: null, // Current subject context
65
+ primarySubject: null, // First non-fragment subject declaration
66
+ primaryType: null, // First rdf:type declaration
67
+ primaryLabel: null, // First rdfs:label literal
65
68
  tokens: null, // Tokenized input
66
69
  currentTokenIndex: -1, // Current processing position
67
70
  statements: [], // Elevated statements array
@@ -79,6 +82,9 @@ const state = {
79
82
  | **removeSet** | External retractions | Accumulated for removal operations |
80
83
  | **origin** | Provenance tracking | Updated with each quad |
81
84
  | **currentSubject** | Subject context management | Reset and updated during parsing |
85
+ | **primarySubject** | First non-fragment subject | Set once, never changed |
86
+ | **primaryType** | First rdf:type declaration | Set once, never changed |
87
+ | **primaryLabel** | First rdfs:label literal | Set once, never changed |
82
88
  | **statements** | Elevated statements | Accumulated during pattern detection |
83
89
  | **statementCandidates** | Pattern completion tracking | Modified during rdf:Statement detection |
84
90
 
@@ -357,7 +363,57 @@ function quadIndexKey(subject, predicate, object) {
357
363
  }
358
364
  ```
359
365
 
360
- ### 6. Elevated Statements Detection
366
+ ### 6. Primary Metadata Extraction
367
+
368
+ #### First-Occurrence Tracking
369
+
370
+ The parser tracks primary metadata during single-pass parsing to provide immediate access to commonly needed document identity:
371
+
372
+ ```javascript
373
+ // Track primary subject: first non-fragment subject declaration
374
+ if (newSubject && !state.primarySubject && !sem.subject.startsWith('=#')) {
375
+ state.primarySubject = newSubject.value; // Store as string IRI
376
+ }
377
+
378
+ // Track primary type and label during quad emission
379
+ if (state) {
380
+ if (!state.primaryType && predicate.value === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
381
+ state.primaryType = object.value;
382
+ }
383
+ if (!state.primaryLabel && predicate.value === 'http://www.w3.org/2000/01/rdf-schema#label' && object.termType === 'Literal') {
384
+ state.primaryLabel = object.value;
385
+ }
386
+ }
387
+ ```
388
+
389
+ #### Primary Metadata Trio
390
+
391
+ | Field | Source | Purpose | Use Case |
392
+ |-------|--------|---------|-----------|
393
+ | **primarySubject** | First non-fragment `{=subject}` | Document identity | Nostr addressing tags, document routing |
394
+ | **primaryType** | First `.Class` declaration | Document category | Stream filtering, UI categorization |
395
+ | **primaryLabel** | First `{label}` literal | Human-readable name | Display titles, search indexing |
396
+
397
+ #### Document Identity Applications
398
+
399
+ The primary metadata trio provides sufficient document/append stream identity for most scenarios:
400
+
401
+ ```javascript
402
+ // Nostr integration example
403
+ const nostrTags = [
404
+ ['s', result.primarySubject], // subject addressing
405
+ ['t', result.primaryType], // type filtering
406
+ ['d', result.primaryLabel] // display identifier
407
+ ];
408
+ ```
409
+
410
+ **Benefits:**
411
+ - **Immediate access** - No post-processing required
412
+ - **Deterministic** - First-occurrence semantics
413
+ - **Lightweight** - Minimal parsing overhead
414
+ - **Sufficient** - Covers 80% of use cases
415
+
416
+ ### 7. Elevated Statements Detection
361
417
 
362
418
  #### Pattern Detection System
363
419
 
@@ -474,13 +530,22 @@ for (let i = 0; i < tokens.length; i++) {
474
530
  // Process retractions and ensure hard invariant
475
531
  const filteredRemove = processRetractions(state.quads, state.removeSet);
476
532
 
477
- return {
478
- quads: state.quads, // Final resolved graph state
479
- remove: filteredRemove, // External retractions only
480
- statements: state.statements, // Elevated statements
481
- origin: state.origin, // Complete provenance
482
- context: state.ctx // Final prefix context
483
- };
533
+ // Create structured primary object for semantic surface
534
+ const primary = {
535
+ subject: state.primarySubject,
536
+ type: state.primaryType,
537
+ label: state.primaryLabel
538
+ };
539
+
540
+ return {
541
+ quads: state.quads, // Final resolved graph state
542
+ remove: filteredRemove, // External retractions only
543
+ statements: state.statements, // Elevated statements
544
+ origin: state.origin, // Complete provenance
545
+ context: state.ctx, // Final prefix context
546
+ primarySubject: state.primarySubject, // Canonical append identity
547
+ primary // Semantic surface descriptor
548
+ };
484
549
  ```
485
550
 
486
551
  #### Retraction Processing
@@ -1,11 +1,56 @@
1
- # Primary Subject Example
1
+ # Primary Metadata Example
2
2
 
3
- This example demonstrates the Primary Subject feature in MD-LD, which identifies the main entity described by a document.
3
+ This example demonstrates the Primary Metadata feature in MD-LD, which provides immediate access to the main entity, type, and label described by a document.
4
+
5
+ ## Primary Metadata Trio
6
+
7
+ MD-LD tracks three primary metadata fields during parsing to provide immediate document identity:
8
+
9
+ | Field | Source | Purpose |
10
+ |-------|--------|---------|
11
+ | **primarySubject** | First non-fragment `{=subject}` | Document identity (canonical append identity) |
12
+ | **primaryType** | First `rdf:type` declaration | Document category (semantic classification) |
13
+ | **primaryLabel** | First `rdfs:label` literal | Human-readable name (UI & display) |
14
+
15
+ ## Dual-Layer Architecture
16
+
17
+ MD-LD implements a **dual-layer architecture** that separates concerns cleanly:
18
+
19
+ | Layer | Field | Role | Use Cases |
20
+ | ------ | ------ | --------- | ---------- |
21
+ | **Canonical Identity** | `primarySubject` | Storage, routing, synchronization, append operations |
22
+ | **Semantic Surface** | `primary` object | UI rendering, indexing, navigation, agent orientation |
4
23
 
5
24
  ## What is a Primary Subject?
6
25
 
7
26
  The Primary Subject is the first non-fragment subject declaration (`{=IRI}`) in a document. It provides a deterministic way to identify the central focus of a document.
8
27
 
28
+ ## What is a Primary Type?
29
+
30
+ The Primary Type is the first `rdf:type` declaration associated with the Primary Subject. It provides a way to identify the type of the main entity described by a document.
31
+
32
+ ### 1. First Type Declaration Wins
33
+
34
+ ```markdown
35
+ # Person {=my:alice .prov:Person .schema:Person}
36
+ [Details] {label}
37
+ ```
38
+
39
+ `primaryType` captures the **first rdf:type assertion emitted while current subject == primarySubject**. This is parser-precise and avoids ambiguity with later type declarations for other entities.
40
+
41
+ ## What is a Primary Label?
42
+
43
+ The Primary Label is the first `rdfs:label` literal associated with the Primary Subject. It provides a human-readable name for the main entity described by a document.
44
+
45
+ ### 2. First Label Declaration Wins
46
+
47
+ ```markdown
48
+ # Person {=my:alice .prov:Person label}
49
+ [Alice Smith] {rdfs:label}
50
+ ```
51
+
52
+ `primaryLabel` captures the **first emitted rdfs:label literal while current subject == primarySubject**. This ensures the label is associated with the primary subject, not other entities in the document.
53
+
9
54
  ## Example Document
10
55
 
11
56
  ```markdown
@@ -22,21 +67,30 @@ The Primary Subject is the first non-fragment subject declaration (`{=IRI}`) in
22
67
  This article explains the Primary Subject concept in MD-LD, which allows applications to quickly identify the main entity described by a document.
23
68
  ```
24
69
 
25
- ## Parsing with Primary Subject
70
+ ## Parsing with Primary Metadata
26
71
 
27
72
  ```javascript
28
73
  import { parse } from 'mdld-parse';
29
74
 
30
- const md = `[ex] <http://example.org/>
75
+ const md = `[ex] <tag:alice@example.org,2026:articles/>
31
76
  [schema] <http://schema.org/>
32
77
 
33
- # Article {=ex:article .schema:Article label}
34
- [Understanding Primary Subjects] {schema:headline}`;
78
+ # Understanding Primary Subjects {=ex:understanding-primary-subject .schema:Article label}
79
+ `;
35
80
 
36
81
  const result = parse({ text: md });
37
82
 
38
- console.log(result.primarySubject);
39
- // Output: http://example.org/article
83
+ // Canonical append identity
84
+ console.log('Primary Subject:', result.primarySubject);
85
+ // Semantic surface descriptor
86
+ console.log('Primary Object:', result.primary);
87
+ // Output:
88
+ // Primary Subject: tag:alice@example.org,2026:articles/understanding-primary-subject
89
+ // Primary Object: {
90
+ // subject: 'tag:alice@example.org,2026:articles/understanding-primary-subject',
91
+ // type: 'http://schema.org/Article',
92
+ // label: 'Understanding Primary Subjects'
93
+ // }
40
94
  ```
41
95
 
42
96
  ## Key Behaviors
@@ -92,16 +146,27 @@ const doc2 = `[ex] <http://example.org/>
92
146
 
93
147
  const result = merge([doc1, doc2], { context: { ex: 'http://example.org/' } });
94
148
 
95
- console.log(result.primarySubjects.map(s => s.value));
96
- // Output: ['http://example.org/doc1', 'http://example.org/doc2']
149
+ // Canonical append identities
150
+ console.log('Primary Subjects:', result.primarySubjects);
151
+ // Semantic surface descriptors
152
+ console.log('Primary Objects:', result.primary);
153
+ // Output:
154
+ // Primary Subjects: ['http://example.org/doc1', 'http://example.org/doc2']
155
+ // Primary Objects: [
156
+ // { subject: 'http://example.org/doc1', type: 'http://schema.org/Article', label: 'Document 1' },
157
+ // { subject: 'http://example.org/doc2', type: 'http://schema.org/Article', label: 'Document 2' }
158
+ // ]
97
159
  ```
98
160
 
99
161
  ## Use Cases
100
162
 
101
- - **Document Identification** - Quickly determine what a document is about
163
+ - **Document Identity** - Primary subject + type + label provide complete document fingerprint
164
+ - **Stream Addressing** - Perfect for Nostr tags and distributed systems
102
165
  - **Merge Tracking** - Track which entities are being merged across documents
103
- - **UI Navigation** - Provide a default focus point for document viewers
104
- - **Query Optimization** - Use primary subject as default query context
166
+ - **UI Navigation** - Provide default focus and display information for document viewers
167
+ - **Query Optimization** - Use primary metadata as default query context and filters
168
+ - **Content Classification** - Automatic categorization based on primary type
169
+ - **Search Indexing** - Use primary label for human-readable search results
105
170
 
106
171
  ## RDF Philosophy Alignment
107
172
 
@@ -180,8 +245,18 @@ const doc1 = `[blog] <https://example.com/blog/>
180
245
  [Hello World] {schema:headline}`;
181
246
 
182
247
  const result1 = parse({ text: doc1, context: { blog: 'https://example.com/blog/', schema: 'http://schema.org/' } });
183
- console.log('Primary subject:', result1.primarySubject);
184
- // Output: https://example.com/blog/post1
248
+
249
+ // Canonical append identity
250
+ console.log('Primary Subject:', result1.primarySubject);
251
+ // Semantic surface descriptor
252
+ console.log('Primary Object:', result1.primary);
253
+ // Output:
254
+ // Primary Subject: https://example.com/blog/post1
255
+ // Primary Object: {
256
+ // subject: 'https://example.com/blog/post1',
257
+ // type: 'http://schema.org/BlogPosting',
258
+ // label: 'My First Post'
259
+ // }
185
260
 
186
261
  // Multiple documents
187
262
  const doc2 = `[blog] <https://example.com/blog/>
@@ -190,10 +265,61 @@ const doc2 = `[blog] <https://example.com/blog/>
190
265
 
191
266
  const merged = merge([doc1, doc2], { context: { blog: 'https://example.com/blog/', schema: 'http://schema.org/' } });
192
267
 
193
- console.log('Primary subjects:', merged.primarySubjects);
194
- // Output: ['https://example.com/blog/post1', 'https://example.com/blog/post2']
268
+ // Canonical append identities
269
+ console.log('Primary Subjects:', merged.primarySubjects);
270
+ // Semantic surface descriptors
271
+ console.log('Primary Objects:', merged.primary);
272
+ // Output:
273
+ // Primary Subjects: ['https://example.com/blog/post1', 'https://example.com/blog/post2']
274
+ // Primary Objects: [
275
+ // { subject: 'https://example.com/blog/post1', type: 'http://schema.org/BlogPosting', label: 'My First Post' },
276
+ // { subject: 'https://example.com/blog/post2', type: 'http://schema.org/BlogPosting', label: 'My Second Post' }
277
+ // ]
278
+ ```
279
+
280
+ ## Document Identity Trio
281
+
282
+ The primary metadata trio provides sufficient document/append stream identity for most scenarios:
283
+
284
+ ```javascript
285
+ // Nostr integration example
286
+ const nostrTags = [
287
+ ['s', result.primarySubject], // subject addressing (canonical)
288
+ ['t', result.primary.type], // type filtering
289
+ ['d', result.primary.label] // display identifier
290
+ ];
291
+
292
+ // Document fingerprint
293
+ const fingerprint = {
294
+ canonical: result.primarySubject, // What the document is about (append identity)
295
+ semantic: result.primary // How to present and index the document
296
+ };
195
297
  ```
196
298
 
299
+ ## Primary Metadata is Parser-Level Metadata
300
+
301
+ **Important**: Primary metadata is derived parser state and does not introduce additional RDF triples or semantic entailments. It describes document intent, not graph semantics.
302
+
303
+ ### Key Distinctions
304
+
305
+ | Aspect | Primary Metadata | RDF Semantics |
306
+ |---------|------------------|----------------|
307
+ | **Source** | Parser extraction rules | Triple patterns |
308
+ | **Scope** | Document-level intent | Graph-level relationships |
309
+ | **Purpose** | Routing, UI, indexing | Reasoning, inference |
310
+ | **Stability** | Deterministic extraction | Dynamic inference |
311
+
312
+ ### Stability Guarantees
313
+
314
+ `primarySubject` is **stable under rewrite() operations that preserve extracted quads**. This means:
315
+
316
+ - Append routing remains consistent across document updates
317
+ - Synchronization identity is preserved during merges
318
+ - File mapping stays stable through rewrite operations
319
+ - Index references remain valid
320
+
321
+ This stability is crucial for distributed systems and local-first architectures.
322
+
197
323
  ## Specification
198
324
 
199
325
  For complete details on Primary Subject semantics, see the [MD-LD Specification](../spec/Spec.md#17-primary-subject).
package/dist/index.html CHANGED
@@ -30,10 +30,10 @@
30
30
 
31
31
 
32
32
  <script type="module" crossorigin>var e=Object.defineProperty,t=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports),n=(t,n)=>{let r={};for(var i in t)e(r,i,{get:t[i],enumerable:!0});return n||e(r,Symbol.toStringTag,{value:`Module`}),r};(function(){let e=document.createElement(`link`).relList;if(e&&e.supports&&e.supports(`modulepreload`))return;for(let e of document.querySelectorAll(`link[rel="modulepreload"]`))n(e);new MutationObserver(e=>{for(let t of e)if(t.type===`childList`)for(let e of t.addedNodes)e.tagName===`LINK`&&e.rel===`modulepreload`&&n(e)}).observe(document,{childList:!0,subtree:!0});function t(e){let t={};return e.integrity&&(t.integrity=e.integrity),e.referrerPolicy&&(t.referrerPolicy=e.referrerPolicy),e.crossOrigin===`use-credentials`?t.credentials=`include`:e.crossOrigin===`anonymous`?t.credentials=`omit`:t.credentials=`same-origin`,t}function n(e){if(e.ep)return;e.ep=!0;let n=t(e);fetch(e.href,n)}})();var r={"@vocab":`http://www.w3.org/2000/01/rdf-schema#`,rdf:`http://www.w3.org/1999/02/22-rdf-syntax-ns#`,rdfs:`http://www.w3.org/2000/01/rdf-schema#`,xsd:`http://www.w3.org/2001/XMLSchema#`,sh:`http://www.w3.org/ns/shacl#`,prov:`http://www.w3.org/ns/prov#`},i=/^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file|urn:uuid):/,a=/^(https?|ftp|mailto|tag|nih|urn|uuid|did|web|ipfs|ipns|data|file):/,o=class{constructor(e){this.id=e}equals(e){return!!e&&this.termType===e.termType&&this.value===e.value}},s=class extends o{constructor(e){super(e),this.termType=`NamedNode`,this.value=e}},c=class extends o{constructor(e){super(e),this.termType=`Literal`,this.value=``,this.language=``,this.datatype=null;let t=e.match(/^"([^"\\]*(?:\\.[^"\\]*)*)"(\^\^([^"]+))?(@([^-]+)(--(.+))?)?$/);t?(this.value=t[1].replace(/\\"/g,`"`).replace(/\\\\/g,`\\`),t[5]?(this.language=t[5],this.datatype=new s(`http://www.w3.org/1999/02/22-rdf-syntax-ns#langString`)):t[3]?this.datatype=new s(t[3]):this.datatype=new s(`http://www.w3.org/2001/XMLSchema#string`)):(this.value=e.replace(/^"|"$/g,``),this.datatype=new s(`http://www.w3.org/2001/XMLSchema#string`))}equals(e){return!!e&&this.termType===e.termType&&this.value===e.value&&this.language===e.language&&this.datatype?.value===e.datatype?.value}},l=class extends o{constructor(e){super(e||`b${Math.random().toString(36).slice(2,11)}`),this.termType=`BlankNode`,this.value=this.id}},u=class extends o{constructor(e){super(e),this.termType=`Variable`,this.value=e}},d=new class extends o{constructor(){super(``),this.termType=`DefaultGraph`,this.value=``}equals(e){return!!e&&this.termType===e.termType}},f=class extends o{constructor(e,t,n,r=d){super(`${e.id}|${t.id}|${n.id}|${r.id}`),this.termType=`Quad`,this.subject=e,this.predicate=t,this.object=n,this.graph=r}equals(e){return!!e&&this.termType===e.termType&&this.subject.equals(e.subject)&&this.predicate.equals(e.predicate)&&this.object.equals(e.object)&&this.graph.equals(e.graph)}toJSON(){return{termType:this.termType,subject:this.subject.toJSON?this.subject.toJSON():{termType:this.subject.termType,value:this.subject.value},predicate:this.predicate.toJSON?this.predicate.toJSON():{termType:this.predicate.termType,value:this.predicate.value},object:this.object.toJSON?this.object.toJSON():{termType:this.object.termType,value:this.object.value},graph:this.graph.toJSON?this.graph.toJSON():{termType:this.graph.termType,value:this.graph.value}}}},p={boolean:`http://www.w3.org/2001/XMLSchema#boolean`,integer:`http://www.w3.org/2001/XMLSchema#integer`,double:`http://www.w3.org/2001/XMLSchema#double`,string:`http://www.w3.org/2001/XMLSchema#string`},m={namedNode:e=>new s(e),blankNode:e=>new l(e),literal:(e,t)=>{let n=String(e).replace(/"/g,`\\"`);if(typeof t==`string`)return new c(`"${n}"@${t.toLowerCase()}`);if(t!==void 0&&!(`termType`in t)){let e=t.direction?`--${t.direction.toLowerCase()}`:``;return new c(`"${n}"@${t.language.toLowerCase()}${e}`)}let r=t?t.value:``;return r===``&&(typeof e==`boolean`?r=p.boolean:typeof e==`number`&&(Number.isFinite(e)?r=Number.isInteger(e)?p.integer:p.double:(r=p.double,Number.isNaN(e)||(e=e>0?`INF`:`-INF`)))),r===``||r===p.string?new c(`"${n}"`):new c(`"${n}"^^${r}`)},variable:e=>new u(e),defaultGraph:()=>d,quad:(e,t,n,r)=>new f(e,t,n,r),triple:(e,t,n,r)=>new f(e,t,n,r),fromTerm:e=>{if(e instanceof o)return e;switch(e.termType){case`NamedNode`:return new s(e.value);case`BlankNode`:return new l(e.value);case`Variable`:return new u(e.value);case`DefaultGraph`:return d;case`Literal`:let t=String(e.value).replace(/"/g,`\\"`);return e.language?new c(`"${t}"@${e.language}`):e.datatype?new c(`"${t}"^^${e.datatype.value||e.datatype}`):new c(`"${t}"`);case`Quad`:return m.fromQuad(e);default:throw Error(`Unexpected termType: ${e.termType}`)}},fromQuad:e=>{if(e instanceof f)return e;if(e.termType!==`Quad`){if(e.subject&&e.predicate&&e.object)return new f(m.fromTerm(e.subject),m.fromTerm(e.predicate),m.fromTerm(e.object),m.fromTerm(e.graph||m.defaultGraph()));throw Error(`Unexpected termType: ${e.termType}`)}return new f(m.fromTerm(e.subject),m.fromTerm(e.predicate),m.fromTerm(e.object),m.fromTerm(e.graph))}};function h(e,t){if(!e||!t||!t.quadIndex)return null;let n=C(e);return n&&t.quadIndex.get(n)||null}function g(e){let t=5381;for(let n=0;n<e.length;n++)t=(t<<5)+t+e.charCodeAt(n);return Math.abs(t).toString(16).slice(0,12)}var _=new Map;function v(e,t){if(e==null)return null;let n=`${e}|${t[`@vocab`]||``}|${Object.keys(t).filter(e=>e!==`@vocab`).sort().map(e=>`${e}:${t[e]}`).join(`,`)}`;if(_.has(n))return _.get(n);let r=(typeof e==`string`?e:typeof e==`object`&&typeof e.value==`string`?e.value:String(e)).trim(),a;if(r.match(i))a=r;else if(r.includes(`:`)){let[e,n]=r.split(`:`,2);e&&!t[e]&&e!==`@vocab`&&console.warn(`Undefined prefix "${e}" in IRI "${r}" - treating as literal`),a=t[e]?t[e]+n:r}else a=(t[`@vocab`]||``)+r;return _.set(n,a),a}function y(e,t){if(!e||!a.test(e))return e;if(t[`@vocab`]&&e.startsWith(t[`@vocab`]))return e.substring(t[`@vocab`].length);for(let[n,r]of Object.entries(t))if(n!==`@vocab`&&e.startsWith(r)&&Object.entries(t).filter(([t,n])=>t!==`@vocab`&&e.startsWith(n)).every(([e,t])=>r.length>=t.length||e===n&&t.length===r.length))return n+`:`+e.substring(r.length);return e}var b={"=#":{kind:`fragment`,extract:e=>e.substring(2).replace(`}`,``)},"+#":{kind:`softFragment`,extract:e=>e.substring(2).replace(`}`,``)},"+":{kind:`object`,extract:e=>e.substring(1)},"^^":{kind:`datatype`,extract:e=>e.substring(2)},"@":{kind:`language`,extract:e=>e.substring(1)},".":{kind:`type`,extract:e=>e.substring(1)},"!":{kind:`property`,form:`!`,extract:e=>e.substring(1)},"?":{kind:`property`,form:`?`,extract:e=>e.substring(1)}};function x(e){try{let t=String(e||``).trim().replace(/^\{|\}$/g,``).trim();if(!t)return{subject:null,object:null,types:[],predicates:[],datatype:null,language:null,entries:[]};let n={subject:null,object:null,types:[],predicates:[],datatype:null,language:null,entries:[]},r=/\S+/g,i;for(;(i=r.exec(t))!==null;){let e=i[0],t=1+i.index,r=t+e.length,a=n.entries.length,o=!1;if(e.startsWith(`-`)&&e.length>1&&(o=!0,e=e.slice(1)),e===`=`){o&&console.warn(`-= is not valid, subject declarations have no polarity`),n.subject=`RESET`,n.entries.push({kind:`subjectReset`,relRange:{start:t,end:r},raw:e});continue}if(e.startsWith(`=`)&&!e.startsWith(`=#`)){o&&console.warn(`-= is not valid, subject declarations have no polarity`);let i=e.substring(1);n.subject=i,n.entries.push({kind:`subject`,iri:i,relRange:{start:t,end:r},raw:e});continue}let s=!1;for(let[c,l]of Object.entries(b))if(e.startsWith(c)){let c={kind:l.kind,relRange:{start:t,end:r},raw:i[0]},u=l.extract(e);l.kind===`fragment`?(n.subject=`=#${u}`,c.fragment=u):l.kind===`softFragment`?(n.object=`#${u}`,c.fragment=u):l.kind===`object`?(o&&=(console.warn(`-+ is not valid, object declarations have no polarity`),!1),n.object=u,c.iri=u):l.kind===`datatype`?(o&&=(console.warn(`-^^ is not valid, datatype modifiers have no polarity`),!1),n.language||(n.datatype=u),c.datatype=u):l.kind===`language`?(o&&=(console.warn(`-@ is not valid, language modifiers have no polarity`),!1),n.language=u,n.datatype=null,c.language=u):l.kind===`type`?(n.types.push({iri:u,entryIndex:a,remove:o}),c.iri=u,c.remove=o):l.kind===`property`&&(n.predicates.push({iri:u,form:l.form,entryIndex:a,remove:o}),c.iri=u,c.form=l.form,c.remove=o),n.entries.push(c),s=!0;break}s||(n.predicates.push({iri:e,form:``,entryIndex:a,remove:o}),n.entries.push({kind:`property`,iri:e,form:``,relRange:{start:t,end:r},raw:i[0],remove:o}))}return n}catch(t){return console.error(`Error parsing semantic block ${e}:`,t),{subject:null,object:null,types:[],predicates:[],datatype:null,language:null,entries:[]}}}function S(e,t,n){let r=n.termType===`Literal`?JSON.stringify({t:`Literal`,v:n.value,lang:n.language||``,dt:n.datatype?.value||``}):JSON.stringify({t:n.termType,v:n.value});return JSON.stringify([e.value,t.value,r])}function C(e){return e?S(e.subject,e.predicate,e.object):null}function w(e,t,n,r,i){return t?i.literal(e,i.namedNode(v(t,r))):n?i.literal(e,n):i.literal(e)}function T(e){if(e.length<3)return null;let t=e[0];if(t!=="`"&&t!==`~`)return null;let n=1;for(;n<e.length&&e[n]===t;)n++;if(n<3)return null;let r=e.slice(n).trimStart(),i=r.match(/^([^\s{]+)/),a=i?i[1]:``,o=r.match(/\{([^}]+)\}/),s=o?o[1]:null;return{fenceChar:t,fenceLength:n,lang:a,attrs:s,infoString:r}}function E(e){if(e[0]!==`[`)return null;let t=e.indexOf(`]`,1);if(t===-1)return null;let n=e.slice(1,t).trim();if(!n)return null;let r=t+1;for(;r<e.length&&(e[r]===` `||e[r]===` `);)r++;if(r>=e.length||e[r]!==`<`)return null;let i=e.indexOf(`>`,r+1);if(i===-1)return null;let a=e.slice(r+1,i).trim();return a?{prefix:n,iri:a}:null}function D(e){if(e[0]!==`#`)return null;let t=1;for(;t<e.length&&t<6&&e[t]===`#`;)t++;if(t>=e.length||e[t]!==` `&&e[t]!==` `)return null;let n=t;for(;n<e.length&&(e[n]===` `||e[n]===` `);)n++;let r=e.slice(n),i=r.match(/\s*\{([^}]+)\}\s*$/),a=r,o=null;return i&&(o=i[1],a=r.slice(0,-i[0].length).trim()),{depth:t,content:a,attrs:o}}function ee(e){let t=0;for(;t<e.length&&(e[t]===` `||e[t]===` `);)t++;let n=t;if(t>=e.length)return null;let r=e[t],i,a=t+1;if(r===`-`||r===`*`||r===`+`)i=r;else if(r>=`0`&&r<=`9`){let n=t+1;for(;n<e.length&&e[n]>=`0`&&e[n]<=`9`;)n++;if(n>=e.length||e[n]!==`.`)return null;i=e.slice(t,n+1),a=n+1}else return null;if(a>=e.length||e[a]!==` `&&e[a]!==` `)return null;for(;a<e.length&&(e[a]===` `||e[a]===` `);)a++;let o=e.slice(a),s=null,c=o.match(/\s*\{([^}]+)\}\s*$/);return c&&(s=c[1],o=o.slice(0,-c[0].length).trim()),{indent:n,marker:i,content:o,attrs:s}}function O(e){if(e[0]!==`>`||e.length>1&&e[1]!==` `&&e[1]!==` `)return null;let t=1;for(;t<e.length&&(e[t]===` `||e[t]===` `);)t++;let n=e.slice(t),r=null,i=n.match(/\s*\{([^}]+)\}\s*$/);return i&&(r=i[1],n=n.slice(0,-i[0].length).trim()),{content:n,attrs:r}}function k(e){let t=0;for(;t<e.length&&(e[t]===` `||e[t]===` `);)t++;if(t>=e.length||e[t]!==`{`||(t++,t>=e.length||e[t]!==`=`))return null;t++;let n=t,r=1;for(;t<e.length&&r>0;)e[t]===`{`&&r++,e[t]===`}`&&r--,r>0&&t++;if(r>0)return null;let i=e.slice(n,t).trim();for(t++;t<e.length&&(e[t]===` `||e[t]===` `);)t++;return t<e.length?null:{content:i}}function A(e,t,n=`[`,r=`]`){let i=1,a=t+1;for(;a<e.length&&i>0;)e[a]===n&&i++,e[a]===r&&i--,i>0&&a++;return i===0?a:null}function j(e,t){let n=t;for(;n<e.length&&(e[n]===` `||e[n]===` `);)n++;if(n>=e.length||e[n]!==`{`)return null;let r=e.indexOf(`}`,n+1);return r===-1?null:{attrs:e.slice(n+1,r),endPos:r+1}}function M(e,t){if(e[t]!==`<`)return null;let n=e.indexOf(`>`,t+1);if(n===-1)return null;let r=e.slice(t+1,n).trim();return r.match(/^[a-zA-Z][a-zA-Z0-9+\-.]*:/)?{url:r,endPos:n+1}:null}function N(e,t){let n=e[t];if(n!==`*`&&n!==`_`)return null;let r=1;for(;t+r<e.length&&e[t+r]===n;)r++;if(r>2)return null;let i=r===1?`emphasis`:`strong`,a=n.repeat(r),o=t+r,s=o;for(;s<e.length;){if(e.slice(s,s+r)===a){if(s+r<e.length&&e[s+r]===n){s++;continue}let t=e.slice(o,s),a=s+r,c=j(e,a),l=c?c.endPos:a;return{type:i,content:t,attrs:c?.attrs||null,endPos:l,contentEnd:a}}s++}return null}function P(e,t){if(e[t]!=="`")return null;let n=1;for(;t+n<e.length&&e[t+n]==="`";)n++;if(n>2)return null;let r="`".repeat(n),i=t+n,a=i;for(;a<e.length;){if(e.slice(a,a+n)===r){let t=e.slice(i,a),r=a+n,o=j(e,r),s=o?o.endPos:r;return{type:`code`,content:t,attrs:o?.attrs||null,endPos:s,contentEnd:r}}a++}return null}function te(e,t){if(e[t]!==`[`)return null;let n=A(e,t,`[`,`]`);if(!n)return null;let r=e.slice(t+1,n),i=n+1,a=null;if(i<e.length&&e[i]===`(`){let t=e.indexOf(`)`,i+1);t!==-1&&(a=e.slice(i+1,t),i=t+1)}else if(i<e.length&&e[i]===`<`){let t=e.indexOf(`>`,i+1);if(t!==-1){let n=e.slice(i+1,t).trim();n.match(/^[a-zA-Z][a-zA-Z0-9+\-.]*:/)&&(a=n,i=t+1)}}let o=j(e,i),s=o?o.endPos:i;return{type:a?`link`:`span`,text:r,url:a,attrs:o?.attrs||null,endPos:s,bracketEnd:n+1}}function F(e,t=0){let n=[],r=e.length,i=0;for(;i<r;){let r=e[i],a=null;switch(r){case`<`:if(a=M(e,i),a){let t=j(e,a.endPos);t&&(a.attrs=t.attrs,a.endPos=t.endPos),a.type=`link`,a.text=a.url}break;case`[`:a=te(e,i);break;case`*`:case`_`:a=N(e,i);break;case"`":a=P(e,i);break}if(!a){i++;continue}let o=a.url,s=a.type,c=a.attrs;if(o?.startsWith(`=`)||s===`link`&&!c&&!o){i=a.endPos;continue}let l=t+i,u=t+a.endPos,d=t+(a.contentEnd||a.bracketEnd||a.endPos),f={type:s,text:a.content===void 0?a.text===void 0?o:a.text:a.content,range:[l,u],valueRange:[l+ +(s===`url`),d-+(s===`url`)],attrs:c,url:o,pos:a.endPos};c&&(f.attrsRange=[d,u]),n.push(f),i=a.endPos}return n}var I=new Map;function ne(e){return I.has(e)||I.set(e,RegExp(`^(${e}{3,})`)),I.get(e)}function re(e,t,n,r,i){let a=r+(r<e.length&&e[r]===` `?1:e.slice(r).match(/^\s+/)?.[0]?.length||0);return{valueRange:[n+a,n+a+i],attrsRange:L(e,t,n)}}function L(e,t,n){if(!t)return null;let r=e.lastIndexOf(t);return r>=0?[n+r,n+r+t.length]:null}function R(e,t,n,r=null,i=null,a=null,o={}){let s={type:e,range:t,text:n,attrs:r,attrsRange:i,valueRange:a,...o};return Object.defineProperty(s,`_carriers`,{enumerable:!1,writable:!0,value:null}),s}function ie(e,t,n,r,i){let a=i[4]||null,o=re(t,a,n,i[1].length+(i[2]?i[2].length:0),i[3].length);return R(e,[n,r-1],i[3].trim(),a,o.attrsRange,o.valueRange,{indent:i[1].length})}var z={},ae=Object.freeze({predicates:[],types:[],subject:null});function oe(e){if(!e)return ae;let t=z[e];return t||(t=Object.freeze(x(e)),z[e]=t),t}function se(e){if(!e.text)return``;let t=e.text;e.attrsRange&&(t=t.substring(0,e.attrsRange[0]-(e.range?.[0]||0))+t.substring(e.attrsRange[1]-(e.range?.[0]||0)));let n=(e._carriers||[]).filter(e=>e.attrsRange).map(t=>t.attrsRange.map(t=>t-(e.range?.[0]||0))).filter(([e,n])=>e>=0&&n<=t.length).sort((e,t)=>t[0]-e[0]);for(let[e,r]of n)t=t.substring(0,e)+t.substring(r);switch(e.type){case`heading`:return t.replace(/^#+\s*/,``).trim();case`list`:return t.replace(/^[-*+]\s*/,``).trim();case`blockquote`:return t.replace(/^>\s*/,``).trim();default:return t.trim()}}function ce(e,t,n,r=null){return{blockId:e.id,range:e.range,carrierType:e.carrierType,subject:t.value,predicate:n.value,context:e.context,polarity:r?.remove?`-`:`+`,value:e.text||``}}function le(e,t,n){if(!t)return null;let r=t.value,i=r.indexOf(`#`),a=i>-1?r.slice(0,i):r;return n.namedNode(a+`#`+e)}function ue(e,t){return e.subject?e.subject===`RESET`?(t.currentSubject=null,null):e.subject.startsWith(`=#`)?le(e.subject.substring(2),t.currentSubject,t.df):t.df.namedNode(v(e.subject,t.ctx)):null}function de(e,t){return e.object?e.object.startsWith(`#`)?le(e.object.substring(1),t.currentSubject,t.df):t.df.namedNode(v(e.object,t.ctx)):null}function fe(e){return e?.termType===`Literal`}function pe(e){return e?.termType===`NamedNode`}function me(e){return e?.value===`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`}function he(e,t){if(!e)return null;let n=y(e,t);return n.includes(`:`)?n.split(`:`)[0]:null}function ge(e,t){let n=new Set;for(let r of e.values())for(let e of r){let r=he(e.subject.value,t);r&&n.add(r);let i=he(e.predicate.value,t);if(i&&n.add(i),pe(e.object)){let r=he(e.object.value,t);r&&n.add(r)}if(e.object.datatype&&e.object.datatype.value){let r=he(e.object.datatype.value,t);r&&n.add(r)}}return n}function _e(e,t,n,r,i=[]){let a=r(e,t);t.currentBlock=a,t.blockStack.push(a.id),i.forEach(n=>n(e,t)),n(e,t,e.type),t.blockStack.pop(),t.currentBlock=t.blockStack.length>0?t.origin.blocks.get(t.blockStack[t.blockStack.length-1]):null}function B(e){return e.sort((e,t)=>e.predicate.value.localeCompare(t.predicate.value))}var ve=(e,t)=>`[${e}] <${t}>\n`;function V(e,t){let n=y(e.predicate.value,t);e.object.language?n+=` @${e.object.language}`:e.object.datatype.value!==`http://www.w3.org/2001/XMLSchema#string`&&(n+=` ^^${y(e.object.datatype.value,t)}`);let r=e.object.value||e.object,i=typeof r==`string`?r:String(r),a=e.object.datatype?.value||``;return i.includes(`
33
- `)?`~~~ {${n}}\n${i}\n~~~\n\n`:a.includes(`integer`)||a.includes(`decimal`)||a.includes(`double`)||a.includes(`float`)?`\`${i}\` {${n}}\n`:a.includes(`date`)||a.includes(`time`)?`*${i}* {${n}}\n`:a.includes(`boolean`)?`**${i}** {${n}}\n`:`[${i}] {${n}}\n`}var ye=(e,t,n=null)=>{let r=y(e.object.value,t),i=y(e.predicate.value,t);return`[${n&&n.has(e.object.value)?n.get(e.object.value):r}] {+${r} ?${i}}\n`};function be(e){let t=[],n=[],r=[];for(let i of e)me(i.predicate)?t.push(i):fe(i.object)?n.push(i):pe(i.object)&&r.push(i);return{types:t,literals:n,objects:r}}function H(e,t={}){let n=typeof e==`object`&&!!e&&`text`in e,i=n?e.text:e,a=n?{context:e.context,dataFactory:e.dataFactory,graph:e.graph}:t,o={ctx:{...r,...a.context||{}},df:a.dataFactory||m,graph:a.graph?m.namedNode(a.graph):m.defaultGraph(),quads:[],quadBuffer:new Map,removeSet:new Set,origin:{quadIndex:new Map,blocks:new Map,documentStructure:[]},currentSubject:null,primarySubject:null,tokens:null,currentTokenIndex:-1,statements:[],statementCandidates:new Map,currentBlock:null,blockStack:[]},s=Se(i);o.tokens=s.tokens;for(let e=0;e<o.tokens.length;e++){let t=o.tokens[e];if(o.currentTokenIndex=e,t.type===`prefix`){let e=t.iri;if(t.iri.includes(`:`)){let n=t.iri.indexOf(`:`),r=t.iri.substring(0,n),i=t.iri.substring(n+1);o.ctx[r]&&r!==`@vocab`&&(e=o.ctx[r]+i)}o.ctx[t.prefix]=e;continue}Le[t.type]?.(t,o)}let c=new Set;for(let e of o.quads)c.add(S(e.subject,e.predicate,e.object));let l=[];for(let e of o.removeSet){let t=S(e.subject,e.predicate,e.object);c.has(t)||l.push(e)}return{quads:o.quads,remove:l,statements:o.statements,origin:o.origin,context:o.ctx,primarySubject:o.primarySubject,md:s.md}}function xe(e){return e.type===`code`?[]:e._carriers||=Ce(e.text,e.range[0])}function Se(e){let t=[],n=[],r=e.split(`
33
+ `)?`~~~ {${n}}\n${i}\n~~~\n\n`:a.includes(`integer`)||a.includes(`decimal`)||a.includes(`double`)||a.includes(`float`)?`\`${i}\` {${n}}\n`:a.includes(`date`)||a.includes(`time`)?`*${i}* {${n}}\n`:a.includes(`boolean`)?`**${i}** {${n}}\n`:`[${i}] {${n}}\n`}var ye=(e,t,n=null)=>{let r=y(e.object.value,t),i=y(e.predicate.value,t);return`[${n&&n.has(e.object.value)?n.get(e.object.value):r}] {+${r} ?${i}}\n`};function be(e){let t=[],n=[],r=[];for(let i of e)me(i.predicate)?t.push(i):fe(i.object)?n.push(i):pe(i.object)&&r.push(i);return{types:t,literals:n,objects:r}}function H(e,t={}){let n=typeof e==`object`&&!!e&&`text`in e,i=n?e.text:e,a=n?{context:e.context,dataFactory:e.dataFactory,graph:e.graph}:t,o={ctx:{...r,...a.context||{}},df:a.dataFactory||m,graph:a.graph?m.namedNode(a.graph):m.defaultGraph(),quads:[],quadBuffer:new Map,removeSet:new Set,origin:{quadIndex:new Map,blocks:new Map,documentStructure:[]},currentSubject:null,primarySubject:null,primaryType:null,primaryLabel:null,tokens:null,currentTokenIndex:-1,statements:[],statementCandidates:new Map,currentBlock:null,blockStack:[]},s=Se(i);o.tokens=s.tokens;for(let e=0;e<o.tokens.length;e++){let t=o.tokens[e];if(o.currentTokenIndex=e,t.type===`prefix`){let e=t.iri;if(t.iri.includes(`:`)){let n=t.iri.indexOf(`:`),r=t.iri.substring(0,n),i=t.iri.substring(n+1);o.ctx[r]&&r!==`@vocab`&&(e=o.ctx[r]+i)}o.ctx[t.prefix]=e;continue}Le[t.type]?.(t,o)}let c=new Set;for(let e of o.quads)c.add(S(e.subject,e.predicate,e.object));let l=[];for(let e of o.removeSet){let t=S(e.subject,e.predicate,e.object);c.has(t)||l.push(e)}let u={subject:o.primarySubject,type:o.primaryType,label:o.primaryLabel};return{quads:o.quads,remove:l,statements:o.statements,origin:o.origin,context:o.ctx,primarySubject:o.primarySubject,primary:u,md:s.md}}function xe(e){return e.type===`code`?[]:e._carriers||=Ce(e.text,e.range[0])}function Se(e){let t=[],n=[],r=e.split(`
34
34
  `),i=0,a=null,o=[{type:`fence`,test:e=>T(e.trim()),process:s},{type:`content`,test:()=>a,process:e=>a.content.push(e)},{type:`prefix`,test:e=>E(e),process:c},{type:`standalone`,test:e=>k(e),process:p},{type:`heading`,test:e=>D(e),process:l},{type:`list`,test:e=>ee(e),process:u},{type:`blockquote`,test:e=>O(e),process:d},{type:`para`,test:e=>e.trim(),process:f}];function s(e,r,i){let o=e.trim();if(a){let i=a.fence[0],s=i.repeat(a.fence.length),c=o.match(ne(i));if(c&&c[1]===s){let i=a.valueRangeStart,o=Math.max(i,r-1);t.push({type:`code`,range:[a.start,r],text:a.content.join(`
35
35
  `),lang:a.lang,attrs:a.attrs,attrsRange:a.attrsRange,valueRange:[i,o]});for(let e of a.content)n.push(e);a=null;let s=e.replace(/\r?\n.*$/,``);n.push(s)}}else{let t=T(o);if(!t)return!1;let i=t.attrs,s=i?e.indexOf(i):-1,c=r+e.length+1;a={fence:t.fenceChar.repeat(t.fenceLength),start:r,content:[],lang:t.lang,attrs:i,attrsRange:i&&s>=0?[r+s,r+s+i.length]:null,valueRangeStart:c};let l=e.replace(/\s*\{[^}]+\}\s*$/,``);n.push(l)}return!0}function c(e,n,r){let i=E(e);return t.push({type:`prefix`,prefix:i.prefix,iri:i.iri}),!0}function l(e,r,i){let a=D(e),o=a.attrs,s=a.depth,c=re(e,o,r,s,a.content.length);t.push(R(`heading`,[r,i-1],a.content,o,c.attrsRange,c.valueRange,{depth:a.depth}));let l=`${`#`.repeat(a.depth)} ${a.content}`;return n.push(l),!0}function u(e,r,i){let a=ee(e),o=` `.repeat(a.indent),s=[e,o,a.marker,a.content,a.attrs];t.push(ie(`list`,e,r,i,s));let c=`${o}${a.marker} ${a.content}`;return n.push(c),!0}function d(e,r,i){let a=O(e),o=a.attrs,s=e.startsWith(`> `)?2:e.indexOf(`>`)+1,c=s+a.content.length;t.push(R(`blockquote`,[r,i-1],a.content,o,L(e,o,r),[r+s,r+c]));let l=`> ${a.content}`;return n.push(l),!0}function f(e,r,i){t.push(R(`para`,[r,i-1],e.trim()));let a=e,o=F(a,0);for(let e of o)if(e.attrs&&(e.type===`emphasis`||e.type===`code`)){let t=a.substring(0,e.range[0]),n=a.substring(e.range[1]);a=t+(e.text||``)+n}return a=a.replace(/\[([^\]]+)\]\s*\{[^}]+\}/g,`$1`),a=a.replace(/\s*\{[^}]+\}\s*/g,` `),a=a.replace(/\s+/g,` `).trim(),n.push(a),!0}function p(e,n,r){return t.push({type:`standalone`,text:e.trim(),range:[n,r-1]}),!0}for(let e=0;e<r.length;e++){let t=r[e],n=i;i+=t.length+1;for(let e of o)if(e.test(t)&&e.process(t,n,i))break}return{tokens:t,md:n.join(`
36
- `)}}function Ce(e,t=0){return F(e,t)}function we(e,t){let n=e._blockId||g(`${e.type}:${e.range?.[0]}:${e.range?.[1]}`);e._blockId=n;let r=xe(e),i=se(e),a={id:n,type:e.type,range:e.range,text:i,subject:null,types:[],predicates:[],carriers:[],listLevel:e.indent||0,parentBlockId:t.blockStack.length>0?t.blockStack[t.blockStack.length-1]:null,quadKeys:[]};for(let e of r){let t={type:e.type,range:e.range,text:e.text,subject:null,predicates:[],sem:null};if(e.attrs){let n=oe(e.attrs);t.sem=n,t.predicates=n.predicates||[],t.subject=n.subject,t.types=n.types||[]}a.carriers.push(t)}return t.origin.blocks.set(n,a),t.origin.documentStructure.push(a),a}function Te(e,t,n,r){if(t.subject&&t.subject!==`RESET`){let n=ue(t,r);n&&(e.subject=n.value)}if(t.types&&t.types.length>0&&t.types.forEach(t=>{let n=v(typeof t==`string`?t:t.iri,r.ctx);e.types.includes(n)||e.types.push(n)}),t.predicates&&t.predicates.length>0&&t.predicates.forEach(t=>{let n={iri:v(t.iri,r.ctx),form:t.form||``,object:null};e.predicates.push(n)}),n){let t={type:n.type,range:n.range,text:n.text,subject:null,predicates:[]};if(n.attrs){let e=oe(n.attrs);t.sem=e,t.predicates=e.predicates||[],t.subject=e.subject,t.types=e.types||[]}e.carriers.push(t)}}function Ee(e,t,n,r={}){let{preserveGlobalSubject:i=!1,implicitSubject:a=null}=r;if(t.subject===`RESET`){n.currentSubject=null;return}let o=n.currentSubject,s=ue(t,n),c=de(t,n);s&&!n.primarySubject&&!t.subject.startsWith(`=#`)&&(n.primarySubject=s.value),s&&!i&&!a&&(n.currentSubject=s);let l=i?s||o:a||n.currentSubject;if(!l)return;let u=De(l.value,t.types,t.predicates,e.range,e.attrsRange||null,e.valueRange||null,e.type||null,n.ctx,e.text),d=w(e.text,t.datatype,t.language,n.ctx,n.df),f=e.url?n.df.namedNode(v(e.url,n.ctx)):null,p=s||f;n.currentBlock&&Te(n.currentBlock,t,e,n),je(t,s,c,f,l,u,n,e),Ne(t,s,o,c,p,l,d,u,n,e)}function De(e,t,n,r,i,a,o,s,c){let l={subject:e,types:t.map(e=>v(typeof e==`string`?e:e.iri,s)),predicates:n.map(e=>({iri:v(e.iri,s),form:e.form}))};return{id:g([e,o||`unknown`,l.types.join(`,`),l.predicates.map(e=>`${e.form}${e.iri}`).join(`,`)].join(`|`)),range:{start:r[0],end:r[1]},carrierType:o||null,subject:e,types:l.types,predicates:l.predicates,context:s,text:c||``}}function Oe(e,t,n,r,i,a,o,s,c,l=null,u=null,d=null,f=null){if(!a||!o||!s)return;let p=c.quad(a,o,s);if(l?.remove){let i=S(p.subject,p.predicate,p.object);if(t.has(i)){t.delete(i);let n=e.findIndex(e=>e.subject.value===p.subject.value&&e.predicate.value===p.predicate.value&&e.object.value===p.object.value);n!==-1&&e.splice(n,1),r.delete(i)}else n.add(p)}else{let n=S(p.subject,p.predicate,p.object);t.set(n,p),e.push(p),ke(p,c,l,u,d);let s=ce(i,a,o,l);r.set(n,s),f.currentBlock&&i.id===f.currentBlock.id&&(f.currentBlock.quadKeys||(f.currentBlock.quadKeys=[]),f.currentBlock.quadKeys.push(n))}}function ke(e,t,n,r=null,i=null){if(!r||!i)return;let a=e.predicate.value;if(a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`&&a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#subject`&&a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate`&&a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#object`)return;if(a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`&&e.object.value===`http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement`){i.set(e.subject.value,{spo:{}});return}let o=i.get(e.subject.value);if(o&&(a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#subject`?o.spo.subject=e.object:a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate`?o.spo.predicate=e.object:a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#object`&&(o.spo.object=e.object,o.objectQuad=e),o.spo.subject&&o.spo.predicate&&o.spo.object)){let n=t.quad(o.spo.subject,o.spo.predicate,o.spo.object);r.push(n),i.delete(e.subject.value)}}var Ae=(e,t,n,r,i=null)=>{let a=v(e,n.ctx),o=typeof i==`object`?i:{entryIndex:i,remove:!1};Oe(n.quads,n.quadBuffer,n.removeSet,n.origin.quadIndex,r,t,n.df.namedNode(v(`rdf:type`,n.ctx)),n.df.namedNode(a),n.df,{kind:`type`,token:`.${e}`,expandedType:a,entryIndex:o.entryIndex,remove:o.remove},n.statements,n.statementCandidates,n)};function je(e,t,n,r,i,a,o,s){e.types.forEach(e=>{Ae(typeof e==`string`?e:e.iri,t||n||r||i,o,a,typeof e==`string`?{entryIndex:null,remove:!1}:e)})}var Me=(e,t,n,r,i,a,o,s)=>{if(e.form===``&&t?.type===`link`&&t?.url&&t.text===t.url)return null;switch(e.form){case``:return n?{subject:i||o,object:s}:t?.type===`link`&&t?.url&&t.text!==t.url?{subject:a,object:s}:{subject:i||o,object:s};case`?`:return{subject:n?r:o,object:i||a};case`!`:return{subject:i||a,object:n?r:o};default:return null}};function Ne(e,t,n,r,i,a,o,s,c,l){e.predicates.forEach(e=>{let u=Me(e,l,t,n,r,i,a,o);if(u){let t=c.df.namedNode(v(e.iri,c.ctx));Oe(c.quads,c.quadBuffer,c.removeSet,c.origin.quadIndex,s,u.subject,t,u.object,c.df,{kind:`pred`,token:`${e.form}${e.iri}`,form:e.form,expandedPredicate:t.value,entryIndex:e.entryIndex,remove:e.remove||!1},c.statements,c.statementCandidates,c)}})}function Pe(e,t,n,r={}){Ee(e,t,n,r)}function Fe(e,t,n){if(e.attrs){let r=oe(e.attrs);Pe({type:n,text:e.text,range:e.range,attrsRange:e.attrsRange||null,valueRange:e.valueRange||null},r,t)}xe(e).forEach(e=>{e.attrs&&Pe(e,oe(e.attrs),t)})}function Ie(e,t){let n=k(e.text);if(!n)return;let r=oe(`{=${n.content}}`),i=e.range[0]+e.text.indexOf(`{=`);Pe({type:`standalone`,text:``,range:e.range,attrsRange:[i,i+(n.content?n.content.length:0)],valueRange:null},r,t)}var Le={heading:(e,t)=>_e(e,t,Fe,we),code:(e,t)=>_e(e,t,Fe,we),blockquote:(e,t)=>_e(e,t,Fe,we),para:(e,t)=>_e(e,t,Fe,we,[Ie]),list:(e,t)=>_e(e,t,Fe,we),standalone:(e,t)=>Ie(e,t)};function Re(e){return C(e)}function ze(e,t,n){return typeof e==`string`?H({text:e,...t,context:{...n,...t.context}}):e}function U(e,t={}){let n=new Map,i=new Set,a=[],o=new Map,s=[],c=new Map,l=[];for(let u=0;u<e.length;u++){let d=e[u],f=ze(d,t,{...r,...t.context});if(f.context)for(let[e,t]of Object.entries(f.context))!c.has(e)&&!r[e]&&c.set(e,t);let p={index:u,input:typeof d==`string`?`string`:`ParseResult`,origin:f.origin,context:f.context,statementsCount:f.statements?.length||0};a.push(p),f.statements&&f.statements.length>0&&s.push(...f.statements),f.primarySubject&&l.push(f.primarySubject);for(let e of f.quads){let t=Re(e);n.set(t,e);let r=f.origin.quadIndex.get(t);o.set(t,{...r||{},documentIndex:u,polarity:`+`})}for(let e of f.remove){let t=Re(e);n.has(t)?n.delete(t):i.add(e);let r=f.origin.quadIndex.get(t);o.set(t,{...r||{},documentIndex:u,polarity:`-`})}}let u=Array.from(n.values()),d=Array.from(i),f={documents:a,quadIndex:o},p={...r,...t.context,...Object.fromEntries(c)},m=new Set(u.map(Re)),h=new Set(d.map(Re));return{quads:u.filter(e=>!h.has(Re(e))),remove:d.filter(e=>!m.has(Re(e))),statements:s,origin:f,context:p,primarySubjects:l}}var Be=new Map,Ve=1e3;function He(e,t){let n=`${e}|${JSON.stringify(t)}`;if(Be.has(n))return Be.get(n);let r=y(e,t);return Be.size>=Ve&&Array.from(Be.keys()).slice(0,Math.floor(Ve/2)).forEach(e=>Be.delete(e)),Be.set(n,r),r}function Ue(e,t={}){if(!e)return e;for(let[n,r]of Object.entries(t))if(e.startsWith(r)||e.startsWith(r.slice(0,-1)))return e.substring(r.length);for(let t of[`#`,`/`,`:`]){let n=e.lastIndexOf(t);if(n!==-1&&n<e.length-1)return e.substring(n+1)}return e}function We({quads:e,context:t={},primarySubject:n=null}){let i=Object.assign({},r,t),a=Ge(e),o=Ke(a),s=n;!s&&a.length>0&&(s=a[0].subject.value);let{text:c}=qe(o,i,s);return{text:c,context:i}}function Ge(e){return!e||e.length===0?[]:e.map(e=>e.subject.termType&&e.predicate.termType&&e.object.termType?e:{subject:m.fromTerm(e.subject),predicate:m.fromTerm(e.predicate),object:m.fromTerm(e.object)}).sort((e,t)=>{let n=e.subject.value.localeCompare(t.subject.value);if(n!==0)return n;let r=e.predicate.value.localeCompare(t.predicate.value);if(r!==0)return r;let i=(fe(e.object),e.object.value),a=(fe(t.object),t.object.value);return i.localeCompare(a)})}function Ke(e){let t=new Map;for(let n of e){let e=n.subject.value,r=t.get(e);r?r.push(n):t.set(e,[n])}return t}function qe(e,t,n=null){let i=[],a=ge(e,t),o=Je(e),s=Object.entries(t).sort(([e],[t])=>e.localeCompare(t));for(let[e,t]of s)e!==`@vocab`&&!e.startsWith(`@`)&&!r[e]&&a.has(e)&&i.push(ve(e,t));s.length>0&&i.push(`
36
+ `)}}function Ce(e,t=0){return F(e,t)}function we(e,t){let n=e._blockId||g(`${e.type}:${e.range?.[0]}:${e.range?.[1]}`);e._blockId=n;let r=xe(e),i=se(e),a={id:n,type:e.type,range:e.range,text:i,subject:null,types:[],predicates:[],carriers:[],listLevel:e.indent||0,parentBlockId:t.blockStack.length>0?t.blockStack[t.blockStack.length-1]:null,quadKeys:[]};for(let e of r){let t={type:e.type,range:e.range,text:e.text,subject:null,predicates:[],sem:null};if(e.attrs){let n=oe(e.attrs);t.sem=n,t.predicates=n.predicates||[],t.subject=n.subject,t.types=n.types||[]}a.carriers.push(t)}return t.origin.blocks.set(n,a),t.origin.documentStructure.push(a),a}function Te(e,t,n,r){if(t.subject&&t.subject!==`RESET`){let n=ue(t,r);n&&(e.subject=n.value)}if(t.types&&t.types.length>0&&t.types.forEach(t=>{let n=v(typeof t==`string`?t:t.iri,r.ctx);e.types.includes(n)||e.types.push(n)}),t.predicates&&t.predicates.length>0&&t.predicates.forEach(t=>{let n={iri:v(t.iri,r.ctx),form:t.form||``,object:null};e.predicates.push(n)}),n){let t={type:n.type,range:n.range,text:n.text,subject:null,predicates:[]};if(n.attrs){let e=oe(n.attrs);t.sem=e,t.predicates=e.predicates||[],t.subject=e.subject,t.types=e.types||[]}e.carriers.push(t)}}function Ee(e,t,n,r={}){let{preserveGlobalSubject:i=!1,implicitSubject:a=null}=r;if(t.subject===`RESET`){n.currentSubject=null;return}let o=n.currentSubject,s=ue(t,n),c=de(t,n);s&&!n.primarySubject&&!t.subject.startsWith(`=#`)&&(n.primarySubject=s.value),s&&!i&&!a&&(n.currentSubject=s);let l=i?s||o:a||n.currentSubject;if(!l)return;let u=De(l.value,t.types,t.predicates,e.range,e.attrsRange||null,e.valueRange||null,e.type||null,n.ctx,e.text),d=w(e.text,t.datatype,t.language,n.ctx,n.df),f=e.url?n.df.namedNode(v(e.url,n.ctx)):null,p=s||f;n.currentBlock&&Te(n.currentBlock,t,e,n),je(t,s,c,f,l,u,n,e),Ne(t,s,o,c,p,l,d,u,n,e)}function De(e,t,n,r,i,a,o,s,c){let l={subject:e,types:t.map(e=>v(typeof e==`string`?e:e.iri,s)),predicates:n.map(e=>({iri:v(e.iri,s),form:e.form}))};return{id:g([e,o||`unknown`,l.types.join(`,`),l.predicates.map(e=>`${e.form}${e.iri}`).join(`,`)].join(`|`)),range:{start:r[0],end:r[1]},carrierType:o||null,subject:e,types:l.types,predicates:l.predicates,context:s,text:c||``}}function Oe(e,t,n,r,i,a,o,s,c,l=null,u=null,d=null,f=null){if(!a||!o||!s)return;let p=c.quad(a,o,s);if(l?.remove){let i=S(p.subject,p.predicate,p.object);if(t.has(i)){t.delete(i);let n=e.findIndex(e=>e.subject.value===p.subject.value&&e.predicate.value===p.predicate.value&&e.object.value===p.object.value);n!==-1&&e.splice(n,1),r.delete(i)}else n.add(p)}else{let n=S(p.subject,p.predicate,p.object);t.set(n,p),e.push(p),f&&(!f.primaryType&&o.value===`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`&&(f.primaryType=s.value),!f.primaryLabel&&o.value===`http://www.w3.org/2000/01/rdf-schema#label`&&s.termType===`Literal`&&(f.primaryLabel=s.value)),ke(p,c,l,u,d);let m=ce(i,a,o,l);r.set(n,m),f.currentBlock&&i.id===f.currentBlock.id&&(f.currentBlock.quadKeys||(f.currentBlock.quadKeys=[]),f.currentBlock.quadKeys.push(n))}}function ke(e,t,n,r=null,i=null){if(!r||!i)return;let a=e.predicate.value;if(a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`&&a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#subject`&&a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate`&&a!==`http://www.w3.org/1999/02/22-rdf-syntax-ns#object`)return;if(a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#type`&&e.object.value===`http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement`){i.set(e.subject.value,{spo:{}});return}let o=i.get(e.subject.value);if(o&&(a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#subject`?o.spo.subject=e.object:a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate`?o.spo.predicate=e.object:a===`http://www.w3.org/1999/02/22-rdf-syntax-ns#object`&&(o.spo.object=e.object,o.objectQuad=e),o.spo.subject&&o.spo.predicate&&o.spo.object)){let n=t.quad(o.spo.subject,o.spo.predicate,o.spo.object);r.push(n),i.delete(e.subject.value)}}var Ae=(e,t,n,r,i=null)=>{let a=v(e,n.ctx),o=typeof i==`object`?i:{entryIndex:i,remove:!1};Oe(n.quads,n.quadBuffer,n.removeSet,n.origin.quadIndex,r,t,n.df.namedNode(v(`rdf:type`,n.ctx)),n.df.namedNode(a),n.df,{kind:`type`,token:`.${e}`,expandedType:a,entryIndex:o.entryIndex,remove:o.remove},n.statements,n.statementCandidates,n)};function je(e,t,n,r,i,a,o,s){e.types.forEach(e=>{Ae(typeof e==`string`?e:e.iri,t||n||r||i,o,a,typeof e==`string`?{entryIndex:null,remove:!1}:e)})}var Me=(e,t,n,r,i,a,o,s)=>{if(e.form===``&&t?.type===`link`&&t?.url&&t.text===t.url)return null;switch(e.form){case``:return n?{subject:i||o,object:s}:t?.type===`link`&&t?.url&&t.text!==t.url?{subject:a,object:s}:{subject:i||o,object:s};case`?`:return{subject:n?r:o,object:i||a};case`!`:return{subject:i||a,object:n?r:o};default:return null}};function Ne(e,t,n,r,i,a,o,s,c,l){e.predicates.forEach(e=>{let u=Me(e,l,t,n,r,i,a,o);if(u){let t=c.df.namedNode(v(e.iri,c.ctx));Oe(c.quads,c.quadBuffer,c.removeSet,c.origin.quadIndex,s,u.subject,t,u.object,c.df,{kind:`pred`,token:`${e.form}${e.iri}`,form:e.form,expandedPredicate:t.value,entryIndex:e.entryIndex,remove:e.remove||!1},c.statements,c.statementCandidates,c)}})}function Pe(e,t,n,r={}){Ee(e,t,n,r)}function Fe(e,t,n){if(e.attrs){let r=oe(e.attrs);Pe({type:n,text:e.text,range:e.range,attrsRange:e.attrsRange||null,valueRange:e.valueRange||null},r,t)}xe(e).forEach(e=>{e.attrs&&Pe(e,oe(e.attrs),t)})}function Ie(e,t){let n=k(e.text);if(!n)return;let r=oe(`{=${n.content}}`),i=e.range[0]+e.text.indexOf(`{=`);Pe({type:`standalone`,text:``,range:e.range,attrsRange:[i,i+(n.content?n.content.length:0)],valueRange:null},r,t)}var Le={heading:(e,t)=>_e(e,t,Fe,we),code:(e,t)=>_e(e,t,Fe,we),blockquote:(e,t)=>_e(e,t,Fe,we),para:(e,t)=>_e(e,t,Fe,we,[Ie]),list:(e,t)=>_e(e,t,Fe,we),standalone:(e,t)=>Ie(e,t)};function Re(e){return C(e)}function ze(e,t,n){return typeof e==`string`?H({text:e,...t,context:{...n,...t.context}}):e}function U(e,t={}){let n=new Map,i=new Set,a=[],o=new Map,s=[],c=new Map,l=[],u=[];for(let d=0;d<e.length;d++){let f=e[d],p=ze(f,t,{...r,...t.context});if(p.context)for(let[e,t]of Object.entries(p.context))!c.has(e)&&!r[e]&&c.set(e,t);let m={index:d,input:typeof f==`string`?`string`:`ParseResult`,origin:p.origin,context:p.context,statementsCount:p.statements?.length||0};a.push(m),p.statements&&p.statements.length>0&&s.push(...p.statements),p.primary&&(p.primary.subject||p.primary.type||p.primary.label)&&l.push(p.primary),p.primarySubject&&u.push(p.primarySubject);for(let e of p.quads){let t=Re(e);n.set(t,e);let r=p.origin.quadIndex.get(t);o.set(t,{...r||{},documentIndex:d,polarity:`+`})}for(let e of p.remove){let t=Re(e);n.has(t)?n.delete(t):i.add(e);let r=p.origin.quadIndex.get(t);o.set(t,{...r||{},documentIndex:d,polarity:`-`})}}let d=Array.from(n.values()),f=Array.from(i),p={documents:a,quadIndex:o},m={...r,...t.context,...Object.fromEntries(c)},h=new Set(d.map(Re)),g=new Set(f.map(Re));return{quads:d.filter(e=>!g.has(Re(e))),remove:f.filter(e=>!h.has(Re(e))),statements:s,origin:p,context:m,primarySubjects:u,primary:l}}var Be=new Map,Ve=1e3;function He(e,t){let n=`${e}|${JSON.stringify(t)}`;if(Be.has(n))return Be.get(n);let r=y(e,t);return Be.size>=Ve&&Array.from(Be.keys()).slice(0,Math.floor(Ve/2)).forEach(e=>Be.delete(e)),Be.set(n,r),r}function Ue(e,t={}){if(!e)return e;for(let[n,r]of Object.entries(t))if(e.startsWith(r)||e.startsWith(r.slice(0,-1)))return e.substring(r.length);for(let t of[`#`,`/`,`:`]){let n=e.lastIndexOf(t);if(n!==-1&&n<e.length-1)return e.substring(n+1)}return e}function We({quads:e,context:t={},primarySubject:n=null}){let i=Object.assign({},r,t),a=Ge(e),o=Ke(a),s=n;!s&&a.length>0&&(s=a[0].subject.value);let{text:c}=qe(o,i,s);return{text:c,context:i}}function Ge(e){return!e||e.length===0?[]:e.map(e=>e.subject.termType&&e.predicate.termType&&e.object.termType?e:{subject:m.fromTerm(e.subject),predicate:m.fromTerm(e.predicate),object:m.fromTerm(e.object)}).sort((e,t)=>{let n=e.subject.value.localeCompare(t.subject.value);if(n!==0)return n;let r=e.predicate.value.localeCompare(t.predicate.value);if(r!==0)return r;let i=(fe(e.object),e.object.value),a=(fe(t.object),t.object.value);return i.localeCompare(a)})}function Ke(e){let t=new Map;for(let n of e){let e=n.subject.value,r=t.get(e);r?r.push(n):t.set(e,[n])}return t}function qe(e,t,n=null){let i=[],a=ge(e,t),o=Je(e),s=Object.entries(t).sort(([e],[t])=>e.localeCompare(t));for(let[e,t]of s)e!==`@vocab`&&!e.startsWith(`@`)&&!r[e]&&a.has(e)&&i.push(ve(e,t));s.length>0&&i.push(`
37
37
  `);let c=Array.from(e.keys()).sort(),l=n,u=l?[l,...c.filter(e=>e!==l)]:c;for(let n of u){let r=e.get(n);if(!r)continue;let a=He(n,t),{types:s,literals:c,objects:l}=be(r),u=o.has(n),d=u?o.get(n):Ue(n,t),f=s.length>0?s.map(e=>`.`+He(e.object.value,t)).sort().join(` `):``;u&&(f+=(f?` `:``)+`label`);let p=f?` `+f:``;i.push(`# ${d} {=${a}${p}}\n\n`);let m=u?o.get(n):null;B(c).forEach(e=>{e.predicate.value===`http://www.w3.org/2000/01/rdf-schema#label`&&e.object.value===m||i.push(V(e,t))}),B(l).forEach(e=>{i.push(ye(e,t,o))}),i.push(`
38
38
  `)}return{text:i.join(``)}}function Je(e){let t=new Map;for(let n of e.values())for(let e of n)e.predicate.value===`http://www.w3.org/2000/01/rdf-schema#label`&&e.object.termType===`Literal`&&t.set(e.subject.value,e.object.value);return t}function W(e){return String(e).replaceAll(`&`,`&amp;`).replaceAll(`<`,`&lt;`).replaceAll(`>`,`&gt;`).replaceAll(`"`,`&quot;`).replaceAll(`'`,`&#39;`)}var G={text:`#6b7280`,marker:`#f97316`,retraction:`#dc2626`,value:`#eab308`,annotation:`#4b5563`};function Ye(e,t){let n=t;for(;n<e.length&&e[n]!==`
39
39
  `&&e[n]!==`\r`;){if(e[n]===`{`)return!0;n++}return!1}function Xe(e,t){let n=e[t];if(n===`*`&&t+1<e.length&&e[t+1]===`*`){let n=e.indexOf(`**`,t+2);if(n!==-1){let r=e.slice(t+2,n),i=Ye(e,n+2)?` style="color: ${G.marker}"`:``;return{html:`<span${i}>**</span><strong>${W(r)}</strong><span${i}>**</span>`,nextIndex:n+2}}}if(n===`_`&&t+1<e.length&&e[t+1]===`_`){let n=e.indexOf(`__`,t+2);if(n!==-1){let r=e.slice(t+2,n),i=Ye(e,n+2)?` style="color: ${G.marker}"`:``;return{html:`<span${i}>__</span><strong>${W(r)}</strong><span${i}>__</span>`,nextIndex:n+2}}}if(n===`*`){let n=e.indexOf(`*`,t+1);if(n!==-1){let r=e.slice(t+1,n),i=Ye(e,n+1)?` style="color: ${G.marker}"`:``;return{html:`<span${i}>*</span><em>${W(r)}</em><span${i}>*</span>`,nextIndex:n+1}}}if(n===`_`){let n=e.indexOf(`_`,t+1);if(n!==-1){let r=e.slice(t+1,n),i=Ye(e,n+1)?` style="color: ${G.marker}"`:``;return{html:`<span${i}>_</span><em>${W(r)}</em><span${i}>_</span>`,nextIndex:n+1}}}if(n==="`"){let n=e.indexOf("`",t+1);if(n!==-1){let r=e.slice(t+1,n),i=Ye(e,n+1)?` style="color: ${G.marker}"`:``;return{html:`<span${i}>\`</span><code style="background-color:#7773">${W(r)}</code><span${i}>\`</span>`,nextIndex:n+1}}}return null}var Ze=new Map;function Qe(e){let t=0;for(let n=0;n<e.length;n++){let r=e.charCodeAt(n);t=(t<<5)-t+r,t&=t}return Math.abs(t)}function $e(e){if(Ze.has(e))return Ze.get(e);let t=Qe(e),n=`hsl(${t%360}, ${15+t%10}%, ${45+t%10}%)`;return Ze.set(e,n),n}function et(e){let t=new Map,n=/^\[(\w+)\]\s*<([^>]*)>\s*$/gm,i;for(;(i=n.exec(e))!==null;){let e=i[1],n=i[2];if(n.includes(`:`)){let e=n.indexOf(`:`);if(e>0){let r=n.slice(0,e),i=n.slice(e+1);t.has(r)&&(n=t.get(r)+i)}}t.set(e,n)}for(let[e,n]of Object.entries(r))e!==`@vocab`&&t.set(e,n);return t}function tt(e,t,n){let r=t?G.retraction:G.text,i=e.indexOf(`:`);if(i>0){let t=e.slice(0,i),r=e.slice(i+1);if(n.has(t)){let e=n.get(t),i=$e(e),a=e+r,o=$e(a);return`<span style="color: ${i}">${W(t)}</span><span style="color: ${G.marker}">:</span><span data-iri="${a}" style="color: ${o}">${W(r)}</span> `}}return e.startsWith(`http:`)||e.startsWith(`https:`)||e.startsWith(`tag:`)||e.startsWith(`urn:`)?`<span style="color: ${$e(e)}">${W(e)}</span> `:`<span style="color: ${r}">${W(e)}</span> `}function nt(e,t){let n=e.trim().split(/\s+/),r=``,i=!1;for(let e=0;e<n.length;e++){let a=n[e];if(!a)continue;if(a.startsWith(`=`)||a.startsWith(`+`)){let e=a[0],n=a.slice(1);r+=`<span style="color: ${G.marker}">${W(e)}</span>${tt(n,!1,t)}`;continue}if(a===`-`){i=!0,r+=`<span style="color: ${G.retraction}">${W(a)}</span> `;continue}if(a.startsWith(`-`)&&a.length>1){r+=`<span style="color: ${G.retraction}">${W(a)}</span> `;continue}let o=i;if(i=!1,a.startsWith(`?`)||a.startsWith(`!`)){let e=a[0],n=a.slice(1),i=o?G.retraction:G.marker;r+=`<span style="color: ${i}">${W(e)}</span>${tt(n,o,t)}`;continue}if(a.startsWith(`^^`)){let e=a.slice(2),n=o?G.retraction:G.marker;r+=`<span style="color: ${n}">${W(`^^`)}</span>${tt(e,o,t)}`;continue}if(a.startsWith(`@`)){let e=a.slice(1),n=o?G.retraction:G.marker;r+=`<span style="color: ${n}">${W(`@`)}</span>${tt(e,o,t)}`;continue}if(a.startsWith(`.`)){let e=a.slice(1),n=o?G.retraction:G.marker;r+=`<span style="color: ${n}">${W(`.`)}</span>${tt(e,o,t)}`;continue}r+=tt(a,o,t)}return r.trim()}function rt(e){let t=et(e),n=``,r=0;for(;r<e.length;){let i=e[r];if(i===`{`){let a=e.indexOf(`}`,r);if(a===-1){n+=W(i),r++;continue}let o=nt(e.slice(r+1,a),t);n+=`<span style="color: ${G.annotation}; opacity: 0.75">{</span>`,n+=o,n+=`<span style="color: ${G.annotation}; opacity: 0.75">}</span>`,r=a+1;continue}if(i===`[`){let a=e.indexOf(`]`,r);if(a===-1){n+=W(i),r++;continue}let o=e.slice(r+1,a),s=a+1;for(;s<e.length&&/\s/.test(e[s]);)s++;if(s<e.length&&e[s]===`<`){let i=e.indexOf(`>`,s);if(i!==-1){let a=e.slice(s+1,i),c=t.has(o)?$e(t.get(o)):G.text;n+=`<span style="color: ${G.annotation}; opacity: 0.75">[</span><span style="color: ${c}">${W(o)}</span><span style="color: ${G.annotation}; opacity: 0.75">]</span> <span style="color: ${G.text}; opacity: 0.6">&lt;${W(a)}&gt;</span>`,r=i+1;continue}}let c=e.slice(r+1,a),l=Ye(e,a+1)?G.marker:G.value,u=W(c);n+=`<span style="color: ${l}; opacity: 0.85">[</span><span style=" opacity: 1.0">${u}</span><span style="color: ${l}; opacity: 0.85">]</span>`,r=a+1;continue}if(i===`*`||i===`_`||i==="`"){let t=Xe(e,r);if(t){n+=t.html,r=t.nextIndex;continue}}if(i===`#`){let i=0,a=r;for(;a<e.length&&e[a]===`#`;)i++,a++;if(i<=6&&(a>=e.length||e[a]===` `||e[a]===` `)){let o=e.indexOf(`
@@ -41,14 +41,14 @@
41
41
  `;if((t===`
42
42
  `||t===`\r`||t===` `||t===` `)&&e[r+1]===` `){let t=Ye(e,r+2)?`color: ${G.marker}; opacity: 0.85`:``;n+=`<span style="${t}">${W(i)}</span>`,r++;continue}}if(i===`>`){let t=r>0?e[r-1]:`
43
43
  `;if((t===`
44
- `||t===`\r`||t===` `||t===` `)&&e[r+1]===` `){let t=Ye(e,r+2)?`color: ${G.marker}; opacity: 0.85`:``;n+=`<span style="${t}">${W(i)}</span>`,r++;continue}}n+=W(i),r++}return n}function K(e,t){if(!e)throw Error(t)}function it(e,t,n,r){return e.find(e=>e.subject.value===t&&e.predicate.value===n&&(r===null||(e.object.termType,e.object.value===r)))}function q(e,t,n,r){return!!it(e,t,n,r)}var at=[{name:`Primary subject - first subject declaration`,fn:()=>{let{primarySubject:e}=H({text:`[ex] <http://example.org/>
44
+ `||t===`\r`||t===` `||t===` `)&&e[r+1]===` `){let t=Ye(e,r+2)?`color: ${G.marker}; opacity: 0.85`:``;n+=`<span style="${t}">${W(i)}</span>`,r++;continue}}n+=W(i),r++}return n}function K(e,t){if(!e)throw Error(t)}function it(e,t,n,r){return e.find(e=>e.subject.value===t&&e.predicate.value===n&&(r===null||(e.object.termType,e.object.value===r)))}function q(e,t,n,r){return!!it(e,t,n,r)}var at=[{name:`Primary subject - first subject declaration`,fn:()=>{let{primarySubject:e,primary:t}=H({text:`[ex] <http://example.org/>
45
45
 
46
46
  # Document {=ex:doc .ex:Article label}
47
- [Alice] {ex:author}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/doc`,`Primary subject should be ex:doc, got ${e}`)}},{name:`Primary subject - null when no subject declared`,fn:()=>{let{primarySubject:e}=H({text:`[Alice] {label}`});K(e===null,`Primary subject should be null when no subject declared`)}},{name:`Primary subject - fragment does not become primary`,fn:()=>{let{primarySubject:e}=H({text:`[ex] <http://example.org/>
47
+ [Alice] {ex:author}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/doc`,`Primary subject should be ex:doc, got ${e}`),K(t!==null,`Should have a primary object`),K(t.subject===`http://example.org/doc`,`Primary object subject should be ex:doc, got ${t.subject}`),K(t.type===`http://example.org/Article`,`Primary object type should be ex:Article, got ${t.type}`),K(t.label===`Document`,`Primary object label should be Document, got ${t.label}`)}},{name:`Primary subject - null when no subject declared`,fn:()=>{let{primarySubject:e,primary:t}=H({text:`[Alice] {label}`});K(e===null,`Primary subject should be null when no subject declared`),K(t!==null,`Primary object should exist even when no subject declared`),K(t.subject===null,`Primary object subject should be null when no subject declared`),K(t.type===null,`Primary object type should be null when no subject declared`),K(t.label===null,`Primary object label should be null when no label exists`)}},{name:`Primary subject - fragment does not become primary`,fn:()=>{let{primarySubject:e,primary:t}=H({text:`[ex] <http://example.org/>
48
48
 
49
49
  # Document {=ex:doc}
50
50
  {=#summary}
51
- [Content] {label}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/doc`,`Primary subject should be ex:doc, not fragment`)}},{name:`Primary subject - reset does not clear primary`,fn:()=>{let{primarySubject:e}=H({text:`[ex] <http://example.org/>
51
+ [Content] {label}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/doc`,`Primary subject should be ex:doc, not fragment`),K(t!==null,`Should have a primary object`),K(t.subject===`http://example.org/doc`,`Primary object subject should be ex:doc, not fragment`)}},{name:`Primary subject - reset does not clear primary`,fn:()=>{let{primarySubject:e,primary:t}=H({text:`[ex] <http://example.org/>
52
52
 
53
53
  # First {=ex:first}
54
54
  [Value] {label}
@@ -56,16 +56,16 @@
56
56
  # Reset {=}
57
57
 
58
58
  # Second {=ex:second}
59
- [Value] {label}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/first`,`Primary subject should remain ex:first after reset, got ${e}`)}},{name:`Primary subject - first non-fragment subject wins`,fn:()=>{let{primarySubject:e}=H({text:`[ex] <http://example.org/>
59
+ [Value] {label}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/first`,`Primary subject should remain ex:first after reset, got ${e}`),K(t!==null,`Should have a primary object`),K(t.subject===`http://example.org/first`,`Primary object subject should be ex:first after reset, got ${t.subject}`)}},{name:`Primary subject - first non-fragment subject wins`,fn:()=>{let{primarySubject:e,primary:t}=H({text:`[ex] <http://example.org/>
60
60
 
61
61
  {=#summary}
62
62
  [Content] {label}
63
63
 
64
64
  # Document {=ex:doc}
65
- [Alice] {ex:author}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/doc`,`Primary subject should be ex:doc, not fragment`)}},{name:`Primary subject - round-trip with generate()`,fn:()=>{let{quads:e,primarySubject:t}=H({text:`[ex] <http://example.org/>
65
+ [Alice] {ex:author}`});K(e!==null,`Should have a primary subject`),K(e===`http://example.org/doc`,`Primary subject should be ex:doc, not fragment`),K(t!==null,`Should have a primary object`),K(t.subject===`http://example.org/doc`,`Primary object subject should be ex:doc, not fragment`)}},{name:`Primary subject - round-trip with generate()`,fn:()=>{let{quads:e,primarySubject:t,primary:n}=H({text:`[ex] <http://example.org/>
66
66
 
67
67
  # Document {=ex:doc .ex:Article label}
68
- [Alice] {ex:author}`}),n=We({quads:e,context:{ex:`http://example.org/`},primarySubject:t}),{primarySubject:r}=H({text:n.text,context:n.context});K(t!==null,`Original should have primary subject`),K(r!==null,`Regenerated should have primary subject`),K(t===r,`Primary subject should be preserved in round-trip: ${t} vs ${r}`)}},{name:`Subject declaration sets context`,fn:()=>{let{quads:e,context:t}=H({text:`[ex] <http://example.org/>
68
+ [Alice] {ex:author}`}),r=We({quads:e,context:{ex:`http://example.org/`},primarySubject:t}),{primarySubject:i,primary:a}=H({text:r.text,context:r.context});K(t!==null,`Original should have primary subject`),K(n!==null,`Original should have primary object`),K(i!==null,`Regenerated should have primary subject`),K(a!==null,`Regenerated should have primary object`),K(t===i,`Primary subject should be preserved in round-trip: ${t} vs ${i}`),K(n.subject===a.subject,`Primary object subject should be preserved: ${n.subject} vs ${a.subject}`),K(n.type===a.type,`Primary object type should be preserved: ${n.type} vs ${a.type}`),K(n.label===a.label,`Primary object label should be preserved: ${n.label} vs ${a.label}`)}},{name:`Subject declaration sets context`,fn:()=>{let{quads:e,context:t}=H({text:`[ex] <http://example.org/>
69
69
  # Title {=ex:doc}
70
70
 
71
71
  [value] {label}`});K(q(e,`http://example.org/doc`,`http://www.w3.org/2000/01/rdf-schema#label`,`value`),`Subject should be ex:doc`)}},{name:`Subject reset with {=}`,fn:()=>{let{quads:e}=H({text:`[ex] <http://example.org/>
@@ -549,7 +549,7 @@ List items:
549
549
 
550
550
  [Alice Smith] {author}`}),t=h(e.quads.find(e=>e.subject.value===`http://example.org/article`&&e.predicate.value===`http://www.w3.org/2000/01/rdf-schema#author`),e.origin);ot(t,`Should locate quad`),ot(t.range.start>=0,`Range start should be non-negative`),ot(t.range.end<=87,`Range end should be within text`),ot(t.range.start<t.range.end,`Range start should be before end`),J(t.value,`Alice Smith`),J(t.polarity,`+`)}}];function Y(e,t){if(!e)throw Error(t||`Assertion failed`)}function ct(e){let t=e.object.datatype?.value||``,n=e.object.language||``;return`${e.subject.value}|${e.predicate.value}|${e.object.value}|${t}|${n}`}function lt(e,t,n,r){return e.some(e=>e.subject.value===t&&e.predicate.value===n&&e.object.value===r)}var ut=[{name:`Primary subjects - single document`,fn:()=>{let e=U([`[my] <tag:hr@example.com,2026:>
551
551
  # Employee {=my:emp456 .my:Employee}
552
- [Alice] {my:name}`]);Y(e.primarySubjects.length===1,`Should have 1 primary subject, got ${e.primarySubjects.length}`),Y(e.primarySubjects[0]===`tag:hr@example.com,2026:emp456`,`Primary subject should be my:emp456, got ${e.primarySubjects[0]}`)}},{name:`Primary subjects - multiple documents`,fn:()=>{let e=U([`[ex] <http://example.org/>
552
+ [Alice] {my:name}`]);Y(e.primarySubjects.length===1,`Should have 1 primary subject, got ${e.primarySubjects.length}`),Y(e.primarySubjects[0]===`tag:hr@example.com,2026:emp456`,`Primary subject should be my:emp456, got ${e.primarySubjects[0]}`),Y(e.primary.length===1,`Should have 1 primary object, got ${e.primary.length}`),Y(e.primary[0].subject===`tag:hr@example.com,2026:emp456`,`Primary object subject should be my:emp456, got ${e.primary[0].subject}`),Y(e.primary[0].type===`tag:hr@example.com,2026:Employee`,`Primary object type should be my:Employee, got ${e.primary[0].type}`),Y(e.primary[0].label===null,`Primary object label should be null, got ${e.primary[0].label}`)}},{name:`Primary subjects - multiple documents`,fn:()=>{let e=U([`[ex] <http://example.org/>
553
553
  # Doc1 {=ex:doc1}
554
554
  [Value] {label}`,`[ex] <http://example.org/>
555
555
  # Doc2 {=ex:doc2}
package/dist/index.js CHANGED
@@ -715,6 +715,8 @@ function I(t, n = {}) {
715
715
  },
716
716
  currentSubject: null,
717
717
  primarySubject: null,
718
+ primaryType: null,
719
+ primaryLabel: null,
718
720
  tokens: null,
719
721
  currentTokenIndex: -1,
720
722
  statements: [],
@@ -743,6 +745,11 @@ function I(t, n = {}) {
743
745
  let t = v(e.subject, e.predicate, e.object);
744
746
  c.has(t) || l.push(e);
745
747
  }
748
+ let u = {
749
+ subject: o.primarySubject,
750
+ type: o.primaryType,
751
+ label: o.primaryLabel
752
+ };
746
753
  return {
747
754
  quads: o.quads,
748
755
  remove: l,
@@ -750,6 +757,7 @@ function I(t, n = {}) {
750
757
  origin: o.origin,
751
758
  context: o.ctx,
752
759
  primarySubject: o.primarySubject,
760
+ primary: u,
753
761
  md: s.md
754
762
  };
755
763
  }
@@ -1014,9 +1022,9 @@ function z(e, t, n, r, i, a, o, s, c, l = null, u = null, d = null, f = null) {
1014
1022
  } else n.add(p);
1015
1023
  } else {
1016
1024
  let n = v(p.subject, p.predicate, p.object);
1017
- t.set(n, p), e.push(p), Me(p, c, l, u, d);
1018
- let s = ye(i, a, o, l);
1019
- r.set(n, s), f.currentBlock && i.id === f.currentBlock.id && (f.currentBlock.quadKeys || (f.currentBlock.quadKeys = []), f.currentBlock.quadKeys.push(n));
1025
+ t.set(n, p), e.push(p), f && (!f.primaryType && o.value === "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" && (f.primaryType = s.value), !f.primaryLabel && o.value === "http://www.w3.org/2000/01/rdf-schema#label" && s.termType === "Literal" && (f.primaryLabel = s.value)), Me(p, c, l, u, d);
1026
+ let m = ye(i, a, o, l);
1027
+ r.set(n, m), f.currentBlock && i.id === f.currentBlock.id && (f.currentBlock.quadKeys || (f.currentBlock.quadKeys = []), f.currentBlock.quadKeys.push(n));
1020
1028
  }
1021
1029
  }
1022
1030
  function Me(e, t, n, r = null, i = null) {
@@ -1148,57 +1156,58 @@ function Re(e, t, n) {
1148
1156
  }) : e;
1149
1157
  }
1150
1158
  function ze(t, n = {}) {
1151
- let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set(), a = [], o = /* @__PURE__ */ new Map(), s = [], c = /* @__PURE__ */ new Map(), l = [];
1152
- for (let u = 0; u < t.length; u++) {
1153
- let d = t[u], f = Re(d, n, {
1159
+ let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set(), a = [], o = /* @__PURE__ */ new Map(), s = [], c = /* @__PURE__ */ new Map(), l = [], u = [];
1160
+ for (let d = 0; d < t.length; d++) {
1161
+ let f = t[d], p = Re(f, n, {
1154
1162
  ...e,
1155
1163
  ...n.context
1156
1164
  });
1157
- if (f.context) for (let [t, n] of Object.entries(f.context)) !c.has(t) && !e[t] && c.set(t, n);
1158
- let p = {
1159
- index: u,
1160
- input: typeof d == "string" ? "string" : "ParseResult",
1161
- origin: f.origin,
1162
- context: f.context,
1163
- statementsCount: f.statements?.length || 0
1165
+ if (p.context) for (let [t, n] of Object.entries(p.context)) !c.has(t) && !e[t] && c.set(t, n);
1166
+ let m = {
1167
+ index: d,
1168
+ input: typeof f == "string" ? "string" : "ParseResult",
1169
+ origin: p.origin,
1170
+ context: p.context,
1171
+ statementsCount: p.statements?.length || 0
1164
1172
  };
1165
- a.push(p), f.statements && f.statements.length > 0 && s.push(...f.statements), f.primarySubject && l.push(f.primarySubject);
1166
- for (let e of f.quads) {
1173
+ a.push(m), p.statements && p.statements.length > 0 && s.push(...p.statements), p.primary && (p.primary.subject || p.primary.type || p.primary.label) && l.push(p.primary), p.primarySubject && u.push(p.primarySubject);
1174
+ for (let e of p.quads) {
1167
1175
  let t = U(e);
1168
1176
  r.set(t, e);
1169
- let n = f.origin.quadIndex.get(t);
1177
+ let n = p.origin.quadIndex.get(t);
1170
1178
  o.set(t, {
1171
1179
  ...n || {},
1172
- documentIndex: u,
1180
+ documentIndex: d,
1173
1181
  polarity: "+"
1174
1182
  });
1175
1183
  }
1176
- for (let e of f.remove) {
1184
+ for (let e of p.remove) {
1177
1185
  let t = U(e);
1178
1186
  r.has(t) ? r.delete(t) : i.add(e);
1179
- let n = f.origin.quadIndex.get(t);
1187
+ let n = p.origin.quadIndex.get(t);
1180
1188
  o.set(t, {
1181
1189
  ...n || {},
1182
- documentIndex: u,
1190
+ documentIndex: d,
1183
1191
  polarity: "-"
1184
1192
  });
1185
1193
  }
1186
1194
  }
1187
- let u = Array.from(r.values()), d = Array.from(i), f = {
1195
+ let d = Array.from(r.values()), f = Array.from(i), p = {
1188
1196
  documents: a,
1189
1197
  quadIndex: o
1190
- }, p = {
1198
+ }, m = {
1191
1199
  ...e,
1192
1200
  ...n.context,
1193
1201
  ...Object.fromEntries(c)
1194
- }, m = new Set(u.map(U)), h = new Set(d.map(U));
1202
+ }, h = new Set(d.map(U)), g = new Set(f.map(U));
1195
1203
  return {
1196
- quads: u.filter((e) => !h.has(U(e))),
1197
- remove: d.filter((e) => !m.has(U(e))),
1204
+ quads: d.filter((e) => !g.has(U(e))),
1205
+ remove: f.filter((e) => !h.has(U(e))),
1198
1206
  statements: s,
1199
- origin: f,
1200
- context: p,
1201
- primarySubjects: l
1207
+ origin: p,
1208
+ context: m,
1209
+ primarySubjects: u,
1210
+ primary: l
1202
1211
  };
1203
1212
  }
1204
1213
  //#endregion