react-native-nitro-markdown 0.7.0 → 0.7.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.
Files changed (36) hide show
  1. package/README.md +128 -518
  2. package/lib/commonjs/MarkdownSession.js +6 -2
  3. package/lib/commonjs/MarkdownSession.js.map +1 -1
  4. package/lib/commonjs/index.js.map +1 -1
  5. package/lib/commonjs/markdown-stream.js +8 -6
  6. package/lib/commonjs/markdown-stream.js.map +1 -1
  7. package/lib/commonjs/use-markdown-stream.js +23 -4
  8. package/lib/commonjs/use-markdown-stream.js.map +1 -1
  9. package/lib/module/MarkdownSession.js +6 -2
  10. package/lib/module/MarkdownSession.js.map +1 -1
  11. package/lib/module/index.js.map +1 -1
  12. package/lib/module/markdown-stream.js +8 -6
  13. package/lib/module/markdown-stream.js.map +1 -1
  14. package/lib/module/use-markdown-stream.js +22 -5
  15. package/lib/module/use-markdown-stream.js.map +1 -1
  16. package/lib/typescript/commonjs/MarkdownSession.d.ts +1 -1
  17. package/lib/typescript/commonjs/MarkdownSession.d.ts.map +1 -1
  18. package/lib/typescript/commonjs/index.d.ts +1 -0
  19. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  20. package/lib/typescript/commonjs/markdown-stream.d.ts +2 -1
  21. package/lib/typescript/commonjs/markdown-stream.d.ts.map +1 -1
  22. package/lib/typescript/commonjs/use-markdown-stream.d.ts +4 -1
  23. package/lib/typescript/commonjs/use-markdown-stream.d.ts.map +1 -1
  24. package/lib/typescript/module/MarkdownSession.d.ts +1 -1
  25. package/lib/typescript/module/MarkdownSession.d.ts.map +1 -1
  26. package/lib/typescript/module/index.d.ts +1 -0
  27. package/lib/typescript/module/index.d.ts.map +1 -1
  28. package/lib/typescript/module/markdown-stream.d.ts +2 -1
  29. package/lib/typescript/module/markdown-stream.d.ts.map +1 -1
  30. package/lib/typescript/module/use-markdown-stream.d.ts +4 -1
  31. package/lib/typescript/module/use-markdown-stream.d.ts.map +1 -1
  32. package/package.json +2 -1
  33. package/src/MarkdownSession.ts +9 -2
  34. package/src/index.ts +1 -0
  35. package/src/markdown-stream.tsx +12 -7
  36. package/src/use-markdown-stream.ts +53 -13
