@skitter/assistant_md 1.0.0-alpha.0 → 1.0.0

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.
@@ -0,0 +1,119 @@
1
+ import React from "react";
2
+
3
+ const URL_REGEX = /[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=-]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/;
4
+
5
+ const normalStr = (str) => ({
6
+ type: "normal_str",
7
+ value: str
8
+ });
9
+
10
+ const urlStr = (str) => ({
11
+ type: "url",
12
+ value: str
13
+ });
14
+
15
+ const normalWrapper = (TagName) => ({ mdText }) => (
16
+ <TagName>
17
+ <AssistantMd mdText={mdText.value} />
18
+ </TagName>
19
+ );
20
+
21
+ const listWrapper = (TagName) => ({ mdText: { list } }) => (
22
+ <TagName>
23
+ {list.map((mdText) => (
24
+ <li>
25
+ <AssistantMd mdText={mdText} />
26
+ </li>
27
+ ))}
28
+ </TagName>
29
+ );
30
+
31
+ export function parseUrlAndString(str) {
32
+ const results = [];
33
+ let remaining = str;
34
+ while (remaining) {
35
+ let match = remaining.match(URL_REGEX);
36
+ if (!match) {
37
+ results.push(normalStr(remaining));
38
+ break;
39
+ }
40
+
41
+ if (match.index !== 0) {
42
+ results.push(normalStr(remaining.slice(0, match.index)));
43
+ results.push(
44
+ urlStr(remaining.slice(match.index, match.index + match[0].length))
45
+ );
46
+ remaining = remaining.slice(match.index + match[0].length);
47
+ continue;
48
+ }
49
+ results.push(urlStr(remaining.slice(0, match[0].length)));
50
+ remaining = remaining.slice(match[0].length);
51
+ }
52
+ return results;
53
+ }
54
+
55
+ const mdTypeRenderers = {
56
+ normal_text: NormalTextMd,
57
+ bold: normalWrapper("strong"),
58
+ italic: normalWrapper("em"),
59
+ underline: normalWrapper("u"),
60
+ hyperlink: HyperLinkMd,
61
+ ordered_list: listWrapper("ol"),
62
+ unordered_list: listWrapper("ul"),
63
+ newline: () => <br />,
64
+ join: JoinMd
65
+ };
66
+
67
+ const DisplayLineBreaks = ({ text }) => {
68
+ let texts = text.split("\n");
69
+ let lastOne = texts[texts.length - 1];
70
+ let exceptLastOne = texts.slice(0, texts.length - 1);
71
+ return (
72
+ <>
73
+ {exceptLastOne.reduce(
74
+ (arr, text) => [...arr, text, <br key={arr.length} />],
75
+ []
76
+ )}
77
+ {lastOne}
78
+ </>
79
+ );
80
+ };
81
+
82
+ const DisplayURL = ({ text }) => {
83
+ const parsed = parseUrlAndString(text);
84
+ return parsed.map(({ type, value }, i) =>
85
+ type === "url" ? (
86
+ <a href={value} target="_blank" key={i} rel="noreferrer">
87
+ {value}
88
+ </a>
89
+ ) : (
90
+ <DisplayLineBreaks text={value} key={i}></DisplayLineBreaks>
91
+ )
92
+ );
93
+ };
94
+
95
+ function NormalTextMd({ mdText }) {
96
+ return <DisplayURL text={mdText} />;
97
+ }
98
+
99
+ function HyperLinkMd({ mdText }) {
100
+ return (
101
+ <a href={mdText.link}>
102
+ <AssistantMd mdText={mdText.value} />
103
+ </a>
104
+ );
105
+ }
106
+
107
+ function JoinMd({ mdText: { lhs, rhs } }) {
108
+ return (
109
+ <>
110
+ <AssistantMd mdText={lhs} />
111
+ <AssistantMd mdText={rhs} />
112
+ </>
113
+ );
114
+ }
115
+
116
+ export default function AssistantMd({ mdText }) {
117
+ let RenderMd = mdTypeRenderers[mdText.markdown_text];
118
+ return <RenderMd mdText={mdText[mdText.markdown_text]} />;
119
+ }
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "@skitter/assistant_md",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0",
4
4
  "description": "",
5
- "source": "src/assistant_md.tsx",
6
- "main": "dist/assistant_md.js",
7
- "types": "dist/assistant_md.d.ts",
5
+ "main": "./assistant_md.js",
8
6
  "scripts": {
9
- "watch": "parcel watch",
10
- "build": "parcel build"
7
+ "test": "echo \"Error: no test specified\" && exit 1"
11
8
  },
12
9
  "keywords": [],
13
10
  "author": "",
@@ -17,12 +14,5 @@
17
14
  },
18
15
  "volta": {
19
16
  "node": "14.19.3"
20
- },
21
- "devDependencies": {
22
- "@parcel/packager-ts": "^2.6.0",
23
- "@parcel/transformer-typescript-types": "^2.6.0",
24
- "@types/react": "^18.0.12",
25
- "parcel": "^2.6.0",
26
- "typescript": "^4.7.3"
27
17
  }
28
18
  }
Binary file
Binary file