mdld-parse 0.9.0 → 0.9.1

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.
package/README.md CHANGED
@@ -199,7 +199,7 @@ Parse MD-LD markdown and return RDF quads with lean origin tracking.
199
199
  - `dataFactory` (object, optional) — Custom RDF/JS DataFactory
200
200
  - `graph` (string, optional) — Named graph IRI
201
201
 
202
- **Returns:** `{ quads, remove, statements, origin, context, primarySubject }`
202
+ **Returns:** `{ quads, remove, statements, origin, context, primarySubject, md }`
203
203
 
204
204
  > **Legacy:** `parse(text, options)` still works for backward compatibility
205
205
 
@@ -209,6 +209,7 @@ Parse MD-LD markdown and return RDF quads with lean origin tracking.
209
209
  - `origin` — Lean origin tracking object with quadIndex for UI navigation
210
210
  - `context` — Final context used (includes prefixes)
211
211
  - `primarySubject` — String IRI or null (first non-fragment subject declaration)
212
+ - `md` — Clean Markdown with all MD-LD annotations stripped (round-trip safe)
212
213
 
213
214
  ### `merge(docs, options)`
214
215
 
@@ -286,11 +287,43 @@ const nodeView = generateNode({ ...parse({ text }), focusIRI: 'http://example.or
286
287
  ```
287
288
 
288
289
  **Why this works:**
289
- - `parse()` returns `{ quads, context, primarySubject, ... }`
290
+ - `parse()` returns `{ quads, context, primarySubject, md, ... }`
290
291
  - `generate()` accepts `{ quads, context, primarySubject }`
291
292
  - `generateNode()` accepts `{ quads, context, focusIRI }` (with focusIRI override)
292
293
  - Perfect shape alignment enables elegant `{ ...spread }` composition
293
294
 
295
+ ### The `md` Field — Clean Markdown Extraction
296
+
297
+ Every `parse()` result includes a `md` field containing the original Markdown with all MD-LD annotations stripped:
298
+
299
+ ```javascript
300
+ const result = parse({
301
+ text: `# Document {=ex:doc .Article}\n[Content] {ex:content}`,
302
+ context: { ex: 'http://example.org/' }
303
+ });
304
+
305
+ console.log(result.md);
306
+ // # Document\nContent
307
+
308
+ // Round-trip safety: re-parsing clean MD produces zero quads
309
+ const reparsed = parse({ text: result.md });
310
+ console.log(reparsed.quads.length); // 0
311
+ ```
312
+
313
+ **Behavior:**
314
+ - Valid MD-LD annotations (`{=...}`, `{+...}`, `{...}`) are completely removed
315
+ - Content from value carriers (`[text]`, `**bold**`, `` `code` ``) is preserved
316
+ - Invalid syntax (annotations not at end-of-line) is preserved as visible markers
317
+ - Headings, lists, blockquotes, code blocks maintain their structure
318
+ - Prefix declarations at start of line are stripped
319
+ - Standalone subject declarations (`{=ex:subject}`) are stripped
320
+
321
+ **Use cases:**
322
+ - **Content extraction** — Get readable Markdown without semantic markup
323
+ - **Syntax validation** — Remaining `{...}` patterns indicate invalid MD-LD syntax
324
+ - **Round-trip testing** — `parse(md).md` should parse to zero quads
325
+ - **Preview generation** — Show clean document before publishing
326
+
294
327
  ### `locate(quad, origin)`
295
328
 
296
329
  Locate the origin entry for a quad using the lean origin system.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdld-parse",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "A standards-compliant parser for **MD-LD (Markdown-Linked Data)** — a human-friendly RDF authoring format that extends Markdown with semantic annotations.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -13,7 +13,8 @@
13
13
  ],
14
14
  "scripts": {
15
15
  "test": "node tests/index.js",
16
- "dev": "pnpx live-server"
16
+ "dev": "vite dev",
17
+ "build": "vite build && vite build --config vite.config.lib.js && cp -r docs/ dist/docs/ && cp -r examples/ dist/examples/ && cp -r spec/ dist/spec/"
17
18
  },
18
19
  "keywords": [
19
20
  "mdld",
@@ -38,5 +39,14 @@
38
39
  "homepage": "https://mdld.js.org",
39
40
  "bugs": {
40
41
  "url": "https://github.com/davay42/mdld-parse/issues"
42
+ },
43
+ "devDependencies": {
44
+ "@unocss/extractor-pug": "^66.6.8",
45
+ "@vitejs/plugin-vue": "^6.0.6",
46
+ "unocss": "^66.6.8",
47
+ "vite": "^8.0.10",
48
+ "vite-plugin-singlefile": "^2.3.3",
49
+ "markdown-it": "^14.1.1",
50
+ "n3": "^2.0.3"
41
51
  }
42
52
  }
package/src/parse.js CHANGED
@@ -76,7 +76,8 @@ export function parse(firstArg, secondArg = {}) {
76
76
  blockStack: []
77
77
  };
78
78
 
79
- state.tokens = scanTokens(text);
79
+ const scanResult = scanTokens(text);
80
+ state.tokens = scanResult.tokens;
80
81
 
81
82
  // Single-pass processing: resolve prefixes AND process tokens together
82
83
  for (let i = 0; i < state.tokens.length; i++) {
@@ -117,7 +118,15 @@ export function parse(firstArg, secondArg = {}) {
117
118
  }
118
119
  }
119
120
 
120
- return { quads: state.quads, remove: filteredRemove, statements: state.statements, origin: state.origin, context: state.ctx, primarySubject: state.primarySubject };
121
+ return {
122
+ quads: state.quads,
123
+ remove: filteredRemove,
124
+ statements: state.statements,
125
+ origin: state.origin,
126
+ context: state.ctx,
127
+ primarySubject: state.primarySubject,
128
+ md: scanResult.md
129
+ };
121
130
  }
122
131
 
123
132
 
@@ -130,6 +139,7 @@ function getCarriers(token) {
130
139
 
131
140
  function scanTokens(text) {
132
141
  const tokens = [];
142
+ const mdLines = [];
133
143
  const lines = text.split('\n');
134
144
  let pos = 0;
135
145
  let codeBlock = null;
@@ -137,6 +147,7 @@ function scanTokens(text) {
137
147
  { type: 'fence', test: line => FENCE_REGEX.test(line.trim()), process: handleFence },
138
148
  { type: 'content', test: () => codeBlock, process: line => codeBlock.content.push(line) },
139
149
  { type: 'prefix', test: line => PREFIX_REGEX.test(line), process: handlePrefix },
150
+ { type: 'standalone', test: line => STANDALONE_SUBJECT_REGEX.test(line), process: handleStandaloneSubject },
140
151
  { type: 'heading', test: line => HEADING_REGEX.test(line), process: handleHeading },
141
152
  { type: 'list', test: line => UNORDERED_LIST_REGEX.test(line), process: handleList },
142
153
  { type: 'blockquote', test: line => BLOCKQUOTE_REGEX.test(line), process: handleBlockquote },
@@ -162,6 +173,10 @@ function scanTokens(text) {
162
173
  attrsRange: attrsText && attrsStartInLine >= 0 ? [lineStart + attrsStartInLine, lineStart + attrsStartInLine + attrsText.length] : null,
163
174
  valueRangeStart: contentStart
164
175
  };
176
+
177
+ // Add stripped fence line to mdLines
178
+ const cleanFence = line.replace(/\s*\{[^}]+\}\s*$/, '');
179
+ mdLines.push(cleanFence);
165
180
  } else {
166
181
  const fenceChar = codeBlock.fence[0];
167
182
  const expectedFence = fenceChar.repeat(codeBlock.fence.length);
@@ -179,7 +194,20 @@ function scanTokens(text) {
179
194
  attrsRange: codeBlock.attrsRange,
180
195
  valueRange: [valueStart, valueEnd]
181
196
  });
197
+
198
+ // Add all code content to mdLines before closing fence
199
+ for (const contentLine of codeBlock.content) {
200
+ mdLines.push(contentLine);
201
+ }
202
+
182
203
  codeBlock = null;
204
+
205
+ // Add closing fence to mdLines without trailing newline
206
+ const closingFence = line.replace(/\r?\n.*$/, '');
207
+ mdLines.push(closingFence);
208
+ } else {
209
+ // Code content - don't add to mdLines here, will be added when closing block
210
+ // No need to add individual lines as they're processed when closing
183
211
  }
184
212
  }
185
213
  return true;
@@ -188,6 +216,7 @@ function scanTokens(text) {
188
216
  function handlePrefix(line, lineStart, pos) {
189
217
  const match = PREFIX_REGEX.exec(line);
190
218
  tokens.push({ type: 'prefix', prefix: match[1], iri: match[2].trim() });
219
+ // Prefixes are typically stripped from clean MD
191
220
  return true;
192
221
  }
193
222
 
@@ -198,12 +227,20 @@ function scanTokens(text) {
198
227
  const rangeInfo = calcRangeInfo(line, attrs, lineStart, afterHashes, match[2].length);
199
228
  tokens.push(createToken('heading', [lineStart, pos - 1], match[2].trim(), attrs,
200
229
  rangeInfo.attrsRange, rangeInfo.valueRange, { depth: match[1].length }));
230
+
231
+ // Add stripped heading to mdLines
232
+ const cleanHeading = `${match[1]} ${match[2].trim()}`;
233
+ mdLines.push(cleanHeading);
201
234
  return true;
202
235
  }
203
236
 
204
237
  function handleList(line, lineStart, pos) {
205
238
  const match = UNORDERED_LIST_REGEX.exec(line);
206
239
  tokens.push(createListToken('list', line, lineStart, pos, match));
240
+
241
+ // Add stripped list item to mdLines
242
+ const cleanList = `${match[1]}${match[2]} ${match[3].trim()}`;
243
+ mdLines.push(cleanList);
207
244
  return true;
208
245
  }
209
246
 
@@ -215,11 +252,45 @@ function scanTokens(text) {
215
252
  tokens.push(createToken('blockquote', [lineStart, pos - 1], match[1].trim(), attrs,
216
253
  calcAttrsRange(line, attrs, lineStart),
217
254
  [lineStart + valueStartInLine, lineStart + valueEndInLine]));
255
+
256
+ // Add stripped blockquote to mdLines
257
+ const cleanBlockquote = `> ${match[1].trim()}`;
258
+ mdLines.push(cleanBlockquote);
218
259
  return true;
219
260
  }
220
261
 
221
262
  function handlePara(line, lineStart, pos) {
222
263
  tokens.push(createToken('para', [lineStart, pos - 1], line.trim()));
264
+
265
+ // Add stripped paragraph to mdLines
266
+ let cleanPara = line;
267
+
268
+ // Remove inline carrier annotations using existing patterns
269
+ for (const [patternName, regex] of CARRIER_PATTERN_ARRAY) {
270
+ const globalRegex = new RegExp(regex.source, 'gy');
271
+ cleanPara = cleanPara.replace(globalRegex, (match, content, attrs) => {
272
+ return content || '';
273
+ });
274
+ }
275
+
276
+ // Remove bracket-style annotations [text] {annotation}
277
+ cleanPara = cleanPara.replace(/\[([^\]]+)\]\s*\{[^}]+\}/g, '$1');
278
+
279
+ // Remove any remaining standalone annotations
280
+ cleanPara = cleanPara.replace(/\s*\{[^}]+\}\s*/g, ' ');
281
+
282
+ // Clean up extra whitespace
283
+ cleanPara = cleanPara.replace(/\s+/g, ' ').trim();
284
+
285
+ mdLines.push(cleanPara);
286
+ return true;
287
+ }
288
+
289
+ function handleStandaloneSubject(line, lineStart, pos) {
290
+ tokens.push({ type: 'standalone', text: line.trim(), range: [lineStart, pos - 1] });
291
+
292
+ // Standalone subjects are stripped from MD by default
293
+ // Don't add anything to mdLines
223
294
  return true;
224
295
  }
225
296
 
@@ -236,7 +307,9 @@ function scanTokens(text) {
236
307
  }
237
308
  }
238
309
 
239
- return tokens;
310
+ // Join without trailing newline to match expected output
311
+ const mdContent = mdLines.join('\n');
312
+ return { tokens, md: mdContent };
240
313
  }
241
314
 
242
315
  function extractInlineCarriers(text, baseOffset = 0) {
@@ -688,4 +761,5 @@ const TOKEN_PROCESSORS = {
688
761
  blockquote: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
689
762
  para: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry, [processStandaloneSubject]),
690
763
  list: (token, state) => processTokenWithBlockTracking(token, state, processTokenAnnotations, createBlockEntry),
764
+ standalone: (token, state) => processStandaloneSubject(token, state),
691
765
  };
package/src/render.js CHANGED
@@ -2,36 +2,40 @@ import { parse } from './parse.js';
2
2
  import {
3
3
  DataFactory,
4
4
  expandIRI,
5
- shortenIRI,
6
- parseSemanticBlock,
7
- hash
5
+ shortenIRI
8
6
  } from './utils.js';
9
7
  import {
10
8
  escapeHtml,
11
- getIndentLevel,
12
- processPredicates
9
+ extractContentFromRange,
10
+ getIndentLevel
13
11
  } from './shared.js';
14
12
  import { DEFAULT_CONTEXT } from './constants.js';
15
13
 
16
14
  /**
17
- * Render MD-LD to HTML+RDFa
15
+ * Render MD-LD to HTML+RDFa with round-trip safety using origin system
16
+ *
17
+ * This implementation leverages the origin tracking system to ensure that:
18
+ * 1. Every quad from parsing is represented in the RDFa output
19
+ * 2. RDFa attributes are placed exactly where the original annotations were
20
+ * 3. The rendered HTML can be parsed back to identical quads
21
+ *
18
22
  * @param {string} mdld - MD-LD input string
19
23
  * @param {Object} options - Rendering options
20
24
  * @param {Object} options.context - Additional context prefixes
21
25
  * @param {string} options.baseIRI - Base IRI for relative URLs
22
- * @param {boolean} options.strict - Enable strict quad-RDFa correspondence checking
23
- * @param {boolean} options.pretty - Pretty print output
24
- * @returns {Object} Render result with HTML, metadata, and optional quad mapping
26
+ * @param {boolean} options.validate - Enable round-trip validation
27
+ * @param {boolean} options.debug - Enable debug output
28
+ * @returns {Object} Render result with HTML, metadata, and validation
25
29
  */
26
30
  export function render(mdld, options = {}) {
27
- // Phase 1: Parse MD-LD (reuse parser)
31
+ // Phase 1: Parse MD-LD with full origin tracking
28
32
  const parsed = parse({ text: mdld, context: options.context || {} });
29
33
 
30
- // Phase 2: Build render state (includes quads for validation)
31
- const state = buildRenderState(parsed, options, mdld);
34
+ // Phase 2: Build render state using origin system
35
+ const state = buildOriginBasedState(parsed, options, mdld);
32
36
 
33
- // Phase 3: Render blocks to HTML
34
- const html = renderBlocks(parsed.origin.blocks, state);
37
+ // Phase 3: Render HTML using origin-based positioning
38
+ const html = renderWithOriginTracking(parsed.origin.blocks, state);
35
39
 
36
40
  // Phase 4: Wrap with RDFa context
37
41
  const wrapped = wrapWithRDFaContext(html, state.ctx);
@@ -42,17 +46,16 @@ export function render(mdld, options = {}) {
42
46
  metadata: {
43
47
  blockCount: parsed.origin.blocks.size,
44
48
  quadCount: parsed.quads.length,
45
- renderedRDFaCount: state.renderedRDFaCount || 0,
49
+ renderedRDFaCount: state.renderedRDFaCount,
46
50
  renderTime: Date.now()
47
51
  }
48
52
  };
49
53
 
50
- // Add quad mapping if requested
51
- if (options.strict) {
52
- result.quadMap = state.quadMap || [];
53
- result.validation = {
54
- allQuadsRendered: (state.quadMap || []).length === parsed.quads.length,
55
- orphanedQuads: identifyOrphanedQuads(parsed.quads, state.quadMap || [])
54
+ if (options.debug) {
55
+ result.debug = {
56
+ originBlocks: Array.from(parsed.origin.blocks.values()),
57
+ quadIndex: Array.from(parsed.origin.quadIndex.entries()),
58
+ renderMap: state.renderMap
56
59
  };
57
60
  }
58
61
 
@@ -60,396 +63,295 @@ export function render(mdld, options = {}) {
60
63
  }
61
64
 
62
65
  /**
63
- * Build render state following parser pattern
66
+ * Build render state using origin system for precise quad tracking
64
67
  */
65
- function buildRenderState(parsed, options, mdld) {
66
- // Use the parser's context which already includes document prefixes
67
- const ctx = parsed.context || { ...DEFAULT_CONTEXT, ...(options.context || {}) };
68
-
68
+ function buildOriginBasedState(parsed, options, mdld) {
69
69
  return {
70
- ctx,
70
+ ctx: parsed.context || { ...DEFAULT_CONTEXT, ...(options.context || {}) },
71
71
  df: options.dataFactory || DataFactory,
72
72
  baseIRI: options.baseIRI || '',
73
- sourceText: mdld, // Store original text for content extraction
73
+ sourceText: mdld,
74
74
  output: [],
75
75
  currentSubject: null,
76
- documentSubject: null,
77
- blockStack: [],
78
- carrierStack: [],
79
- // Quad tracking for 1-1 correspondence
80
- quads: parsed.quads,
81
- quadMap: options.strict ? [] : null, // Maps rendered RDFa to quads
76
+ // Origin-based tracking
77
+ quadIndex: parsed.origin.quadIndex,
78
+ blocks: parsed.origin.blocks,
79
+ renderMap: new Map(), // Maps block ID to rendered RDFa attributes
82
80
  renderedRDFaCount: 0,
83
- strict: options.strict || false
81
+ validation: options.validate || false
84
82
  };
85
83
  }
86
84
 
87
85
  /**
88
- * Helper: Check if a quad exists in the quads array
86
+ * Render blocks using origin tracking for precise RDFa placement
89
87
  */
90
- function quadExists(quads, subject, predicate, object) {
91
- return quads.some(q =>
92
- q.subject.equals(subject) &&
93
- q.predicate.equals(predicate) &&
94
- q.object.equals(object)
95
- );
96
- }
97
-
98
- /**
99
- * Helper: Identify quads that weren't rendered
100
- */
101
- function identifyOrphanedQuads(allQuads, renderedQuads) {
102
- const renderedKeys = new Set(renderedQuads.map(qm =>
103
- `${qm.subject.value}|${qm.predicate.value}|${qm.object.value}`
104
- ));
105
-
106
- return allQuads.filter(q =>
107
- !renderedKeys.has(`${q.subject.value}|${q.predicate.value}|${q.object.value}`)
108
- );
109
- }
110
-
111
- /**
112
- * Render blocks to HTML with RDFa annotations
113
- */
114
- function renderBlocks(blocks, state) {
115
- // Sort blocks by position
88
+ function renderWithOriginTracking(blocks, state) {
89
+ // Sort blocks by document order
116
90
  const sortedBlocks = Array.from(blocks.values()).sort((a, b) => {
117
91
  return (a.range?.start || 0) - (b.range?.start || 0);
118
92
  });
119
93
 
120
- // Separate list blocks from other blocks
94
+ // Separate list blocks for special handling
121
95
  const listBlocks = sortedBlocks.filter(block => block.carrierType === 'list');
122
96
  const otherBlocks = sortedBlocks.filter(block => block.carrierType !== 'list');
123
97
 
124
- // Render non-list blocks normally
98
+ // Render non-list blocks
125
99
  otherBlocks.forEach(block => {
126
- renderBlock(block, state);
100
+ renderBlockWithOrigin(block, state);
127
101
  });
128
102
 
129
- // Render lists using Markdown approach with RDFa enrichment
103
+ // Render list blocks with proper structure
130
104
  if (listBlocks.length > 0) {
131
- renderListsWithRDFa(listBlocks, state);
105
+ renderListsWithOrigin(listBlocks, state);
132
106
  }
133
107
 
134
108
  return state.output.join('');
135
109
  }
136
110
 
137
111
  /**
138
- * Render lists using Markdown structure with RDFa enrichment
139
- */
140
- function renderListsWithRDFa(listBlocks, state) {
141
- // Group list blocks by their context (consecutive blocks with similar context)
142
- const listGroups = groupListBlocksByContext(listBlocks, state.sourceText);
143
-
144
- listGroups.forEach(group => {
145
- renderListGroup(group, state);
146
- });
147
- }
148
-
149
- /**
150
- * Group list blocks by their structural hierarchy
112
+ * Render a single block using origin information
151
113
  */
152
- function groupListBlocksByContext(listBlocks, sourceText) {
153
- const groups = [];
154
-
155
- // Group consecutive list blocks
156
- let currentGroup = null;
157
-
158
- for (const block of listBlocks) {
159
- // Start new group for each top-level item (indent 0)
160
- const indent = getIndentLevel(block, sourceText);
114
+ function renderBlockWithOrigin(block, state) {
115
+ // Update subject context
116
+ if (block.subject && block.subject !== 'RESET') {
117
+ state.currentSubject = block.subject;
118
+ }
161
119
 
162
- if (indent === 0) {
163
- // Close previous group
164
- if (currentGroup) {
165
- groups.push(currentGroup);
166
- }
120
+ // Build RDFa attributes from origin data
121
+ const rdfaAttrs = buildRDFaFromOrigin(block, state);
167
122
 
168
- // Start new group with a generic name
169
- currentGroup = {
170
- contextName: 'Items',
171
- blocks: [block]
172
- };
173
- } else {
174
- // Add nested items to current group
175
- if (currentGroup) {
176
- currentGroup.blocks.push(block);
177
- } else {
178
- // This shouldn't happen, but handle it
179
- currentGroup = {
180
- contextName: 'Items',
181
- blocks: [block]
182
- };
183
- }
184
- }
123
+ // Store render mapping for validation
124
+ if (rdfaAttrs) {
125
+ state.renderMap.set(block.id || hashBlock(block), rdfaAttrs);
185
126
  }
186
127
 
187
- if (currentGroup) {
188
- groups.push(currentGroup);
128
+ // Render based on block type
129
+ switch (block.type || block.carrierType) {
130
+ case 'heading':
131
+ renderHeading(block, rdfaAttrs, state);
132
+ break;
133
+ case 'para':
134
+ renderParagraph(block, rdfaAttrs, state);
135
+ break;
136
+ case 'list':
137
+ // Handled separately
138
+ break;
139
+ case 'quote':
140
+ renderBlockquote(block, rdfaAttrs, state);
141
+ break;
142
+ case 'code':
143
+ renderCode(block, rdfaAttrs, state);
144
+ break;
145
+ default:
146
+ renderDiv(block, rdfaAttrs, state);
189
147
  }
190
-
191
- return groups;
192
148
  }
193
149
 
194
150
  /**
195
- * Render a list group with proper Markdown structure and RDFa enrichment
151
+ * Build RDFa attributes from origin tracking data
196
152
  */
197
- function renderListGroup(group, state) {
198
- // Extract the list anchor text from the first block's position
199
- const firstBlock = group.blocks[0];
200
- const listAnchorText = extractListAnchorText(firstBlock, state.sourceText);
153
+ function buildRDFaFromOrigin(block, state) {
154
+ const attrs = [];
201
155
 
202
- // Render the list anchor as a paragraph if it exists
203
- if (listAnchorText) {
204
- state.output.push(`<p>${escapeHtml(listAnchorText)}</p>`);
156
+ // Skip invalid subjects
157
+ if (!block.subject || block.subject === 'RESET' ||
158
+ block.subject.startsWith('=#') || block.subject.startsWith('+')) {
159
+ return '';
205
160
  }
206
161
 
207
- // Render the list directly without the semantic-list wrapper
208
- state.output.push(`<ul>`);
162
+ // Subject resolution - use full IRI for round-trip compatibility
163
+ const expandedSubject = expandIRI(block.subject, state.ctx);
164
+ attrs.push(`about="${escapeHtml(expandedSubject)}"`);
209
165
 
210
- // Render list items preserving Markdown structure
211
- const markdownList = group.blocks.map(block =>
212
- state.sourceText.substring(block.range.start, block.range.end)
213
- ).join('\n');
214
-
215
- // Parse markdown list and enrich with RDFa
216
- const htmlList = parseMarkdownList(markdownList, group.blocks, state);
217
- state.output.push(htmlList);
166
+ // Types from block annotations - use full IRIs
167
+ if (block.types && block.types.length > 0) {
168
+ const types = block.types.map(t => {
169
+ const iri = typeof t === 'string' ? t : t.iri;
170
+ return expandIRI(iri, state.ctx);
171
+ }).join(' ');
172
+ attrs.push(`typeof="${escapeHtml(types)}"`);
173
+ }
218
174
 
219
- state.output.push(`</ul>`);
220
- }
175
+ // Predicates from carriers and block
176
+ const allPredicates = [];
221
177
 
222
- /**
223
- * Extract list anchor text (the paragraph before the list)
224
- */
225
- function extractListAnchorText(firstBlock, sourceText) {
226
- if (!firstBlock.range || !sourceText) return null;
178
+ // Add predicates from block (for paragraph-level annotations)
179
+ if (block.predicates && block.predicates.length > 0) {
180
+ allPredicates.push(...block.predicates);
181
+ }
227
182
 
228
- // Look backwards from the first list item to find the list anchor
229
- const startPos = firstBlock.range.start;
183
+ // Add predicates from carriers
184
+ if (block.carriers && block.carriers.length > 0) {
185
+ for (const carrier of block.carriers) {
186
+ if (carrier.predicates && carrier.predicates.length > 0) {
187
+ allPredicates.push(...carrier.predicates);
188
+ }
189
+ }
190
+ }
230
191
 
231
- // Search backwards for a line that has semantic annotation but no value carrier
232
- let searchPos = startPos;
233
- let foundAnchor = null;
192
+ if (allPredicates.length > 0) {
193
+ const { literalProps, objectProps, reverseProps } = processPredicates(allPredicates, state.ctx);
234
194
 
235
- while (searchPos > 0 && !foundAnchor) {
236
- // Find the start of the current line
237
- let lineStart = searchPos - 1;
238
- while (lineStart > 0 && sourceText[lineStart - 1] !== '\n') {
239
- lineStart--;
195
+ if (literalProps.length > 0) {
196
+ attrs.push(`property="${escapeHtml(literalProps.join(' '))}"`);
240
197
  }
241
-
242
- // Extract the line
243
- let lineEnd = searchPos;
244
- while (lineEnd < sourceText.length && sourceText[lineEnd] !== '\n') {
245
- lineEnd++;
198
+ if (objectProps.length > 0) {
199
+ attrs.push(`rel="${escapeHtml(objectProps.join(' '))}"`);
246
200
  }
247
-
248
- const line = sourceText.substring(lineStart, lineEnd).trim();
249
-
250
- // Check if this looks like a list anchor (has semantic annotation but no value carrier)
251
- if (line.includes('{') && !line.match(/^-\s/)) {
252
- foundAnchor = line;
253
- break;
201
+ if (reverseProps.length > 0) {
202
+ attrs.push(`rev="${escapeHtml(reverseProps.join(' '))}"`);
254
203
  }
255
-
256
- // Continue searching backwards
257
- searchPos = lineStart - 1;
258
204
  }
259
205
 
260
- if (foundAnchor) {
261
- // Clean the line by removing MD-LD annotations
262
- const cleanLine = foundAnchor.replace(/\s*\{[^}]+\}\s*$/, '');
263
- return cleanLine;
206
+ // Count rendered RDFa attributes
207
+ if (attrs.length > 1) { // More than just about
208
+ state.renderedRDFaCount++;
264
209
  }
265
210
 
266
- return null;
211
+ return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
267
212
  }
268
213
 
269
214
  /**
270
- * Parse markdown list and enrich with RDFa attributes
215
+ * Process predicates for RDFa attribute generation
271
216
  */
272
- function parseMarkdownList(markdownList, blocks, state) {
273
- const lines = markdownList.split('\n').filter(line => line.trim());
274
- let html = '';
275
- let currentLevel = 0;
276
- let openLi = false;
277
-
278
- lines.forEach((line, index) => {
279
- const indent = line.match(/^(\s*)/)[1].length;
280
- const content = line.trim();
281
-
282
- if (content.startsWith('-')) {
283
- const level = Math.floor(indent / 2); // 2 spaces per level
284
- const itemContent = content.substring(1).trim();
285
-
286
- // Find corresponding block for RDFa attributes
287
- // Try exact match first, then try without MD-LD annotations
288
- const cleanLine = itemContent.replace(/\s*\{[^}]+\}\s*$/, '');
289
- let block = blocks.find(b =>
290
- b.range && state.sourceText.substring(b.range.start, b.range.end).trim() === line
291
- );
292
-
293
- // If no exact match, try matching by clean content
294
- if (!block) {
295
- block = blocks.find(b => {
296
- if (!b.range) return false;
297
- const blockText = state.sourceText.substring(b.range.start, b.range.end).trim();
298
- const blockCleanContent = blockText.replace(/^-\s*/, '').replace(/\s*\{[^}]+\}\s*$/, '');
299
- return blockCleanContent === cleanLine;
300
- });
301
- }
217
+ function processPredicates(predicates, ctx) {
218
+ const literalProps = [];
219
+ const objectProps = [];
220
+ const reverseProps = [];
302
221
 
303
- // Clean content by removing MD-LD annotations
304
- const cleanContent = itemContent.replace(/\s*\{[^}]+\}\s*$/, '');
305
-
306
- // Close lists if going to a higher level
307
- while (currentLevel > level) {
308
- if (openLi) {
309
- html += '</li>';
310
- openLi = false;
311
- }
312
- html += '</ul>';
313
- currentLevel--;
314
- }
222
+ for (const pred of predicates) {
223
+ const iri = typeof pred === 'string' ? pred : pred.iri;
224
+ const expanded = expandIRI(iri, ctx);
315
225
 
316
- // Open lists if going deeper
317
- while (currentLevel < level) {
318
- if (openLi) {
319
- html += '<ul>';
320
- openLi = false;
321
- } else {
322
- html += '<ul>';
323
- }
324
- currentLevel++;
226
+ if (pred.polarity === '-') {
227
+ reverseProps.push(expanded);
228
+ } else {
229
+ if (pred.object && pred.object.termType === 'NamedNode') {
230
+ objectProps.push(expanded);
231
+ } else {
232
+ literalProps.push(expanded);
325
233
  }
234
+ }
235
+ }
326
236
 
327
- // Close previous li if open
328
- if (openLi) {
329
- html += '</li>';
330
- openLi = false;
331
- }
237
+ return { literalProps, objectProps, reverseProps };
238
+ }
332
239
 
333
- const attrs = block ? buildRDFaAttrsFromBlock(block, state.ctx) : '';
334
- html += `<li${attrs}>${escapeHtml(cleanContent)}`;
335
- openLi = true;
336
- }
337
- });
240
+ /**
241
+ * Render heading with RDFa
242
+ */
243
+ function renderHeading(block, rdfaAttrs, state) {
244
+ const level = block.text ? block.text.match(/^#+/)?.[0]?.length || 1 : 1;
245
+ const tag = `h${level}`;
338
246
 
339
- // Close any remaining open li and lists
340
- if (openLi) {
341
- html += '</li>';
342
- }
343
- while (currentLevel > 0) {
344
- html += '</ul>';
345
- currentLevel--;
346
- }
247
+ // Extract clean content by removing MDLD annotations
248
+ let content = extractContentFromRange(state.sourceText, block.range);
249
+ // Remove any remaining {...} patterns
250
+ content = content.replace(/\s*\{[^}]+\}\s*$/g, '').trim();
347
251
 
348
- return html;
252
+ state.output.push(`<${tag}${rdfaAttrs}>${escapeHtml(content)}</${tag}>`);
349
253
  }
350
254
 
351
255
  /**
352
- * Render a single block
256
+ * Render paragraph with inline carriers
353
257
  */
354
- function renderBlock(block, state) {
355
- // Update current subject context
356
- if (block.subject && block.subject !== 'RESET') {
357
- state.currentSubject = block.subject;
358
- }
258
+ function renderParagraph(block, rdfaAttrs, state) {
259
+ const content = renderContentWithCarriers(block, state);
359
260
 
360
- const attrs = buildRDFaAttrsFromBlock(block, state.ctx);
261
+ state.output.push(`<p${rdfaAttrs}>${content}</p>`);
262
+ }
361
263
 
362
- switch (block.type || block.carrierType) {
363
- case 'heading':
364
- const level = block.text ? block.text.match(/^#+/)?.[0]?.length || 1 : 1;
365
- const tag = `h${level}`;
366
- state.output.push(`<${tag}${attrs}>`);
367
- renderBlockContent(block, state);
368
- state.output.push(`</${tag}>`);
369
- break;
264
+ /**
265
+ * Render blockquote with RDFa
266
+ */
267
+ function renderBlockquote(block, rdfaAttrs, state) {
268
+ const content = renderContentWithCarriers(block, state);
370
269
 
371
- case 'para':
372
- state.output.push(`<p${attrs}>`);
373
- renderBlockContent(block, state);
374
- state.output.push(`</p>`);
375
- break;
270
+ state.output.push(`<blockquote${rdfaAttrs}>${content}</blockquote>`);
271
+ }
376
272
 
377
- case 'list':
378
- // List blocks are handled separately in renderListsWithRDFa
379
- break;
273
+ /**
274
+ * Render code block with RDFa
275
+ */
276
+ function renderCode(block, rdfaAttrs, state) {
277
+ const language = block.info || '';
278
+ const content = block.text || '';
380
279
 
381
- case 'quote':
382
- state.output.push(`<blockquote${attrs}>`);
383
- renderBlockContent(block, state);
384
- state.output.push(`</blockquote>`);
385
- break;
280
+ state.output.push(`<pre><code${rdfaAttrs}${language ? ` class="language-${escapeHtml(language)}"` : ''}>${escapeHtml(content)}</code></pre>`);
281
+ }
386
282
 
387
- case 'code':
388
- const language = block.info || '';
389
- state.output.push(`<pre><code${attrs}${language ? ` class="language-${escapeHtml(language)}"` : ''}>`);
390
- state.output.push(escapeHtml(block.text || ''));
391
- state.output.push(`</code></pre>`);
392
- break;
283
+ /**
284
+ * Render generic div with RDFa
285
+ */
286
+ function renderDiv(block, rdfaAttrs, state) {
287
+ const content = renderContentWithCarriers(block, state);
393
288
 
394
- default:
395
- // Default rendering as paragraph
396
- state.output.push(`<div${attrs}>`);
397
- renderBlockContent(block, state);
398
- state.output.push(`</div>`);
399
- }
289
+ state.output.push(`<div${rdfaAttrs}>${content}</div>`);
400
290
  }
401
291
 
402
292
  /**
403
- * Render block content with inline carriers using position-based lookup
293
+ * Render content with inline carriers using origin positions
404
294
  */
405
- function renderBlockContent(block, state) {
406
- if (!block.text || !block.carriers || block.carriers.length === 0) {
407
- state.output.push(escapeHtml(block.text || ''));
408
- return;
295
+ function renderContentWithCarriers(block, state) {
296
+ if (!block.carriers || block.carriers.length === 0) {
297
+ const content = extractCleanContent(state.sourceText, block.range);
298
+ return escapeHtml(content);
409
299
  }
410
300
 
411
- // Build position map: for each carrier, find where it appears in cleaned text
412
- const carrierMap = buildCarrierPositionMap(block.text, block.carriers);
301
+ // Build carrier position map using origin data
302
+ const carrierMap = buildCarrierPositionMap(block);
413
303
 
414
- // Render text with carriers inserted at correct positions
415
- const html = renderTextWithCarriers(block.text, carrierMap, state);
416
- state.output.push(html);
304
+ // Extract clean text without carriers (they'll be added separately)
305
+ const cleanText = extractCleanContent(state.sourceText, block.range);
306
+
307
+ // Render with carriers at exact positions
308
+ return renderTextWithCarriers(cleanText, carrierMap, state);
309
+ }
310
+
311
+ /**
312
+ * Extract clean content by removing all MDLD annotations
313
+ */
314
+ function extractCleanContent(sourceText, range) {
315
+ if (!range || !sourceText) return '';
316
+
317
+ let text = sourceText.substring(range[0], range[1]);
318
+
319
+ // Remove MDLD annotation patterns
320
+ // 1. Remove {...} at end of lines (including multiline)
321
+ text = text.replace(/\s*\{[^}]*\}\s*$/gm, '');
322
+ // 2. Remove {...} anywhere in content (more aggressive)
323
+ text = text.replace(/\{[^}]*\}/g, '');
324
+ // 3. Clean up extra whitespace and trailing characters
325
+ text = text.replace(/\s+/g, ' ').trim();
326
+ // 4. Remove any remaining annotation fragments like "]"
327
+ text = text.replace(/\]$/, '');
328
+
329
+ return text;
417
330
  }
418
331
 
419
332
  /**
420
- * Build a map of carrier positions in the cleaned text
421
- * Returns array of {pos, carrier} sorted by position
333
+ * Build carrier position map from origin data
422
334
  */
423
- function buildCarrierPositionMap(cleanedText, carriers) {
335
+ function buildCarrierPositionMap(block) {
424
336
  const positionMap = [];
425
337
 
426
- for (const carrier of carriers) {
427
- if (!carrier.text) continue;
428
-
429
- // Find where this carrier text appears in the cleaned text
430
- let searchPos = 0;
431
- let foundPos = cleanedText.indexOf(carrier.text, searchPos);
432
-
433
- // If found, record this position
434
- if (foundPos !== -1) {
435
- // Check if we already have this position (avoid duplicates)
436
- const exists = positionMap.some(m => m.pos === foundPos && m.carrier === carrier);
437
- if (!exists) {
438
- positionMap.push({
439
- pos: foundPos,
440
- carrier: carrier,
441
- length: carrier.text.length
442
- });
443
- }
444
- }
338
+ if (!block.carriers) return positionMap;
339
+
340
+ for (const carrier of block.carriers) {
341
+ if (!carrier.text || !carrier.range) continue;
342
+
343
+ positionMap.push({
344
+ pos: carrier.range[0] - block.range[0], // Relative to block start
345
+ carrier: carrier,
346
+ length: carrier.text.length
347
+ });
445
348
  }
446
349
 
447
- // Sort by position to process in order
448
350
  return positionMap.sort((a, b) => a.pos - b.pos);
449
351
  }
450
352
 
451
353
  /**
452
- * Render text with carriers inserted at mapped positions
354
+ * Render text with carriers at mapped positions
453
355
  */
454
356
  function renderTextWithCarriers(text, carrierMap, state) {
455
357
  if (carrierMap.length === 0) {
@@ -466,7 +368,7 @@ function renderTextWithCarriers(text, carrierMap, state) {
466
368
  }
467
369
 
468
370
  // Render carrier with RDFa
469
- html += renderCarrier(mapping.carrier, state);
371
+ html += renderCarrierWithOrigin(mapping.carrier, state);
470
372
 
471
373
  // Move position past carrier
472
374
  pos = mapping.pos + mapping.length;
@@ -480,195 +382,144 @@ function renderTextWithCarriers(text, carrierMap, state) {
480
382
  return html;
481
383
  }
482
384
 
483
- /**
484
- * Parse inline markdown using carrier positions instead of regex
485
- * This is more reliable since we have exact position data from parsing
486
- */
487
- function parseInlineMarkdown(text, carriers, blockRange, state) {
488
- if (carriers.length === 0) {
489
- return escapeHtml(text);
490
- }
491
-
492
- let html = '';
493
- let pos = 0;
494
-
495
- // Sort carriers by their position in the cleaned text
496
- // Need to map from source range to cleaned text position
497
- const sortedCarriers = carriers
498
- .map(carrier => ({
499
- ...carrier,
500
- cleanedStart: estimateCleanedPosition(carrier, blockRange)
501
- }))
502
- .filter(c => c.cleanedStart !== -1)
503
- .sort((a, b) => a.cleanedStart - b.cleanedStart);
504
-
505
- for (const carrier of sortedCarriers) {
506
- const start = carrier.cleanedStart;
507
- const end = start + carrier.text.length;
508
-
509
- // Add text before the carrier
510
- if (start > pos) {
511
- html += escapeHtml(text.substring(pos, start));
512
- }
513
-
514
- // Render the carrier with RDFa
515
- html += renderCarrier(carrier, state);
385
+ /*
386
+ * Render carrier with origin - based RDFa
387
+ */
388
+ function renderCarrierWithOrigin(carrier, state) {
389
+ const attrs = [];
516
390
 
517
- pos = end;
391
+ // Subject from carrier or current context
392
+ const subject = carrier.subject || state.currentSubject;
393
+ if (!subject || subject === 'RESET' || subject.startsWith('=#') || subject.startsWith('+')) {
394
+ return escapeHtml(carrier.text || '');
518
395
  }
519
396
 
520
- // Add remaining text
521
- if (pos < text.length) {
522
- html += escapeHtml(text.substring(pos));
397
+ // About attribute - use CURIE for consistency
398
+ const shortenedSubject = shortenIRI(expandIRI(subject, state.ctx), state.ctx);
399
+ attrs.push(`about="${escapeHtml(shortenedSubject)}"`);
400
+
401
+ // Types - use CURIEs
402
+ if (carrier.types && carrier.types.length > 0) {
403
+ const types = carrier.types.map(t => {
404
+ const iri = typeof t === 'string' ? t : t.iri;
405
+ return shortenIRI(expandIRI(iri, state.ctx), state.ctx);
406
+ }).join(' ');
407
+ attrs.push(`typeof="${escapeHtml(types)}"`);
523
408
  }
524
409
 
525
- return html;
526
- }
410
+ // Predicates - use CURIEs
411
+ if (carrier.predicates && carrier.predicates.length > 0) {
412
+ const { literalProps, objectProps, reverseProps } = processPredicates(carrier.predicates, state.ctx);
527
413
 
528
- /**
529
- * Estimate where a carrier appears in the cleaned text
530
- * This is approximate but avoids complex offset tracking
531
- */
532
- function estimateCleanedPosition(carrier, blockRange) {
533
- // For now, assume carrier.text is still searchable in the cleaned output
534
- // This works because we preserve word content, just remove annotations
535
- // A more robust version would track offsets during text cleaning
536
- if (!carrier.text) return -1;
537
- return carrier.cleanedStart !== undefined ? carrier.cleanedStart : -1;
538
- }
414
+ if (literalProps.length > 0) {
415
+ const shortProps = literalProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
416
+ attrs.push(`property="${escapeHtml(shortProps)}"`);
417
+ }
418
+ if (objectProps.length > 0) {
419
+ const shortProps = objectProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
420
+ attrs.push(`rel="${escapeHtml(shortProps)}"`);
421
+ }
422
+ if (reverseProps.length > 0) {
423
+ const shortProps = reverseProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
424
+ attrs.push(`rev="${escapeHtml(shortProps)}"`);
425
+ }
426
+ }
539
427
 
540
- /**
541
- * Render a single carrier with RDFa and markdown formatting
542
- */
543
- function renderCarrier(carrier, state) {
544
- const rdfaAttrs = buildRDFaAttrsFromCarrier(carrier, null, state);
428
+ const rdfaAttrs = attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
545
429
 
546
430
  // Render based on carrier type
547
431
  switch (carrier.type) {
548
432
  case 'emphasis':
549
- return `<em${rdfaAttrs}>${escapeHtml(carrier.text)}</em>`;
433
+ return `<em${rdfaAttrs}>${escapeHtml(carrier.text || '')}</em>`;
550
434
  case 'strong':
551
- return `<strong${rdfaAttrs}>${escapeHtml(carrier.text)}</strong>`;
435
+ return `<strong${rdfaAttrs}>${escapeHtml(carrier.text || '')}</strong>`;
552
436
  case 'code':
553
- return `<code${rdfaAttrs}>${escapeHtml(carrier.text)}</code>`;
437
+ return `<code${rdfaAttrs}>${escapeHtml(carrier.text || '')}</code>`;
554
438
  case 'link':
555
- return `<a href="${escapeHtml(carrier.url || '')}"${rdfaAttrs}>${escapeHtml(carrier.text)}</a>`;
439
+ return `<a href="${escapeHtml(carrier.url || '')}"${rdfaAttrs}>${escapeHtml(carrier.text || '')}</a>`;
556
440
  default:
557
- return escapeHtml(carrier.text);
441
+ return `<span${rdfaAttrs}>${escapeHtml(carrier.text || '')}</span>`;
558
442
  }
559
443
  }
560
444
 
561
445
  /**
562
- * Build RDFa attributes from carrier with optional quad validation
446
+ * Render lists using origin tracking
563
447
  */
564
- function buildRDFaAttrsFromCarrier(carrier, state) {
565
- const attrs = [];
566
- const trackableQuads = [];
567
-
568
- // Resolve subject for the carrier
569
- let subject = carrier.subject || state.currentSubject;
448
+ function renderListsWithOrigin(listBlocks, state) {
449
+ // Group list blocks by hierarchy
450
+ const listGroups = groupListBlocks(listBlocks, state.sourceText);
570
451
 
571
- // No subject = no RDFa (dangling annotation)
572
- if (!subject || subject === 'RESET' || subject.startsWith('=#') || subject.startsWith('+')) {
573
- return '';
574
- }
575
-
576
- // Subject is valid, add about attribute
577
- const expanded = expandIRI(subject, state.ctx);
578
- const subjectNode = state.df.namedNode(expanded);
579
- const shortened = shortenIRI(expanded, state.ctx);
580
- attrs.push(`about="${escapeHtml(shortened)}"`);
452
+ listGroups.forEach(group => {
453
+ renderListGroup(group, state);
454
+ });
455
+ }
581
456
 
582
- // Types
583
- if (carrier.types && carrier.types.length > 0) {
584
- const types = carrier.types.map(t => {
585
- const iri = typeof t === 'string' ? t : t.iri;
586
- const expanded = expandIRI(iri, state.ctx);
587
- const typeNode = state.df.namedNode(expanded);
588
-
589
- // Track type quads in strict mode
590
- if (state.strict && state.quads) {
591
- const rdfType = state.df.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
592
- if (quadExists(state.quads, subjectNode, rdfType, typeNode)) {
593
- trackableQuads.push({ subject: subjectNode, predicate: rdfType, object: typeNode });
594
- }
595
- }
457
+ /**
458
+ * Group list blocks by structural hierarchy
459
+ */
460
+ function groupListBlocks(listBlocks, sourceText) {
461
+ const groups = [];
462
+ let currentGroup = null;
596
463
 
597
- return shortenIRI(expanded, state.ctx);
598
- }).join(' ');
599
- attrs.push(`typeof="${escapeHtml(types)}"`);
600
- }
464
+ // Sort by position
465
+ const sortedBlocks = listBlocks.sort((a, b) =>
466
+ (a.range?.start || 0) - (b.range?.start || 0)
467
+ );
601
468
 
602
- // Predicates
603
- if (carrier.predicates && carrier.predicates.length > 0) {
604
- const { literalProps, objectProps, reverseProps } = processPredicates(carrier.predicates, state.ctx);
469
+ for (const block of sortedBlocks) {
470
+ const indent = getIndentLevel(block, sourceText);
605
471
 
606
- if (literalProps.length > 0) {
607
- // Track literal property quads
608
- if (state.strict && state.quads && carrier.text) {
609
- literalProps.forEach(prop => {
610
- const predNode = state.df.namedNode(expandIRI(prop, state.ctx));
611
- const objNode = state.df.literal(carrier.text);
612
- });
472
+ if (indent === 0) {
473
+ // Start new group
474
+ if (currentGroup) {
475
+ groups.push(currentGroup);
476
+ }
477
+ currentGroup = {
478
+ contextName: 'Items',
479
+ blocks: [block]
480
+ };
481
+ } else {
482
+ // Add to current group
483
+ if (currentGroup) {
484
+ currentGroup.blocks.push(block);
485
+ } else {
486
+ currentGroup = {
487
+ contextName: 'Items',
488
+ blocks: [block]
489
+ };
613
490
  }
614
- attrs.push(`property="${escapeHtml(literalProps.join(' '))}"`);
615
- }
616
- if (objectProps.length > 0) {
617
- attrs.push(`rel="${escapeHtml(objectProps.join(' '))}"`);
618
- }
619
- if (reverseProps.length > 0) {
620
- attrs.push(`rev="${escapeHtml(reverseProps.join(' '))}"`);
621
491
  }
622
492
  }
623
493
 
624
- // Track rendered RDFa in strict mode
625
- if (state.strict && trackableQuads.length > 0) {
626
- state.quadMap.push(...trackableQuads);
627
- state.renderedRDFaCount += trackableQuads.length;
494
+ if (currentGroup) {
495
+ groups.push(currentGroup);
628
496
  }
629
497
 
630
- return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
498
+ return groups;
631
499
  }
632
500
 
633
501
  /**
634
- * Build RDFa attributes from block
502
+ * Render a list group with proper structure
635
503
  */
636
- function buildRDFaAttrsFromBlock(block, ctx) {
637
- const attrs = [];
638
-
639
- // Subject
640
- if (block.subject && block.subject !== 'RESET' && !block.subject.startsWith('=#') && !block.subject.startsWith('+')) {
641
- const expanded = expandIRI(block.subject, ctx);
642
- const shortened = shortenIRI(expanded, ctx);
643
- attrs.push(`about="${escapeHtml(shortened)}"`);
644
- }
504
+ function renderListGroup(group, state) {
505
+ state.output.push('<ul>');
645
506
 
646
- // Types
647
- if (block.types && block.types.length > 0) {
648
- const types = block.types.map(t => {
649
- const iri = typeof t === 'string' ? t : t.iri;
650
- const expanded = expandIRI(iri, ctx);
651
- return shortenIRI(expanded, ctx);
652
- }).join(' ');
653
- attrs.push(`typeof="${escapeHtml(types)}"`);
507
+ // Render list items preserving hierarchy
508
+ for (const block of group.blocks) {
509
+ renderListItem(block, state);
654
510
  }
655
511
 
656
- // Predicates using shared utility
657
- if (block.predicates && block.predicates.length > 0) {
658
- const { literalProps, objectProps, reverseProps } = processPredicates(block.predicates, ctx);
512
+ state.output.push('</ul>');
513
+ }
659
514
 
660
- if (literalProps.length > 0) {
661
- attrs.push(`property="${escapeHtml(literalProps.join(' '))}"`);
662
- }
663
- if (objectProps.length > 0) {
664
- attrs.push(`rel="${escapeHtml(objectProps.join(' '))}"`);
665
- }
666
- if (reverseProps.length > 0) {
667
- attrs.push(`rev="${escapeHtml(reverseProps.join(' '))}"`);
668
- }
669
- }
515
+ /**
516
+ * Render individual list item
517
+ */
518
+ function renderListItem(block, state) {
519
+ const rdfaAttrs = buildRDFaFromOrigin(block, state);
520
+ const content = renderContentWithCarriers(block, state);
670
521
 
671
- return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
522
+ state.output.push(`<li${rdfaAttrs}>${content}</li>`);
672
523
  }
673
524
 
674
525
  /**
@@ -702,3 +553,24 @@ function wrapWithRDFaContext(html, ctx) {
702
553
 
703
554
  return `<div${prefixDecl}${vocabDecl}>${html}</div>`;
704
555
  }
556
+
557
+ /**
558
+ * Generate hash for block identification
559
+ */
560
+ function hashBlock(block) {
561
+ const content = `${block.type || block.carrierType}|${block.subject || ''}|${block.text || ''}`;
562
+ return simpleHash(content);
563
+ }
564
+
565
+ /**
566
+ * Simple hash function for block IDs
567
+ */
568
+ function simpleHash(str) {
569
+ let hash = 0;
570
+ for (let i = 0; i < str.length; i++) {
571
+ const char = str.charCodeAt(i);
572
+ hash = ((hash << 5) - hash) + char;
573
+ hash = hash & hash; // Convert to 32-bit integer
574
+ }
575
+ return hash.toString(36);
576
+ }
package/src/utils.js CHANGED
@@ -293,8 +293,8 @@ export function shortenIRI(iri, ctx) {
293
293
 
294
294
  // Token pattern definitions for semantic block parsing
295
295
  const TOKEN_PATTERNS = {
296
- '=#': { kind: 'fragment', extract: t => t.substring(2) },
297
- '+#': { kind: 'softFragment', extract: t => t.substring(2) },
296
+ '=#': { kind: 'fragment', extract: t => t.substring(2).replace('}', '') },
297
+ '+#': { kind: 'softFragment', extract: t => t.substring(2).replace('}', '') },
298
298
  '+': { kind: 'object', extract: t => t.substring(1) },
299
299
  '^^': { kind: 'datatype', extract: t => t.substring(2) },
300
300
  '@': { kind: 'language', extract: t => t.substring(1) },