@mintlify/common 1.0.1151 → 1.0.1153

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.
@@ -3,4 +3,5 @@ import { type UserMentionNameResolver } from '../../mentions/userMentionToken.js
3
3
  export type RemarkUserMentionsOptions = {
4
4
  resolveUserName?: UserMentionNameResolver;
5
5
  };
6
+ export declare function collectUserMentionText(tree: Root): string[];
6
7
  export declare const remarkUserMentions: ({ resolveUserName }?: RemarkUserMentionsOptions) => (tree: Root) => void;
@@ -1,12 +1,12 @@
1
1
  import { visit } from 'unist-util-visit';
2
- import { escapeMentionNameForSource, flattenUserMentionTokens, hasUserMentionTokens, isMentionTextAttribute, isUserMentionId, } from '../../mentions/userMentionToken.js';
2
+ import { escapeMentionNameForSource, extractUserMentionIds, flattenUserMentionTokens, hasUserMentionTokens, isMentionTextAttribute, isUserMentionId, userMentionToken, } from '../../mentions/userMentionToken.js';
3
3
  import { isPromptBodySourceAttribute } from './remarkPrompt.js';
4
4
  import { TITLE_COMPONENTS } from './remarkTitleMarkdown.js';
5
5
  const IN_TEXT = { inText: true };
6
6
  const isMdxJsxElement = (node) => node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement';
7
7
  const isParent = (node) => 'children' in node && Array.isArray(node.children);
8
8
  const isRecord = (value) => typeof value === 'object' && value !== null;
