@dita-viewer/core 0.2.0 → 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.
- package/LICENSE +21 -21
- package/README.md +90 -58
- 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/index.js +3 -3
- package/dist/language/ditaLanguageUtils.js +2 -0
- package/dist/profiling.d.ts +9 -3
- package/dist/profiling.js +12 -6
- package/dist/render/baseTypeMap.js +28 -2
- package/dist/render/mapTypeMap.js +23 -23
- package/dist/render/renderer.d.ts +24 -0
- package/dist/render/renderer.js +36 -9
- 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 +659 -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 +519 -0
- package/dist-esm/siteRender.js +261 -0
- package/package.json +55 -53
- package/styles/TOKENS.md +59 -38
- package/styles/dita-content.css +1171 -1157
package/dist/render/renderer.js
CHANGED
|
@@ -27,12 +27,14 @@ function isContainerBaseType(baseType) {
|
|
|
27
27
|
// Per the OASIS DITA 1.3 select-atts entity (commonElements.mod): the full
|
|
28
28
|
// set of attributes conditional-processing tools (Oxygen's "Conditional
|
|
29
29
|
// Text" / DITA-OT's .ditaval) act on is props, platform, product, audience,
|
|
30
|
-
// otherprops (the filter-atts subgroup
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
|
|
30
|
+
// otherprops, deliveryTarget (the filter-atts subgroup -- DITA 1.3 added
|
|
31
|
+
// deliveryTarget, deprecating the DITA 1.2 @print), plus base, importance,
|
|
32
|
+
// rev, and status. This only *highlights* profiled content (matches it
|
|
33
|
+
// visually, the way Oxygen shows profiling by default) — it does not hide
|
|
34
|
+
// anything; that would be a real conditional-processing engine (a
|
|
35
|
+
// .ditaval-driven filter), which is a separate, larger feature this doesn't
|
|
36
|
+
// attempt.
|
|
37
|
+
const PROFILING_ATTRS = ['props', 'platform', 'product', 'audience', 'otherprops', 'deliveryTarget', 'base', 'importance', 'rev', 'status'];
|
|
36
38
|
const PROFILING_ATTR_LABELS = {
|
|
37
39
|
otherprops: 'Other',
|
|
38
40
|
};
|
|
@@ -377,6 +379,32 @@ function resolveConrefForNode(node, context) {
|
|
|
377
379
|
const end = followConrefChain(target, context);
|
|
378
380
|
return { node: mergeConrefTarget(node, end), chainKey: conref, mark: conrefMarkFor(end, context) };
|
|
379
381
|
}
|
|
382
|
+
/**
|
|
383
|
+
* Per DITA keyref semantics, the key's resource target becomes the
|
|
384
|
+
* referencing element's effective href when the element carries no href of
|
|
385
|
+
* its own -- this is what turns `<xref keyref="k"/>` into a link to the
|
|
386
|
+
* key's target. Applies to link-shaped elements only (topic/xref): other
|
|
387
|
+
* keyref consumers (ph, keyword, term) never link, and handing topic/link
|
|
388
|
+
* a href would change its rendered shape without the webview-side handling
|
|
389
|
+
* to click it safely, so the gate stays on the element's base type.
|
|
390
|
+
* Resolved for local content too: `<xref keyref="k">text</xref>` keeps its
|
|
391
|
+
* text and still links to the key's target.
|
|
392
|
+
*/
|
|
393
|
+
function withResolvedKeyHref(node, keyref, context) {
|
|
394
|
+
if (node.baseType !== 'topic/xref')
|
|
395
|
+
return node;
|
|
396
|
+
if (!context.resolveKeyHref || node.attributes?.href)
|
|
397
|
+
return node;
|
|
398
|
+
const keyHref = context.resolveKeyHref(keyref);
|
|
399
|
+
if (!keyHref) {
|
|
400
|
+
// The key resolves to text but names no resource (or the caller wired no
|
|
401
|
+
// defs): keep the keyref so the xref renderer can mark the element as an
|
|
402
|
+
// unlinkable keyref (unresolved-keyref) instead of a bare span that
|
|
403
|
+
// gives no hint anything is missing.
|
|
404
|
+
return { ...node, attributes: { ...node.attributes, keyref } };
|
|
405
|
+
}
|
|
406
|
+
return { ...node, attributes: { ...node.attributes, href: keyHref } };
|
|
407
|
+
}
|
|
380
408
|
function resolveKeyrefForNode(node, context) {
|
|
381
409
|
const keyref = node.attributes?.keyref;
|
|
382
410
|
if (!keyref || !context.resolveKey)
|
|
@@ -385,16 +413,15 @@ function resolveKeyrefForNode(node, context) {
|
|
|
385
413
|
// text — only substitute when the element is effectively empty.
|
|
386
414
|
const hasLocalContent = (node.children || []).some((c) => c.type === 'element' || (c.text || '').trim() !== '');
|
|
387
415
|
if (hasLocalContent)
|
|
388
|
-
return node;
|
|
416
|
+
return withResolvedKeyHref(node, keyref, context);
|
|
389
417
|
const resolved = context.resolveKey(keyref);
|
|
390
418
|
if (!resolved)
|
|
391
419
|
return node;
|
|
392
420
|
// Strip keyref after resolving, replace children with resolved text
|
|
393
421
|
const restAttrs = Object.fromEntries(Object.entries(node.attributes || {}).filter(([k]) => k !== 'keyref'));
|
|
394
422
|
return {
|
|
395
|
-
...node,
|
|
423
|
+
...withResolvedKeyHref({ ...node, attributes: restAttrs }, keyref, context),
|
|
396
424
|
children: [{ type: 'text', text: resolved, children: [], sourceRange: node.sourceRange }],
|
|
397
|
-
attributes: restAttrs,
|
|
398
425
|
};
|
|
399
426
|
}
|
|
400
427
|
// The "given a fully-resolved node, actually render it" half of
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// Render a .ditamap as a book: every topic it references, in map order, on
|
|
2
|
+
// one page.
|
|
3
|
+
//
|
|
4
|
+
// Like mapRender.ts this is only glue around the copied parser and renderer:
|
|
5
|
+
// parse the map, splice in referenced submaps, collect the entries, then
|
|
6
|
+
// render each topic with renderTopicXml and wrap it in a section. The topic
|
|
7
|
+
// files are read from disk through readSourceText (the same cache and
|
|
8
|
+
// overlay rules the rest of the core uses).
|
|
9
|
+
//
|
|
10
|
+
// The wrapper carries the topic's absolute path in data-dita-book-file. A
|
|
11
|
+
// cross-file xref inside the book renders as data-dita-book-xref="path#id"
|
|
12
|
+
// (see baseTypeMap.ts); the host uses the wrapper to find the target section
|
|
13
|
+
// and scroll to it instead of opening another file.
|
|
14
|
+
import { dirname, isAbsolute, resolve } from 'path';
|
|
15
|
+
import { parseDitamap, preprocessEntities } from './parser/ditaParser';
|
|
16
|
+
import { decodeHrefPart, expandDitamapRefs } from './editor/refResolvers';
|
|
17
|
+
import { readSourceText } from './editor/sourceText';
|
|
18
|
+
import { renderTopicXml } from './editor/topicRender';
|
|
19
|
+
import { collectMapEntries, getMapTitleText } from './render/mapTypeMap';
|
|
20
|
+
import { pruneExcluded } from './profiling';
|
|
21
|
+
const URL_SCHEME_RE = /^[a-z][a-z0-9+.-]*:/i;
|
|
22
|
+
function escapeHtml(s) {
|
|
23
|
+
return s
|
|
24
|
+
.replace(/&/g, '&')
|
|
25
|
+
.replace(/</g, '<')
|
|
26
|
+
.replace(/>/g, '>')
|
|
27
|
+
.replace(/"/g, '"')
|
|
28
|
+
.replace(/'/g, ''');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The absolute path of the topic an entry points at, or undefined when it is
|
|
32
|
+
* not a local topic file (a URL, an absolute path, a fragment, a .ditamap,
|
|
33
|
+
* a PDF, ...). Map hrefs were rebased onto the root map's folder by
|
|
34
|
+
* expandDitamapRefs, so docDir is the only base needed here.
|
|
35
|
+
*/
|
|
36
|
+
export function topicPathOf(entry, docDir) {
|
|
37
|
+
const href = entry.href;
|
|
38
|
+
if (!href || href.startsWith('#'))
|
|
39
|
+
return undefined;
|
|
40
|
+
if (URL_SCHEME_RE.test(href) || isAbsolute(href))
|
|
41
|
+
return undefined;
|
|
42
|
+
const pathPart = decodeHrefPart(href.split('#')[0]);
|
|
43
|
+
const lower = pathPart.toLowerCase();
|
|
44
|
+
if (!lower.endsWith('.dita') && !lower.endsWith('.xml'))
|
|
45
|
+
return undefined;
|
|
46
|
+
return resolve(docDir, pathPart);
|
|
47
|
+
}
|
|
48
|
+
function roleHtml(entry) {
|
|
49
|
+
return entry.role ? `<p class="dita-book-role">${escapeHtml(entry.role)}</p>` : '';
|
|
50
|
+
}
|
|
51
|
+
function headingLevelFor(entry, base) {
|
|
52
|
+
return Math.min(6, Math.max(1, base + entry.depth));
|
|
53
|
+
}
|
|
54
|
+
export function renderBookXml(input) {
|
|
55
|
+
const doc = parseDitamap(preprocessEntities(input.xml));
|
|
56
|
+
const dependencies = input.collectDependencies ?? new Set();
|
|
57
|
+
const readFile = input.readFile ?? ((path) => readSourceText(path, 'utf-8'));
|
|
58
|
+
const read = (path, _enc) => {
|
|
59
|
+
dependencies.add(resolve(path));
|
|
60
|
+
return readFile(path);
|
|
61
|
+
};
|
|
62
|
+
expandDitamapRefs(doc.root, input.docDir, read);
|
|
63
|
+
pruneExcluded(doc.root, input.filter);
|
|
64
|
+
const keyMap = input.keyMap ?? new Map();
|
|
65
|
+
const resolveKey = (key) => keyMap.get(key);
|
|
66
|
+
const entries = collectMapEntries(doc.root, resolveKey, input.roleFormat);
|
|
67
|
+
const title = getMapTitleText(doc.root, resolveKey);
|
|
68
|
+
// Members are settled before any topic renders: every topic's cross-file
|
|
69
|
+
// xrefs need the complete set to decide whether they point inside the book.
|
|
70
|
+
const members = new Set();
|
|
71
|
+
const items = [];
|
|
72
|
+
for (const entry of entries) {
|
|
73
|
+
if (entry.resourceOnly)
|
|
74
|
+
continue;
|
|
75
|
+
const absPath = topicPathOf(entry, input.docDir);
|
|
76
|
+
if (absPath)
|
|
77
|
+
members.add(absPath);
|
|
78
|
+
items.push({ entry, absPath });
|
|
79
|
+
}
|
|
80
|
+
const baseLevel = input.headingLevel ?? 1;
|
|
81
|
+
const sections = [];
|
|
82
|
+
const renderedTopics = new Set();
|
|
83
|
+
for (const item of items) {
|
|
84
|
+
const { entry, absPath } = item;
|
|
85
|
+
if (!absPath) {
|
|
86
|
+
// A topichead (or a structural entry) has a title but no file: show it
|
|
87
|
+
// as a divider so "Part I" still appears between its chapters.
|
|
88
|
+
if (entry.href || !entry.displayName)
|
|
89
|
+
continue;
|
|
90
|
+
const level = headingLevelFor(entry, baseLevel);
|
|
91
|
+
sections.push(`<section class="dita-book-division" data-depth="${entry.depth}">${roleHtml(entry)}` +
|
|
92
|
+
`<h${level} class="dita-book-division-title">${escapeHtml(entry.displayName)}</h${level}></section>`);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
// A nested topicref with a fragment ("chapter.dita#section") points into
|
|
96
|
+
// a topic that may already be on the page in full; rendering the whole
|
|
97
|
+
// file again would duplicate it. The section itself is already there.
|
|
98
|
+
if ((entry.href ?? '').includes('#') && renderedTopics.has(absPath))
|
|
99
|
+
continue;
|
|
100
|
+
renderedTopics.add(absPath);
|
|
101
|
+
// The file is a dependency whether it exists or not: creating a missing
|
|
102
|
+
// topic should make the book re-render, just like editing an existing one.
|
|
103
|
+
dependencies.add(absPath);
|
|
104
|
+
let xml;
|
|
105
|
+
try {
|
|
106
|
+
xml = readFile(absPath);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
sections.push(`<p class="dita-book-missing">${escapeHtml(input.labels?.missingTopic ?? 'Missing topic')}: <code>${escapeHtml(entry.href ?? absPath)}</code></p>`);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const docDir = dirname(absPath);
|
|
113
|
+
const asWebviewUri = (relPath) => {
|
|
114
|
+
if (!input.toResourceUrl)
|
|
115
|
+
return '';
|
|
116
|
+
return input.toResourceUrl(resolve(docDir, decodeHrefPart(relPath)));
|
|
117
|
+
};
|
|
118
|
+
const result = renderTopicXml({
|
|
119
|
+
xml,
|
|
120
|
+
docDir,
|
|
121
|
+
keyMap,
|
|
122
|
+
asWebviewUri,
|
|
123
|
+
headingLevel: headingLevelFor(entry, baseLevel),
|
|
124
|
+
uiLanguage: input.uiLanguage,
|
|
125
|
+
collectDependencies: dependencies,
|
|
126
|
+
bookMembers: members,
|
|
127
|
+
docFile: absPath,
|
|
128
|
+
markConrefs: true,
|
|
129
|
+
});
|
|
130
|
+
if (result.error !== undefined) {
|
|
131
|
+
sections.push(`<p class="dita-book-error">${escapeHtml(result.error)}</p>`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
sections.push(`<section class="dita-book-topic" data-dita-book-file="${escapeHtml(absPath)}" data-depth="${entry.depth}">` +
|
|
135
|
+
`${roleHtml(entry)}${result.html}</section>`);
|
|
136
|
+
}
|
|
137
|
+
const header = title
|
|
138
|
+
? `<header class="dita-book-titlepage"><h1 class="dita-book-title">${escapeHtml(title)}</h1></header>`
|
|
139
|
+
: '';
|
|
140
|
+
return {
|
|
141
|
+
html: `<article class="dita-book">${header}${sections.join('\n')}</article>`,
|
|
142
|
+
title,
|
|
143
|
+
members: [...members],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Completion for DITA source text: elements, attributes, attribute values, key
|
|
2
|
+
// references, file paths and ids.
|
|
3
|
+
//
|
|
4
|
+
// Host-neutral. It works on the document text and a cursor offset and returns
|
|
5
|
+
// plain suggestions; the host turns them into its own completion items (the
|
|
6
|
+
// Obsidian plugin maps them onto CodeMirror's). Everything it needs from
|
|
7
|
+
// outside -- the key space, the files on disk -- comes through CompletionEnv,
|
|
8
|
+
// so it runs unchanged in plain mocha.
|
|
9
|
+
//
|
|
10
|
+
// Context detection (where in a tag the cursor is) and tag-stack scanning are
|
|
11
|
+
// the VS Code DITA Viewer's own (language/ditaLanguageUtils.ts, copied
|
|
12
|
+
// verbatim); what is new here is ranking elements by their parent, image paths
|
|
13
|
+
// for <image href>, attribute de-duplication, and enumerated values.
|
|
14
|
+
import { STANDARD_TAG_TO_BASETYPE } from './parser/standardTagMap';
|
|
15
|
+
import { MAP_STANDARD_TAG_TO_BASETYPE } from './parser/mapTagMap';
|
|
16
|
+
import { collectIds, findUnclosedTag, getAttributesForTag, getCompletionContext } from './language/ditaLanguageUtils';
|
|
17
|
+
import { CHILD_HINTS, ENUM_VALUES, FILE_ATTRS, IMAGE_HREF_TAGS, ROOT_HINTS } from './completionData';
|
|
18
|
+
const BOOST_MAX = 90;
|
|
19
|
+
/** Rank by position in a hint list: earlier is higher. */
|
|
20
|
+
function rankBoost(index) {
|
|
21
|
+
return Math.max(1, BOOST_MAX - index);
|
|
22
|
+
}
|
|
23
|
+
export async function complete(text, offset, env) {
|
|
24
|
+
const ctx = getCompletionContext(text, offset);
|
|
25
|
+
switch (ctx.kind) {
|
|
26
|
+
case 'tag':
|
|
27
|
+
return ctx.closing ? completeCloseTag(text, offset, ctx.prefix ?? '') : completeTag(text, offset, ctx.prefix ?? '', env);
|
|
28
|
+
case 'attrName':
|
|
29
|
+
return completeAttrName(text, offset, ctx.tagName, ctx.prefix ?? '');
|
|
30
|
+
case 'attrValue':
|
|
31
|
+
return completeAttrValue(text, offset, ctx.tagName, ctx.attrName ?? '', ctx.prefix ?? '', env);
|
|
32
|
+
default:
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// ── elements ──
|
|
37
|
+
function completeCloseTag(text, offset, prefix) {
|
|
38
|
+
const lt = offset - prefix.length - 2;
|
|
39
|
+
const open = findUnclosedTag(text.slice(0, Math.max(lt, 0)));
|
|
40
|
+
if (!open)
|
|
41
|
+
return undefined;
|
|
42
|
+
return {
|
|
43
|
+
from: offset - prefix.length,
|
|
44
|
+
suggestions: [{ label: open, kind: 'closeTag', detail: 'close tag', insert: `${open}>`, boost: BOOST_MAX }],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function completeTag(text, offset, prefix, env) {
|
|
48
|
+
const lt = offset - prefix.length - 1;
|
|
49
|
+
const parent = findUnclosedTag(text.slice(0, Math.max(lt, 0)));
|
|
50
|
+
const vocabulary = env.isMap ? MAP_STANDARD_TAG_TO_BASETYPE : STANDARD_TAG_TO_BASETYPE;
|
|
51
|
+
const hints = parent
|
|
52
|
+
? CHILD_HINTS[parent] ?? []
|
|
53
|
+
: env.isMap
|
|
54
|
+
? ROOT_HINTS.map
|
|
55
|
+
: ROOT_HINTS.topic;
|
|
56
|
+
const rank = new Map(hints.map((name, i) => [name, rankBoost(i)]));
|
|
57
|
+
const suggestions = Object.keys(vocabulary).map((name) => ({
|
|
58
|
+
label: name,
|
|
59
|
+
kind: 'tag',
|
|
60
|
+
detail: vocabulary[name],
|
|
61
|
+
insert: name,
|
|
62
|
+
boost: rank.get(name) ?? 0,
|
|
63
|
+
}));
|
|
64
|
+
// A hinted name the vocabulary does not list (a specialization the hint table
|
|
65
|
+
// knows) is still worth offering where it is expected.
|
|
66
|
+
for (const [name, boost] of rank) {
|
|
67
|
+
if (!(name in vocabulary))
|
|
68
|
+
suggestions.push({ label: name, kind: 'tag', insert: name, boost });
|
|
69
|
+
}
|
|
70
|
+
return { from: offset - prefix.length, suggestions };
|
|
71
|
+
}
|
|
72
|
+
// ── attributes ──
|
|
73
|
+
/** Names of the attributes already written on the tag that ends at `offset`. */
|
|
74
|
+
function attributesPresent(text, offset, prefix) {
|
|
75
|
+
const lt = text.lastIndexOf('<', offset - 1);
|
|
76
|
+
const written = text.slice(lt, offset - prefix.length);
|
|
77
|
+
const names = new Set();
|
|
78
|
+
for (const m of written.matchAll(/([\w:.-]+)\s*=\s*(?:"[^"]*"|'[^']*')/g))
|
|
79
|
+
names.add(m[1]);
|
|
80
|
+
return names;
|
|
81
|
+
}
|
|
82
|
+
function completeAttrName(text, offset, tag, prefix) {
|
|
83
|
+
const present = attributesPresent(text, offset, prefix);
|
|
84
|
+
const names = getAttributesForTag(tag).filter((a) => !present.has(a));
|
|
85
|
+
if (names.length === 0)
|
|
86
|
+
return undefined;
|
|
87
|
+
return {
|
|
88
|
+
from: offset - prefix.length,
|
|
89
|
+
suggestions: names.map((attr, i) => ({
|
|
90
|
+
label: attr,
|
|
91
|
+
kind: 'attribute',
|
|
92
|
+
insert: `${attr}=""`,
|
|
93
|
+
cursor: attr.length + 2,
|
|
94
|
+
continueAfter: hasValueCompletion(tag, attr),
|
|
95
|
+
boost: rankBoost(i),
|
|
96
|
+
})),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function hasValueCompletion(tag, attr) {
|
|
100
|
+
return (attr === 'keyref' || attr === 'conkeyref' || attr in FILE_ATTRS ||
|
|
101
|
+
(tag !== undefined && `${tag}:${attr}` in ENUM_VALUES) || attr in ENUM_VALUES);
|
|
102
|
+
}
|
|
103
|
+
// ── attribute values ──
|
|
104
|
+
async function completeAttrValue(text, offset, tag, attr, prefix, env) {
|
|
105
|
+
const from = offset - prefix.length;
|
|
106
|
+
if (attr === 'keyref' || attr === 'conkeyref') {
|
|
107
|
+
// conkeyref="key/elementid": only the key is completed here.
|
|
108
|
+
if (prefix.includes('/'))
|
|
109
|
+
return undefined;
|
|
110
|
+
const suggestions = [];
|
|
111
|
+
for (const [key, value] of env.keys()) {
|
|
112
|
+
suggestions.push({ label: key, kind: 'key', detail: value !== key ? value : undefined, insert: key, boost: 0 });
|
|
113
|
+
}
|
|
114
|
+
return suggestions.length > 0 ? { from, suggestions } : undefined;
|
|
115
|
+
}
|
|
116
|
+
if (attr in FILE_ATTRS)
|
|
117
|
+
return completeReference(text, offset, tag, prefix, env);
|
|
118
|
+
const values = (tag !== undefined ? ENUM_VALUES[`${tag}:${attr}`] : undefined) ?? ENUM_VALUES[attr];
|
|
119
|
+
if (values) {
|
|
120
|
+
return { from, suggestions: values.map((v, i) => ({ label: v, kind: 'value', insert: v, boost: rankBoost(i) })) };
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
async function completeReference(text, offset, tag, prefix, env) {
|
|
125
|
+
const valueStart = offset - prefix.length;
|
|
126
|
+
const hash = prefix.indexOf('#');
|
|
127
|
+
// After '#': ids in the target file ("#topicid/elementid" replaces from the last '/').
|
|
128
|
+
if (hash >= 0) {
|
|
129
|
+
const filePart = prefix.slice(0, hash);
|
|
130
|
+
const ids = filePart ? env.idsIn(filePart) : collectIds(text);
|
|
131
|
+
if (!ids || ids.length === 0)
|
|
132
|
+
return undefined;
|
|
133
|
+
const fragment = prefix.slice(hash + 1);
|
|
134
|
+
const from = valueStart + hash + 1 + fragment.lastIndexOf('/') + 1;
|
|
135
|
+
return { from, suggestions: ids.map((id) => ({ label: id, kind: 'id', insert: id, boost: 0 })) };
|
|
136
|
+
}
|
|
137
|
+
const kind = tag !== undefined && IMAGE_HREF_TAGS.has(tag) ? 'image' : 'topic';
|
|
138
|
+
const files = await env.files(kind);
|
|
139
|
+
if (files.length === 0)
|
|
140
|
+
return undefined;
|
|
141
|
+
return { from: valueStart, suggestions: files.map((f) => ({ label: f, kind: 'file', insert: f, boost: 0 })) };
|
|
142
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// What the completion engine knows about DITA, as data.
|
|
2
|
+
//
|
|
3
|
+
// CHILD_HINTS says which elements are the usual children of a parent. It is a
|
|
4
|
+
// ranking aid, not a content model: every known element is still offered
|
|
5
|
+
// everywhere, and a hinted one is merely listed first. A wrong or missing
|
|
6
|
+
// entry therefore costs a suggestion's position, never its availability, which
|
|
7
|
+
// is why this can be a curated table rather than a copy of the DTDs.
|
|
8
|
+
//
|
|
9
|
+
// Entries follow the DITA 1.3 technical-content and map vocabularies. Order is
|
|
10
|
+
// the order the suggestions appear in.
|
|
11
|
+
const INLINE = [
|
|
12
|
+
'ph', 'codeph', 'keyword', 'term', 'xref', 'b', 'i', 'u', 'tt', 'sup', 'sub',
|
|
13
|
+
'uicontrol', 'menucascade', 'userinput', 'systemoutput', 'varname', 'filepath',
|
|
14
|
+
'cmdname', 'apiname', 'parmname', 'option', 'image', 'q', 'indexterm', 'fn',
|
|
15
|
+
'data', 'tm', 'cite', 'abbreviated-form', 'xmlelement', 'xmlatt', 'msgph',
|
|
16
|
+
'wintitle', 'shortcut', 'synph',
|
|
17
|
+
];
|
|
18
|
+
const BLOCKS = [
|
|
19
|
+
'p', 'ul', 'ol', 'dl', 'note', 'table', 'simpletable', 'fig', 'codeblock',
|
|
20
|
+
'pre', 'lines', 'lq', 'sl', 'image', 'object', 'div', 'hazardstatement',
|
|
21
|
+
'draft-comment', 'required-cleanup', 'foreign', 'data',
|
|
22
|
+
];
|
|
23
|
+
const BODY = ['p', 'section', 'example', 'ul', 'ol', 'dl', 'note', 'table', 'simpletable', 'fig', 'codeblock', 'pre', 'lines', 'lq', 'sl', 'image', 'div', 'bodydiv', 'sectiondiv', 'hazardstatement', 'object', 'foreign', 'data', 'draft-comment', 'required-cleanup', 'fn', 'indexterm'];
|
|
24
|
+
const MIXED = [...new Set([...BLOCKS, ...INLINE])];
|
|
25
|
+
const TITLED_SECTION = ['title', ...BLOCKS];
|
|
26
|
+
const TOPIC_PARTS = ['title', 'titlealts', 'shortdesc', 'abstract', 'prolog'];
|
|
27
|
+
const MAP_REF_CHILDREN = ['topicmeta', 'topicref', 'keydef', 'mapref', 'topicgroup', 'topichead'];
|
|
28
|
+
export const CHILD_HINTS = {
|
|
29
|
+
// Topic roots
|
|
30
|
+
concept: [...TOPIC_PARTS, 'conbody', 'related-links'],
|
|
31
|
+
task: [...TOPIC_PARTS, 'taskbody', 'related-links'],
|
|
32
|
+
reference: [...TOPIC_PARTS, 'refbody', 'related-links'],
|
|
33
|
+
topic: [...TOPIC_PARTS, 'body', 'related-links'],
|
|
34
|
+
glossentry: ['glossterm', 'glossdef', 'glossBody', 'prolog'],
|
|
35
|
+
troubleshooting: [...TOPIC_PARTS, 'troublebody', 'related-links'],
|
|
36
|
+
// Bodies
|
|
37
|
+
body: BODY,
|
|
38
|
+
conbody: BODY,
|
|
39
|
+
refbody: ['section', 'refsyn', 'properties', 'table', 'simpletable', 'example', 'dl', 'p', 'ul', 'ol', 'note', 'codeblock'],
|
|
40
|
+
taskbody: ['prereq', 'context', 'steps', 'steps-unordered', 'steps-informal', 'result', 'example', 'postreq', 'section'],
|
|
41
|
+
troublebody: ['condition', 'troubleSolution', 'section', 'example'],
|
|
42
|
+
section: TITLED_SECTION,
|
|
43
|
+
example: TITLED_SECTION,
|
|
44
|
+
sectiondiv: BLOCKS,
|
|
45
|
+
bodydiv: BODY,
|
|
46
|
+
div: MIXED,
|
|
47
|
+
prereq: BLOCKS,
|
|
48
|
+
context: BLOCKS,
|
|
49
|
+
result: BLOCKS,
|
|
50
|
+
postreq: BLOCKS,
|
|
51
|
+
// Task steps
|
|
52
|
+
steps: ['step', 'stepsection'],
|
|
53
|
+
'steps-unordered': ['step', 'stepsection'],
|
|
54
|
+
step: ['cmd', 'info', 'substeps', 'stepxmp', 'choices', 'choicetable', 'stepresult', 'steptroubleshooting', 'tutorialinfo'],
|
|
55
|
+
substeps: ['substep'],
|
|
56
|
+
substep: ['cmd', 'info', 'stepxmp', 'choices', 'choicetable', 'stepresult'],
|
|
57
|
+
choices: ['choice'],
|
|
58
|
+
// Lists
|
|
59
|
+
ul: ['li'],
|
|
60
|
+
ol: ['li'],
|
|
61
|
+
sl: ['sli'],
|
|
62
|
+
dl: ['dlhead', 'dlentry'],
|
|
63
|
+
dlentry: ['dt', 'dd'],
|
|
64
|
+
dlhead: ['dthd', 'ddhd'],
|
|
65
|
+
li: MIXED,
|
|
66
|
+
dd: MIXED,
|
|
67
|
+
sli: INLINE,
|
|
68
|
+
// Tables
|
|
69
|
+
table: ['title', 'desc', 'tgroup'],
|
|
70
|
+
tgroup: ['colspec', 'thead', 'tbody'],
|
|
71
|
+
thead: ['row'],
|
|
72
|
+
tbody: ['row'],
|
|
73
|
+
row: ['entry'],
|
|
74
|
+
entry: MIXED,
|
|
75
|
+
simpletable: ['sthead', 'strow'],
|
|
76
|
+
sthead: ['stentry'],
|
|
77
|
+
strow: ['stentry'],
|
|
78
|
+
stentry: MIXED,
|
|
79
|
+
// Figures and blocks that hold mixed content
|
|
80
|
+
fig: ['title', 'desc', 'image', 'figgroup', 'codeblock', 'pre', 'lines', 'simpletable', 'table'],
|
|
81
|
+
note: MIXED,
|
|
82
|
+
p: INLINE,
|
|
83
|
+
title: INLINE,
|
|
84
|
+
shortdesc: INLINE,
|
|
85
|
+
desc: INLINE,
|
|
86
|
+
cmd: INLINE,
|
|
87
|
+
info: MIXED,
|
|
88
|
+
lq: MIXED,
|
|
89
|
+
// Metadata
|
|
90
|
+
prolog: ['author', 'source', 'publisher', 'copyright', 'critdates', 'permissions', 'metadata', 'resourceid', 'data'],
|
|
91
|
+
metadata: ['audience', 'category', 'keywords', 'prodinfo', 'othermeta', 'data'],
|
|
92
|
+
keywords: ['keyword', 'indexterm'],
|
|
93
|
+
prodinfo: ['prodname', 'vrmlist', 'brand', 'component', 'featnum', 'prognum', 'platform', 'series'],
|
|
94
|
+
'related-links': ['link', 'linklist', 'linkpool'],
|
|
95
|
+
link: ['linktext', 'desc'],
|
|
96
|
+
linklist: ['title', 'desc', 'link', 'linklist'],
|
|
97
|
+
// Maps
|
|
98
|
+
map: ['title', 'topicmeta', 'topicref', 'topichead', 'topicgroup', 'keydef', 'mapref', 'reltable', 'anchor', 'navref', 'topicset', 'data'],
|
|
99
|
+
bookmap: ['booktitle', 'bookmeta', 'frontmatter', 'chapter', 'part', 'appendices', 'appendix', 'backmatter', 'reltable'],
|
|
100
|
+
booktitle: ['mainbooktitle', 'booktitlealt'],
|
|
101
|
+
topicref: MAP_REF_CHILDREN,
|
|
102
|
+
topichead: MAP_REF_CHILDREN,
|
|
103
|
+
topicgroup: ['topicref', 'topichead', 'keydef', 'mapref', 'topicmeta'],
|
|
104
|
+
chapter: MAP_REF_CHILDREN,
|
|
105
|
+
part: [...MAP_REF_CHILDREN, 'chapter'],
|
|
106
|
+
appendix: MAP_REF_CHILDREN,
|
|
107
|
+
appendices: ['appendix', 'topicmeta'],
|
|
108
|
+
keydef: ['topicmeta', 'topicref'],
|
|
109
|
+
mapref: ['topicmeta'],
|
|
110
|
+
topicmeta: ['navtitle', 'linktext', 'shortdesc', 'keywords', 'data', 'othermeta', 'searchtitle', 'resourceid', 'author'],
|
|
111
|
+
reltable: ['relheader', 'relrow'],
|
|
112
|
+
relheader: ['relcolspec'],
|
|
113
|
+
relrow: ['relcell'],
|
|
114
|
+
relcolspec: ['topicref'],
|
|
115
|
+
relcell: ['topicref'],
|
|
116
|
+
};
|
|
117
|
+
/** Roots offered where no element is open yet. */
|
|
118
|
+
export const ROOT_HINTS = {
|
|
119
|
+
topic: ['concept', 'task', 'reference', 'topic', 'glossentry', 'troubleshooting'],
|
|
120
|
+
map: ['map', 'bookmap'],
|
|
121
|
+
};
|
|
122
|
+
/** Values for enumerated attributes. A `tag:attr` key outranks a bare `attr`. */
|
|
123
|
+
export const ENUM_VALUES = {
|
|
124
|
+
'note:type': ['note', 'tip', 'important', 'caution', 'warning', 'danger', 'attention', 'remember', 'restriction', 'fastpath', 'trouble', 'notice', 'other'],
|
|
125
|
+
scope: ['local', 'peer', 'external'],
|
|
126
|
+
format: ['dita', 'ditamap', 'html', 'pdf', 'xml', 'txt'],
|
|
127
|
+
'processing-role': ['normal', 'resource-only'],
|
|
128
|
+
toc: ['yes', 'no'],
|
|
129
|
+
print: ['yes', 'no', 'printonly'],
|
|
130
|
+
search: ['yes', 'no'],
|
|
131
|
+
locktitle: ['yes', 'no'],
|
|
132
|
+
linking: ['normal', 'targetonly', 'sourceonly', 'none'],
|
|
133
|
+
'collection-type': ['unordered', 'sequence', 'choice', 'family'],
|
|
134
|
+
type: ['fn', 'section', 'table', 'fig', 'li', 'step', 'topic', 'concept', 'task', 'reference'],
|
|
135
|
+
placement: ['inline', 'break'],
|
|
136
|
+
align: ['left', 'right', 'center', 'current', 'justify'],
|
|
137
|
+
valign: ['top', 'middle', 'bottom'],
|
|
138
|
+
frame: ['all', 'bottom', 'none', 'sides', 'top', 'topbot'],
|
|
139
|
+
colsep: ['0', '1'],
|
|
140
|
+
rowsep: ['0', '1'],
|
|
141
|
+
pgwide: ['0', '1'],
|
|
142
|
+
importance: ['obsolete', 'deprecated', 'optional', 'default', 'low', 'normal', 'high', 'recommended', 'required', 'urgent'],
|
|
143
|
+
status: ['new', 'changed', 'deleted', 'unchanged'],
|
|
144
|
+
translate: ['yes', 'no', '-dita-use-conref-target'],
|
|
145
|
+
'xml:lang': ['en-US', 'en-GB', 'zh-CN', 'zh-TW', 'ja-JP', 'ko-KR', 'de-DE', 'fr-FR', 'es-ES', 'ru-RU'],
|
|
146
|
+
};
|
|
147
|
+
/** Attributes whose value is a path, and which kind of file it names. */
|
|
148
|
+
export const FILE_ATTRS = {
|
|
149
|
+
href: 'topic',
|
|
150
|
+
conref: 'topic',
|
|
151
|
+
conrefend: 'topic',
|
|
152
|
+
};
|
|
153
|
+
/** Elements whose `href` points at an image or other binary rather than a topic. */
|
|
154
|
+
export const IMAGE_HREF_TAGS = new Set(['image', 'object', 'imagemap']);
|
|
155
|
+
export const IMAGE_EXT = /\.(png|jpe?g|gif|svg|webp|bmp|tiff?|ico|emf)$/i;
|