@astratra/native-ui 1.3.0 → 1.3.1

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 CHANGED
@@ -297,6 +297,11 @@ import * as Clipboard from 'expo-clipboard';
297
297
  />
298
298
  ```
299
299
 
300
+ Un lien n'est touchable que si son schéma est permis : `http`, `https` et `mailto` par défaut
301
+ (`linkSchemes` pour en permettre d'autres, par exemple le schéma de ton application). Ce
302
+ composant affiche des réponses d'assistant, donc un texte qu'un contenu piégé peut dicter : un
303
+ lien `tel:`, `sms:` ou vers une autre application reste du texte.
304
+
300
305
  Ce que l'analyseur garantit, chaque règle née d'un défaut vu à l'écran :
301
306
 
302
307
  - **une liste numérotée continue** malgré les puces glissées sous chaque étape —
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astratra/native-ui",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Mobile UI kit for React Native / Expo: Apple's Liquid Glass on iOS and a calibrated visible surface on Android, glass buttons, pale cards, bars that fold on scroll, an Instagram-style tab bar, a collapsible header, a Markdown renderer for AI answers, pictures (natural-ratio images revealed gently, a shimmer placeholder, a full-screen viewer with pinch, double-tap and pull-to-close), and a thumbs up/down feedback bar for AI answers. Pure rules exported separately and tested in plain Node.",
5
5
  "engines": {
6
6
  "node": ">=22"
@@ -23,9 +23,6 @@
23
23
  "README.md",
24
24
  "LICENSE"
25
25
  ],
26
- "scripts": {
27
- "test": "jest"
28
- },
29
26
  "jest": {
30
27
  "testMatch": [
31
28
  "**/__tests__/**/*.test.js"
@@ -72,5 +69,8 @@
72
69
  "jest": "30.4.2",
73
70
  "jsdom": "^30.0.1",
74
71
  "react-dom": "^19.2.8"
72
+ },
73
+ "scripts": {
74
+ "test": "jest"
75
75
  }
76
- }
76
+ }
@@ -89,16 +89,28 @@ function openLink(href) {
89
89
  Promise.resolve(RN.Linking.openURL(href)).catch(() => {});
90
90
  }
91
91
 
92
+ /* Ce composant affiche des réponses d'assistant : un texte qu'un contenu piégé peut dicter. Un lien n'est touchable
93
+ que si son schéma est permis (le web et le courriel par défaut) ; « tel: », « sms: », un schéma d'application ou
94
+ « file: » restent du texte. Une adresse sans schéma (un chemin de l'application) est laissée à onLinkPress. */
95
+ const DEFAULT_LINK_SCHEMES = Object.freeze(['http', 'https', 'mailto']);
96
+ function linkAllowed(href, schemes) {
97
+ const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(String(href));
98
+ if (!scheme) return true;
99
+ const allowed = Array.isArray(schemes) ? schemes : DEFAULT_LINK_SCHEMES;
100
+ return allowed.some((name) => String(name).toLowerCase() === scheme[1].toLowerCase());
101
+ }
102
+
92
103
  const DEFAULT_VEIL = 'rgba(255,255,255,0.92)';
93
104
  /* The default context lets `MarkdownTable` render on its own, outside a
94
105
  MarkdownView. */
95
- const Ctx = React.createContext({ styles: DEFAULT_STYLES, onLinkPress: openLink, tableVeilColor: DEFAULT_VEIL });
106
+ const Ctx = React.createContext({ styles: DEFAULT_STYLES, onLinkPress: openLink, linkSchemes: DEFAULT_LINK_SCHEMES, tableVeilColor: DEFAULT_VEIL });
96
107
 
97
108
  function MarkdownView({
98
109
  content,
99
110
  plain = false,
100
111
  styles: overrides,
101
112
  onLinkPress = openLink,
113
+ linkSchemes = DEFAULT_LINK_SCHEMES,
102
114
  onCopyCode,
103
115
  copyLabel,
104
116
  copyIcon,
@@ -119,7 +131,7 @@ function MarkdownView({
119
131
  if (plain) return h(RN.Text, { testID, style: [styles.body, styles.plain] }, content);
120
132
  if (!blocks.length) return null;
121
133
 
122
- const context = { styles, onLinkPress, onCopyCode, copyLabel, copyIcon, copiedIcon, fontScale, tableVeilColor };
134
+ const context = { styles, onLinkPress, linkSchemes, onCopyCode, copyLabel, copyIcon, copiedIcon, fontScale, tableVeilColor };
123
135
  return h(
124
136
  Ctx.Provider,
125
137
  { value: context },
@@ -166,7 +178,7 @@ function Block({ block }) {
166
178
  }
167
179
 
168
180
  function Inlines({ inlines }) {
169
- const { styles, onLinkPress } = React.useContext(Ctx);
181
+ const { styles, onLinkPress, linkSchemes } = React.useContext(Ctx);
170
182
  return h(
171
183
  React.Fragment,
172
184
  null,
@@ -174,6 +186,7 @@ function Inlines({ inlines }) {
174
186
  if (inline.kind === 'bold') return h(RN.Text, { key: index, style: styles.bold }, inline.text);
175
187
  if (inline.kind === 'italic') return h(RN.Text, { key: index, style: styles.italic }, inline.text);
176
188
  if (inline.kind === 'code') return h(RN.Text, { key: index, style: styles.inlineCode }, inline.text);
189
+ if (inline.kind === 'link' && !linkAllowed(inline.href, linkSchemes)) return h(RN.Text, { key: index }, inline.text);
177
190
  if (inline.kind === 'link') {
178
191
  return h(
179
192
  RN.Text,
package/src/index.d.ts CHANGED
@@ -214,6 +214,8 @@ export interface MarkdownViewProps {
214
214
  /** Style overrides by name (body, heading, tableCell, …), merged with the defaults. */
215
215
  styles?: Record<string, NativeStyle>;
216
216
  onLinkPress?: (href: string) => void;
217
+ /** Schemes a link may have to be tappable. Default: http, https, mailto. Any other link is shown as plain text. */
218
+ linkSchemes?: string[];
217
219
  /** Copies one code block; the button shows only with a copyIcon. */
218
220
  onCopyCode?: (text: string) => void | Promise<void>;
219
221
  copyLabel?: string;
@@ -174,46 +174,91 @@ function parseInline(source) {
174
174
  plain = '';
175
175
  };
176
176
 
177
+ /*
178
+ * Where the next occurrence of `pattern` is, from `position` on. The answer is remembered: asked again from a
179
+ * position between the last question and its answer, it is the same. Without that, a paragraph made of delimiters
180
+ * that never close ("[[[[…", "**a **a …") was read again from every delimiter, and the time grew as the square
181
+ * of its length: on a phone, one long reply froze the screen.
182
+ */
183
+ const finder = (pattern) => {
184
+ let asked = -1;
185
+ let found = -2;
186
+ return (position) => {
187
+ if (asked !== -1 && position >= asked && (found === -1 || position <= found)) return found;
188
+ pattern.lastIndex = position;
189
+ const match = pattern.exec(text);
190
+ asked = position;
191
+ found = match ? match.index : -1;
192
+ return found;
193
+ };
194
+ };
195
+ const nextNewline = finder(/\n/g);
196
+ const nextBacktick = finder(/`/g);
197
+ const nextStar = finder(/\*/g);
198
+ const nextBracket = finder(/\]/g);
199
+ const nextUrlEnd = finder(/[)\s]/g);
200
+ const nextBoldEnd = { '**': finder(/\S\*\*/g), __: finder(/\S__/g) };
201
+ /* No line break between `from` (included) and `to` (excluded). */
202
+ const sameLine = (from, to) => { const line = nextNewline(from); return line === -1 || line >= to; };
203
+ const isSpace = (char) => /\s/.test(char);
204
+
177
205
  let index = 0;
