mdld-parse 0.8.4 → 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 +102 -13
- package/package.json +12 -2
- package/src/generate.js +73 -2
- package/src/index.js +1 -1
- package/src/merge.js +2 -1
- package/src/parse.js +117 -8
- package/src/render.js +400 -277
- package/src/shared.js +16 -2
- package/src/utils.js +2 -2
package/src/render.js
CHANGED
|
@@ -2,422 +2,524 @@ 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.validate - Enable validation
|
|
23
|
-
* @param {boolean} options.
|
|
24
|
-
* @returns {Object} Render result with HTML 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
|
|
28
|
-
const parsed = parse(mdld,
|
|
31
|
+
// Phase 1: Parse MD-LD with full origin tracking
|
|
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);
|
|
38
42
|
|
|
39
|
-
|
|
43
|
+
const result = {
|
|
40
44
|
html: wrapped,
|
|
41
45
|
context: state.ctx,
|
|
42
46
|
metadata: {
|
|
43
47
|
blockCount: parsed.origin.blocks.size,
|
|
44
48
|
quadCount: parsed.quads.length,
|
|
49
|
+
renderedRDFaCount: state.renderedRDFaCount,
|
|
45
50
|
renderTime: Date.now()
|
|
46
51
|
}
|
|
47
52
|
};
|
|
53
|
+
|
|
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
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return result;
|
|
48
63
|
}
|
|
49
64
|
|
|
50
65
|
/**
|
|
51
|
-
* Build render state
|
|
66
|
+
* Build render state using origin system for precise quad tracking
|
|
52
67
|
*/
|
|
53
|
-
function
|
|
54
|
-
// Use the parser's context which already includes document prefixes
|
|
55
|
-
const ctx = parsed.context || { ...DEFAULT_CONTEXT, ...(options.context || {}) };
|
|
56
|
-
|
|
68
|
+
function buildOriginBasedState(parsed, options, mdld) {
|
|
57
69
|
return {
|
|
58
|
-
ctx,
|
|
70
|
+
ctx: parsed.context || { ...DEFAULT_CONTEXT, ...(options.context || {}) },
|
|
59
71
|
df: options.dataFactory || DataFactory,
|
|
60
72
|
baseIRI: options.baseIRI || '',
|
|
61
|
-
sourceText: mdld,
|
|
73
|
+
sourceText: mdld,
|
|
62
74
|
output: [],
|
|
63
75
|
currentSubject: null,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
|
80
|
+
renderedRDFaCount: 0,
|
|
81
|
+
validation: options.validate || false
|
|
67
82
|
};
|
|
68
83
|
}
|
|
69
84
|
|
|
70
85
|
/**
|
|
71
|
-
* Render blocks
|
|
86
|
+
* Render blocks using origin tracking for precise RDFa placement
|
|
72
87
|
*/
|
|
73
|
-
function
|
|
74
|
-
// Sort blocks by
|
|
88
|
+
function renderWithOriginTracking(blocks, state) {
|
|
89
|
+
// Sort blocks by document order
|
|
75
90
|
const sortedBlocks = Array.from(blocks.values()).sort((a, b) => {
|
|
76
91
|
return (a.range?.start || 0) - (b.range?.start || 0);
|
|
77
92
|
});
|
|
78
93
|
|
|
79
|
-
// Separate list blocks
|
|
94
|
+
// Separate list blocks for special handling
|
|
80
95
|
const listBlocks = sortedBlocks.filter(block => block.carrierType === 'list');
|
|
81
96
|
const otherBlocks = sortedBlocks.filter(block => block.carrierType !== 'list');
|
|
82
97
|
|
|
83
|
-
// Render non-list blocks
|
|
98
|
+
// Render non-list blocks
|
|
84
99
|
otherBlocks.forEach(block => {
|
|
85
|
-
|
|
100
|
+
renderBlockWithOrigin(block, state);
|
|
86
101
|
});
|
|
87
102
|
|
|
88
|
-
// Render
|
|
103
|
+
// Render list blocks with proper structure
|
|
89
104
|
if (listBlocks.length > 0) {
|
|
90
|
-
|
|
105
|
+
renderListsWithOrigin(listBlocks, state);
|
|
91
106
|
}
|
|
92
107
|
|
|
93
108
|
return state.output.join('');
|
|
94
109
|
}
|
|
95
110
|
|
|
96
111
|
/**
|
|
97
|
-
* Render
|
|
112
|
+
* Render a single block using origin information
|
|
98
113
|
*/
|
|
99
|
-
function
|
|
100
|
-
//
|
|
101
|
-
|
|
114
|
+
function renderBlockWithOrigin(block, state) {
|
|
115
|
+
// Update subject context
|
|
116
|
+
if (block.subject && block.subject !== 'RESET') {
|
|
117
|
+
state.currentSubject = block.subject;
|
|
118
|
+
}
|
|
102
119
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
120
|
+
// Build RDFa attributes from origin data
|
|
121
|
+
const rdfaAttrs = buildRDFaFromOrigin(block, state);
|
|
122
|
+
|
|
123
|
+
// Store render mapping for validation
|
|
124
|
+
if (rdfaAttrs) {
|
|
125
|
+
state.renderMap.set(block.id || hashBlock(block), rdfaAttrs);
|
|
126
|
+
}
|
|
127
|
+
|
|
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);
|
|
147
|
+
}
|
|
106
148
|
}
|
|
107
149
|
|
|
108
150
|
/**
|
|
109
|
-
*
|
|
151
|
+
* Build RDFa attributes from origin tracking data
|
|
110
152
|
*/
|
|
111
|
-
function
|
|
112
|
-
const
|
|
153
|
+
function buildRDFaFromOrigin(block, state) {
|
|
154
|
+
const attrs = [];
|
|
113
155
|
|
|
114
|
-
//
|
|
115
|
-
|
|
156
|
+
// Skip invalid subjects
|
|
157
|
+
if (!block.subject || block.subject === 'RESET' ||
|
|
158
|
+
block.subject.startsWith('=#') || block.subject.startsWith('+')) {
|
|
159
|
+
return '';
|
|
160
|
+
}
|
|
116
161
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
162
|
+
// Subject resolution - use full IRI for round-trip compatibility
|
|
163
|
+
const expandedSubject = expandIRI(block.subject, state.ctx);
|
|
164
|
+
attrs.push(`about="${escapeHtml(expandedSubject)}"`);
|
|
120
165
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
+
}
|
|
126
174
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
blocks: [block]
|
|
141
|
-
};
|
|
175
|
+
// Predicates from carriers and block
|
|
176
|
+
const allPredicates = [];
|
|
177
|
+
|
|
178
|
+
// Add predicates from block (for paragraph-level annotations)
|
|
179
|
+
if (block.predicates && block.predicates.length > 0) {
|
|
180
|
+
allPredicates.push(...block.predicates);
|
|
181
|
+
}
|
|
182
|
+
|
|
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);
|
|
142
188
|
}
|
|
143
189
|
}
|
|
144
190
|
}
|
|
145
191
|
|
|
146
|
-
if (
|
|
147
|
-
|
|
192
|
+
if (allPredicates.length > 0) {
|
|
193
|
+
const { literalProps, objectProps, reverseProps } = processPredicates(allPredicates, state.ctx);
|
|
194
|
+
|
|
195
|
+
if (literalProps.length > 0) {
|
|
196
|
+
attrs.push(`property="${escapeHtml(literalProps.join(' '))}"`);
|
|
197
|
+
}
|
|
198
|
+
if (objectProps.length > 0) {
|
|
199
|
+
attrs.push(`rel="${escapeHtml(objectProps.join(' '))}"`);
|
|
200
|
+
}
|
|
201
|
+
if (reverseProps.length > 0) {
|
|
202
|
+
attrs.push(`rev="${escapeHtml(reverseProps.join(' '))}"`);
|
|
203
|
+
}
|
|
148
204
|
}
|
|
149
205
|
|
|
150
|
-
|
|
206
|
+
// Count rendered RDFa attributes
|
|
207
|
+
if (attrs.length > 1) { // More than just about
|
|
208
|
+
state.renderedRDFaCount++;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
|
|
151
212
|
}
|
|
152
213
|
|
|
153
214
|
/**
|
|
154
|
-
*
|
|
215
|
+
* Process predicates for RDFa attribute generation
|
|
155
216
|
*/
|
|
156
|
-
function
|
|
157
|
-
|
|
158
|
-
const
|
|
159
|
-
const
|
|
217
|
+
function processPredicates(predicates, ctx) {
|
|
218
|
+
const literalProps = [];
|
|
219
|
+
const objectProps = [];
|
|
220
|
+
const reverseProps = [];
|
|
160
221
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
222
|
+
for (const pred of predicates) {
|
|
223
|
+
const iri = typeof pred === 'string' ? pred : pred.iri;
|
|
224
|
+
const expanded = expandIRI(iri, ctx);
|
|
225
|
+
|
|
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);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
164
235
|
}
|
|
165
236
|
|
|
166
|
-
|
|
167
|
-
|
|
237
|
+
return { literalProps, objectProps, reverseProps };
|
|
238
|
+
}
|
|
168
239
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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}`;
|
|
173
246
|
|
|
174
|
-
//
|
|
175
|
-
|
|
176
|
-
|
|
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();
|
|
177
251
|
|
|
178
|
-
state.output.push(
|
|
252
|
+
state.output.push(`<${tag}${rdfaAttrs}>${escapeHtml(content)}</${tag}>`);
|
|
179
253
|
}
|
|
180
254
|
|
|
181
255
|
/**
|
|
182
|
-
*
|
|
256
|
+
* Render paragraph with inline carriers
|
|
183
257
|
*/
|
|
184
|
-
function
|
|
185
|
-
|
|
258
|
+
function renderParagraph(block, rdfaAttrs, state) {
|
|
259
|
+
const content = renderContentWithCarriers(block, state);
|
|
260
|
+
|
|
261
|
+
state.output.push(`<p${rdfaAttrs}>${content}</p>`);
|
|
262
|
+
}
|
|
186
263
|
|
|
187
|
-
|
|
188
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Render blockquote with RDFa
|
|
266
|
+
*/
|
|
267
|
+
function renderBlockquote(block, rdfaAttrs, state) {
|
|
268
|
+
const content = renderContentWithCarriers(block, state);
|
|
189
269
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
let foundAnchor = null;
|
|
270
|
+
state.output.push(`<blockquote${rdfaAttrs}>${content}</blockquote>`);
|
|
271
|
+
}
|
|
193
272
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Render code block with RDFa
|
|
275
|
+
*/
|
|
276
|
+
function renderCode(block, rdfaAttrs, state) {
|
|
277
|
+
const language = block.info || '';
|
|
278
|
+
const content = block.text || '';
|
|
200
279
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
while (lineEnd < sourceText.length && sourceText[lineEnd] !== '\n') {
|
|
204
|
-
lineEnd++;
|
|
205
|
-
}
|
|
280
|
+
state.output.push(`<pre><code${rdfaAttrs}${language ? ` class="language-${escapeHtml(language)}"` : ''}>${escapeHtml(content)}</code></pre>`);
|
|
281
|
+
}
|
|
206
282
|
|
|
207
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Render generic div with RDFa
|
|
285
|
+
*/
|
|
286
|
+
function renderDiv(block, rdfaAttrs, state) {
|
|
287
|
+
const content = renderContentWithCarriers(block, state);
|
|
208
288
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
foundAnchor = line;
|
|
212
|
-
break;
|
|
213
|
-
}
|
|
289
|
+
state.output.push(`<div${rdfaAttrs}>${content}</div>`);
|
|
290
|
+
}
|
|
214
291
|
|
|
215
|
-
|
|
216
|
-
|
|
292
|
+
/**
|
|
293
|
+
* Render content with inline carriers using origin positions
|
|
294
|
+
*/
|
|
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);
|
|
217
299
|
}
|
|
218
300
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
301
|
+
// Build carrier position map using origin data
|
|
302
|
+
const carrierMap = buildCarrierPositionMap(block);
|
|
303
|
+
|
|
304
|
+
// Extract clean text without carriers (they'll be added separately)
|
|
305
|
+
const cleanText = extractCleanContent(state.sourceText, block.range);
|
|
224
306
|
|
|
225
|
-
|
|
307
|
+
// Render with carriers at exact positions
|
|
308
|
+
return renderTextWithCarriers(cleanText, carrierMap, state);
|
|
226
309
|
}
|
|
227
310
|
|
|
228
311
|
/**
|
|
229
|
-
*
|
|
312
|
+
* Extract clean content by removing all MDLD annotations
|
|
230
313
|
*/
|
|
231
|
-
function
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
let
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
lines
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
let block = blocks.find(b =>
|
|
249
|
-
b.range && state.sourceText.substring(b.range.start, b.range.end).trim() === line
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
// If no exact match, try matching by clean content
|
|
253
|
-
if (!block) {
|
|
254
|
-
block = blocks.find(b => {
|
|
255
|
-
if (!b.range) return false;
|
|
256
|
-
const blockText = state.sourceText.substring(b.range.start, b.range.end).trim();
|
|
257
|
-
const blockCleanContent = blockText.replace(/^-\s*/, '').replace(/\s*\{[^}]+\}\s*$/, '');
|
|
258
|
-
return blockCleanContent === cleanLine;
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// Clean content by removing MD-LD annotations
|
|
263
|
-
const cleanContent = itemContent.replace(/\s*\{[^}]+\}\s*$/, '');
|
|
264
|
-
|
|
265
|
-
// Close lists if going to a higher level
|
|
266
|
-
while (currentLevel > level) {
|
|
267
|
-
if (openLi) {
|
|
268
|
-
html += '</li>';
|
|
269
|
-
openLi = false;
|
|
270
|
-
}
|
|
271
|
-
html += '</ul>';
|
|
272
|
-
currentLevel--;
|
|
273
|
-
}
|
|
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;
|
|
330
|
+
}
|
|
274
331
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
} else {
|
|
281
|
-
html += '<ul>';
|
|
282
|
-
}
|
|
283
|
-
currentLevel++;
|
|
284
|
-
}
|
|
332
|
+
/**
|
|
333
|
+
* Build carrier position map from origin data
|
|
334
|
+
*/
|
|
335
|
+
function buildCarrierPositionMap(block) {
|
|
336
|
+
const positionMap = [];
|
|
285
337
|
|
|
286
|
-
|
|
287
|
-
if (openLi) {
|
|
288
|
-
html += '</li>';
|
|
289
|
-
openLi = false;
|
|
290
|
-
}
|
|
338
|
+
if (!block.carriers) return positionMap;
|
|
291
339
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
openLi = true;
|
|
295
|
-
}
|
|
296
|
-
});
|
|
340
|
+
for (const carrier of block.carriers) {
|
|
341
|
+
if (!carrier.text || !carrier.range) continue;
|
|
297
342
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
html += '</ul>';
|
|
304
|
-
currentLevel--;
|
|
343
|
+
positionMap.push({
|
|
344
|
+
pos: carrier.range[0] - block.range[0], // Relative to block start
|
|
345
|
+
carrier: carrier,
|
|
346
|
+
length: carrier.text.length
|
|
347
|
+
});
|
|
305
348
|
}
|
|
306
349
|
|
|
307
|
-
return
|
|
350
|
+
return positionMap.sort((a, b) => a.pos - b.pos);
|
|
308
351
|
}
|
|
309
352
|
|
|
310
353
|
/**
|
|
311
|
-
* Render
|
|
354
|
+
* Render text with carriers at mapped positions
|
|
312
355
|
*/
|
|
313
|
-
function
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
case 'heading':
|
|
318
|
-
const level = block.text ? block.text.match(/^#+/)?.[0]?.length || 1 : 1;
|
|
319
|
-
const tag = `h${level}`;
|
|
320
|
-
state.output.push(`<${tag}${attrs}>`);
|
|
321
|
-
renderBlockContent(block, state);
|
|
322
|
-
state.output.push(`</${tag}>`);
|
|
323
|
-
break;
|
|
324
|
-
|
|
325
|
-
case 'para':
|
|
326
|
-
state.output.push(`<p${attrs}>`);
|
|
327
|
-
renderBlockContent(block, state);
|
|
328
|
-
state.output.push(`</p>`);
|
|
329
|
-
break;
|
|
356
|
+
function renderTextWithCarriers(text, carrierMap, state) {
|
|
357
|
+
if (carrierMap.length === 0) {
|
|
358
|
+
return escapeHtml(text);
|
|
359
|
+
}
|
|
330
360
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
break;
|
|
361
|
+
let html = '';
|
|
362
|
+
let pos = 0;
|
|
334
363
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
364
|
+
for (const mapping of carrierMap) {
|
|
365
|
+
// Add text before carrier
|
|
366
|
+
if (mapping.pos > pos) {
|
|
367
|
+
html += escapeHtml(text.substring(pos, mapping.pos));
|
|
368
|
+
}
|
|
340
369
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
state.output.push(`<pre><code${attrs}${language ? ` class="language-${escapeHtml(language)}"` : ''}>`);
|
|
344
|
-
state.output.push(escapeHtml(block.text || ''));
|
|
345
|
-
state.output.push(`</code></pre>`);
|
|
346
|
-
break;
|
|
370
|
+
// Render carrier with RDFa
|
|
371
|
+
html += renderCarrierWithOrigin(mapping.carrier, state);
|
|
347
372
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
state.output.push(`<div${attrs}>`);
|
|
351
|
-
renderBlockContent(block, state);
|
|
352
|
-
state.output.push(`</div>`);
|
|
373
|
+
// Move position past carrier
|
|
374
|
+
pos = mapping.pos + mapping.length;
|
|
353
375
|
}
|
|
354
|
-
}
|
|
355
376
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
function renderBlockContent(block, state) {
|
|
360
|
-
// Extract text from source using range information
|
|
361
|
-
if (block.range && state.sourceText) {
|
|
362
|
-
let text = state.sourceText.substring(block.range.start, block.range.end);
|
|
363
|
-
|
|
364
|
-
// Remove semantic block annotations from the text
|
|
365
|
-
if (block.attrsRange) {
|
|
366
|
-
const beforeAttrs = text.substring(0, block.attrsRange.start - block.range.start);
|
|
367
|
-
const afterAttrs = text.substring(block.attrsRange.end - block.range.start);
|
|
368
|
-
text = beforeAttrs + afterAttrs;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// For headings, extract text content from the heading
|
|
372
|
-
if (block.carrierType === 'heading') {
|
|
373
|
-
// Remove heading markers (#) and trim
|
|
374
|
-
const content = text.replace(/^#+\s*/, '').trim();
|
|
375
|
-
state.output.push(escapeHtml(content));
|
|
376
|
-
} else {
|
|
377
|
-
state.output.push(escapeHtml(text.trim()));
|
|
378
|
-
}
|
|
377
|
+
// Add remaining text
|
|
378
|
+
if (pos < text.length) {
|
|
379
|
+
html += escapeHtml(text.substring(pos));
|
|
379
380
|
}
|
|
381
|
+
|
|
382
|
+
return html;
|
|
380
383
|
}
|
|
381
384
|
|
|
382
|
-
|
|
383
|
-
*
|
|
384
|
-
|
|
385
|
-
function
|
|
385
|
+
/*
|
|
386
|
+
* Render carrier with origin - based RDFa
|
|
387
|
+
*/
|
|
388
|
+
function renderCarrierWithOrigin(carrier, state) {
|
|
386
389
|
const attrs = [];
|
|
387
390
|
|
|
388
|
-
// Subject
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
attrs.push(`about="${escapeHtml(shortened)}"`);
|
|
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 || '');
|
|
393
395
|
}
|
|
394
396
|
|
|
395
|
-
//
|
|
396
|
-
|
|
397
|
-
|
|
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 => {
|
|
398
404
|
const iri = typeof t === 'string' ? t : t.iri;
|
|
399
|
-
|
|
400
|
-
return shortenIRI(expanded, ctx);
|
|
405
|
+
return shortenIRI(expandIRI(iri, state.ctx), state.ctx);
|
|
401
406
|
}).join(' ');
|
|
402
407
|
attrs.push(`typeof="${escapeHtml(types)}"`);
|
|
403
408
|
}
|
|
404
409
|
|
|
405
|
-
// Predicates
|
|
406
|
-
if (
|
|
407
|
-
const { literalProps, objectProps, reverseProps } = processPredicates(
|
|
410
|
+
// Predicates - use CURIEs
|
|
411
|
+
if (carrier.predicates && carrier.predicates.length > 0) {
|
|
412
|
+
const { literalProps, objectProps, reverseProps } = processPredicates(carrier.predicates, state.ctx);
|
|
408
413
|
|
|
409
414
|
if (literalProps.length > 0) {
|
|
410
|
-
|
|
415
|
+
const shortProps = literalProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
|
|
416
|
+
attrs.push(`property="${escapeHtml(shortProps)}"`);
|
|
411
417
|
}
|
|
412
418
|
if (objectProps.length > 0) {
|
|
413
|
-
|
|
419
|
+
const shortProps = objectProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
|
|
420
|
+
attrs.push(`rel="${escapeHtml(shortProps)}"`);
|
|
414
421
|
}
|
|
415
422
|
if (reverseProps.length > 0) {
|
|
416
|
-
|
|
423
|
+
const shortProps = reverseProps.map(prop => shortenIRI(prop, state.ctx)).join(' ');
|
|
424
|
+
attrs.push(`rev="${escapeHtml(shortProps)}"`);
|
|
417
425
|
}
|
|
418
426
|
}
|
|
419
427
|
|
|
420
|
-
|
|
428
|
+
const rdfaAttrs = attrs.length > 0 ? ` ${attrs.join(' ')}` : '';
|
|
429
|
+
|
|
430
|
+
// Render based on carrier type
|
|
431
|
+
switch (carrier.type) {
|
|
432
|
+
case 'emphasis':
|
|
433
|
+
return `<em${rdfaAttrs}>${escapeHtml(carrier.text || '')}</em>`;
|
|
434
|
+
case 'strong':
|
|
435
|
+
return `<strong${rdfaAttrs}>${escapeHtml(carrier.text || '')}</strong>`;
|
|
436
|
+
case 'code':
|
|
437
|
+
return `<code${rdfaAttrs}>${escapeHtml(carrier.text || '')}</code>`;
|
|
438
|
+
case 'link':
|
|
439
|
+
return `<a href="${escapeHtml(carrier.url || '')}"${rdfaAttrs}>${escapeHtml(carrier.text || '')}</a>`;
|
|
440
|
+
default:
|
|
441
|
+
return `<span${rdfaAttrs}>${escapeHtml(carrier.text || '')}</span>`;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Render lists using origin tracking
|
|
447
|
+
*/
|
|
448
|
+
function renderListsWithOrigin(listBlocks, state) {
|
|
449
|
+
// Group list blocks by hierarchy
|
|
450
|
+
const listGroups = groupListBlocks(listBlocks, state.sourceText);
|
|
451
|
+
|
|
452
|
+
listGroups.forEach(group => {
|
|
453
|
+
renderListGroup(group, state);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Group list blocks by structural hierarchy
|
|
459
|
+
*/
|
|
460
|
+
function groupListBlocks(listBlocks, sourceText) {
|
|
461
|
+
const groups = [];
|
|
462
|
+
let currentGroup = null;
|
|
463
|
+
|
|
464
|
+
// Sort by position
|
|
465
|
+
const sortedBlocks = listBlocks.sort((a, b) =>
|
|
466
|
+
(a.range?.start || 0) - (b.range?.start || 0)
|
|
467
|
+
);
|
|
468
|
+
|
|
469
|
+
for (const block of sortedBlocks) {
|
|
470
|
+
const indent = getIndentLevel(block, sourceText);
|
|
471
|
+
|
|
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
|
+
};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (currentGroup) {
|
|
495
|
+
groups.push(currentGroup);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return groups;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Render a list group with proper structure
|
|
503
|
+
*/
|
|
504
|
+
function renderListGroup(group, state) {
|
|
505
|
+
state.output.push('<ul>');
|
|
506
|
+
|
|
507
|
+
// Render list items preserving hierarchy
|
|
508
|
+
for (const block of group.blocks) {
|
|
509
|
+
renderListItem(block, state);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
state.output.push('</ul>');
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Render individual list item
|
|
517
|
+
*/
|
|
518
|
+
function renderListItem(block, state) {
|
|
519
|
+
const rdfaAttrs = buildRDFaFromOrigin(block, state);
|
|
520
|
+
const content = renderContentWithCarriers(block, state);
|
|
521
|
+
|
|
522
|
+
state.output.push(`<li${rdfaAttrs}>${content}</li>`);
|
|
421
523
|
}
|
|
422
524
|
|
|
423
525
|
/**
|
|
@@ -451,3 +553,24 @@ function wrapWithRDFaContext(html, ctx) {
|
|
|
451
553
|
|
|
452
554
|
return `<div${prefixDecl}${vocabDecl}>${html}</div>`;
|
|
453
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
|
+
}
|