@effect/chat-whatsapp 0.0.0-stage → 4.1.5
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/AGENTS.md +434 -0
- package/CLAUDE.md +434 -0
- package/LICENSE +21 -0
- package/README.md +60 -2
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +28 -0
- package/dist/WhatsAppChat.d.ts +373 -0
- package/dist/WhatsAppChat.d.ts.map +1 -0
- package/dist/WhatsAppChat.js +388 -0
- package/dist/WhatsAppChat.js.map +1 -0
- package/dist/WhatsAppGraphApi.d.ts +90 -0
- package/dist/WhatsAppGraphApi.d.ts.map +1 -0
- package/dist/WhatsAppGraphApi.js +97 -0
- package/dist/WhatsAppGraphApi.js.map +1 -0
- package/dist/WhatsAppUpdate.d.ts +168 -0
- package/dist/WhatsAppUpdate.d.ts.map +1 -0
- package/dist/WhatsAppUpdate.js +103 -0
- package/dist/WhatsAppUpdate.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/actions.d.ts +2 -0
- package/dist/internal/actions.d.ts.map +1 -0
- package/dist/internal/actions.js +224 -0
- package/dist/internal/actions.js.map +1 -0
- package/dist/internal/adapter.d.ts +2 -0
- package/dist/internal/adapter.d.ts.map +1 -0
- package/dist/internal/adapter.js +330 -0
- package/dist/internal/adapter.js.map +1 -0
- package/dist/internal/address.d.ts +2 -0
- package/dist/internal/address.d.ts.map +1 -0
- package/dist/internal/address.js +36 -0
- package/dist/internal/address.js.map +1 -0
- package/dist/internal/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +93 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/normalize.d.ts +2 -0
- package/dist/internal/normalize.d.ts.map +1 -0
- package/dist/internal/normalize.js +292 -0
- package/dist/internal/normalize.js.map +1 -0
- package/dist/internal/options.d.ts +2 -0
- package/dist/internal/options.d.ts.map +1 -0
- package/dist/internal/options.js +10 -0
- package/dist/internal/options.js.map +1 -0
- package/dist/internal/text.d.ts +11 -0
- package/dist/internal/text.d.ts.map +1 -0
- package/dist/internal/text.js +485 -0
- package/dist/internal/text.js.map +1 -0
- package/dist/internal/webhook.d.ts +2 -0
- package/dist/internal/webhook.d.ts.map +1 -0
- package/dist/internal/webhook.js +150 -0
- package/dist/internal/webhook.js.map +1 -0
- package/package.json +58 -3
- package/src/WhatsAppChat.ts +505 -0
- package/src/WhatsAppGraphApi.ts +147 -0
- package/src/WhatsAppUpdate.ts +142 -0
- package/src/index.ts +24 -0
- package/src/internal/actions.ts +220 -0
- package/src/internal/adapter.ts +284 -0
- package/src/internal/address.ts +41 -0
- package/src/internal/errors.ts +102 -0
- package/src/internal/normalize.ts +250 -0
- package/src/internal/options.ts +13 -0
- package/src/internal/text.ts +495 -0
- package/src/internal/webhook.ts +216 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as Context from "effect/Context"
|
|
2
|
+
import type { WhatsAppPostOptions } from "../WhatsAppChat.ts"
|
|
3
|
+
|
|
4
|
+
/** @internal */
|
|
5
|
+
export const CurrentPostOptions = Context.Reference<WhatsAppPostOptions>("@effect/chat-whatsapp/CurrentPostOptions", {
|
|
6
|
+
defaultValue: () => ({})
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
/** @internal */
|
|
10
|
+
export const defaultListButton = "Options"
|
|
11
|
+
|
|
12
|
+
/** @internal */
|
|
13
|
+
export const defaultButtonsSection = "Actions"
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown to WhatsApp text.
|
|
3
|
+
*
|
|
4
|
+
* Each construct the renderer knows has a candidate replacement, rendered
|
|
5
|
+
* bottom-up, which is emitted only when it is no longer than the
|
|
6
|
+
* construct's source in code points, UTF-16 units and UTF-8 bytes. Otherwise
|
|
7
|
+
* the source is emitted as it is. Text between constructs is always kept, so
|
|
8
|
+
* the output is never longer than the input in any of the three units.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The longest text message, and the measure's unit: UTF-16 code units, which
|
|
13
|
+
* are never fewer than the code points Meta counts. @internal
|
|
14
|
+
*/
|
|
15
|
+
export const messageLength = 4096
|
|
16
|
+
|
|
17
|
+
/** The longest caption, and the longest body beside reply buttons or a link button. @internal */
|
|
18
|
+
export const shortBodyLength = 1024
|
|
19
|
+
|
|
20
|
+
/** @internal */
|
|
21
|
+
export const measure = (markdown: string): number => markdown.length
|
|
22
|
+
|
|
23
|
+
const codePoints = (s: string): number => {
|
|
24
|
+
let n = 0
|
|
25
|
+
for (const _ of s) n++
|
|
26
|
+
return n
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @internal */
|
|
30
|
+
export const utf8Bytes = (s: string): number => {
|
|
31
|
+
let n = 0
|
|
32
|
+
for (const char of s) {
|
|
33
|
+
const code = char.codePointAt(0)!
|
|
34
|
+
n += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4
|
|
35
|
+
}
|
|
36
|
+
return n
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Whether `candidate` is no longer than `source` in all three units. @internal */
|
|
40
|
+
export const fits = (candidate: string, source: string): boolean =>
|
|
41
|
+
candidate.length <= source.length && codePoints(candidate) <= codePoints(source) &&
|
|
42
|
+
utf8Bytes(candidate) <= utf8Bytes(source)
|
|
43
|
+
|
|
44
|
+
const pick = (candidate: string, source: string): string => fits(candidate, source) ? candidate : source
|
|
45
|
+
|
|
46
|
+
/** The characters WhatsApp formats with, which a decoded reference must not produce. */
|
|
47
|
+
const formatting = new Set(["*", "_", "~", "`"])
|
|
48
|
+
|
|
49
|
+
const entities: Record<string, string> = {
|
|
50
|
+
amp: "&",
|
|
51
|
+
lt: "<",
|
|
52
|
+
gt: ">",
|
|
53
|
+
quot: "\"",
|
|
54
|
+
apos: "'",
|
|
55
|
+
nbsp: " ",
|
|
56
|
+
copy: "©",
|
|
57
|
+
reg: "®",
|
|
58
|
+
trade: "™",
|
|
59
|
+
hellip: "…",
|
|
60
|
+
mdash: "—",
|
|
61
|
+
ndash: "–",
|
|
62
|
+
lsquo: "‘",
|
|
63
|
+
rsquo: "’",
|
|
64
|
+
ldquo: "“",
|
|
65
|
+
rdquo: "”",
|
|
66
|
+
laquo: "«",
|
|
67
|
+
raquo: "»",
|
|
68
|
+
bull: "•",
|
|
69
|
+
middot: "·",
|
|
70
|
+
deg: "°",
|
|
71
|
+
plusmn: "±",
|
|
72
|
+
times: "×",
|
|
73
|
+
divide: "÷",
|
|
74
|
+
minus: "−",
|
|
75
|
+
euro: "€",
|
|
76
|
+
pound: "£",
|
|
77
|
+
yen: "¥",
|
|
78
|
+
cent: "¢",
|
|
79
|
+
sect: "§",
|
|
80
|
+
para: "¶",
|
|
81
|
+
larr: "←",
|
|
82
|
+
rarr: "→",
|
|
83
|
+
uarr: "↑",
|
|
84
|
+
darr: "↓",
|
|
85
|
+
harr: "↔",
|
|
86
|
+
lArr: "⇐",
|
|
87
|
+
rArr: "⇒",
|
|
88
|
+
hArr: "⇔",
|
|
89
|
+
le: "≤",
|
|
90
|
+
ge: "≥",
|
|
91
|
+
ne: "≠",
|
|
92
|
+
asymp: "≈",
|
|
93
|
+
infin: "∞",
|
|
94
|
+
frac12: "½",
|
|
95
|
+
frac14: "¼",
|
|
96
|
+
frac34: "¾",
|
|
97
|
+
iexcl: "¡",
|
|
98
|
+
iquest: "¿"
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const decodeReference = (reference: string): string | undefined => {
|
|
102
|
+
const body = reference.slice(1, -1)
|
|
103
|
+
let decoded: string | undefined
|
|
104
|
+
if (body.startsWith("#")) {
|
|
105
|
+
const code = body[1] === "x" || body[1] === "X"
|
|
106
|
+
? Number.parseInt(body.slice(2), 16)
|
|
107
|
+
: Number.parseInt(body.slice(1), 10)
|
|
108
|
+
decoded = code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff) ? "�" : String.fromCodePoint(code)
|
|
109
|
+
} else {
|
|
110
|
+
decoded = Object.hasOwn(entities, body) ? entities[body] : undefined
|
|
111
|
+
}
|
|
112
|
+
return decoded === undefined || formatting.has(decoded) ? undefined : decoded
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const isWhitespace = (char: string | undefined) => char === undefined || /\s/u.test(char)
|
|
116
|
+
const isPunctuation = (char: string | undefined) => char !== undefined && /[\p{P}\p{S}]/u.test(char)
|
|
117
|
+
const isAsciiPunctuation = (char: string | undefined) => char !== undefined && /[!-/:-@[-`{-~]/.test(char)
|
|
118
|
+
|
|
119
|
+
interface Atom {
|
|
120
|
+
readonly end: number
|
|
121
|
+
readonly text: string
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const runLength = (s: string, i: number, char: string) => {
|
|
125
|
+
let j = i
|
|
126
|
+
while (s[j] === char) j++
|
|
127
|
+
return j - i
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A code span from the backtick run at `i`, or the run itself when it has no match. */
|
|
131
|
+
const codeSpan = (s: string, i: number): Atom => {
|
|
132
|
+
const n = runLength(s, i, "`")
|
|
133
|
+
let j = i + n
|
|
134
|
+
while (j < s.length) {
|
|
135
|
+
if (s[j] !== "`") {
|
|
136
|
+
j++
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
139
|
+
const m = runLength(s, j, "`")
|
|
140
|
+
if (m === n) return { end: j + m, text: s.slice(i, j + m) }
|
|
141
|
+
j += m
|
|
142
|
+
}
|
|
143
|
+
return { end: i + n, text: s.slice(i, i + n) }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const autolink = /^<([A-Za-z][A-Za-z0-9+.-]{1,31}:[^\s<>]*)>/
|
|
147
|
+
const emailAutolink =
|
|
148
|
+
/^<([A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*)>/
|
|
149
|
+
const html = /^(?:<!--[\s\S]*?-->|<\/?[A-Za-z][A-Za-z0-9-]*(?:\s[^<>]*)?\/?>)/
|
|
150
|
+
const reference = /^&(?:#[0-9]{1,7};|#[xX][0-9A-Fa-f]{1,6};|[A-Za-z][A-Za-z0-9]{1,31};)/
|
|
151
|
+
|
|
152
|
+
/** The index of the `]` closing the `[` at `i`, skipping escapes and code spans. */
|
|
153
|
+
const closeBracket = (s: string, i: number): number => {
|
|
154
|
+
let depth = 0
|
|
155
|
+
let j = i
|
|
156
|
+
while (j < s.length) {
|
|
157
|
+
const char = s[j]
|
|
158
|
+
if (char === "\\") j += 2
|
|
159
|
+
else if (char === "`") j = codeSpan(s, j).end
|
|
160
|
+
else {
|
|
161
|
+
if (char === "[") depth++
|
|
162
|
+
else if (char === "]" && --depth === 0) return j
|
|
163
|
+
j++
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return -1
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** An inline destination and optional title from the `(` at `i`: the URL and the index after `)`. */
|
|
170
|
+
const destination = (s: string, i: number): { readonly url: string; readonly end: number } | undefined => {
|
|
171
|
+
let j = i + 1
|
|
172
|
+
while (s[j] === " " || s[j] === "\t") j++
|
|
173
|
+
let url: string
|
|
174
|
+
if (s[j] === "<") {
|
|
175
|
+
const close = s.indexOf(">", j)
|
|
176
|
+
if (close < 0 || s.slice(j + 1, close).includes("<")) return undefined
|
|
177
|
+
url = s.slice(j + 1, close)
|
|
178
|
+
j = close + 1
|
|
179
|
+
} else {
|
|
180
|
+
const start = j
|
|
181
|
+
let depth = 0
|
|
182
|
+
while (j < s.length && !/\s/.test(s[j])) {
|
|
183
|
+
if (s[j] === "\\" && isAsciiPunctuation(s[j + 1])) j++
|
|
184
|
+
else if (s[j] === "(") depth++
|
|
185
|
+
else if (s[j] === ")") {
|
|
186
|
+
if (depth === 0) break
|
|
187
|
+
depth--
|
|
188
|
+
}
|
|
189
|
+
j++
|
|
190
|
+
}
|
|
191
|
+
if (depth !== 0) return undefined
|
|
192
|
+
url = s.slice(start, j)
|
|
193
|
+
}
|
|
194
|
+
const afterUrl = j
|
|
195
|
+
while (s[j] === " " || s[j] === "\t") j++
|
|
196
|
+
if (j > afterUrl && (s[j] === "\"" || s[j] === "'" || s[j] === "(")) {
|
|
197
|
+
const close = s[j] === "(" ? ")" : s[j]
|
|
198
|
+
let k = j + 1
|
|
199
|
+
while (k < s.length && s[k] !== close) k += s[k] === "\\" ? 2 : 1
|
|
200
|
+
if (k >= s.length) return undefined
|
|
201
|
+
j = k + 1
|
|
202
|
+
while (s[j] === " " || s[j] === "\t") j++
|
|
203
|
+
}
|
|
204
|
+
return s[j] === ")" ? { url, end: j + 1 } : undefined
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** A link or image from the `[` at `i`, `start` being the `!` of an image. */
|
|
208
|
+
const link = (s: string, start: number, i: number): Atom | undefined => {
|
|
209
|
+
if (s[i + 1] === "^") {
|
|
210
|
+
const close = s.indexOf("]", i)
|
|
211
|
+
return close < 0 ? undefined : { end: close + 1, text: s.slice(start, close + 1) }
|
|
212
|
+
}
|
|
213
|
+
const close = closeBracket(s, i)
|
|
214
|
+
if (close < 0) return undefined
|
|
215
|
+
if (s[close + 1] === "[") {
|
|
216
|
+
const end = s.indexOf("]", close + 1)
|
|
217
|
+
return end < 0 ? undefined : { end: end + 1, text: s.slice(start, end + 1) }
|
|
218
|
+
}
|
|
219
|
+
// A shortcut reference, or plain brackets: either way it is kept as typed.
|
|
220
|
+
const verbatim = { end: close + 1, text: s.slice(start, close + 1) }
|
|
221
|
+
if (s[close + 1] !== "(") return verbatim
|
|
222
|
+
const target = destination(s, close + 1)
|
|
223
|
+
if (target === undefined) return verbatim
|
|
224
|
+
const label = s.slice(i + 1, close)
|
|
225
|
+
const shown = start < i ? label : inline(label)
|
|
226
|
+
const candidate = label === "" || label === target.url ? target.url : shown + " (" + target.url + ")"
|
|
227
|
+
return { end: target.end, text: pick(candidate, s.slice(start, target.end)) }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The construct at `i` that is kept or replaced as a whole, if any. */
|
|
231
|
+
const atom = (s: string, i: number): Atom | undefined => {
|
|
232
|
+
switch (s[i]) {
|
|
233
|
+
case "\\":
|
|
234
|
+
return isAsciiPunctuation(s[i + 1]) ? { end: i + 2, text: s.slice(i, i + 2) } : undefined
|
|
235
|
+
case "`":
|
|
236
|
+
return codeSpan(s, i)
|
|
237
|
+
case "<": {
|
|
238
|
+
const rest = s.slice(i)
|
|
239
|
+
const match = autolink.exec(rest) ?? emailAutolink.exec(rest)
|
|
240
|
+
if (match !== null) return { end: i + match[0].length, text: pick(match[1], match[0]) }
|
|
241
|
+
const tag = html.exec(rest)
|
|
242
|
+
return tag === null ? undefined : { end: i + tag[0].length, text: tag[0] }
|
|
243
|
+
}
|
|
244
|
+
case "!":
|
|
245
|
+
return s[i + 1] === "[" ? link(s, i, i + 1) : undefined
|
|
246
|
+
case "[":
|
|
247
|
+
return link(s, i, i)
|
|
248
|
+
case "&": {
|
|
249
|
+
const match = reference.exec(s.slice(i, i + 40))
|
|
250
|
+
if (match === null) return undefined
|
|
251
|
+
const decoded = decodeReference(match[0])
|
|
252
|
+
return { end: i + match[0].length, text: decoded === undefined ? match[0] : pick(decoded, match[0]) }
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return undefined
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const leftFlanking = (before: string | undefined, after: string | undefined) =>
|
|
259
|
+
!isWhitespace(after) && (!isPunctuation(after) || isWhitespace(before) || isPunctuation(before))
|
|
260
|
+
|
|
261
|
+
const rightFlanking = (before: string | undefined, after: string | undefined) =>
|
|
262
|
+
!isWhitespace(before) && (!isPunctuation(before) || isWhitespace(after) || isPunctuation(after))
|
|
263
|
+
|
|
264
|
+
const canOpen = (s: string, i: number, n: number) => {
|
|
265
|
+
const before = s[i - 1]
|
|
266
|
+
const after = s[i + n]
|
|
267
|
+
const left = leftFlanking(before, after)
|
|
268
|
+
return s[i] === "_" ? left && (!rightFlanking(before, after) || isPunctuation(before)) : left
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const canClose = (s: string, i: number, n: number) => {
|
|
272
|
+
const before = s[i - 1]
|
|
273
|
+
const after = s[i + n]
|
|
274
|
+
const right = rightFlanking(before, after)
|
|
275
|
+
return s[i] === "_" ? right && (!leftFlanking(before, after) || isPunctuation(after)) : right
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Emphasis or strikethrough from the delimiter run at `i`, if it has a closer. */
|
|
279
|
+
const emphasis = (s: string, i: number): Atom | undefined => {
|
|
280
|
+
const char = s[i]
|
|
281
|
+
const n = runLength(s, i, char)
|
|
282
|
+
if (n > 2 || !canOpen(s, i, n)) return undefined
|
|
283
|
+
let j = i + n
|
|
284
|
+
while (j < s.length) {
|
|
285
|
+
const skipped = s[j] === "*" || s[j] === "_" || s[j] === "~" ? undefined : atom(s, j)
|
|
286
|
+
if (skipped !== undefined) {
|
|
287
|
+
j = skipped.end
|
|
288
|
+
continue
|
|
289
|
+
}
|
|
290
|
+
if (s[j] !== char) {
|
|
291
|
+
j++
|
|
292
|
+
continue
|
|
293
|
+
}
|
|
294
|
+
const m = runLength(s, j, char)
|
|
295
|
+
if (m === n && j > i + n && canClose(s, j, m)) {
|
|
296
|
+
const marker = char === "~" ? "~" : n === 2 ? "*" : "_"
|
|
297
|
+
const candidate = marker + inline(s.slice(i + n, j)) + marker
|
|
298
|
+
return { end: j + m, text: pick(candidate, s.slice(i, j + m)) }
|
|
299
|
+
}
|
|
300
|
+
j += m
|
|
301
|
+
}
|
|
302
|
+
return undefined
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** One line of inline Markdown. @internal */
|
|
306
|
+
export const inline = (s: string): string => {
|
|
307
|
+
let out = ""
|
|
308
|
+
let i = 0
|
|
309
|
+
while (i < s.length) {
|
|
310
|
+
const char = s[i]
|
|
311
|
+
const found = char === "*" || char === "_" || char === "~" ? emphasis(s, i) : atom(s, i)
|
|
312
|
+
if (found !== undefined) {
|
|
313
|
+
out += found.text
|
|
314
|
+
i = found.end
|
|
315
|
+
} else if (char === "*" || char === "_" || char === "~") {
|
|
316
|
+
const n = runLength(s, i, char)
|
|
317
|
+
out += s.slice(i, i + n)
|
|
318
|
+
i += n
|
|
319
|
+
} else {
|
|
320
|
+
out += char
|
|
321
|
+
i++
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return out
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const fenceOpen = /^( {0,3})(`{3,}|~{3,})(.*)$/
|
|
328
|
+
const atxHeading = /^ {0,3}(#{1,6})(?:[ \t]+(.*))?$/
|
|
329
|
+
const setextUnderline = /^ {0,3}(?:=+|-+)[ \t]*$/
|
|
330
|
+
const thematicBreak = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/
|
|
331
|
+
const definition = /^ {0,3}\[[^\]]+\]:/
|
|
332
|
+
const blockStart = /^(?: {4}|\t| {0,3}(?:[>#|]|[-*+](?:[ \t]|$)|\d{1,9}[.)](?:[ \t]|$)|`{3}|~{3}))/
|
|
333
|
+
const tableDelimiter = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/
|
|
334
|
+
const quoteMarker = /^ {0,3}>[ \t]?/
|
|
335
|
+
const listMarker = /^( {0,3}(?:[-*+]|\d{1,9}[.)]))([ \t]+|$)/
|
|
336
|
+
|
|
337
|
+
const cells = (row: string) => row.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").length
|
|
338
|
+
|
|
339
|
+
const isBlank = (line: string) => line.trim() === ""
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The quotes and list items a line opens, outermost first, and the rest of
|
|
343
|
+
* the line. A quote is 0, and a list item the indentation its content needs.
|
|
344
|
+
*/
|
|
345
|
+
const containers = (line: string): { readonly stack: ReadonlyArray<number>; readonly rest: string } => {
|
|
346
|
+
const stack: Array<number> = []
|
|
347
|
+
let rest = line
|
|
348
|
+
while (true) {
|
|
349
|
+
const quote = quoteMarker.exec(rest)
|
|
350
|
+
if (quote !== null) {
|
|
351
|
+
stack.push(0)
|
|
352
|
+
rest = rest.slice(quote[0].length)
|
|
353
|
+
continue
|
|
354
|
+
}
|
|
355
|
+
const item = listMarker.exec(rest)
|
|
356
|
+
if (item === null || thematicBreak.test(rest)) break
|
|
357
|
+
// Five spaces or more after the marker start indented code, whose content starts after one.
|
|
358
|
+
const width = item[2] === "" || item[2].length >= 5 ? item[1].length + 1 : item[0].length
|
|
359
|
+
stack.push(width)
|
|
360
|
+
rest = rest.slice(Math.min(width, item[0].length))
|
|
361
|
+
}
|
|
362
|
+
return { stack, rest }
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* A line with the tabs of its indentation and container markers expanded to
|
|
367
|
+
* the next multiple of four columns, so a tab only partly taken by a
|
|
368
|
+
* container leaves its other columns. Used to find blocks, never as output.
|
|
369
|
+
*/
|
|
370
|
+
const expandIndent = (line: string): string => {
|
|
371
|
+
let out = ""
|
|
372
|
+
for (let k = 0; k < line.length; k++) {
|
|
373
|
+
const char = line[k]
|
|
374
|
+
if (char === "\t") out += " ".repeat(4 - (out.length % 4))
|
|
375
|
+
else if (/[ >\-*+.)0-9]/.test(char)) out += char
|
|
376
|
+
else return out + line.slice(k)
|
|
377
|
+
}
|
|
378
|
+
return out
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* What is left of a line, with its indentation expanded, inside the
|
|
383
|
+
* containers of `stack`, or `undefined` when the line ends one of them.
|
|
384
|
+
*/
|
|
385
|
+
const inside = (line: string, stack: ReadonlyArray<number>): string | undefined => {
|
|
386
|
+
let rest = line
|
|
387
|
+
for (const width of stack) {
|
|
388
|
+
if (width === 0) {
|
|
389
|
+
const quote = quoteMarker.exec(rest)
|
|
390
|
+
if (quote === null) return undefined
|
|
391
|
+
rest = rest.slice(quote[0].length)
|
|
392
|
+
continue
|
|
393
|
+
}
|
|
394
|
+
if (isBlank(rest)) return ""
|
|
395
|
+
if (!rest.startsWith(" ".repeat(width))) return undefined
|
|
396
|
+
rest = rest.slice(width)
|
|
397
|
+
}
|
|
398
|
+
return rest
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Whether a line starts a block, which ends a definition or a table that was being kept as typed. */
|
|
402
|
+
const opensBlock = (line: string) =>
|
|
403
|
+
fenceOpen.test(line) || atxHeading.test(line) || thematicBreak.test(line) || containers(line).stack.length > 0
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Markdown as WhatsApp text, never longer than the Markdown in code points,
|
|
407
|
+
* UTF-16 units or UTF-8 bytes.
|
|
408
|
+
*
|
|
409
|
+
* @internal
|
|
410
|
+
*/
|
|
411
|
+
export const render = (markdown: string): string => {
|
|
412
|
+
const lines = markdown.split(/(?<=\n)/)
|
|
413
|
+
const body = (k: number) => lines[k].replace(/\r?\n$/, "")
|
|
414
|
+
const eol = (k: number) => lines[k].slice(body(k).length)
|
|
415
|
+
let out = ""
|
|
416
|
+
let i = 0
|
|
417
|
+
while (i < lines.length) {
|
|
418
|
+
const line = body(i)
|
|
419
|
+
const { rest, stack } = containers(expandIndent(line))
|
|
420
|
+
const fence = fenceOpen.exec(rest)
|
|
421
|
+
if (fence !== null && !(fence[2][0] === "`" && fence[3].includes("`"))) {
|
|
422
|
+
// The closer is matched inside the opener's quotes and list items. A line outside them ends the code too.
|
|
423
|
+
const closing = new RegExp("^ {0,3}" + (fence[2][0] === "~" ? "~" : "`") + "{" + fence[2].length + ",}[ \\t]*$")
|
|
424
|
+
let j = i + 1
|
|
425
|
+
let closed = false
|
|
426
|
+
for (; j < lines.length; j++) {
|
|
427
|
+
const code = inside(expandIndent(body(j)), stack)
|
|
428
|
+
if (code === undefined) break
|
|
429
|
+
if (closing.test(code)) {
|
|
430
|
+
closed = true
|
|
431
|
+
break
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (!closed) {
|
|
435
|
+
out += lines.slice(i, j).join("")
|
|
436
|
+
i = j
|
|
437
|
+
continue
|
|
438
|
+
}
|
|
439
|
+
const content = lines.slice(i + 1, j).join("")
|
|
440
|
+
const source = lines.slice(i, j + 1).join("")
|
|
441
|
+
const candidate = "```" + eol(i) + content + "```" + eol(j)
|
|
442
|
+
out += stack.length > 0 || content.includes("```") ? source : pick(candidate, source)
|
|
443
|
+
i = j + 1
|
|
444
|
+
continue
|
|
445
|
+
}
|
|
446
|
+
if (definition.test(rest)) {
|
|
447
|
+
// A destination or title may continue on the next lines, up to a blank line or the start of a block.
|
|
448
|
+
out += lines[i]
|
|
449
|
+
let j = i + 1
|
|
450
|
+
while (j < lines.length) {
|
|
451
|
+
const next = inside(expandIndent(body(j)), stack) ?? expandIndent(body(j))
|
|
452
|
+
if (isBlank(next) || opensBlock(next)) break
|
|
453
|
+
out += lines[j++]
|
|
454
|
+
}
|
|
455
|
+
i = j
|
|
456
|
+
continue
|
|
457
|
+
}
|
|
458
|
+
if (i + 1 < lines.length && line.includes("|") && tableDelimiter.test(body(i + 1)) && body(i + 1).includes("|")) {
|
|
459
|
+
if (cells(line) === cells(body(i + 1))) {
|
|
460
|
+
out += lines[i]
|
|
461
|
+
let j = i + 2
|
|
462
|
+
while (j < lines.length && !isBlank(body(j)) && !opensBlock(expandIndent(body(j)))) out += lines[j++]
|
|
463
|
+
i = j
|
|
464
|
+
continue
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
const heading = atxHeading.exec(line)
|
|
468
|
+
if (heading !== null) {
|
|
469
|
+
const content = (heading[2] ?? "").replace(/(?:^|[ \t]+)#+[ \t]*$/, "").trim()
|
|
470
|
+
out += (content === "" ? line : pick("*" + inline(content) + "*", line)) + eol(i)
|
|
471
|
+
i++
|
|
472
|
+
continue
|
|
473
|
+
}
|
|
474
|
+
if (
|
|
475
|
+
i + 1 < lines.length && !isBlank(line) && !blockStart.test(line) && !thematicBreak.test(line) &&
|
|
476
|
+
!definition.test(line) && (i === 0 || isBlank(body(i - 1))) && setextUnderline.test(body(i + 1))
|
|
477
|
+
) {
|
|
478
|
+
const source = line + eol(i) + body(i + 1)
|
|
479
|
+
out += pick("*" + inline(line.trim()) + "*", source) + eol(i + 1)
|
|
480
|
+
i += 2
|
|
481
|
+
continue
|
|
482
|
+
}
|
|
483
|
+
// Indented code, measured inside its quotes and list items with tabs expanded, is kept as typed.
|
|
484
|
+
if (/^ {4}/.test(rest) || thematicBreak.test(line)) {
|
|
485
|
+
out += lines[i]
|
|
486
|
+
i++
|
|
487
|
+
continue
|
|
488
|
+
}
|
|
489
|
+
// The text is cut from the line as typed, so the output keeps its tabs.
|
|
490
|
+
const text = containers(line).rest
|
|
491
|
+
out += line.slice(0, line.length - text.length) + inline(text) + eol(i)
|
|
492
|
+
i++
|
|
493
|
+
}
|
|
494
|
+
return out
|
|
495
|
+
}
|