@dita-viewer/core 0.2.1 → 0.2.3
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 +20 -7
- package/dist/editor/renderContext.d.ts +7 -0
- package/dist/editor/renderContext.js +24 -0
- package/dist/editor/topicRender.d.ts +9 -0
- package/dist/editor/topicRender.js +4 -2
- package/dist/language/ditaLanguageUtils.d.ts +15 -2
- package/dist/language/ditaLanguageUtils.js +36 -4
- package/dist/profiling.d.ts +7 -1
- package/dist/profiling.js +9 -3
- package/dist/render/baseTypeMap.js +28 -2
- package/dist/render/renderer.d.ts +24 -0
- package/dist/render/renderer.js +47 -10
- package/dist-esm/bookRender.js +145 -0
- package/dist-esm/completion.js +142 -0
- package/dist-esm/completionData.js +155 -0
- package/dist-esm/diagnostics.js +378 -0
- package/dist-esm/editor/conkeyref.js +104 -0
- package/dist-esm/editor/keySpace.js +201 -0
- package/dist-esm/editor/refResolvers.js +633 -0
- package/dist-esm/editor/renderContext.js +138 -0
- package/dist-esm/editor/sourceText.js +142 -0
- package/dist-esm/editor/topicRender.js +328 -0
- package/dist-esm/index.js +66 -0
- package/dist-esm/keyMapForDocument.js +23 -0
- package/dist-esm/language/ditaLanguageUtils.js +689 -0
- package/dist-esm/language/referenceableFiles.js +108 -0
- package/dist-esm/mapRender.js +24 -0
- package/dist-esm/package.json +1 -0
- package/dist-esm/parser/ditaParser.js +409 -0
- package/dist-esm/parser/domTypes.js +1 -0
- package/dist-esm/parser/mapTagMap.js +77 -0
- package/dist-esm/parser/namedEntities.js +321 -0
- package/dist-esm/parser/standardTagMap.js +393 -0
- package/dist-esm/profiling.js +224 -0
- package/dist-esm/render/baseTypeMap.js +1057 -0
- package/dist-esm/render/cjkSpacing.js +94 -0
- package/dist-esm/render/mapTypeMap.js +568 -0
- package/dist-esm/render/renderer.js +529 -0
- package/dist-esm/siteRender.js +261 -0
- package/package.json +4 -2
- package/styles/TOKENS.md +21 -0
- package/styles/dita-content.css +31 -17
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// conkeyref resolution (P3) -- the DITA "content reference by key".
|
|
2
|
+
//
|
|
3
|
+
// A conkeyref value ("keyname", "keyname/elementid" or
|
|
4
|
+
// "keyname/topicid/elementid") is an indirect content reference: the leading
|
|
5
|
+
// segment is a key, resolved through the map key space to the target topic's
|
|
6
|
+
// href, and the remaining segments address an element inside that topic. It is
|
|
7
|
+
// the key-based analogue of a direct conref -- once the target element is
|
|
8
|
+
// found, the renderer substitutes it exactly as it would a conref target
|
|
9
|
+
// (same-type merge, cross-type replacement, cycle guard).
|
|
10
|
+
//
|
|
11
|
+
// This module is pure: no vscode, no filesystem, no parser. It takes the key
|
|
12
|
+
// space's href definitions plus an injected `loadTopic` (the caller owns file
|
|
13
|
+
// IO, caching and parsing) and returns the resolved target element or
|
|
14
|
+
// undefined. Returning undefined is how the caller learns to fall back: per the
|
|
15
|
+
// DITA 1.3 spec (and DITA-OT's DOTJ046E behaviour), when a conkeyref cannot be
|
|
16
|
+
// resolved -- key not defined, key with no href, target file missing, addressed
|
|
17
|
+
// id not present -- an element that also carries a direct @conref uses that
|
|
18
|
+
// instead, and when there is no conref the element renders its own literal
|
|
19
|
+
// content.
|
|
20
|
+
//
|
|
21
|
+
// Key-space precedence is inherited unchanged from the definitions it is given
|
|
22
|
+
// (first definition wins; a context map fully replaces the ancestor scan) --
|
|
23
|
+
// this function only reads the map it is handed, so switching the key context
|
|
24
|
+
// switches conkeyref targets for free.
|
|
25
|
+
/**
|
|
26
|
+
* Addressing inside the key's target topic: the whole topic when the value has
|
|
27
|
+
* only the key name, otherwise the last path segment (the element id, for both
|
|
28
|
+
* the "key/elemid" and the "key/topicid/elemid" forms -- the intermediate topic
|
|
29
|
+
* id is redundant for a by-id lookup).
|
|
30
|
+
*/
|
|
31
|
+
function parseConkeyref(conkeyref) {
|
|
32
|
+
const segments = conkeyref.split('/').filter((s) => s !== '');
|
|
33
|
+
const keyName = segments[0];
|
|
34
|
+
const elementId = segments.length > 1 ? segments[segments.length - 1] : undefined;
|
|
35
|
+
return { keyName, elementId };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The element id a key's own href addresses: the last path segment of its
|
|
39
|
+
* fragment ("file.dita#topicid/elemid" -> "elemid"; "file.dita#topicid" ->
|
|
40
|
+
* "topicid"), or undefined when the href has no fragment. This is what a bare
|
|
41
|
+
* conkeyref="keyname" points at when the key was defined against an element.
|
|
42
|
+
*/
|
|
43
|
+
function hrefFragmentTarget(href) {
|
|
44
|
+
const hash = href.indexOf('#');
|
|
45
|
+
if (hash < 0)
|
|
46
|
+
return undefined;
|
|
47
|
+
const segments = href.slice(hash + 1).split('/').filter((s) => s !== '');
|
|
48
|
+
if (segments.length === 0)
|
|
49
|
+
return undefined;
|
|
50
|
+
const last = segments[segments.length - 1];
|
|
51
|
+
try {
|
|
52
|
+
return decodeURIComponent(last);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return last;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function findElementById(root, targetId) {
|
|
59
|
+
if (root.attributes?.id === targetId)
|
|
60
|
+
return root;
|
|
61
|
+
for (const child of root.children || []) {
|
|
62
|
+
const found = findElementById(child, targetId);
|
|
63
|
+
if (found)
|
|
64
|
+
return found;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Resolve a conkeyref to its target element.
|
|
70
|
+
*
|
|
71
|
+
* Cycle protection is deliberately NOT done here. A conkeyref that resolves
|
|
72
|
+
* back into an already-rendered chain (A -> B -> A) is caught one level up, by
|
|
73
|
+
* the renderer: resolveConrefForNode only calls this when the value is not
|
|
74
|
+
* already on its conrefChain, mirroring direct-conref cycle protection. Keeping
|
|
75
|
+
* the guard out of this pure function means one owner for the rule and no dead
|
|
76
|
+
* parameter at the call sites (buildRenderContext never threads a chain in).
|
|
77
|
+
*
|
|
78
|
+
* @param conkeyref the raw @conkeyref value.
|
|
79
|
+
* @param defs the key space's href definitions (from KeySpace.defs).
|
|
80
|
+
* @param loadTopic loads and parses the topic at `href` (relative to
|
|
81
|
+
* `baseDir`), returning its root, or undefined when the file
|
|
82
|
+
* is missing or unparseable.
|
|
83
|
+
*/
|
|
84
|
+
export function resolveConkeyref(conkeyref, defs, loadTopic) {
|
|
85
|
+
if (!conkeyref)
|
|
86
|
+
return undefined;
|
|
87
|
+
const { keyName, elementId } = parseConkeyref(conkeyref);
|
|
88
|
+
if (!keyName)
|
|
89
|
+
return undefined;
|
|
90
|
+
const def = defs.get(keyName);
|
|
91
|
+
// Key not defined, or defined without a resource target: nothing to pull.
|
|
92
|
+
if (!def || !def.href)
|
|
93
|
+
return undefined;
|
|
94
|
+
const root = loadTopic(def.baseDir, def.href);
|
|
95
|
+
if (!root)
|
|
96
|
+
return undefined;
|
|
97
|
+
// An id in the conkeyref itself wins; otherwise the key's own href may name
|
|
98
|
+
// the element ("file.dita#topicid/elemid"); with neither, the reference
|
|
99
|
+
// targets the key's whole topic.
|
|
100
|
+
const targetId = elementId ?? hrefFragmentTarget(def.href);
|
|
101
|
+
if (!targetId)
|
|
102
|
+
return root;
|
|
103
|
+
return findElementById(root, targetId);
|
|
104
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// ── key definitions of one ditamap (vscode-free, unit-testable) ──
|
|
2
|
+
//
|
|
3
|
+
// Split out of keyMap.ts, which imports vscode and so cannot be loaded by
|
|
4
|
+
// the mocha suite. buildKeyMap (keyMap.ts) and any other consumer of "the
|
|
5
|
+
// keys a map defines" go through collectMapKeys so there is one rule for
|
|
6
|
+
// what a keydef contributes.
|
|
7
|
+
import { dirname } from 'path';
|
|
8
|
+
import { parseDitamap, preprocessEntities } from '../parser/ditaParser';
|
|
9
|
+
import { expandDitamapRefs } from './refResolvers';
|
|
10
|
+
import { sourceStamp } from './sourceText';
|
|
11
|
+
function extractTextFromNode(node) {
|
|
12
|
+
if (node.type === 'text')
|
|
13
|
+
return node.text || '';
|
|
14
|
+
return (node.children || []).map(extractTextFromNode).join('');
|
|
15
|
+
}
|
|
16
|
+
function getNodeValue(node, childBaseTypes) {
|
|
17
|
+
for (const bt of childBaseTypes) {
|
|
18
|
+
const child = (node.children || []).find((c) => c.type === 'element' && c.baseType === bt);
|
|
19
|
+
if (child) {
|
|
20
|
+
const text = extractTextFromNode(child).trim();
|
|
21
|
+
if (text)
|
|
22
|
+
return text;
|
|
23
|
+
}
|
|
24
|
+
// DITA wraps <keyword> inside <keywords>; also search inside known wrappers
|
|
25
|
+
const wrapper = (node.children || []).find((c) => c.type === 'element' && (c.baseType === 'map/keywords'));
|
|
26
|
+
if (wrapper) {
|
|
27
|
+
const inner = (wrapper.children || []).find((c) => c.type === 'element' && c.baseType === bt);
|
|
28
|
+
if (inner) {
|
|
29
|
+
const text = extractTextFromNode(inner).trim();
|
|
30
|
+
if (text)
|
|
31
|
+
return text;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
function getKeyValueFromRef(node) {
|
|
38
|
+
// Priority: keyword > linktext > navtitle > shortdesc > indexterm
|
|
39
|
+
const topicmeta = (node.children || []).find((c) => c.type === 'element' && (c.baseType === 'map/topicmeta'));
|
|
40
|
+
if (!topicmeta)
|
|
41
|
+
return undefined; // No topicmeta, no value
|
|
42
|
+
return getNodeValue(topicmeta, [
|
|
43
|
+
'map/keyword',
|
|
44
|
+
'map/linktext',
|
|
45
|
+
'map/navtitle',
|
|
46
|
+
'map/shortdesc',
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
/** The individual key names in a `keys` attribute (whitespace-separated). */
|
|
50
|
+
export function splitKeyNames(keys) {
|
|
51
|
+
return keys.split(/\s+/).filter(Boolean);
|
|
52
|
+
}
|
|
53
|
+
/** All names a key defined under the given scope chain answers to: bare plus each qualified form. */
|
|
54
|
+
export function qualifiedNames(name, scopeChain) {
|
|
55
|
+
const out = [name];
|
|
56
|
+
for (let i = 0; i < scopeChain.length; i++) {
|
|
57
|
+
out.push(scopeChain.slice(i).join('.') + '.' + name);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/** Like qualifiedNames, for a definition that lives in several scope chains at once. */
|
|
62
|
+
export function qualifiedNamesMulti(name, scopeChains) {
|
|
63
|
+
const out = new Set([name]);
|
|
64
|
+
for (const chain of scopeChains)
|
|
65
|
+
for (const q of qualifiedNames(name, chain))
|
|
66
|
+
out.add(q);
|
|
67
|
+
return [...out];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Parses one ditamap, expands the ditamaps it references, and adds every
|
|
71
|
+
* key it defines to `into` (first definition wins, in document order).
|
|
72
|
+
* When `hrefs` is supplied, each newly defined key also records its resource
|
|
73
|
+
* target there (same first-definition-wins gate as the value), keyed by the
|
|
74
|
+
* individual name. Throws if the map itself cannot be read or parsed; the
|
|
75
|
+
* caller decides whether that is fatal.
|
|
76
|
+
*/
|
|
77
|
+
export function collectMapKeys(mapPath, into, read, hrefs) {
|
|
78
|
+
const content = read(mapPath, 'utf-8');
|
|
79
|
+
const doc = parseDitamap(preprocessEntities(content));
|
|
80
|
+
const mapRoot = doc.root;
|
|
81
|
+
const mapDir = dirname(mapPath);
|
|
82
|
+
// Expand referenced ditamaps so keydefs from included maps are visible
|
|
83
|
+
expandDitamapRefs(mapRoot, mapDir, read);
|
|
84
|
+
// scopeChains: the keyscope names enclosing `node`, outermost first; several
|
|
85
|
+
// chains when a keyscope lists several names. A key answers to its bare name
|
|
86
|
+
// and to every qualified form (`a.k`, `x.a.k`), so a keyref written either
|
|
87
|
+
// way finds it. The bare name is still defined unscoped -- the lookup does
|
|
88
|
+
// not know where the referencing element sits, so it cannot tell which
|
|
89
|
+
// scope's `k` is meant; the qualified names are what tell scopes apart.
|
|
90
|
+
function walk(node, scopeChains) {
|
|
91
|
+
if (node.type !== 'element')
|
|
92
|
+
return;
|
|
93
|
+
const baseType = node.baseType;
|
|
94
|
+
let chains = scopeChains;
|
|
95
|
+
const keyscope = node.attributes?.keyscope;
|
|
96
|
+
if (keyscope) {
|
|
97
|
+
const scopes = splitKeyNames(keyscope);
|
|
98
|
+
if (scopes.length)
|
|
99
|
+
chains = chains.flatMap((chain) => scopes.map((s) => chain.concat(s)));
|
|
100
|
+
}
|
|
101
|
+
if ((baseType === 'map/topicref' || baseType === 'map/keydef') && node.attributes?.keys) {
|
|
102
|
+
const value = getKeyValueFromRef(node);
|
|
103
|
+
// keys is a space-separated list: one keydef defines every name in it
|
|
104
|
+
// with the same resource. First definition of each name wins (DITA
|
|
105
|
+
// precedence; nearest map scanned first).
|
|
106
|
+
for (const keyName of splitKeyNames(node.attributes.keys)) {
|
|
107
|
+
for (const name of qualifiedNamesMulti(keyName, chains)) {
|
|
108
|
+
if (!into.has(name)) {
|
|
109
|
+
into.set(name, value || keyName);
|
|
110
|
+
hrefs?.set(name, { href: node.attributes?.href, baseDir: mapDir });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (const child of node.children || [])
|
|
116
|
+
walk(child, chains);
|
|
117
|
+
}
|
|
118
|
+
for (const child of mapRoot.children || [])
|
|
119
|
+
walk(child, [[]]);
|
|
120
|
+
}
|
|
121
|
+
/** A context counts only while its file (or an unsaved copy of it) exists. */
|
|
122
|
+
export function isKeyContextAvailable(contextMap) {
|
|
123
|
+
return contextMap !== undefined && sourceStamp(contextMap) !== '?';
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The ditamaps the key definitions come from: the context map alone when one
|
|
127
|
+
* is set and available, otherwise the ancestor maps. The single answer to
|
|
128
|
+
* "where do keys come from" -- the key map behind previews and diagnostics
|
|
129
|
+
* (buildKeySpace) and go-to-definition on a keyref must both ask this, or a
|
|
130
|
+
* preview can show one brand's value while F12 jumps to another's.
|
|
131
|
+
* `ancestorMaps` is a function so the directory walk is skipped when the
|
|
132
|
+
* context answers.
|
|
133
|
+
*/
|
|
134
|
+
export function keySourceMaps(contextMap, ancestorMaps) {
|
|
135
|
+
return isKeyContextAvailable(contextMap) ? [contextMap] : ancestorMaps();
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The key space every keyref in the workspace resolves against.
|
|
139
|
+
*
|
|
140
|
+
* With a context map (Oxygen's DITA Maps Manager "context"), the keys are
|
|
141
|
+
* those of that one map, expanded through its map references, and nothing
|
|
142
|
+
* else: the ancestor maps are NOT consulted, so choosing one brand's keydef
|
|
143
|
+
* map cannot pick up another brand's definition of the same key. Without a
|
|
144
|
+
* context it is the ancestor scan buildKeyMap always did.
|
|
145
|
+
*
|
|
146
|
+
* A context whose file no longer exists falls back to the ancestor scan and
|
|
147
|
+
* says so ('missing') -- the caller decides how to tell the user. A context
|
|
148
|
+
* that exists but cannot be parsed is reported through onError and yields an
|
|
149
|
+
* empty key space rather than quietly resolving against other maps.
|
|
150
|
+
*/
|
|
151
|
+
export function buildKeySpace(contextMap, ancestorMaps, read, onError = () => undefined) {
|
|
152
|
+
const files = [];
|
|
153
|
+
const recordingRead = (path, encoding) => {
|
|
154
|
+
files.push(path);
|
|
155
|
+
return read(path, encoding);
|
|
156
|
+
};
|
|
157
|
+
const sources = keySourceMaps(contextMap, () => ancestorMaps);
|
|
158
|
+
let status = 'none';
|
|
159
|
+
if (contextMap !== undefined) {
|
|
160
|
+
// Listed even when missing, so the file reappearing changes the stamp.
|
|
161
|
+
files.push(contextMap);
|
|
162
|
+
status = isKeyContextAvailable(contextMap) ? 'active' : 'missing';
|
|
163
|
+
}
|
|
164
|
+
const keys = new Map();
|
|
165
|
+
const defs = new Map();
|
|
166
|
+
for (const mf of sources) {
|
|
167
|
+
try {
|
|
168
|
+
collectMapKeys(mf, keys, recordingRead, defs);
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
onError(mf, e);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// Pair the href definitions with the values map they were built from, so a
|
|
175
|
+
// consumer handed only the values map (the render paths thread `keyMap`
|
|
176
|
+
// everywhere but not `defs`) can still find the conkeyref targets for that
|
|
177
|
+
// exact instance. Registered here rather than in keyMap.ts so every producer
|
|
178
|
+
// of a KeySpace -- including the direct buildKeySpace callers the conkeyref
|
|
179
|
+
// tests drive -- gets it, and so renderContext.ts can read it without
|
|
180
|
+
// importing the vscode-dependent keyMap.ts.
|
|
181
|
+
registerKeyDefs(keys, defs);
|
|
182
|
+
return { keys, defs, files: [...new Set(files)], status };
|
|
183
|
+
}
|
|
184
|
+
// ── values map -> href defs, by instance identity ──
|
|
185
|
+
//
|
|
186
|
+
// The keys a map defines (values) and their resource targets (defs) are always
|
|
187
|
+
// produced together by one buildKeySpace call, and buildKeyMap hands back the
|
|
188
|
+
// SAME values-map instance for as long as its cache entry is valid -- the exact
|
|
189
|
+
// identity property renderTopicCached already leans on for cache keying. So a
|
|
190
|
+
// WeakMap from the values map to its defs travels with it: any render path that
|
|
191
|
+
// has the keyMap has its conkeyref defs, with no new field to thread through the
|
|
192
|
+
// many interfaces keyMap already passes through (and no path able to forget to).
|
|
193
|
+
const defsByKeyMap = new WeakMap();
|
|
194
|
+
export function registerKeyDefs(keys, defs) {
|
|
195
|
+
defsByKeyMap.set(keys, defs);
|
|
196
|
+
}
|
|
197
|
+
/** The href defs paired with a given values map, or undefined if it was not
|
|
198
|
+
* produced by buildKeySpace (e.g. a hand-built map in a test). */
|
|
199
|
+
export function getKeyDefs(keys) {
|
|
200
|
+
return defsByKeyMap.get(keys);
|
|
201
|
+
}
|