@dita-viewer/core 0.2.1 → 0.2.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.
Files changed (41) hide show
  1. package/README.md +20 -7
  2. package/dist/editor/renderContext.d.ts +7 -0
  3. package/dist/editor/renderContext.js +24 -0
  4. package/dist/editor/topicRender.d.ts +9 -0
  5. package/dist/editor/topicRender.js +4 -2
  6. package/dist/language/ditaLanguageUtils.js +2 -0
  7. package/dist/profiling.d.ts +7 -1
  8. package/dist/profiling.js +9 -3
  9. package/dist/render/baseTypeMap.js +28 -2
  10. package/dist/render/renderer.d.ts +24 -0
  11. package/dist/render/renderer.js +36 -9
  12. package/dist-esm/bookRender.js +145 -0
  13. package/dist-esm/completion.js +142 -0
  14. package/dist-esm/completionData.js +155 -0
  15. package/dist-esm/diagnostics.js +378 -0
  16. package/dist-esm/editor/conkeyref.js +104 -0
  17. package/dist-esm/editor/keySpace.js +201 -0
  18. package/dist-esm/editor/refResolvers.js +633 -0
  19. package/dist-esm/editor/renderContext.js +138 -0
  20. package/dist-esm/editor/sourceText.js +142 -0
  21. package/dist-esm/editor/topicRender.js +328 -0
  22. package/dist-esm/index.js +66 -0
  23. package/dist-esm/keyMapForDocument.js +23 -0
  24. package/dist-esm/language/ditaLanguageUtils.js +659 -0
  25. package/dist-esm/language/referenceableFiles.js +108 -0
  26. package/dist-esm/mapRender.js +24 -0
  27. package/dist-esm/package.json +1 -0
  28. package/dist-esm/parser/ditaParser.js +409 -0
  29. package/dist-esm/parser/domTypes.js +1 -0
  30. package/dist-esm/parser/mapTagMap.js +77 -0
  31. package/dist-esm/parser/namedEntities.js +321 -0
  32. package/dist-esm/parser/standardTagMap.js +393 -0
  33. package/dist-esm/profiling.js +224 -0
  34. package/dist-esm/render/baseTypeMap.js +1057 -0
  35. package/dist-esm/render/cjkSpacing.js +94 -0
  36. package/dist-esm/render/mapTypeMap.js +568 -0
  37. package/dist-esm/render/renderer.js +519 -0
  38. package/dist-esm/siteRender.js +261 -0
  39. package/package.json +4 -2
  40. package/styles/TOKENS.md +21 -0
  41. package/styles/dita-content.css +31 -17
@@ -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
+ }