mdld-parse 0.9.0 → 0.9.2
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 +69 -3
- package/package.json +18 -2
- package/src/highlight.js +362 -0
- package/src/parse.js +120 -100
- package/src/render.js +337 -465
- package/src/tokenizers.js +594 -0
- package/src/utils.js +2 -2
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
|
-
|
|
12
|
-
|
|
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.
|
|
23
|
-
* @param {boolean} options.
|
|
24
|
-
* @returns {Object} Render result with HTML, metadata, and
|
|
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
|
|
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
|
|
31
|
-
const state =
|
|
34
|
+
// Phase 2: Build render state using origin system
|
|
35
|
+
const state = buildOriginBasedState(parsed, options, mdld);
|
|
32
36
|
|
|
33
|
-
// Phase 3: Render
|
|
34
|
-
const html =
|
|
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
|
|
49
|
+
renderedRDFaCount: state.renderedRDFaCount,
|
|
46
50
|
renderTime: Date.now()
|
|
47
51
|
}
|
|
48
52
|
};
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
66
|
+
* Build render state using origin system for precise quad tracking
|
|
64
67
|
*/
|
|
65
|
-
function
|
|
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,
|
|
73
|
+
sourceText: mdld,
|
|
74
74
|
output: [],
|
|
75
75
|
currentSubject: null,
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//
|
|
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
|
-
|
|
81
|
+
validation: options.validate || false
|
|
84
82
|
};
|
|
85
83
|
}
|
|
86
84
|
|
|
87
85
|
/**
|
|
88
|
-
*
|
|
86
|
+
* Render blocks using origin tracking for precise RDFa placement
|
|
89
87
|
*/
|
|
90
|
-
function
|
|
91
|
-
|
|
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
|
|
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
|
|
98
|
+
// Render non-list blocks
|
|
125
99
|
otherBlocks.forEach(block => {
|
|
126
|
-
|
|
100
|
+
renderBlockWithOrigin(block, state);
|
|
127
101
|
});
|
|
128
102
|
|
|
129
|
-
// Render
|
|
103
|
+
// Render list blocks with proper structure
|
|
130
104
|
if (listBlocks.length > 0) {
|
|
131
|
-
|
|
105
|
+
renderListsWithOrigin(listBlocks, state);
|
|
132
106
|
}
|
|
133
107
|
|
|
134
108
|
return state.output.join('');
|
|
135
109
|
}
|
|
136
110
|
|
|
137
111
|
/**
|
|
138
|
-
* Render
|
|
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
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
if (currentGroup) {
|
|
165
|
-
groups.push(currentGroup);
|
|
166
|
-
}
|
|
120
|
+
// Build RDFa attributes from origin data
|
|
121
|
+
const rdfaAttrs = buildRDFaFromOrigin(block, state);
|
|
167
122
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
188
|
-
|
|
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
|
-
*
|
|
151
|
+
* Build RDFa attributes from origin tracking data
|
|
196
152
|
*/
|
|
197
|
-
function
|
|
198
|
-
|
|
199
|
-
const firstBlock = group.blocks[0];
|
|
200
|
-
const listAnchorText = extractListAnchorText(firstBlock, state.sourceText);
|
|
153
|
+
function buildRDFaFromOrigin(block, state) {
|
|
154
|
+
const attrs = [];
|
|
201
155
|
|
|
202
|
-
//
|
|
203
|
-
if (
|
|
204
|
-
|
|
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
|
-
//
|
|
208
|
-
state.
|
|
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
|
-
//
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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
|
-
|
|
220
|
-
|
|
175
|
+
// Predicates from carriers and block
|
|
176
|
+
const allPredicates = [];
|
|
221
177
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
-
//
|
|
229
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
let foundAnchor = null;
|
|
192
|
+
if (allPredicates.length > 0) {
|
|
193
|
+
const { literalProps, objectProps, reverseProps } = processPredicates(allPredicates, state.ctx);
|
|
234
194
|
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
|
211
|
+
return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
|
|
267
212
|
}
|
|
268
213
|
|
|
269
214
|
/**
|
|
270
|
-
*
|
|
215
|
+
* Process predicates for RDFa attribute generation
|
|
271
216
|
*/
|
|
272
|
-
function
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
304
|
-
|
|
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
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
-
|
|
328
|
-
|
|
329
|
-
html += '</li>';
|
|
330
|
-
openLi = false;
|
|
331
|
-
}
|
|
237
|
+
return { literalProps, objectProps, reverseProps };
|
|
238
|
+
}
|
|
332
239
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
-
//
|
|
340
|
-
|
|
341
|
-
|
|
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
|
-
|
|
252
|
+
state.output.push(`<${tag}${rdfaAttrs}>${escapeHtml(content)}</${tag}>`);
|
|
349
253
|
}
|
|
350
254
|
|
|
351
255
|
/**
|
|
352
|
-
* Render
|
|
256
|
+
* Render paragraph with inline carriers
|
|
353
257
|
*/
|
|
354
|
-
function
|
|
355
|
-
|
|
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
|
-
|
|
261
|
+
state.output.push(`<p${rdfaAttrs}>${content}</p>`);
|
|
262
|
+
}
|
|
361
263
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
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
|
-
|
|
372
|
-
|
|
373
|
-
renderBlockContent(block, state);
|
|
374
|
-
state.output.push(`</p>`);
|
|
375
|
-
break;
|
|
270
|
+
state.output.push(`<blockquote${rdfaAttrs}>${content}</blockquote>`);
|
|
271
|
+
}
|
|
376
272
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
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
|
-
|
|
382
|
-
|
|
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
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
|
|
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
|
|
293
|
+
* Render content with inline carriers using origin positions
|
|
404
294
|
*/
|
|
405
|
-
function
|
|
406
|
-
if (!block.
|
|
407
|
-
state.
|
|
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
|
|
412
|
-
const carrierMap = buildCarrierPositionMap(block
|
|
301
|
+
// Build carrier position map using origin data
|
|
302
|
+
const carrierMap = buildCarrierPositionMap(block);
|
|
413
303
|
|
|
414
|
-
//
|
|
415
|
-
const
|
|
416
|
-
|
|
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
|
|
421
|
-
* Returns array of {pos, carrier} sorted by position
|
|
333
|
+
* Build carrier position map from origin data
|
|
422
334
|
*/
|
|
423
|
-
function buildCarrierPositionMap(
|
|
335
|
+
function buildCarrierPositionMap(block) {
|
|
424
336
|
const positionMap = [];
|
|
425
337
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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
|
|
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 +=
|
|
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
|
-
*
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
|
|
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
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
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
|
-
*
|
|
446
|
+
* Render lists using origin tracking
|
|
563
447
|
*/
|
|
564
|
-
function
|
|
565
|
-
|
|
566
|
-
const
|
|
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
|
-
|
|
572
|
-
|
|
573
|
-
|
|
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
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
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
|
-
|
|
598
|
-
|
|
599
|
-
|
|
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
|
-
|
|
603
|
-
|
|
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 (
|
|
607
|
-
//
|
|
608
|
-
if (
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
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
|
-
|
|
625
|
-
|
|
626
|
-
state.quadMap.push(...trackableQuads);
|
|
627
|
-
state.renderedRDFaCount += trackableQuads.length;
|
|
494
|
+
if (currentGroup) {
|
|
495
|
+
groups.push(currentGroup);
|
|
628
496
|
}
|
|
629
497
|
|
|
630
|
-
return
|
|
498
|
+
return groups;
|
|
631
499
|
}
|
|
632
500
|
|
|
633
501
|
/**
|
|
634
|
-
*
|
|
502
|
+
* Render a list group with proper structure
|
|
635
503
|
*/
|
|
636
|
-
function
|
|
637
|
-
|
|
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
|
-
//
|
|
647
|
-
|
|
648
|
-
|
|
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
|
-
|
|
657
|
-
|
|
658
|
-
const { literalProps, objectProps, reverseProps } = processPredicates(block.predicates, ctx);
|
|
512
|
+
state.output.push('</ul>');
|
|
513
|
+
}
|
|
659
514
|
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
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
|
-
|
|
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
|
+
}
|