178
206
  while (index < text.length) {
179
- const rest = text.slice(index);
207
+ const char = text[index];
180
208
 
181
- const code = /^`([^`\n]+)`/.exec(rest);
182
- if (code) {
183
- flush();
184
- inlines.push({ kind: 'code', text: code[1] });
185
- index += code[0].length;
186
- continue;
209
+ /* `code`: up to the next backtick, on the same line, not empty. */
210
+ if (char === '`') {
211
+ const close = nextBacktick(index + 1);
212
+ if (close > index + 1 && sameLine(index + 1, close)) {
213
+ flush();
214
+ inlines.push({ kind: 'code', text: text.slice(index + 1, close) });
215
+ index = close + 1;
216
+ continue;
217
+ }
187
218
  }
188
219
 
189
- const bold = /^(\*\*|__)(?=\S)([\s\S]*?\S)\1/.exec(rest);
190
- if (bold) {
191
- flush();
192
- inlines.push({ kind: 'bold', text: bold[2] });
193
- index += bold[0].length;
194
- continue;
220
+ /* **bold** or __bold__: starts and ends on a character that is not a blank; the first such ending wins. */
221
+ const mark = text.startsWith('**', index) ? '**' : text.startsWith('__', index) ? '__' : null;
222
+ if (mark && index + 2 < text.length && !isSpace(text[index + 2])) {
223
+ const last = nextBoldEnd[mark](index + 2);
224
+ if (last !== -1) {
225
+ flush();
226
+ inlines.push({ kind: 'bold', text: text.slice(index + 2, last + 1) });
227
+ index = last + 3;
228
+ continue;
229
+ }
195
230
  }
196
231
 
197
232
  /* Italic is recognised by the star only: a lone underscore appears in
198
233
  identifiers (`final_grade`) far more often than as a style intent, and
199
234
  taking it for a delimiter ate the word. */
200
- const italic = /^\*(?=[^\s*])([^*\n]*[^\s*])\*/.exec(rest);
201
- if (italic) {
202
- flush();
203
- inlines.push({ kind: 'italic', text: italic[1] });
204
- index += italic[0].length;
205
- continue;
235
+ if (char === '*') {
236
+ const close = nextStar(index + 1);
237
+ if (close > index + 1 && !isSpace(text[index + 1]) && !isSpace(text[close - 1]) && sameLine(index + 1, close)) {
238
+ flush();
239
+ inlines.push({ kind: 'italic', text: text.slice(index + 1, close) });
240
+ index = close + 1;
241
+ continue;
242
+ }
206
243
  }
207
244
 
208
- const link = /^\[([^\]\n]*)\]\(([^)\s]+)\)/.exec(rest);
209
- if (link) {
210
- flush();
211
- inlines.push({ kind: 'link', text: link[1] || link[2], href: link[2] });
212
- index += link[0].length;
213
- continue;
245
+ /* [text](address): the text on one line, the address without blank, not empty. */
246
+ if (char === '[') {
247
+ const bracket = nextBracket(index + 1);
248
+ if (bracket !== -1 && text[bracket + 1] === '(' && sameLine(index + 1, bracket)) {
249
+ const end = nextUrlEnd(bracket + 2);
250
+ if (end > bracket + 2 && text[end] === ')') {
251
+ const label = text.slice(index + 1, bracket);
252
+ const href = text.slice(bracket + 2, end);
253
+ flush();
254
+ inlines.push({ kind: 'link', text: label || href, href });
255
+ index = end + 1;
256
+ continue;
257
+ }
258
+ }
214
259
  }
215
260
 
216
- plain += text[index];
261
+ plain += char;
217
262
  index += 1;
218
263
  }
219
264