@novastera-oss/react-native-markdown-display 8.0.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 (69) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +1206 -0
  3. package/dist/cjs/index.js +164 -0
  4. package/dist/cjs/lib/AstRenderer.js +129 -0
  5. package/dist/cjs/lib/data/textStyleProps.js +24 -0
  6. package/dist/cjs/lib/parser.js +22 -0
  7. package/dist/cjs/lib/renderRules.js +148 -0
  8. package/dist/cjs/lib/styles.js +183 -0
  9. package/dist/cjs/lib/util/Token.js +11 -0
  10. package/dist/cjs/lib/util/cleanupTokens.js +61 -0
  11. package/dist/cjs/lib/util/convertAdditionalStyles.js +39 -0
  12. package/dist/cjs/lib/util/flattenInlineTokens.js +17 -0
  13. package/dist/cjs/lib/util/getTokenTypeByToken.js +21 -0
  14. package/dist/cjs/lib/util/getUniqueID.js +8 -0
  15. package/dist/cjs/lib/util/groupTextTokens.js +30 -0
  16. package/dist/cjs/lib/util/hasParents.js +6 -0
  17. package/dist/cjs/lib/util/omitListItemParagraph.js +33 -0
  18. package/dist/cjs/lib/util/openUrl.js +15 -0
  19. package/dist/cjs/lib/util/removeTextStyleProps.js +15 -0
  20. package/dist/cjs/lib/util/renderInlineAsText.js +16 -0
  21. package/dist/cjs/lib/util/splitTextNonTextNodes.js +21 -0
  22. package/dist/cjs/lib/util/stringToTokens.js +13 -0
  23. package/dist/cjs/lib/util/tokensToAST.js +63 -0
  24. package/dist/cjs/types.js +2 -0
  25. package/dist/esm/index.js +112 -0
  26. package/dist/esm/lib/AstRenderer.js +123 -0
  27. package/dist/esm/lib/data/textStyleProps.js +22 -0
  28. package/dist/esm/lib/parser.js +16 -0
  29. package/dist/esm/lib/renderRules.js +143 -0
  30. package/dist/esm/lib/styles.js +180 -0
  31. package/dist/esm/lib/util/Token.js +8 -0
  32. package/dist/esm/lib/util/cleanupTokens.js +55 -0
  33. package/dist/esm/lib/util/convertAdditionalStyles.js +36 -0
  34. package/dist/esm/lib/util/flattenInlineTokens.js +14 -0
  35. package/dist/esm/lib/util/getTokenTypeByToken.js +18 -0
  36. package/dist/esm/lib/util/getUniqueID.js +5 -0
  37. package/dist/esm/lib/util/groupTextTokens.js +24 -0
  38. package/dist/esm/lib/util/hasParents.js +3 -0
  39. package/dist/esm/lib/util/omitListItemParagraph.js +30 -0
  40. package/dist/esm/lib/util/openUrl.js +12 -0
  41. package/dist/esm/lib/util/removeTextStyleProps.js +9 -0
  42. package/dist/esm/lib/util/renderInlineAsText.js +13 -0
  43. package/dist/esm/lib/util/splitTextNonTextNodes.js +18 -0
  44. package/dist/esm/lib/util/stringToTokens.js +10 -0
  45. package/dist/esm/lib/util/tokensToAST.js +57 -0
  46. package/dist/esm/types.js +1 -0
  47. package/dist/types/index.d.ts +23 -0
  48. package/dist/types/lib/AstRenderer.d.ts +16 -0
  49. package/dist/types/lib/data/textStyleProps.d.ts +2 -0
  50. package/dist/types/lib/parser.d.ts +3 -0
  51. package/dist/types/lib/renderRules.d.ts +3 -0
  52. package/dist/types/lib/styles.d.ts +141 -0
  53. package/dist/types/lib/util/Token.d.ts +8 -0
  54. package/dist/types/lib/util/cleanupTokens.d.ts +2 -0
  55. package/dist/types/lib/util/convertAdditionalStyles.d.ts +1 -0
  56. package/dist/types/lib/util/flattenInlineTokens.d.ts +2 -0
  57. package/dist/types/lib/util/getTokenTypeByToken.d.ts +2 -0
  58. package/dist/types/lib/util/getUniqueID.d.ts +1 -0
  59. package/dist/types/lib/util/groupTextTokens.d.ts +2 -0
  60. package/dist/types/lib/util/hasParents.d.ts +2 -0
  61. package/dist/types/lib/util/omitListItemParagraph.d.ts +2 -0
  62. package/dist/types/lib/util/openUrl.d.ts +1 -0
  63. package/dist/types/lib/util/removeTextStyleProps.d.ts +1 -0
  64. package/dist/types/lib/util/renderInlineAsText.d.ts +2 -0
  65. package/dist/types/lib/util/splitTextNonTextNodes.d.ts +7 -0
  66. package/dist/types/lib/util/stringToTokens.d.ts +2 -0
  67. package/dist/types/lib/util/tokensToAST.d.ts +2 -0
  68. package/dist/types/types.d.ts +59 -0
  69. package/package.json +69 -0
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Base Markdown component
3
+ * @author Novastera + contributors
4
+ */
5
+ import React from 'react';
6
+ import MarkdownIt from 'markdown-it';
7
+ import { Image as MarkdownImage } from 'expo-image';
8
+ import parser from './lib/parser';
9
+ import getUniqueID from './lib/util/getUniqueID';
10
+ import hasParents from './lib/util/hasParents';
11
+ import openUrl from './lib/util/openUrl';
12
+ import tokensToAST from './lib/util/tokensToAST';
13
+ import renderRules from './lib/renderRules';
14
+ import AstRenderer from './lib/AstRenderer';
15
+ import removeTextStyleProps from './lib/util/removeTextStyleProps';
16
+ import { styles } from './lib/styles';
17
+ import { stringToTokens } from './lib/util/stringToTokens';
18
+ import textStyleProps from './lib/data/textStyleProps';
19
+ import type { ASTNode, MarkdownParser, MarkdownProps, RenderFunction, RenderRules } from './types';
20
+ export type { ASTNode, MarkdownParser, MarkdownProps, RenderFunction, RenderRules };
21
+ export { getUniqueID, openUrl, hasParents, renderRules, AstRenderer, parser, stringToTokens, tokensToAST, MarkdownIt, styles, removeTextStyleProps, textStyleProps, MarkdownImage, MarkdownImage as FitImage, };
22
+ declare const Markdown: React.MemoExoticComponent<({ children, renderer, rules, style, mergeStyle, markdownit, onLinkPress, maxTopLevelChildren, topLevelMaxExceededItem, allowedImageHandlers, defaultImageHandler, debugPrintTree, }: React.PropsWithChildren<MarkdownProps>) => React.ReactNode>;
23
+ export default Markdown;
@@ -0,0 +1,16 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { ASTNode, RenderFunction, RenderImageFunction, RenderLinkFunction, RenderRules } from '../types';
3
+ export default class AstRenderer {
4
+ private _renderRules;
5
+ private _style;
6
+ private _onLinkPress?;
7
+ private _maxTopLevelChildren?;
8
+ private _topLevelMaxExceededItem?;
9
+ private _allowedImageHandlers?;
10
+ private _defaultImageHandler?;
11
+ private _debugPrintTree?;
12
+ constructor(renderRules: RenderRules, style: Record<string, unknown>, onLinkPress?: (url: string) => boolean, maxTopLevelChildren?: number | null, topLevelMaxExceededItem?: ReactNode, allowedImageHandlers?: string[], defaultImageHandler?: string | null, debugPrintTree?: boolean);
13
+ getRenderFunction: (type: string) => RenderFunction | RenderLinkFunction | RenderImageFunction;
14
+ renderNode: (node: ASTNode, parentNodes: ASTNode[], isRoot?: boolean) => ReactNode;
15
+ render: (nodes: ASTNode[]) => ReactNode;
16
+ }
@@ -0,0 +1,2 @@
1
+ declare const textStyleProps: readonly ["textShadowOffset", "color", "fontSize", "fontStyle", "fontWeight", "lineHeight", "textAlign", "textDecorationLine", "textShadowColor", "fontFamily", "textShadowRadius", "includeFontPadding", "textAlignVertical", "fontVariant", "letterSpacing", "textDecorationColor", "textDecorationStyle", "textTransform", "writingDirection"];
2
+ export default textStyleProps;
@@ -0,0 +1,3 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { ASTNode, MarkdownParser } from '../types';
3
+ export default function parser(source: string | ASTNode[], renderer: (nodes: ASTNode[]) => ReactNode, markdownIt: MarkdownParser): ReactNode;
@@ -0,0 +1,3 @@
1
+ import type { RenderRules } from '../types';
2
+ declare const renderRules: RenderRules;
3
+ export default renderRules;
@@ -0,0 +1,141 @@
1
+ export declare const styles: {
2
+ body: {};
3
+ heading1: {
4
+ flexDirection: string;
5
+ fontSize: number;
6
+ };
7
+ heading2: {
8
+ flexDirection: string;
9
+ fontSize: number;
10
+ };
11
+ heading3: {
12
+ flexDirection: string;
13
+ fontSize: number;
14
+ };
15
+ heading4: {
16
+ flexDirection: string;
17
+ fontSize: number;
18
+ };
19
+ heading5: {
20
+ flexDirection: string;
21
+ fontSize: number;
22
+ };
23
+ heading6: {
24
+ flexDirection: string;
25
+ fontSize: number;
26
+ };
27
+ hr: {
28
+ backgroundColor: string;
29
+ height: number;
30
+ };
31
+ strong: {
32
+ fontWeight: string;
33
+ };
34
+ em: {
35
+ fontStyle: string;
36
+ };
37
+ s: {
38
+ textDecorationLine: string;
39
+ };
40
+ blockquote: {
41
+ backgroundColor: string;
42
+ borderColor: string;
43
+ borderLeftWidth: number;
44
+ marginLeft: number;
45
+ paddingHorizontal: number;
46
+ };
47
+ bullet_list: {};
48
+ ordered_list: {};
49
+ list_item: {
50
+ flexDirection: string;
51
+ justifyContent: string;
52
+ };
53
+ bullet_list_icon: {
54
+ marginLeft: number;
55
+ marginRight: number;
56
+ };
57
+ bullet_list_content: {
58
+ flex: number;
59
+ };
60
+ ordered_list_icon: {
61
+ marginLeft: number;
62
+ marginRight: number;
63
+ };
64
+ ordered_list_content: {
65
+ flex: number;
66
+ };
67
+ code_inline: {
68
+ fontFamily?: string | undefined;
69
+ borderWidth: number;
70
+ borderColor: string;
71
+ backgroundColor: string;
72
+ padding: number;
73
+ borderRadius: number;
74
+ };
75
+ code_block: {
76
+ fontFamily?: string | undefined;
77
+ borderWidth: number;
78
+ borderColor: string;
79
+ backgroundColor: string;
80
+ padding: number;
81
+ borderRadius: number;
82
+ };
83
+ fence: {
84
+ fontFamily?: string | undefined;
85
+ borderWidth: number;
86
+ borderColor: string;
87
+ backgroundColor: string;
88
+ padding: number;
89
+ borderRadius: number;
90
+ };
91
+ table: {
92
+ borderWidth: number;
93
+ borderColor: string;
94
+ borderRadius: number;
95
+ };
96
+ thead: {};
97
+ tbody: {};
98
+ th: {
99
+ flex: number;
100
+ padding: number;
101
+ };
102
+ tr: {
103
+ borderBottomWidth: number;
104
+ borderColor: string;
105
+ flexDirection: string;
106
+ };
107
+ td: {
108
+ flex: number;
109
+ padding: number;
110
+ };
111
+ link: {
112
+ textDecorationLine: string;
113
+ };
114
+ blocklink: {
115
+ flex: number;
116
+ borderColor: string;
117
+ borderBottomWidth: number;
118
+ };
119
+ image: {
120
+ flex: number;
121
+ };
122
+ text: {};
123
+ textgroup: {};
124
+ paragraph: {
125
+ marginTop: number;
126
+ marginBottom: number;
127
+ flexWrap: string;
128
+ flexDirection: string;
129
+ alignItems: string;
130
+ justifyContent: string;
131
+ width: string;
132
+ };
133
+ hardbreak: {
134
+ width: string;
135
+ height: number;
136
+ };
137
+ softbreak: {};
138
+ pre: {};
139
+ inline: {};
140
+ span: {};
141
+ };
@@ -0,0 +1,8 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export default class Token implements MarkdownTokenLike {
3
+ type: string;
4
+ nesting: number;
5
+ children: MarkdownTokenLike[] | null;
6
+ block: boolean;
7
+ constructor(type: string, nesting?: number, children?: MarkdownTokenLike[] | null, block?: boolean);
8
+ }
@@ -0,0 +1,2 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export declare function cleanupTokens(tokens: MarkdownTokenLike[]): MarkdownTokenLike[];
@@ -0,0 +1 @@
1
+ export default function convertAdditionalStyles(style: string): Record<string, string | number>;
@@ -0,0 +1,2 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export default function flattenTokens(tokens: MarkdownTokenLike[]): MarkdownTokenLike[];
@@ -0,0 +1,2 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export default function getTokenTypeByToken(token: MarkdownTokenLike): string;
@@ -0,0 +1 @@
1
+ export default function getUniqueID(): string;
@@ -0,0 +1,2 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export default function groupTextTokens(tokens: MarkdownTokenLike[]): MarkdownTokenLike[];
@@ -0,0 +1,2 @@
1
+ import type { ASTNode } from '../../types';
2
+ export default function hasParents(parents: ASTNode[], type: string): boolean;
@@ -0,0 +1,2 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export default function omitListItemParagraph(tokens: MarkdownTokenLike[]): MarkdownTokenLike[];
@@ -0,0 +1 @@
1
+ export default function openUrl(url: string, customCallback?: (url: string) => boolean): void;
@@ -0,0 +1 @@
1
+ export default function removeTextStyleProps(style: Record<string, unknown>): Record<string, unknown>;
@@ -0,0 +1,2 @@
1
+ import type { MarkdownTokenLike } from '../../types';
2
+ export default function renderInlineAsText(tokens: MarkdownTokenLike[]): string;
@@ -0,0 +1,7 @@
1
+ import type { ReactElement } from 'react';
2
+ type SplitNodes = {
3
+ textNodes: ReactElement[];
4
+ nonTextNodes: ReactElement[];
5
+ };
6
+ export default function splitTextNonTextNodes(children: ReactElement[]): SplitNodes;
7
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { MarkdownParser, MarkdownTokenLike } from '../../types';
2
+ export declare function stringToTokens(source: string, markdownIt: MarkdownParser): MarkdownTokenLike[];
@@ -0,0 +1,2 @@
1
+ import type { ASTNode, MarkdownTokenLike } from '../../types';
2
+ export default function tokensToAST(tokens: MarkdownTokenLike[]): ASTNode[];
@@ -0,0 +1,59 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { StyleSheet } from 'react-native';
3
+ export interface MarkdownTokenLike {
4
+ type: string;
5
+ nesting: number;
6
+ children?: MarkdownTokenLike[] | null;
7
+ block?: boolean;
8
+ content?: string;
9
+ attrs?: Array<[string, string]> | null;
10
+ tag?: string;
11
+ info?: string;
12
+ meta?: unknown;
13
+ markup?: string;
14
+ attrIndex?: (name: string) => number;
15
+ }
16
+ export interface ASTNode {
17
+ type: string;
18
+ sourceType: string;
19
+ sourceInfo?: string;
20
+ sourceMeta?: unknown;
21
+ block?: boolean;
22
+ key: string;
23
+ content: string;
24
+ markup: string;
25
+ tokenIndex: number;
26
+ index: number;
27
+ attributes: Record<string, unknown>;
28
+ children: ASTNode[];
29
+ }
30
+ export type RenderFunction = (node: ASTNode, children: ReactNode[], parentNodes: ASTNode[], styles: Record<string, unknown>, styleObj?: Record<string, unknown>, ...args: unknown[]) => ReactNode;
31
+ export type RenderLinkFunction = (node: ASTNode, children: ReactNode[], parentNodes: ASTNode[], styles: Record<string, unknown>, onLinkPress?: (url: string) => boolean) => ReactNode;
32
+ export type RenderImageFunction = (node: ASTNode, children: ReactNode[], parentNodes: ASTNode[], styles: Record<string, unknown>, allowedImageHandlers: string[], defaultImageHandler: string | null) => ReactNode;
33
+ export interface RenderRules {
34
+ [name: string]: RenderFunction | RenderLinkFunction | RenderImageFunction | undefined;
35
+ link?: RenderLinkFunction;
36
+ blocklink?: RenderLinkFunction;
37
+ image?: RenderImageFunction;
38
+ }
39
+ export interface MarkdownParser {
40
+ parse: (value: string, options: Record<string, unknown>) => MarkdownTokenLike[];
41
+ }
42
+ export interface MarkdownProps {
43
+ rules?: RenderRules;
44
+ style?: StyleSheet.NamedStyles<Record<string, unknown>>;
45
+ renderer?: AstRenderer | ((nodes: ASTNode[]) => ReactNode);
46
+ markdownit?: MarkdownParser;
47
+ mergeStyle?: boolean;
48
+ debugPrintTree?: boolean;
49
+ onLinkPress?: (url: string) => boolean;
50
+ maxTopLevelChildren?: number | null;
51
+ topLevelMaxExceededItem?: ReactNode;
52
+ allowedImageHandlers?: string[];
53
+ defaultImageHandler?: string | null;
54
+ }
55
+ export interface AstRenderer {
56
+ getRenderFunction(type: string): RenderFunction | RenderLinkFunction | RenderImageFunction;
57
+ renderNode(node: ASTNode, parentNodes: ReadonlyArray<ASTNode>, isRoot?: boolean): ReactNode;
58
+ render(nodes: ReadonlyArray<ASTNode>): ReactNode;
59
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@novastera-oss/react-native-markdown-display",
3
+ "version": "8.0.1",
4
+ "description": "Markdown renderer for react-native, with CommonMark spec support. Maintained by Novastera.",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/types/index.d.ts",
11
+ "import": "./dist/esm/index.js",
12
+ "require": "./dist/cjs/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "scripts": {
17
+ "build:types": "tsc -p tsconfig.types.json",
18
+ "build:esm": "tsc -p tsconfig.esm.json",
19
+ "build:cjs": "tsc -p tsconfig.cjs.json",
20
+ "build": "npm run build:types && npm run build:esm && npm run build:cjs",
21
+ "lint": "./node_modules/.bin/eslint --fix --cache ./src"
22
+ },
23
+ "pre-commit": [
24
+ "lint"
25
+ ],
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/novastera/react-native-markdown-display.git"
29
+ },
30
+ "keywords": [
31
+ "react",
32
+ "react-native",
33
+ "native",
34
+ "markdown",
35
+ "commonmark",
36
+ "markdown-it",
37
+ "crm",
38
+ "erp",
39
+ "novastera"
40
+ ],
41
+ "author": "Novastera (originally by Mient-jan Stelling and Tom Pickard + community)",
42
+ "license": "MIT",
43
+ "bugs": {
44
+ "url": "https://github.com/novastera/react-native-markdown-display/issues"
45
+ },
46
+ "homepage": "https://novastera.com",
47
+ "dependencies": {
48
+ "markdown-it": "^14.1.0",
49
+ "expo-image": "^3.0.0"
50
+ },
51
+ "peerDependencies": {
52
+ "react": ">=18.0.0",
53
+ "react-native": ">=0.79.0"
54
+ },
55
+ "devDependencies": {
56
+ "@types/markdown-it": "^14.1.0",
57
+ "@types/react": "^19.0.0",
58
+ "@babel/core": "^7.9.6",
59
+ "@babel/runtime": "^7.9.6",
60
+ "@typescript-eslint/parser": "^8.53.0",
61
+ "eslint": "^9.0.0",
62
+ "typescript": "^5.9.3"
63
+ },
64
+ "files": [
65
+ "dist",
66
+ "LICENSE",
67
+ "README.md"
68
+ ]
69
+ }