@dita-viewer/core 0.2.2 → 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.
@@ -16,8 +16,21 @@ export interface RefEntry {
16
16
  /** format attribute on the same tag, if any */
17
17
  format?: string;
18
18
  }
19
- /** Collects every reference-bearing attribute in the document text. */
20
- export declare function collectRefEntries(text: string): RefEntry[];
19
+ /**
20
+ * Collects every reference-bearing attribute in the document text.
21
+ *
22
+ * `attrs` names the attributes to collect and defaults to the navigable set
23
+ * (href / conref / conkeyref / keyref). A caller that needs more -- the rename
24
+ * participant also has to follow conrefend, copy-to, data and longdescref --
25
+ * passes its own set, so go-to-definition and find-references keep seeing
26
+ * exactly what they saw before.
27
+ *
28
+ * Comments, CDATA sections and processing instructions are not scanned: an
29
+ * `href` inside them is example text or a commented-out reference, not a live
30
+ * one, so rename, go-to-definition, find-references and diagnostics all leave
31
+ * it alone. Offsets still index the original `text`.
32
+ */
33
+ export declare function collectRefEntries(text: string, attrs?: ReadonlySet<string>): RefEntry[];
21
34
  /** Finds the reference attribute whose value contains the given offset. */
22
35
  export declare function findRefAttrAt(text: string, offset: number): RefEntry | undefined;
23
36
  /** True when the reference points outside the local project (not resolvable from disk). */
@@ -30,14 +30,44 @@ const mapTypeMap_1 = require("../render/mapTypeMap");
30
30
  // ── Reference attribute scanning ──
31
31
  /** Attributes whose values reference other files/keys/elements. */
32
32
  const REF_ATTRS = new Set(['href', 'conref', 'conkeyref', 'keyref']);
33
+ // One alternation, so whichever region opens first wins: a "<!--" inside a
34
+ // CDATA section is CDATA text, and a "<![CDATA[" inside a comment is comment
35
+ // text. An opener with no terminator matches nothing and is left alone, so a
36
+ // stray "<!--" cannot hide every reference after it.
37
+ const NON_MARKUP_RE = /<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<\?[\s\S]*?\?>/g;
38
+ /**
39
+ * Blanks comments, CDATA sections and processing instructions with spaces of
40
+ * the same length (line breaks are kept), so every offset into the result is
41
+ * also an offset into `text`. What is left is only live markup and character
42
+ * data, which is what a reference scan should look at.
43
+ */
44
+ function blankNonMarkup(text) {
45
+ if (!text.includes('<!') && !text.includes('<?'))
46
+ return text;
47
+ return text.replace(NON_MARKUP_RE, (region) => region.replace(/[^\r\n]/g, ' '));
48
+ }
33
49
  const TAG_RE = /<([A-Za-z][\w.-]*)((?:"[^"]*"|'[^']*'|[^>"'])*)>/g;
34
50
  const ATTR_RE = /([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
35
- /** Collects every reference-bearing attribute in the document text. */
36
- function collectRefEntries(text) {
51
+ /**
52
+ * Collects every reference-bearing attribute in the document text.
53
+ *
54
+ * `attrs` names the attributes to collect and defaults to the navigable set
55
+ * (href / conref / conkeyref / keyref). A caller that needs more -- the rename
56
+ * participant also has to follow conrefend, copy-to, data and longdescref --
57
+ * passes its own set, so go-to-definition and find-references keep seeing
58
+ * exactly what they saw before.
59
+ *
60
+ * Comments, CDATA sections and processing instructions are not scanned: an
61
+ * `href` inside them is example text or a commented-out reference, not a live
62
+ * one, so rename, go-to-definition, find-references and diagnostics all leave
63
+ * it alone. Offsets still index the original `text`.
64
+ */
65
+ function collectRefEntries(text, attrs = REF_ATTRS) {
37
66
  const entries = [];
67
+ const scanText = blankNonMarkup(text);
38
68
  TAG_RE.lastIndex = 0;
39
69
  let tagMatch;
40
- while ((tagMatch = TAG_RE.exec(text)) !== null) {
70
+ while ((tagMatch = TAG_RE.exec(scanText)) !== null) {
41
71
  const tagName = tagMatch[1];
42
72
  const attrSegment = tagMatch[2];
43
73
  if (!attrSegment)
@@ -57,7 +87,7 @@ function collectRefEntries(text) {
57
87
  }
58
88
  ATTR_RE.lastIndex = 0;
59
89
  while ((am = ATTR_RE.exec(attrSegment)) !== null) {
60
- if (!REF_ATTRS.has(am[1]))
90
+ if (!attrs.has(am[1]))
61
91
  continue;
62
92
  const eqAndQuote = am[0].indexOf('=') + 1 + (am[0].slice(am[0].indexOf('=') + 1).match(/["']/)?.index ?? 0);
63
93
  const valueStart = segmentOffset + am.index + eqAndQuote + 1;
@@ -257,7 +257,17 @@ function stampSourceRange(node, range) {
257
257
  function mergeConrefTarget(node, target) {
258
258
  const restAttrs = Object.fromEntries(Object.entries(node.attributes || {}).filter(([k]) => k !== 'conref' && k !== 'conrefend' && k !== 'conkeyref'));
259
259
  if (target.baseType && target.baseType === node.baseType) {
260
- return { ...node, children: (target.children || []).map((c) => stampSourceRange(c, node.sourceRange)), attributes: restAttrs };
260
+ // The merged element keeps the referencing element's attributes, so a
261
+ // keyref written on the target itself (`<ph id="x" keyref="prod"/>`) would
262
+ // vanish and the reuse would render empty. Conref resolves before keys do,
263
+ // so the pulled-in element's keyref is resolved here, in this document's
264
+ // key space. The referencing element's own keyref still wins.
265
+ const carried = target.attributes?.keyref && !restAttrs.keyref ? { keyref: target.attributes.keyref } : {};
266
+ return {
267
+ ...node,
268
+ children: (target.children || []).map((c) => stampSourceRange(c, node.sourceRange)),
269
+ attributes: { ...carried, ...restAttrs },
270
+ };
261
271
  }
262
272
  // Cross-type merge: the target's element shape wins, but never its source
263
273
  // position — the target and all of its descendants sit at the referencing
@@ -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
- /** Collects every reference-bearing attribute in the document text. */
12
- export function collectRefEntries(text) {
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(text)) !== null) {
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 (!REF_ATTRS.has(am[1]))
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;
@@ -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
- return { ...node, children: (target.children || []).map((c) => stampSourceRange(c, node.sourceRange)), attributes: restAttrs };
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dita-viewer/core",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Host-neutral DITA parsing and HTML rendering core, shared by the VS Code DITA Viewer and obsidian-dita.",
5
5
  "keywords": [
6
6
  "dita",