9
- function flattenStringLiteralExpression(expression, resolve, { escapeNames, rawSource }) {
9
+ function stringLiteralSurface(expression, { escapeNames, scan }, onSurface) {
10
10
  var _a;
11
11
  const program = (_a = expression.data) === null || _a === void 0 ? void 0 : _a.estree;
12
12
  const statement = program === null || program === void 0 ? void 0 : program.body[0];
@@ -15,45 +15,63 @@ function flattenStringLiteralExpression(expression, resolve, { escapeNames, rawS
15
15
  const literal = statement.expression;
16
16
  if (!isRecord(literal) || literal.type !== 'Literal' || typeof literal.value !== 'string')
17
17
  return;
18
- const scan = rawSource ? undefined : IN_TEXT;
19
- if (!hasUserMentionTokens(literal.value, scan))
20
- return;
21
- const flat = flattenUserMentionTokens(literal.value, (userId) => {
22
- const name = resolve === null || resolve === void 0 ? void 0 : resolve(userId);
23
- return name === undefined || !escapeNames ? name : escapeMentionNameForSource(name);
24
- }, scan);
25
- literal.value = flat;
26
- literal.raw = JSON.stringify(flat);
27
- expression.value = JSON.stringify(flat);
18
+ onSurface({
19
+ value: literal.value,
20
+ scan,
21
+ escapeNames,
22
+ set: (next) => {
23
+ literal.value = next;
24
+ literal.raw = JSON.stringify(next);
25
+ expression.value = JSON.stringify(next);
26
+ },
27
+ });
28
28
  }
29
- function foldShortcutReferences(parent, resolve) {
29
+ function shortcutMentionId(text, reference) {
30
30
  var _a;
31
+ if (text.type !== 'text' || !text.value.endsWith('@'))
32
+ return undefined;
33
+ if (reference.type !== 'linkReference' || reference.referenceType !== 'shortcut')
34
+ return undefined;
35
+ const label = (_a = reference.label) !== null && _a !== void 0 ? _a : reference.identifier;
36
+ if (!isUserMentionId(label))
37
+ return undefined;
38
+ const [only, ...rest] = reference.children;
39
+ if (rest.length > 0 || !only || only.type !== 'text' || only.value !== label)
40
+ return undefined;
41
+ return label;
42
+ }
43
+ function shortcutReferenceSurfaces(parent, onSurface) {
31
44
  const children = parent.children;
32
45
  for (let index = 0; index < children.length - 1; index++) {
33
46
  const text = children[index];
34
47
  const reference = children[index + 1];
35
- if (!text || !reference || text.type !== 'text' || !text.value.endsWith('@'))
36
- continue;
37
- if (reference.type !== 'linkReference' || reference.referenceType !== 'shortcut')
38
- continue;
39
- const label = (_a = reference.label) !== null && _a !== void 0 ? _a : reference.identifier;
40
- if (!isUserMentionId(label))
48
+ if (!text || !reference || text.type !== 'text')
41
49
  continue;
42
- const [only, ...rest] = reference.children;
43
- if (rest.length > 0 || !only || only.type !== 'text' || only.value !== label)
50
+ const label = shortcutMentionId(text, reference);
51
+ if (label === undefined)
44
52
  continue;
45
- text.value = text.value.slice(0, -1) + flattenUserMentionTokens(`@[${label}]`, resolve);
46
- children.splice(index + 1, 1);
47
- const next = children[index + 1];
48
- if ((next === null || next === void 0 ? void 0 : next.type) === 'text') {
49
- text.value += next.value;
50
- children.splice(index + 1, 1);
51
- }
52
- index--;
53
+ let folded = false;
54
+ onSurface({
55
+ value: userMentionToken(label),
56
+ scan: undefined,
57
+ escapeNames: false,
58
+ set: (next) => {
59
+ text.value = text.value.slice(0, -1) + next;
60
+ children.splice(index + 1, 1);
61
+ const following = children[index + 1];
62
+ if ((following === null || following === void 0 ? void 0 : following.type) === 'text') {
63
+ text.value += following.value;
64
+ children.splice(index + 1, 1);
65
+ }
66
+ folded = true;
67
+ },
68
+ });
69
+ if (folded)
70
+ index--;
53
71
  }
54
72
  }
55
73
  // A Prompt body is plain text on the page, so code and math inside it are text there too.
56
- function flattenPromptCode(prompt, resolve) {
74
+ function promptCodeSurfaces(prompt, onSurface) {
57
75
  visit(prompt, (child) => {
58
76
  if (child.type !== 'inlineCode' &&
59
77
  child.type !== 'code' &&
@@ -61,47 +79,78 @@ function flattenPromptCode(prompt, resolve) {
61
79
  child.type !== 'math') {
62
80
  return;
63
81
  }
64
- if (hasUserMentionTokens(child.value)) {
65
- child.value = flattenUserMentionTokens(child.value, resolve);
66
- }
82
+ onSurface({
83
+ value: child.value,
84
+ scan: undefined,
85
+ escapeNames: false,
86
+ set: (next) => {
87
+ child.value = next;
88
+ },
89
+ });
67
90
  });
68
91
  }
69
- export const remarkUserMentions = ({ resolveUserName } = {}) => (tree) => {
70
- // Body text and text-bearing component attributes only: image alt/title, link and definition titles are HTML attributes and stay as written.
92
+ // Body text and text-bearing component attributes only: image alt/title, link
93
+ // and definition titles are HTML attributes and stay as written.
94
+ function visitUserMentionSurfaces(tree, onSurface) {
71
95
  visit(tree, (node) => {
72
96
  if (node.type === 'text') {
73
- if (hasUserMentionTokens(node.value, IN_TEXT)) {
74
- node.value = flattenUserMentionTokens(node.value, resolveUserName, IN_TEXT);
75
- }
97
+ const text = node;
98
+ onSurface({
99
+ value: text.value,
100
+ scan: IN_TEXT,
101
+ escapeNames: false,
102
+ set: (next) => {
103
+ text.value = next;
104
+ },
105
+ });
76
106
  return;
77
107
  }
78
108
  if (isMdxJsxElement(node)) {
79
109
  if (node.name === 'Prompt')
80
- flattenPromptCode(node, resolveUserName);
110
+ promptCodeSurfaces(node, onSurface);
81
111
  const titleIsMarkdown = node.name !== null && TITLE_COMPONENTS.has(node.name);
82
112
  for (const attribute of node.attributes) {
83
113
  if (attribute.type !== 'mdxJsxAttribute' || !isMentionTextAttribute(attribute.name)) {
84
114
  continue;
85
115
  }
116
+ const escapeNames = titleIsMarkdown && attribute.name === 'title';
86
117
  if (typeof attribute.value === 'string') {
87
- if (!hasUserMentionTokens(attribute.value, IN_TEXT))
88
- continue;
89
- const escape = titleIsMarkdown && attribute.name === 'title';
90
- attribute.value = flattenUserMentionTokens(attribute.value, (userId) => {
91
- const name = resolveUserName === null || resolveUserName === void 0 ? void 0 : resolveUserName(userId);
92
- return name === undefined || !escape ? name : escapeMentionNameForSource(name);
93
- }, IN_TEXT);
118
+ onSurface({
119
+ value: attribute.value,
120
+ scan: IN_TEXT,
121
+ escapeNames,
122
+ set: (next) => {
123
+ attribute.value = next;
124
+ },
125
+ });
94
126
  }
95
127
  else if (attribute.value && typeof attribute.value === 'object') {
96
- flattenStringLiteralExpression(attribute.value, resolveUserName, {
97
- escapeNames: titleIsMarkdown && attribute.name === 'title',
98
- rawSource: isPromptBodySourceAttribute(attribute),
99
- });
128
+ stringLiteralSurface(attribute.value, { escapeNames, scan: isPromptBodySourceAttribute(attribute) ? undefined : IN_TEXT }, onSurface);
100
129
  }
101
130
  }
102
131
  }
103
132
  if (isParent(node) && node.children.some((child) => child.type === 'linkReference')) {
104
- foldShortcutReferences(node, resolveUserName);
133
+ shortcutReferenceSurfaces(node, onSurface);
105
134
  }
106
135
  });
136
+ }
137
+ export function collectUserMentionText(tree) {
138
+ const out = [];
139
+ visitUserMentionSurfaces(tree, ({ value, scan }) => {
140
+ if (scan === IN_TEXT)
141
+ out.push(value);
142
+ else
143
+ out.push(...extractUserMentionIds(value).map(userMentionToken));
144
+ });
145
+ return out;
146
+ }
147
+ export const remarkUserMentions = ({ resolveUserName } = {}) => (tree) => {
148
+ visitUserMentionSurfaces(tree, ({ value, scan, escapeNames, set }) => {
149
+ if (!hasUserMentionTokens(value, scan))
150
+ return;
151
+ set(flattenUserMentionTokens(value, (userId) => {
152
+ const name = resolveUserName === null || resolveUserName === void 0 ? void 0 : resolveUserName(userId);
153
+ return name === undefined || !escapeNames ? name : escapeMentionNameForSource(name);
154
+ }, scan));
155
+ });
107
156
  };