@dita-viewer/core 0.2.2 → 0.2.4
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/dist/editor/conkeyref.d.ts +2 -2
- package/dist/editor/keySpace.d.ts +57 -3
- package/dist/editor/keySpace.js +112 -5
- package/dist/editor/renderContext.d.ts +10 -0
- package/dist/editor/renderContext.js +18 -3
- package/dist/editor/topicRender.d.ts +12 -1
- package/dist/editor/topicRender.js +8 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.js +8 -3
- package/dist/language/ditaLanguageUtils.d.ts +15 -2
- package/dist/language/ditaLanguageUtils.js +34 -4
- package/dist/parser/ditaParser.js +76 -18
- package/dist/parser/domTypes.d.ts +9 -1
- package/dist/render/baseTypeMap.js +15 -3
- package/dist/render/mapTypeMap.d.ts +6 -0
- package/dist/render/mapTypeMap.js +11 -5
- package/dist/render/renderer.js +87 -2
- package/dist-esm/editor/keySpace.js +107 -7
- package/dist-esm/editor/renderContext.js +19 -4
- package/dist-esm/editor/topicRender.js +8 -4
- package/dist-esm/index.js +2 -2
- package/dist-esm/language/ditaLanguageUtils.js +34 -4
- package/dist-esm/parser/ditaParser.js +76 -18
- package/dist-esm/render/baseTypeMap.js +15 -3
- package/dist-esm/render/mapTypeMap.js +11 -5
- package/dist-esm/render/renderer.js +87 -2
- package/package.json +1 -1
- package/styles/dita-content.css +30 -0
package/dist-esm/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// and holds no copies of ./parser, ./render and ./editor (see README), so they
|
|
6
6
|
// are edited here and released as a new version.
|
|
7
7
|
export { renderTopicXml, buildTitleMap, collectDitamapFilesUpward } from './editor/topicRender';
|
|
8
|
-
export { buildKeySpace, getKeyDefs } from './editor/keySpace';
|
|
8
|
+
export { buildKeySpace, getKeyDefs, getKeyScopes, getTopicScopeChains, createKeyScopeIndex } from './editor/keySpace';
|
|
9
9
|
export { readSourceText, setSourceOverlay, clearSourceOverlay } from './editor/sourceText';
|
|
10
10
|
export { decodeHrefPart } from './editor/refResolvers';
|
|
11
11
|
export { buildKeyMapForDocument, buildKeySpaceForDocument } from './keyMapForDocument';
|
|
@@ -39,7 +39,7 @@ export { BASE_TYPE_RENDERERS } from './render/baseTypeMap';
|
|
|
39
39
|
export { computeKeyrefSpacing, needsSpace } from './render/cjkSpacing';
|
|
40
40
|
export { mergeProfilingAttrs, profilingChipsHtml, profilingKeysAttr } from './render/renderer';
|
|
41
41
|
export { resolveConkeyref } from './editor/conkeyref';
|
|
42
|
-
export { collectMapKeys, isKeyContextAvailable, keySourceMaps, qualifiedNames, qualifiedNamesMulti } from './editor/keySpace';
|
|
42
|
+
export { collectMapKeys, isKeyContextAvailable, keySourceMaps, qualifiedNames, qualifiedNamesMulti, resolveKeyInScope, scopeKeyIndexKey } from './editor/keySpace';
|
|
43
43
|
export { DEFAULT_NOTE_LABELS, IMAGE_DIMENSIONS_CACHE_MAX, URL_SCHEME_RE, ZH_NOTE_LABELS, clearImageDimensionsCache, detectIndexLabel, detectNoteLabels, expandDitamapRefs, makeConrefRangeResolver, makeConrefResolver, makeFileCache, makeFileTitleResolver, makeIsInCurrentBook, readImageDimensions } from './editor/refResolvers';
|
|
44
44
|
export { buildRenderContext } from './editor/renderContext';
|
|
45
45
|
export { clearAllSourceOverlays, dependsOn, noteSourceDependencies, sourceOverlaySize, sourceStamp, trackSourceReads } from './editor/sourceText';
|
|
@@ -6,14 +6,44 @@ import { createBookRoleLabeler } from '../render/mapTypeMap';
|
|
|
6
6
|
// ── Reference attribute scanning ──
|
|
7
7
|
/** Attributes whose values reference other files/keys/elements. */
|
|
8
8
|
const REF_ATTRS = new Set(['href', 'conref', 'conkeyref', 'keyref']);
|
|
9
|
+
// One alternation, so whichever region opens first wins: a "<!--" inside a
|
|
10
|
+
// CDATA section is CDATA text, and a "<![CDATA[" inside a comment is comment
|
|
11
|
+
// text. An opener with no terminator matches nothing and is left alone, so a
|
|
12
|
+
// stray "<!--" cannot hide every reference after it.
|
|
13
|
+
const NON_MARKUP_RE = /<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<\?[\s\S]*?\?>/g;
|
|
14
|
+
/**
|
|
15
|
+
* Blanks comments, CDATA sections and processing instructions with spaces of
|
|
16
|
+
* the same length (line breaks are kept), so every offset into the result is
|
|
17
|
+
* also an offset into `text`. What is left is only live markup and character
|
|
18
|
+
* data, which is what a reference scan should look at.
|
|
19
|
+
*/
|
|
20
|
+
function blankNonMarkup(text) {
|
|
21
|
+
if (!text.includes('<!') && !text.includes('<?'))
|
|
22
|
+
return text;
|
|
23
|
+
return text.replace(NON_MARKUP_RE, (region) => region.replace(/[^\r\n]/g, ' '));
|
|
24
|
+
}
|
|
9
25
|
const TAG_RE = /<([A-Za-z][\w.-]*)((?:"[^"]*"|'[^']*'|[^>"'])*)>/g;
|
|
10
26
|
const ATTR_RE = /([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
|
|
11
|
-
/**
|
|
12
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Collects every reference-bearing attribute in the document text.
|
|
29
|
+
*
|
|
30
|
+
* `attrs` names the attributes to collect and defaults to the navigable set
|
|
31
|
+
* (href / conref / conkeyref / keyref). A caller that needs more -- the rename
|
|
32
|
+
* participant also has to follow conrefend, copy-to, data and longdescref --
|
|
33
|
+
* passes its own set, so go-to-definition and find-references keep seeing
|
|
34
|
+
* exactly what they saw before.
|
|
35
|
+
*
|
|
36
|
+
* Comments, CDATA sections and processing instructions are not scanned: an
|
|
37
|
+
* `href` inside them is example text or a commented-out reference, not a live
|
|
38
|
+
* one, so rename, go-to-definition, find-references and diagnostics all leave
|
|
39
|
+
* it alone. Offsets still index the original `text`.
|
|
40
|
+
*/
|
|
41
|
+
export function collectRefEntries(text, attrs = REF_ATTRS) {
|
|
13
42
|
const entries = [];
|
|
43
|
+
const scanText = blankNonMarkup(text);
|
|
14
44
|
TAG_RE.lastIndex = 0;
|
|
15
45
|
let tagMatch;
|
|
16
|
-
while ((tagMatch = TAG_RE.exec(
|
|
46
|
+
while ((tagMatch = TAG_RE.exec(scanText)) !== null) {
|
|
17
47
|
const tagName = tagMatch[1];
|
|
18
48
|
const attrSegment = tagMatch[2];
|
|
19
49
|
if (!attrSegment)
|
|
@@ -33,7 +63,7 @@ export function collectRefEntries(text) {
|
|
|
33
63
|
}
|
|
34
64
|
ATTR_RE.lastIndex = 0;
|
|
35
65
|
while ((am = ATTR_RE.exec(attrSegment)) !== null) {
|
|
36
|
-
if (!
|
|
66
|
+
if (!attrs.has(am[1]))
|
|
37
67
|
continue;
|
|
38
68
|
const eqAndQuote = am[0].indexOf('=') + 1 + (am[0].slice(am[0].indexOf('=') + 1).match(/["']/)?.index ?? 0);
|
|
39
69
|
const valueStart = segmentOffset + am.index + eqAndQuote + 1;
|
|
@@ -63,6 +63,7 @@ function makeRange() {
|
|
|
63
63
|
*/
|
|
64
64
|
function scanTagEvents(xml) {
|
|
65
65
|
const events = [];
|
|
66
|
+
const comments = [];
|
|
66
67
|
let i = 0;
|
|
67
68
|
while (i < xml.length) {
|
|
68
69
|
const lt = xml.indexOf('<', i);
|
|
@@ -70,7 +71,13 @@ function scanTagEvents(xml) {
|
|
|
70
71
|
break;
|
|
71
72
|
if (xml.startsWith('<!--', lt)) {
|
|
72
73
|
const end = xml.indexOf('-->', lt + 4);
|
|
73
|
-
|
|
74
|
+
if (end < 0) {
|
|
75
|
+
// Unterminated: sax rejects it in strict mode, so nothing will ask
|
|
76
|
+
// for a position -- record nothing rather than invent one.
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
comments.push({ start: lt, end: end + 3, contentStart: lt + 4, contentEnd: end });
|
|
80
|
+
i = end + 3;
|
|
74
81
|
continue;
|
|
75
82
|
}
|
|
76
83
|
if (xml.startsWith('<![CDATA[', lt)) {
|
|
@@ -128,7 +135,7 @@ function scanTagEvents(xml) {
|
|
|
128
135
|
events.push({ name, selfClosing, isClose, start: lt, end: gt + 1 });
|
|
129
136
|
i = gt + 1;
|
|
130
137
|
}
|
|
131
|
-
return events;
|
|
138
|
+
return { tags: events, comments };
|
|
132
139
|
}
|
|
133
140
|
function makeParser(tagMap) {
|
|
134
141
|
const parseBaseType = makeParseBaseType(tagMap);
|
|
@@ -147,25 +154,33 @@ function makeParser(tagMap) {
|
|
|
147
154
|
// onclosetag sax fires immediately after for it.
|
|
148
155
|
let selfClosingEnd;
|
|
149
156
|
// The source scan feeding exact tag offsets to the handlers below.
|
|
150
|
-
const tagEvents = scanTagEvents(xml);
|
|
157
|
+
const { tags: tagEvents, comments: commentEvents } = scanTagEvents(xml);
|
|
151
158
|
let q = 0;
|
|
159
|
+
let cq = 0;
|
|
152
160
|
// (line, col) of a raw offset, via an ever-forward line-start cursor.
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
let
|
|
159
|
-
let
|
|
160
|
-
|
|
161
|
-
i =
|
|
162
|
-
line
|
|
163
|
-
nl = xml.indexOf('\n', i);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
161
|
+
// Offsets are consumed in document order (tags and comments each in their
|
|
162
|
+
// own stream), so a cursor never rewinds -- which is why each stream gets
|
|
163
|
+
// its own: a comment can sit between two tags that the tag queue has
|
|
164
|
+
// already looked past.
|
|
165
|
+
function makeLocator() {
|
|
166
|
+
let lastLineStart = 0;
|
|
167
|
+
let currentLine = 0;
|
|
168
|
+
return function locAt(pos) {
|
|
169
|
+
let i = lastLineStart;
|
|
170
|
+
let line = currentLine;
|
|
171
|
+
let nl = xml.indexOf('\n', i);
|
|
172
|
+
while (nl !== -1 && nl < pos) {
|
|
173
|
+
i = nl + 1;
|
|
174
|
+
line++;
|
|
175
|
+
nl = xml.indexOf('\n', i);
|
|
176
|
+
}
|
|
177
|
+
lastLineStart = i;
|
|
178
|
+
currentLine = line;
|
|
179
|
+
return { line, col: pos - i };
|
|
180
|
+
};
|
|
168
181
|
}
|
|
182
|
+
const locAt = makeLocator();
|
|
183
|
+
const commentLocAt = makeLocator();
|
|
169
184
|
/**
|
|
170
185
|
* Pops the queue's next tag of the given kind, trusting sax as the
|
|
171
186
|
* authority on order but the scan as the authority on position. sax
|
|
@@ -191,6 +206,23 @@ function makeParser(tagMap) {
|
|
|
191
206
|
function saxFallbackEnd() {
|
|
192
207
|
return { line: parser.line, col: parser.column };
|
|
193
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Pops the scan's next comment whose inner text matches what sax reported.
|
|
211
|
+
* Matched by content, not by position in the stream alone: `<!--` inside a
|
|
212
|
+
* CDATA section or an attribute value is not a comment, so the two scans
|
|
213
|
+
* may legitimately disagree, and content is the one thing both agree on.
|
|
214
|
+
* A window keeps a single disagreement from desynchronising the rest.
|
|
215
|
+
*/
|
|
216
|
+
function nextComment(text) {
|
|
217
|
+
for (let k = cq; k < Math.min(cq + 8, commentEvents.length); k++) {
|
|
218
|
+
const e = commentEvents[k];
|
|
219
|
+
if (xml.slice(e.contentStart, e.contentEnd) !== text)
|
|
220
|
+
continue;
|
|
221
|
+
cq = k + 1;
|
|
222
|
+
return e;
|
|
223
|
+
}
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
194
226
|
function flushText() {
|
|
195
227
|
if (currentText.length > 0) {
|
|
196
228
|
const parent = stack[stack.length - 1];
|
|
@@ -290,6 +322,32 @@ function makeParser(tagMap) {
|
|
|
290
322
|
}
|
|
291
323
|
currentText += text;
|
|
292
324
|
};
|
|
325
|
+
// An XML comment becomes a node of its own, kept where it was written:
|
|
326
|
+
// the preview shows a comment's text in place (the way Oxygen's Author
|
|
327
|
+
// mode does), so dropping comments here made them invisible. Its range
|
|
328
|
+
// spans `<!--` through `-->` so a click in the preview lands on the
|
|
329
|
+
// comment's own source line. The pending text is flushed first, so the
|
|
330
|
+
// text either side of it stays on the right side of the comment.
|
|
331
|
+
parser.oncomment = (comment) => {
|
|
332
|
+
flushText();
|
|
333
|
+
const parent = stack[stack.length - 1];
|
|
334
|
+
const event = nextComment(comment);
|
|
335
|
+
const start = event ? commentLocAt(event.start) : saxFallbackEnd();
|
|
336
|
+
const end = event ? commentLocAt(event.end) : saxFallbackEnd();
|
|
337
|
+
if (!parent)
|
|
338
|
+
return;
|
|
339
|
+
parent.children.push({
|
|
340
|
+
type: 'comment',
|
|
341
|
+
comment,
|
|
342
|
+
children: [],
|
|
343
|
+
sourceRange: {
|
|
344
|
+
startLine: start.line,
|
|
345
|
+
startCol: start.col,
|
|
346
|
+
endLine: end.line,
|
|
347
|
+
endCol: end.col,
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
};
|
|
293
351
|
parser.onerror = (err) => {
|
|
294
352
|
throw new Error(`SAX parse error at line ${parser.line}:${parser.column}: ${err.message}`);
|
|
295
353
|
};
|
|
@@ -224,7 +224,7 @@ function expandMfenced(node) {
|
|
|
224
224
|
// spurious extra separator characters for the whitespace "children"
|
|
225
225
|
// surrounding the real one.
|
|
226
226
|
const items = (node.children || [])
|
|
227
|
-
.filter((c) => c.type !== 'text' || (c.text || '').trim() !== '')
|
|
227
|
+
.filter((c) => c.type !== 'comment' && (c.type !== 'text' || (c.text || '').trim() !== ''))
|
|
228
228
|
.map(serializeForeignContent);
|
|
229
229
|
let inner = '';
|
|
230
230
|
if (open !== '')
|
|
@@ -242,6 +242,14 @@ function expandMfenced(node) {
|
|
|
242
242
|
inner += `<mo>${escapeHtml(close)}</mo>`;
|
|
243
243
|
return `<mrow>${inner}</mrow>`;
|
|
244
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* True when the element has any child that is author content. An XML comment
|
|
247
|
+
* is a node of its own in the parse tree (shown in place by the renderer) but
|
|
248
|
+
* is never content: `<xref href="a.dita"><!-- note --></xref>` has no label.
|
|
249
|
+
*/
|
|
250
|
+
function hasContentChildren(node) {
|
|
251
|
+
return (node.children || []).some((c) => c.type !== 'comment');
|
|
252
|
+
}
|
|
245
253
|
function serializeForeignContent(node) {
|
|
246
254
|
if (node.type === 'text')
|
|
247
255
|
return escapeHtml(node.text || '');
|
|
@@ -776,6 +784,10 @@ export const BASE_TYPE_RENDERERS = {
|
|
|
776
784
|
},
|
|
777
785
|
'topic/pre': (_node, ctx, renderChildren) => `<pre class="pre preformatted">${renderChildren(_node, ctx)}</pre>`,
|
|
778
786
|
'topic/xref': (node, ctx, renderChildren) => {
|
|
787
|
+
// An xref whose only child is an XML comment (<!-- Pull title from ... -->)
|
|
788
|
+
// has no author-written label: it falls back to the resolved title / href
|
|
789
|
+
// exactly as an empty one does, rather than showing the comment as its
|
|
790
|
+
// link text. hasContentChildren() is the single place that says so.
|
|
779
791
|
const href = getAttr(node, 'href') || '';
|
|
780
792
|
// No target at all: an xref with neither @href nor a keyref that
|
|
781
793
|
// resolves to one. Dropping the element silently (the old behaviour)
|
|
@@ -784,7 +796,7 @@ export const BASE_TYPE_RENDERERS = {
|
|
|
784
796
|
// when it does not (the same treatment topic/ph gives an unresolved
|
|
785
797
|
// keyref) -- means a broken xref reads as one instead of vanishing.
|
|
786
798
|
if (!href) {
|
|
787
|
-
if (node
|
|
799
|
+
if (hasContentChildren(node)) {
|
|
788
800
|
const content = renderChildren(node, ctx);
|
|
789
801
|
const keyref = getAttr(node, 'keyref');
|
|
790
802
|
return keyref
|
|
@@ -797,7 +809,7 @@ export const BASE_TYPE_RENDERERS = {
|
|
|
797
809
|
return `<span class="xref unresolved-keyref" title="Unresolved key: ${escapeAttr(keyref)}">[${escapeAttr(keyref)}]</span>`;
|
|
798
810
|
}
|
|
799
811
|
let content;
|
|
800
|
-
if (node
|
|
812
|
+
if (hasContentChildren(node)) {
|
|
801
813
|
content = renderChildren(node, ctx);
|
|
802
814
|
}
|
|
803
815
|
else if (href.startsWith('#')) {
|
|
@@ -459,10 +459,15 @@ export function isDitamapRef(node) {
|
|
|
459
459
|
const pathPart = href.split('#')[0].toLowerCase();
|
|
460
460
|
return pathPart.endsWith('.ditamap') || node.attributes?.format === 'ditamap';
|
|
461
461
|
}
|
|
462
|
-
function collectEntriesRecursive(node, depth, result, resolveKey, roleLabel, inheritedProcessingRole = 'normal') {
|
|
462
|
+
function collectEntriesRecursive(node, depth, result, resolveKey, roleLabel, inheritedProcessingRole = 'normal', outerScopes = [[]]) {
|
|
463
463
|
if (node.type !== 'element')
|
|
464
464
|
return;
|
|
465
465
|
const baseType = node.baseType;
|
|
466
|
+
// A keyscope names a scope for the element carrying it and everything below.
|
|
467
|
+
const keyscope = getAttr(node, 'keyscope');
|
|
468
|
+
const scopeChains = keyscope
|
|
469
|
+
? outerScopes.flatMap((chain) => keyscope.trim().split(/\s+/).filter(Boolean).map((name) => chain.concat(name)))
|
|
470
|
+
: outerScopes;
|
|
466
471
|
// Skip reltable and its children
|
|
467
472
|
if (baseType === 'map/reltable')
|
|
468
473
|
return;
|
|
@@ -482,7 +487,7 @@ function collectEntriesRecursive(node, depth, result, resolveKey, roleLabel, inh
|
|
|
482
487
|
// level deeper just because expandDitamapRefs happened to splice them
|
|
483
488
|
// in as DOM children of this wrapper node.
|
|
484
489
|
for (const child of node.children || []) {
|
|
485
|
-
collectEntriesRecursive(child, depth, result, resolveKey, roleLabel, inheritedProcessingRole);
|
|
490
|
+
collectEntriesRecursive(child, depth, result, resolveKey, roleLabel, inheritedProcessingRole, scopeChains);
|
|
486
491
|
}
|
|
487
492
|
return;
|
|
488
493
|
}
|
|
@@ -519,21 +524,22 @@ function collectEntriesRecursive(node, depth, result, resolveKey, roleLabel, inh
|
|
|
519
524
|
// been) serves no one.
|
|
520
525
|
role: effectiveProcessingRole === 'resource-only' ? undefined : roleLabel(node.tagName, depth),
|
|
521
526
|
resourceOnly: effectiveProcessingRole === 'resource-only',
|
|
527
|
+
scopeChains,
|
|
522
528
|
});
|
|
523
529
|
// Recurse children at depth+1
|
|
524
530
|
for (const child of node.children || []) {
|
|
525
|
-
collectEntriesRecursive(child, depth + 1, result, resolveKey, roleLabel, effectiveProcessingRole);
|
|
531
|
+
collectEntriesRecursive(child, depth + 1, result, resolveKey, roleLabel, effectiveProcessingRole, scopeChains);
|
|
526
532
|
}
|
|
527
533
|
}
|
|
528
534
|
else if (baseType === 'map/topicgroup' || baseType === 'map/bookmap-structural') {
|
|
529
535
|
// topicgroup / bookmap-structural: no entry itself, but recurse at same depth
|
|
530
536
|
for (const child of node.children || []) {
|
|
531
|
-
collectEntriesRecursive(child, depth, result, resolveKey, roleLabel, effectiveProcessingRole);
|
|
537
|
+
collectEntriesRecursive(child, depth, result, resolveKey, roleLabel, effectiveProcessingRole, scopeChains);
|
|
532
538
|
}
|
|
533
539
|
}
|
|
534
540
|
else {
|
|
535
541
|
for (const child of node.children || []) {
|
|
536
|
-
collectEntriesRecursive(child, depth, result, resolveKey, roleLabel, effectiveProcessingRole);
|
|
542
|
+
collectEntriesRecursive(child, depth, result, resolveKey, roleLabel, effectiveProcessingRole, scopeChains);
|
|
537
543
|
}
|
|
538
544
|
}
|
|
539
545
|
}
|
|
@@ -249,7 +249,17 @@ function stampSourceRange(node, range) {
|
|
|
249
249
|
function mergeConrefTarget(node, target) {
|
|
250
250
|
const restAttrs = Object.fromEntries(Object.entries(node.attributes || {}).filter(([k]) => k !== 'conref' && k !== 'conrefend' && k !== 'conkeyref'));
|
|
251
251
|
if (target.baseType && target.baseType === node.baseType) {
|
|
252
|
-
|
|
252
|
+
// The merged element keeps the referencing element's attributes, so a
|
|
253
|
+
// keyref written on the target itself (`<ph id="x" keyref="prod"/>`) would
|
|
254
|
+
// vanish and the reuse would render empty. Conref resolves before keys do,
|
|
255
|
+
// so the pulled-in element's keyref is resolved here, in this document's
|
|
256
|
+
// key space. The referencing element's own keyref still wins.
|
|
257
|
+
const carried = target.attributes?.keyref && !restAttrs.keyref ? { keyref: target.attributes.keyref } : {};
|
|
258
|
+
return {
|
|
259
|
+
...node,
|
|
260
|
+
children: (target.children || []).map((c) => stampSourceRange(c, node.sourceRange)),
|
|
261
|
+
attributes: { ...carried, ...restAttrs },
|
|
262
|
+
};
|
|
253
263
|
}
|
|
254
264
|
// Cross-type merge: the target's element shape wins, but never its source
|
|
255
265
|
// position — the target and all of its descendants sit at the referencing
|
|
@@ -481,10 +491,70 @@ function renderConrefRange(node, range, context, conref) {
|
|
|
481
491
|
})
|
|
482
492
|
.join('');
|
|
483
493
|
}
|
|
494
|
+
// Parents whose content model is element-only, so a comment written inside
|
|
495
|
+
// them sits between blocks rather than inside a sentence and should get a
|
|
496
|
+
// block of its own. Everything else -- p, ph, li, entry, title, note text --
|
|
497
|
+
// holds mixed content, where an inline <span> is both valid and what the
|
|
498
|
+
// author saw. This is the same inline/block distinction profiling markers
|
|
499
|
+
// make (INLINE_PROFILING_BASETYPES), stated the other way round: there,
|
|
500
|
+
// inline was the short list; here the inline cases are the majority, and
|
|
501
|
+
// getting it wrong on the block side is the harmless direction (a comment on
|
|
502
|
+
// its own line inside a phrase) while a <div> inside a <p> would make the
|
|
503
|
+
// browser close the paragraph and split the sentence around it.
|
|
504
|
+
const BLOCK_COMMENT_PARENTS = new Set([
|
|
505
|
+
'topic/body',
|
|
506
|
+
'topic/section',
|
|
507
|
+
'topic/example',
|
|
508
|
+
'topic/fig',
|
|
509
|
+
'topic/div',
|
|
510
|
+
'topic/bodydiv',
|
|
511
|
+
'topic/sectiondiv',
|
|
512
|
+
'topic/topic',
|
|
513
|
+
'topic/abstract',
|
|
514
|
+
'topic/related-links',
|
|
515
|
+
'topic/imagemap',
|
|
516
|
+
// List containers: a comment written between two <li>s is not part of
|
|
517
|
+
// either item's sentence, and an inline span there would be a stray
|
|
518
|
+
// fragment of the previous item.
|
|
519
|
+
'topic/ul',
|
|
520
|
+
'topic/ol',
|
|
521
|
+
'topic/sl',
|
|
522
|
+
'topic/dl',
|
|
523
|
+
]);
|
|
524
|
+
/**
|
|
525
|
+
* An XML comment, shown in the flow at the position it was authored in --
|
|
526
|
+
* Oxygen's Author mode shows the comment text the same way, instead of the
|
|
527
|
+
* reader having to open the source. The text is the comment's inner content
|
|
528
|
+
* with the `<!-- -->` delimiters stripped: the delimiters are markup, and a
|
|
529
|
+
* comment's *text* is what the author wanted read. data-dita-tagname makes the
|
|
530
|
+
* story explicit through the Tags toggle (its tooltip reads "comment"), and
|
|
531
|
+
* data-line* keeps preview clicks and scroll-sync landing on the comment's own
|
|
532
|
+
* source lines -- its range spans `<!--` through `-->`.
|
|
533
|
+
*
|
|
534
|
+
* An empty or whitespace-only comment renders nothing: it is a spelling of
|
|
535
|
+
* "nothing here", and an empty box in the page would only be noise.
|
|
536
|
+
*/
|
|
537
|
+
function renderComment(node, context) {
|
|
538
|
+
const text = (node.comment || '').replace(/\r\n?/g, '\n').trim();
|
|
539
|
+
if (!text)
|
|
540
|
+
return '';
|
|
541
|
+
const block = BLOCK_COMMENT_PARENTS.has(context.parentBaseType || '');
|
|
542
|
+
const tag = block ? 'div' : 'span';
|
|
543
|
+
const cls = block ? 'dv-comment dv-comment--block' : 'dv-comment';
|
|
544
|
+
const range = node.sourceRange;
|
|
545
|
+
return (`<${tag} class="${cls}" data-dita-tagname="comment"` +
|
|
546
|
+
` data-line="${range.startLine}" data-end-line="${range.endLine}"` +
|
|
547
|
+
` data-start-col="${range.startCol}" data-end-col="${range.endCol}">` +
|
|
548
|
+
escapeHtml(text) +
|
|
549
|
+
`</${tag}>`);
|
|
550
|
+
}
|
|
484
551
|
export function renderElement(node, context, conrefMark) {
|
|
485
552
|
if (node.type === 'text') {
|
|
486
553
|
return escapeHtml(node.text || '');
|
|
487
554
|
}
|
|
555
|
+
if (node.type === 'comment') {
|
|
556
|
+
return renderComment(node, context);
|
|
557
|
+
}
|
|
488
558
|
const conref = node.attributes?.conref;
|
|
489
559
|
const conrefend = node.attributes?.conrefend;
|
|
490
560
|
if (conref && conrefend && context.resolveConrefRange && !context.conrefChain?.has(conref)) {
|
|
@@ -502,8 +572,23 @@ export function renderElement(node, context, conrefMark) {
|
|
|
502
572
|
// The element's own conref wins over a mark handed down by a range.
|
|
503
573
|
return renderEffectiveNode(effectiveNode, context, resolved.chainKey, resolved.mark ?? conrefMark);
|
|
504
574
|
}
|
|
575
|
+
// Parents whose children must not include a comment span or block:
|
|
576
|
+
// - table machinery: a <span>/<div> directly inside <table>, <tbody> or <tr>
|
|
577
|
+
// is foster-parented by the browser to just before the whole table, so the
|
|
578
|
+
// comment would show up away from where it was written;
|
|
579
|
+
// - preformatted blocks: the comment's text would read, and copy, as code;
|
|
580
|
+
// - titles: the heading's text feeds the outline / table of contents.
|
|
581
|
+
// Keyed by the baseType of the element being rendered (the real parent), not
|
|
582
|
+
// by the overloaded context.parentBaseType.
|
|
583
|
+
const NO_COMMENT_CHILDREN = new Set([
|
|
584
|
+
'topic/table', 'topic/tgroup', 'topic/thead', 'topic/tbody', 'topic/row',
|
|
585
|
+
'topic/simpletable', 'topic/sthead', 'topic/strow',
|
|
586
|
+
'topic/pre', 'topic/codeblock', 'topic/lines',
|
|
587
|
+
'topic/title',
|
|
588
|
+
]);
|
|
505
589
|
function renderChildren(node, context) {
|
|
506
|
-
const
|
|
590
|
+
const skipComments = !!node.baseType && NO_COMMENT_CHILDREN.has(node.baseType);
|
|
591
|
+
const children = (node.children || []).filter((c) => !(skipComments && c.type === 'comment'));
|
|
507
592
|
// Resolved key text can leave CJK and Latin glued together (`打开` + `ABC`).
|
|
508
593
|
// Insert a display-only space outside the key element; the source is untouched.
|
|
509
594
|
const spacing = computeKeyrefSpacing(children, node.baseType, context.resolveKey);
|
package/package.json
CHANGED
package/styles/dita-content.css
CHANGED
|
@@ -51,6 +51,9 @@
|
|
|
51
51
|
--color-profiling-label-text: #2a3a12;
|
|
52
52
|
--color-indexterm-chip-bg: #e6f0fa;
|
|
53
53
|
--color-indexterm-chip-text: #2c5282;
|
|
54
|
+
--color-comment-bg: #efefef;
|
|
55
|
+
--color-comment-border: #c4c4c4;
|
|
56
|
+
--color-comment-text: #6b6b6b;
|
|
54
57
|
--color-menucascade-arrow: #767676;
|
|
55
58
|
}
|
|
56
59
|
|
|
@@ -71,6 +74,9 @@
|
|
|
71
74
|
--color-profiling-label-text: #eef7dc;
|
|
72
75
|
--color-indexterm-chip-bg: #1f2f42;
|
|
73
76
|
--color-indexterm-chip-text: #9dc4ea;
|
|
77
|
+
--color-comment-bg: #2b2b2b;
|
|
78
|
+
--color-comment-border: #4d4d4d;
|
|
79
|
+
--color-comment-text: #9a9a9a;
|
|
74
80
|
--color-menucascade-arrow: #aaaaaa;
|
|
75
81
|
}
|
|
76
82
|
|
|
@@ -556,6 +562,30 @@ html.dv-conref-hot [data-conref] *,
|
|
|
556
562
|
}
|
|
557
563
|
.indexterm-chip--ref { font-style: italic; }
|
|
558
564
|
.indexterm-chip__see { font-style: italic; opacity: 0.85; }
|
|
565
|
+
/* An XML comment's own text, shown where the author wrote it (Oxygen's Author
|
|
566
|
+
mode does the same). Deliberately quiet -- muted colour, no border of its
|
|
567
|
+
own -- because it is not document content: the reader should be able to see
|
|
568
|
+
it, and to skip over it without the page looking broken. Block comments
|
|
569
|
+
(one written between two blocks rather than inside a sentence) get a
|
|
570
|
+
leading rule so the whole run reads as one comment instead of stray text. */
|
|
571
|
+
.dv-comment {
|
|
572
|
+
background: var(--color-comment-bg);
|
|
573
|
+
color: var(--color-comment-text);
|
|
574
|
+
font-style: italic;
|
|
575
|
+
border-radius: 3px;
|
|
576
|
+
padding: 0 0.2em;
|
|
577
|
+
white-space: pre-wrap;
|
|
578
|
+
/* A wrapped inline comment must paint each line's box, not just the first. */
|
|
579
|
+
-webkit-box-decoration-break: clone;
|
|
580
|
+
box-decoration-break: clone;
|
|
581
|
+
}
|
|
582
|
+
.dv-comment--block {
|
|
583
|
+
display: block;
|
|
584
|
+
margin: 0.5em 0;
|
|
585
|
+
padding: 0.15em 0.6em;
|
|
586
|
+
border-left: 3px solid var(--color-comment-border);
|
|
587
|
+
border-radius: 0 3px 3px 0;
|
|
588
|
+
}
|
|
559
589
|
body.hide-profiling .profiled,
|
|
560
590
|
.dita-body.hide-profiling .profiled {
|
|
561
591
|
background: none;
|