@@ -1 +1 @@
1
- {"version":3,"file":"use-markdown-stream.d.ts","sourceRoot":"","sources":["../../../src/use-markdown-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAO1D,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEvE,wBAAgB,kBAAkB;;;;;;6BAiCY,MAAM;kBAMjB,MAAM;oBAIJ,MAAM,MAAM,MAAM,QAAQ,MAAM;EAcpE;AAED,wBAAgB,SAAS,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;0BAgBzC,MAAM;;;;;;6BA1CoB,MAAM;kBAMjB,MAAM;oBAIJ,MAAM,MAAM,MAAM,QAAQ,MAAM;EAqDpE"}
1
+ {"version":3,"file":"use-markdown-stream.d.ts","sourceRoot":"","sources":["../../../src/use-markdown-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAO1D,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAEvE,wBAAgB,kBAAkB,CAAC,WAAW,CAAC,EAAE,MAAM;;;;;;6BA2CT,MAAM;kBAMjB,MAAM;oBAIJ,MAAM,MAAM,MAAM,QAAQ,MAAM;EA0BpE;AAED,MAAM,MAAM,yBAAyB,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAE9E,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,eAAe,GAAG,yBAAyB,GACjD,KAAK,IAAI,yBAAyB,CAEpC;AAED,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,eAAe,GAAG,yBAAyB,GACnD,eAAe,CAMjB;AAED,wBAAgB,SAAS,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;;;0BAgBzC,MAAM;;;;;;6BAxEoB,MAAM;kBAMjB,MAAM;oBAIJ,MAAM,MAAM,MAAM,QAAQ,MAAM;EAmFpE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-markdown",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "High-performance Markdown parser for React Native using Nitro Modules and md4c",
5
5
  "main": "lib/commonjs/index.js",
6
6
  "module": "lib/module/index.js",
@@ -57,6 +57,7 @@
57
57
  "clean": "rimraf lib nitrogen/generated",
58
58
  "codegen": "nitrogen --logLevel=\"debug\"",
59
59
  "lint": "bunx eslint src --max-warnings=0 --ignore-pattern 'src/**/__tests__/**' --ignore-pattern 'src/**/*.nitro.ts'",
60
+ "lint:fix": "bunx eslint src --fix --ignore-pattern 'src/**/__tests__/**' --ignore-pattern 'src/**/*.nitro.ts'",
60
61
  "typecheck": "tsc --noEmit",
61
62
  "test": "jest",
62
63
  "test:coverage": "jest --coverage",
@@ -3,6 +3,13 @@ import type { MarkdownSession as MarkdownSessionSpec } from "./specs/MarkdownSes
3
3
 
4
4
  export type MarkdownSession = MarkdownSessionSpec;
5
5
 
6
- export function createMarkdownSession(): MarkdownSession {
7
- return NitroModules.createHybridObject<MarkdownSession>("MarkdownSession");
6
+ export function createMarkdownSession(initialText?: string): MarkdownSession {
7
+ const session =
8
+ NitroModules.createHybridObject<MarkdownSession>("MarkdownSession");
9
+
10
+ if (initialText !== undefined) {
11
+ session.reset(initialText);
12
+ }
13
+
14
+ return session;
8
15
  }
package/src/index.ts CHANGED
@@ -77,6 +77,7 @@ export { MathInline, MathBlock } from "./renderers/math";
77
77
  export { createMarkdownSession } from "./MarkdownSession";
78
78
  export type { MarkdownSession } from "./MarkdownSession";
79
79
  export { useMarkdownSession, useStream } from "./use-markdown-stream";
80
+ export type { MarkdownSessionController } from "./use-markdown-stream";
80
81
 
81
82
  export type {
82
83
  HighlightedToken,
@@ -9,6 +9,10 @@ import {
9
9
  import type { MarkdownNode } from "./headless";
10
10
  import { Markdown, type MarkdownProps } from "./markdown";
11
11
  import type { MarkdownSession } from "./specs/MarkdownSession.nitro";
12
+ import {
13
+ resolveMarkdownSession,
14
+ type MarkdownSessionController,
15
+ } from "./use-markdown-stream";
12
16
  import { getNextStreamAst, parseMarkdownAst } from "./utils/incremental-ast";
13
17
 
14
18
  const normalizeOffset = (value: number): number | null => {
@@ -71,7 +75,7 @@ export type MarkdownStreamProps = {
71
75
  /**
72
76
  * The active MarkdownSession to stream content from.
73
77
  */
74
- session: MarkdownSession;
78
+ session: MarkdownSession | MarkdownSessionController;
75
79
  /**
76
80
  * Throttle UI updates when updateStrategy is "interval".
77
81
  * Ignored when updateStrategy is "raf".
@@ -110,6 +114,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
110
114
  plugins,
111
115
  ...props
112
116
  }) => {
117
+ const activeSession = resolveMarkdownSession(session);
113
118
  const parseText = useCallback(
114
119
  (text: string): MarkdownNode => parseMarkdownAst(text, options),
115
120
  [options],
@@ -118,7 +123,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
118
123
  type: "document",
119
124
  children: [],
120
125
  });
121
- const initialText = session.getAllText();
126
+ const initialText = activeSession.getAllText();
122
127
  const hasBeforeParsePlugins =
123
128
  plugins?.some((plugin) => typeof plugin.beforeParse === "function") ??
124
129
  false;
@@ -148,7 +153,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
148
153
  }, []);
149
154
 
150
155
  useEffect(() => {
151
- const initialText = session.getAllText();
156
+ const initialText = activeSession.getAllText();
152
157
  const initialState = {
153
158
  text: initialText,
154
159
  ast: hasBeforeParsePlugins ? createEmptyAst() : parseText(initialText),
@@ -159,7 +164,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
159
164
  forceFullSyncRef.current = false;
160
165
  renderStateRef.current = initialState;
161
166
  setRenderState(initialState);
162
- }, [hasBeforeParsePlugins, parseText, session]);
167
+ }, [activeSession, hasBeforeParsePlugins, parseText]);
163
168
 
164
169
  useEffect(() => {
165
170
  const flushUpdate = () => {
@@ -184,7 +189,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
184
189
  pendingFrom,
185
190
  pendingTo,
186
191
  previousText: previousState.text,
187
- session,
192
+ session: activeSession,
188
193
  });
189
194
  if (latest === previousState.text) return;
190
195
 
@@ -230,7 +235,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
230
235
  let unsubscribe: (() => void) | null = null;
231
236
 
232
237
  try {
233
- unsubscribe = session.addListener((from, to) => {
238
+ unsubscribe = activeSession.addListener((from, to) => {
234
239
  const nextFrom = normalizeOffset(from);
235
240
  const nextTo = normalizeOffset(to);
236
241
 
@@ -276,7 +281,7 @@ export const MarkdownStream: FC<MarkdownStreamProps> = ({
276
281
  hasBeforeParsePlugins,
277
282
  options,
278
283
  plugins,
279
- session,
284
+ activeSession,
280
285
  updateIntervalMs,
281
286
  updateStrategy,
282
287
  useTransitionUpdates,
@@ -1,4 +1,4 @@
1
- import { useRef, useCallback, useState, useEffect } from "react";
1
+ import { useRef, useCallback, useState, useEffect, useMemo } from "react";
2
2
  import { createMarkdownSession } from "./MarkdownSession";
3
3
  import {
4
4
  createTimestampTimeline,
@@ -8,10 +8,11 @@ import {
8
8
 
9
9
  export type MarkdownSession = ReturnType<typeof createMarkdownSession>;
10
10
 
11
- export function useMarkdownSession() {
11
+ export function useMarkdownSession(initialText?: string) {
12
12
  const sessionRef = useRef<MarkdownSession | null>(null);
13
+ const initialTextRef = useRef(initialText);
13
14
  if (sessionRef.current === null) {
14
- sessionRef.current = createMarkdownSession();
15
+ sessionRef.current = createMarkdownSession(initialText);
15
16
  }
16
17
 
17
18
  const [isStreaming, setIsStreaming] = useState(false);
@@ -31,6 +32,15 @@ export function useMarkdownSession() {
31
32
  };
32
33
  }, []);
33
34
 
35
+ useEffect(() => {
36
+ if (initialText === undefined || initialTextRef.current === initialText) {
37
+ return;
38
+ }
39
+
40
+ initialTextRef.current = initialText;
41
+ sessionRef.current!.reset(initialText);
42
+ }, [initialText]);
43
+
34
44
  const stop = useCallback(() => {
35
45
  setIsStreaming(false);
36
46
  }, []);
@@ -55,16 +65,46 @@ export function useMarkdownSession() {
55
65
  return sessionRef.current!.replace(from, to, text);
56
66
  }, []);
57
67
 
58
- return {
59
- getSession,
60
- isStreaming,
61
- setIsStreaming,
62
- stop,
63
- clear,
64
- setHighlight,
65
- reset,
66
- replace,
67
- };
68
+ return useMemo(
69
+ () => ({
70
+ getSession,
71
+ isStreaming,
72
+ setIsStreaming,
73
+ stop,
74
+ clear,
75
+ setHighlight,
76
+ reset,
77
+ replace,
78
+ }),
79
+ [
80
+ clear,
81
+ getSession,
82
+ isStreaming,
83
+ replace,
84
+ reset,
85
+ setHighlight,
86
+ setIsStreaming,
87
+ stop,
88
+ ],
89
+ );
90
+ }
91
+
92
+ export type MarkdownSessionController = ReturnType<typeof useMarkdownSession>;
93
+
94
+ export function isMarkdownSessionController(
95
+ value: MarkdownSession | MarkdownSessionController,
96
+ ): value is MarkdownSessionController {
97
+ return typeof Reflect.get(value, "getSession") === "function";
98
+ }
99
+
100
+ export function resolveMarkdownSession(
101
+ session: MarkdownSession | MarkdownSessionController,
102
+ ): MarkdownSession {
103
+ if (isMarkdownSessionController(session)) {
104
+ return session.getSession();
105
+ }
106
+
107
+ return session;
68
108
  }
69
109
 
70
110
  export function useStream(timestamps?: Record<number, number>) {