micro-models-agent 2.16.0 → 2.17.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 (427) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/assets/app.js +1 -314
  3. package/dist/assets/hljs.js +5 -1244
  4. package/dist/assets/index.html +11 -0
  5. package/dist/assets/js/api.js +1 -28
  6. package/dist/assets/js/core/menus.js +1 -0
  7. package/dist/assets/js/core/store/feed.js +1 -0
  8. package/dist/assets/js/core/store/index.js +1 -0
  9. package/dist/assets/js/core/store/session.js +1 -0
  10. package/dist/assets/js/core/store/settings.js +1 -0
  11. package/dist/assets/js/core/store/tabs.js +1 -0
  12. package/dist/assets/js/dialog.js +1 -105
  13. package/dist/assets/js/dom.js +1 -178
  14. package/dist/assets/js/feed/core.js +1 -0
  15. package/dist/assets/js/feed/history.js +1 -0
  16. package/dist/assets/js/feed/plan.js +1 -0
  17. package/dist/assets/js/feed/question.js +3 -0
  18. package/dist/assets/js/feed/sidebar.js +1 -0
  19. package/dist/assets/js/feed/stream.js +1 -0
  20. package/dist/assets/js/feed/turn.js +5 -0
  21. package/dist/assets/js/feed.js +1 -1211
  22. package/dist/assets/js/format.js +2 -122
  23. package/dist/assets/js/i18n.js +1 -46
  24. package/dist/assets/js/rail.js +1 -114
  25. package/dist/assets/js/session/cmdk.js +1 -0
  26. package/dist/assets/js/session/composer.js +1 -0
  27. package/dist/assets/js/session/sessions-menu.js +1 -0
  28. package/dist/assets/js/session/tabs.js +1 -0
  29. package/dist/assets/js/settings/controls.js +1 -0
  30. package/dist/assets/js/settings/editors.js +1 -0
  31. package/dist/assets/js/settings/form.js +1 -0
  32. package/dist/assets/js/settings/modal.js +1 -0
  33. package/dist/assets/js/settings/panels.js +1 -0
  34. package/dist/assets/js/settings/providers.js +1 -0
  35. package/dist/assets/js/settings/search.js +1 -0
  36. package/dist/assets/js/settings/subdialog.js +1 -0
  37. package/dist/assets/js/settings/write.js +1 -0
  38. package/dist/assets/js/settings.js +1 -1914
  39. package/dist/assets/js/sidebar/core.js +1 -0
  40. package/dist/assets/js/sidebar/lsp.js +2 -0
  41. package/dist/assets/js/sidebar/map.js +1 -0
  42. package/dist/assets/js/sidebar/state.js +1 -0
  43. package/dist/assets/js/sidebar/trace.js +5 -0
  44. package/dist/assets/js/sidebar.js +1 -705
  45. package/dist/assets/js/store.js +1 -82
  46. package/dist/assets/js/subdialog.js +1 -0
  47. package/dist/assets/md.js +1 -277
  48. package/dist/assets/setup.js +3 -816
  49. package/dist/assets/smd.js +29 -1665
  50. package/dist/assets/styles.css +1 -1592
  51. package/dist/certification/certifications.json +1 -494
  52. package/dist/i18n/en.json +1 -1851
  53. package/dist/i18n/ru.json +1 -1854
  54. package/dist/main.js +815 -55797
  55. package/dist/modules/browser/bridge-server.mjs +2 -252
  56. package/package.json +10 -5
  57. package/dist/assets/js/settings-controls.js +0 -452
  58. package/dist/assets/js/tabs.js +0 -867
  59. package/dist/cli/cache-line.js +0 -31
  60. package/dist/cli/command-suggest.js +0 -39
  61. package/dist/cli/commands.js +0 -781
  62. package/dist/cli/completer.js +0 -268
  63. package/dist/cli/index.js +0 -3
  64. package/dist/cli/json-payload.js +0 -32
  65. package/dist/cli/main.js +0 -346
  66. package/dist/cli/onboarding-commands.js +0 -126
  67. package/dist/cli/plugin-commands.js +0 -38
  68. package/dist/cli/relaunch.js +0 -40
  69. package/dist/cli/repl-commands-lsp.js +0 -75
  70. package/dist/cli/repl-commands-skills.js +0 -105
  71. package/dist/cli/repl-commands.js +0 -1218
  72. package/dist/cli/repl.js +0 -1238
  73. package/dist/cli/run-result.js +0 -38
  74. package/dist/cli/security-commands.js +0 -177
  75. package/dist/cli/setup-order.js +0 -6
  76. package/dist/cli/setup-prompt.js +0 -50
  77. package/dist/cli/setup.js +0 -161
  78. package/dist/cli/web-command.js +0 -98
  79. package/dist/config/budget.js +0 -48
  80. package/dist/config/config.js +0 -345
  81. package/dist/config/defaults.js +0 -197
  82. package/dist/config/domains.js +0 -135
  83. package/dist/config/env.js +0 -46
  84. package/dist/config/experts.js +0 -17
  85. package/dist/config/index.js +0 -5
  86. package/dist/config/paths.js +0 -56
  87. package/dist/config/redact.js +0 -44
  88. package/dist/config/registry-extra.js +0 -334
  89. package/dist/config/registry.js +0 -584
  90. package/dist/config/security.js +0 -212
  91. package/dist/config/timeouts.js +0 -74
  92. package/dist/config/types.js +0 -1
  93. package/dist/config/utils.js +0 -57
  94. package/dist/core/agent/artifact-progress.js +0 -59
  95. package/dist/core/agent/artifact-resolve.js +0 -79
  96. package/dist/core/agent/audit-gate.js +0 -53
  97. package/dist/core/agent/compaction.js +0 -89
  98. package/dist/core/agent/constants.js +0 -90
  99. package/dist/core/agent/context-log.js +0 -454
  100. package/dist/core/agent/context-renderer.js +0 -45
  101. package/dist/core/agent/critic-gate.js +0 -132
  102. package/dist/core/agent/hallucination-gate.js +0 -197
  103. package/dist/core/agent/loop-state.js +0 -68
  104. package/dist/core/agent/prefix-monitor.js +0 -101
  105. package/dist/core/agent/reasoning-resolver.js +0 -65
  106. package/dist/core/agent/token-tracker.js +0 -99
  107. package/dist/core/agent/tool-batch.js +0 -312
  108. package/dist/core/agent/tool-images.js +0 -18
  109. package/dist/core/agent/tool-output.js +0 -116
  110. package/dist/core/agent/tool-pairing.js +0 -60
  111. package/dist/core/agent-moe.js +0 -248
  112. package/dist/core/agent.js +0 -1299
  113. package/dist/core/bootstrap/agent-phase.js +0 -66
  114. package/dist/core/bootstrap/config-phase.js +0 -86
  115. package/dist/core/bootstrap/modules-phase.js +0 -269
  116. package/dist/core/bootstrap/provider-phase.js +0 -183
  117. package/dist/core/bootstrap/session-phase.js +0 -204
  118. package/dist/core/bootstrap/state.js +0 -94
  119. package/dist/core/bootstrap/workspace-phase.js +0 -115
  120. package/dist/core/bootstrap.js +0 -112
  121. package/dist/core/crash-handler.js +0 -62
  122. package/dist/core/environment.js +0 -224
  123. package/dist/core/host-plan.js +0 -43
  124. package/dist/core/host.js +0 -23
  125. package/dist/core/index.js +0 -3
  126. package/dist/core/prompt-builder.js +0 -95
  127. package/dist/core/prompt-overflow.js +0 -306
  128. package/dist/core/session-logger.js +0 -375
  129. package/dist/core/system-prompt.js +0 -117
  130. package/dist/core/types.js +0 -1
  131. package/dist/core/version.js +0 -28
  132. package/dist/core/web-host.js +0 -34
  133. package/dist/core/workspace.js +0 -80
  134. package/dist/i18n/index.js +0 -80
  135. package/dist/index.js +0 -30
  136. package/dist/llm/cache-usage.js +0 -76
  137. package/dist/llm/image-utils.js +0 -138
  138. package/dist/llm/index.js +0 -4
  139. package/dist/llm/llm-errors.js +0 -50
  140. package/dist/llm/model-loader.js +0 -86
  141. package/dist/llm/models-dev.js +0 -121
  142. package/dist/llm/openai-compat.js +0 -756
  143. package/dist/llm/orchestrator.js +0 -294
  144. package/dist/llm/provider-budget.js +0 -68
  145. package/dist/llm/provider-context.js +0 -233
  146. package/dist/llm/provider.js +0 -9
  147. package/dist/llm/response.js +0 -44
  148. package/dist/llm/stream-state.js +0 -26
  149. package/dist/llm/token-counter.js +0 -83
  150. package/dist/llm/types.js +0 -1
  151. package/dist/logger/app-logger.js +0 -212
  152. package/dist/logger/file-log.js +0 -160
  153. package/dist/logger/index.js +0 -2
  154. package/dist/migration/backup.js +0 -56
  155. package/dist/migration/detect.js +0 -61
  156. package/dist/migration/index.js +0 -3
  157. package/dist/modules/artifacts/store.js +0 -62
  158. package/dist/modules/browser/actions.js +0 -79
  159. package/dist/modules/browser/bridge-client.js +0 -203
  160. package/dist/modules/browser/bridge-path.js +0 -10
  161. package/dist/modules/browser/console-buffer.js +0 -37
  162. package/dist/modules/browser/cookie-store.js +0 -29
  163. package/dist/modules/browser/driver-types.js +0 -1
  164. package/dist/modules/browser/driver.js +0 -186
  165. package/dist/modules/browser/index.js +0 -9
  166. package/dist/modules/browser/module.js +0 -30
  167. package/dist/modules/browser/session.js +0 -322
  168. package/dist/modules/browser/snapshot.js +0 -154
  169. package/dist/modules/browser/types.js +0 -13
  170. package/dist/modules/certification/cli.js +0 -264
  171. package/dist/modules/certification/fact-checker.js +0 -90
  172. package/dist/modules/certification/loader.js +0 -127
  173. package/dist/modules/certification/manifest.js +0 -231
  174. package/dist/modules/certification/runner.js +0 -233
  175. package/dist/modules/certification/scenarios.js +0 -586
  176. package/dist/modules/certification/syntax-scenarios.js +0 -50
  177. package/dist/modules/certification/types.js +0 -1
  178. package/dist/modules/context/chunk-query.js +0 -122
  179. package/dist/modules/context/fact-extractor.js +0 -170
  180. package/dist/modules/context/history.js +0 -15
  181. package/dist/modules/context/index.js +0 -2
  182. package/dist/modules/context/manager.js +0 -482
  183. package/dist/modules/critic/client.js +0 -176
  184. package/dist/modules/critic/evidence.js +0 -81
  185. package/dist/modules/critic/provider.js +0 -45
  186. package/dist/modules/critic/tools.js +0 -78
  187. package/dist/modules/critic/types.js +0 -1
  188. package/dist/modules/execution/audit-runners.js +0 -206
  189. package/dist/modules/execution/auditor.js +0 -222
  190. package/dist/modules/execution/execution-plugin.js +0 -478
  191. package/dist/modules/execution/index.js +0 -8
  192. package/dist/modules/execution/input-from.js +0 -70
  193. package/dist/modules/execution/module.js +0 -891
  194. package/dist/modules/execution/moe-executor.js +0 -443
  195. package/dist/modules/execution/plan-actions.js +0 -642
  196. package/dist/modules/execution/plan-checks.js +0 -35
  197. package/dist/modules/execution/plan-coverage.js +0 -71
  198. package/dist/modules/execution/plan-persister.js +0 -46
  199. package/dist/modules/execution/plan-steps.js +0 -31
  200. package/dist/modules/execution/plan-store.js +0 -274
  201. package/dist/modules/execution/plan-tool.js +0 -249
  202. package/dist/modules/execution/plan-validator.js +0 -197
  203. package/dist/modules/execution/planner.js +0 -103
  204. package/dist/modules/execution/stuck-detector.js +0 -834
  205. package/dist/modules/execution/tracker.js +0 -92
  206. package/dist/modules/execution/transient-error.js +0 -30
  207. package/dist/modules/execution/types.js +0 -1
  208. package/dist/modules/execution/verifier.js +0 -343
  209. package/dist/modules/execution/windows-commands.js +0 -52
  210. package/dist/modules/hallucination/artifact-claim.js +0 -93
  211. package/dist/modules/hallucination/confidence.js +0 -81
  212. package/dist/modules/hallucination/consistency.js +0 -37
  213. package/dist/modules/hallucination/detector.js +0 -57
  214. package/dist/modules/hallucination/factual.js +0 -205
  215. package/dist/modules/hallucination/index.js +0 -6
  216. package/dist/modules/hallucination/js-identifiers.js +0 -261
  217. package/dist/modules/hallucination/llm-judge.js +0 -116
  218. package/dist/modules/hallucination/success-claim.js +0 -59
  219. package/dist/modules/index.js +0 -5
  220. package/dist/modules/indexer/cache.js +0 -88
  221. package/dist/modules/indexer/index.js +0 -4
  222. package/dist/modules/indexer/map-command.js +0 -33
  223. package/dist/modules/indexer/map-select.js +0 -87
  224. package/dist/modules/indexer/module.js +0 -293
  225. package/dist/modules/indexer/project-profile.js +0 -184
  226. package/dist/modules/indexer/symbols.js +0 -189
  227. package/dist/modules/indexer/walker.js +0 -285
  228. package/dist/modules/lsp/check-tool.js +0 -61
  229. package/dist/modules/lsp/client.js +0 -406
  230. package/dist/modules/lsp/command.js +0 -53
  231. package/dist/modules/lsp/config.js +0 -189
  232. package/dist/modules/lsp/index.js +0 -4
  233. package/dist/modules/lsp/module.js +0 -295
  234. package/dist/modules/lsp/probe.js +0 -86
  235. package/dist/modules/lsp/project-root.js +0 -69
  236. package/dist/modules/lsp/startup-check.js +0 -154
  237. package/dist/modules/lsp/types.js +0 -1
  238. package/dist/modules/mcp/client.js +0 -445
  239. package/dist/modules/mcp/index.js +0 -4
  240. package/dist/modules/mcp/module.js +0 -295
  241. package/dist/modules/mcp/registry.js +0 -20
  242. package/dist/modules/memory/index.js +0 -2
  243. package/dist/modules/memory/module.js +0 -101
  244. package/dist/modules/memory/search.js +0 -45
  245. package/dist/modules/memory/store.js +0 -107
  246. package/dist/modules/onboarding/init.js +0 -47
  247. package/dist/modules/onboarding/meet.js +0 -30
  248. package/dist/modules/onboarding/mode.js +0 -30
  249. package/dist/modules/onboarding/prompts.js +0 -43
  250. package/dist/modules/pipelines/engine.js +0 -70
  251. package/dist/modules/pipelines/index.js +0 -4
  252. package/dist/modules/pipelines/parser.js +0 -59
  253. package/dist/modules/pipelines/template.js +0 -18
  254. package/dist/modules/plugins/builtin/lint-on-write.js +0 -377
  255. package/dist/modules/plugins/builtin/notify.js +0 -11
  256. package/dist/modules/plugins/index.js +0 -2
  257. package/dist/modules/plugins/loader.js +0 -85
  258. package/dist/modules/plugins/manager.js +0 -244
  259. package/dist/modules/plugins/types.js +0 -1
  260. package/dist/modules/pricing/index.js +0 -70
  261. package/dist/modules/pricing/prices.js +0 -147
  262. package/dist/modules/processes/detect.js +0 -34
  263. package/dist/modules/processes/index.js +0 -4
  264. package/dist/modules/processes/kill-tree.js +0 -52
  265. package/dist/modules/processes/registry.js +0 -294
  266. package/dist/modules/processes/runner.js +0 -23
  267. package/dist/modules/providers/cache.js +0 -23
  268. package/dist/modules/providers/capabilities.js +0 -28
  269. package/dist/modules/providers/create.js +0 -21
  270. package/dist/modules/providers/factory.js +0 -28
  271. package/dist/modules/providers/fallback.js +0 -81
  272. package/dist/modules/providers/health.js +0 -47
  273. package/dist/modules/providers/index.js +0 -6
  274. package/dist/modules/providers/manager.js +0 -207
  275. package/dist/modules/providers/naming.js +0 -15
  276. package/dist/modules/providers/preflight.js +0 -71
  277. package/dist/modules/providers/presets.js +0 -232
  278. package/dist/modules/providers/registry.js +0 -27
  279. package/dist/modules/providers/types.js +0 -1
  280. package/dist/modules/reasoning/policy.js +0 -42
  281. package/dist/modules/reasoning/probe.js +0 -116
  282. package/dist/modules/registry.js +0 -48
  283. package/dist/modules/security/audit-log.js +0 -119
  284. package/dist/modules/security/audit-notifier.js +0 -267
  285. package/dist/modules/security/command-validator.js +0 -272
  286. package/dist/modules/security/content-scanner.js +0 -53
  287. package/dist/modules/security/data-sanitizer.js +0 -82
  288. package/dist/modules/security/encryption.js +0 -216
  289. package/dist/modules/security/index.js +0 -15
  290. package/dist/modules/security/network-validator.js +0 -236
  291. package/dist/modules/security/path-validator.js +0 -261
  292. package/dist/modules/security/protected-paths.js +0 -73
  293. package/dist/modules/security/rate-limiter.js +0 -98
  294. package/dist/modules/security/security-policies.js +0 -499
  295. package/dist/modules/security/session-encryption.js +0 -182
  296. package/dist/modules/security/session-isolation.js +0 -75
  297. package/dist/modules/session/auto-name.js +0 -63
  298. package/dist/modules/session/export.js +0 -98
  299. package/dist/modules/session/index.js +0 -4
  300. package/dist/modules/session/manager.js +0 -234
  301. package/dist/modules/session/module.js +0 -33
  302. package/dist/modules/session/store.js +0 -295
  303. package/dist/modules/session/types.js +0 -1
  304. package/dist/modules/setup/answers.js +0 -35
  305. package/dist/modules/setup/apply.js +0 -79
  306. package/dist/modules/setup/discovery.js +0 -72
  307. package/dist/modules/skills/index.js +0 -3
  308. package/dist/modules/skills/loader.js +0 -86
  309. package/dist/modules/skills/matcher.js +0 -27
  310. package/dist/modules/skills/module.js +0 -141
  311. package/dist/modules/types.js +0 -1
  312. package/dist/modules/updater/changelog-reader.js +0 -93
  313. package/dist/modules/updater/checker.js +0 -101
  314. package/dist/modules/updater/dev-detect.js +0 -18
  315. package/dist/modules/updater/index.js +0 -4
  316. package/dist/modules/updater/module.js +0 -129
  317. package/dist/modules/user-profile/compressor.js +0 -18
  318. package/dist/modules/user-profile/index.js +0 -2
  319. package/dist/modules/user-profile/profile.js +0 -77
  320. package/dist/modules/webui/answer-api.js +0 -29
  321. package/dist/modules/webui/assets.js +0 -50
  322. package/dist/modules/webui/body.js +0 -21
  323. package/dist/modules/webui/commands-settings.js +0 -209
  324. package/dist/modules/webui/commands-shared.js +0 -60
  325. package/dist/modules/webui/commands.js +0 -174
  326. package/dist/modules/webui/config-api.js +0 -134
  327. package/dist/modules/webui/file-api.js +0 -202
  328. package/dist/modules/webui/forward.js +0 -159
  329. package/dist/modules/webui/history-api.js +0 -112
  330. package/dist/modules/webui/host-state.js +0 -60
  331. package/dist/modules/webui/i18n-api.js +0 -9
  332. package/dist/modules/webui/limits.js +0 -6
  333. package/dist/modules/webui/log-forward.js +0 -20
  334. package/dist/modules/webui/memory-commands.js +0 -64
  335. package/dist/modules/webui/model-cache.js +0 -18
  336. package/dist/modules/webui/module.js +0 -148
  337. package/dist/modules/webui/persist.js +0 -24
  338. package/dist/modules/webui/prompt-io.js +0 -97
  339. package/dist/modules/webui/providers-api.js +0 -337
  340. package/dist/modules/webui/providers-view.js +0 -56
  341. package/dist/modules/webui/reasoning-view.js +0 -15
  342. package/dist/modules/webui/run-agent.js +0 -81
  343. package/dist/modules/webui/server.js +0 -284
  344. package/dist/modules/webui/sessions-api.js +0 -178
  345. package/dist/modules/webui/settings-api.js +0 -561
  346. package/dist/modules/webui/setup-api.js +0 -201
  347. package/dist/modules/webui/state.js +0 -74
  348. package/dist/modules/webui/util.js +0 -261
  349. package/dist/output/bus.js +0 -37
  350. package/dist/output/channel.js +0 -285
  351. package/dist/output/format.js +0 -15
  352. package/dist/output/index.js +0 -8
  353. package/dist/output/json-sink.js +0 -24
  354. package/dist/output/machine.js +0 -9
  355. package/dist/output/session-sink.js +0 -27
  356. package/dist/output/types.js +0 -1
  357. package/dist/tools/approve.js +0 -41
  358. package/dist/tools/attach-image.js +0 -102
  359. package/dist/tools/auto-fixer.js +0 -180
  360. package/dist/tools/auto-tags.js +0 -83
  361. package/dist/tools/bash.js +0 -649
  362. package/dist/tools/browser.js +0 -160
  363. package/dist/tools/chunk-query.js +0 -112
  364. package/dist/tools/create-dir.js +0 -60
  365. package/dist/tools/delete-file.js +0 -67
  366. package/dist/tools/download-file.js +0 -142
  367. package/dist/tools/edit-file.js +0 -136
  368. package/dist/tools/enable-tools.js +0 -59
  369. package/dist/tools/executor.js +0 -304
  370. package/dist/tools/file-info.js +0 -49
  371. package/dist/tools/filter-tools.js +0 -17
  372. package/dist/tools/glob-tool.js +0 -37
  373. package/dist/tools/grep-tool.js +0 -142
  374. package/dist/tools/hidden-tools-block.js +0 -37
  375. package/dist/tools/index.js +0 -88
  376. package/dist/tools/list-dir.js +0 -53
  377. package/dist/tools/load-skill.js +0 -51
  378. package/dist/tools/mcp-call.js +0 -80
  379. package/dist/tools/meet-done.js +0 -14
  380. package/dist/tools/move-file.js +0 -94
  381. package/dist/tools/path-utils.js +0 -108
  382. package/dist/tools/pipeline-run.js +0 -167
  383. package/dist/tools/preview.js +0 -2
  384. package/dist/tools/process-ids.js +0 -31
  385. package/dist/tools/process-kill.js +0 -33
  386. package/dist/tools/process-list.js +0 -38
  387. package/dist/tools/process-log.js +0 -49
  388. package/dist/tools/process-wait.js +0 -63
  389. package/dist/tools/prompt-io.js +0 -29
  390. package/dist/tools/question.js +0 -288
  391. package/dist/tools/read-file.js +0 -200
  392. package/dist/tools/recall.js +0 -124
  393. package/dist/tools/registry.js +0 -48
  394. package/dist/tools/remember.js +0 -72
  395. package/dist/tools/scope-check.js +0 -32
  396. package/dist/tools/scope-request.js +0 -96
  397. package/dist/tools/search-history.js +0 -88
  398. package/dist/tools/session-info.js +0 -46
  399. package/dist/tools/set-thinking.js +0 -75
  400. package/dist/tools/subagent.js +0 -257
  401. package/dist/tools/syntax-validator.js +0 -379
  402. package/dist/tools/types.js +0 -1
  403. package/dist/tools/user-input.js +0 -134
  404. package/dist/tools/web-browse.js +0 -93
  405. package/dist/tools/web-fetch.js +0 -108
  406. package/dist/tools/web-search.js +0 -107
  407. package/dist/tools/write-file.js +0 -128
  408. package/dist/ui/action-log.js +0 -13
  409. package/dist/ui/box.js +0 -78
  410. package/dist/ui/colors.js +0 -6
  411. package/dist/ui/diff.js +0 -189
  412. package/dist/ui/index.js +0 -7
  413. package/dist/ui/line-editor.js +0 -1083
  414. package/dist/ui/line-math.js +0 -98
  415. package/dist/ui/md-formatter.js +0 -322
  416. package/dist/ui/output.js +0 -19
  417. package/dist/ui/plan-view.js +0 -109
  418. package/dist/ui/renderer.js +0 -589
  419. package/dist/ui/spinner.js +0 -91
  420. package/dist/ui/table.js +0 -142
  421. package/dist/ui/theme.js +0 -143
  422. package/dist/utils/child-run.js +0 -20
  423. package/dist/utils/error.js +0 -4
  424. package/dist/utils/index.js +0 -6
  425. package/dist/utils/retry.js +0 -17
  426. package/dist/utils/sleep.js +0 -23
  427. package/dist/utils/truncate.js +0 -9
@@ -1,1665 +1,29 @@
1
- /*
2
- Streaming Markdown Parser and Renderer
3
- MIT License
4
- Copyright 2024 Damian Tarnawski
5
- https://github.com/thetarnav/streaming-markdown
6
- */
7
-
8
- export const
9
- DOCUMENT = 1,
10
- PARAGRAPH = 2,
11
- HEADING_1 = 3,
12
- HEADING_2 = 4,
13
- HEADING_3 = 5,
14
- HEADING_4 = 6,
15
- HEADING_5 = 7,
16
- HEADING_6 = 8,
17
- CODE_BLOCK = 9,
18
- CODE_FENCE = 10,
19
- CODE_INLINE = 11,
20
- ITALIC_AST = 12,
21
- ITALIC_UND = 13,
22
- STRONG_AST = 14,
23
- STRONG_UND = 15,
24
- STRIKE = 16,
25
- LINK = 17,
26
- RAW_URL = 18,
27
- IMAGE = 19,
28
- BLOCKQUOTE = 20,
29
- LINE_BREAK = 21,
30
- RULE = 22,
31
- LIST_UNORDERED = 23,
32
- LIST_ORDERED = 24,
33
- LIST_ITEM = 25,
34
- CHECKBOX = 26,
35
- TABLE = 27,
36
- TABLE_ROW = 28,
37
- TABLE_CELL = 29,
38
- EQUATION_BLOCK = 30,
39
- EQUATION_INLINE = 31,
40
- NEWLINE = 101,
41
- MAYBE_URL = 102,
42
- MAYBE_TASK = 103,
43
- MAYBE_BR = 104,
44
- MAYBE_EQ_BLOCK = 105
45
-
46
- /** @enum {(typeof Token)[keyof typeof Token]} */
47
- export const Token = /** @type {const} */({
48
- Document: DOCUMENT,
49
- Blockquote: BLOCKQUOTE,
50
- Paragraph: PARAGRAPH,
51
- Heading_1: HEADING_1,
52
- Heading_2: HEADING_2,
53
- Heading_3: HEADING_3,
54
- Heading_4: HEADING_4,
55
- Heading_5: HEADING_5,
56
- Heading_6: HEADING_6,
57
- Code_Block: CODE_BLOCK,
58
- Code_Fence: CODE_FENCE,
59
- Code_Inline: CODE_INLINE,
60
- Italic_Ast: ITALIC_AST,
61
- Italic_Und: ITALIC_UND,
62
- Strong_Ast: STRONG_AST,
63
- Strong_Und: STRONG_UND,
64
- Strike: STRIKE,
65
- Link: LINK,
66
- Raw_URL: RAW_URL,
67
- Image: IMAGE,
68
- Line_Break: LINE_BREAK,
69
- Rule: RULE,
70
- List_Unordered: LIST_UNORDERED,
71
- List_Ordered: LIST_ORDERED,
72
- List_Item: LIST_ITEM,
73
- Checkbox: CHECKBOX,
74
- Table: TABLE,
75
- Table_Row: TABLE_ROW,
76
- Table_Cell: TABLE_CELL,
77
- Equation_Block: EQUATION_BLOCK,
78
- Equation_Inline:EQUATION_INLINE,
79
- })
80
-
81
- /**
82
- * @param {Token} type
83
- * @returns {string } */
84
- export function token_to_string(type) {
85
- switch (type) {
86
- case DOCUMENT: return "Document"
87
- case BLOCKQUOTE: return "Blockquote"
88
- case PARAGRAPH: return "Paragraph"
89
- case HEADING_1: return "Heading_1"
90
- case HEADING_2: return "Heading_2"
91
- case HEADING_3: return "Heading_3"
92
- case HEADING_4: return "Heading_4"
93
- case HEADING_5: return "Heading_5"
94
- case HEADING_6: return "Heading_6"
95
- case CODE_BLOCK: return "Code_Block"
96
- case CODE_FENCE: return "Code_Fence"
97
- case CODE_INLINE: return "Code_Inline"
98
- case ITALIC_AST: return "Italic_Ast"
99
- case ITALIC_UND: return "Italic_Und"
100
- case STRONG_AST: return "Strong_Ast"
101
- case STRONG_UND: return "Strong_Und"
102
- case STRIKE: return "Strike"
103
- case LINK: return "Link"
104
- case RAW_URL: return "Raw URL"
105
- case IMAGE: return "Image"
106
- case LINE_BREAK: return "Line_Break"
107
- case RULE: return "Rule"
108
- case LIST_UNORDERED: return "List_Unordered"
109
- case LIST_ORDERED: return "List_Ordered"
110
- case LIST_ITEM: return "List_Item"
111
- case CHECKBOX: return "Checkbox"
112
- case TABLE: return "Table"
113
- case TABLE_ROW: return "Table_Row"
114
- case TABLE_CELL: return "Table_Cell"
115
- case EQUATION_BLOCK: return "Equation_Block"
116
- case EQUATION_INLINE:return "Equation_Inline"
117
- }
118
- }
119
-
120
- export const
121
- HREF = 1,
122
- SRC = 2,
123
- LANG = 4,
124
- CHECKED = 8,
125
- START = 16
126
-
127
- /** @enum {(typeof Attr)[keyof typeof Attr]} */
128
- export const Attr = /** @type {const} */({
129
- Href : HREF,
130
- Src : SRC,
131
- Lang : LANG,
132
- Checked: CHECKED,
133
- Start : START,
134
- })
135
-
136
- /**
137
- * @param {Attr} type
138
- * @returns {string } */
139
- export function attr_to_html_attr(type) {
140
- switch (type) {
141
- case HREF: return "href"
142
- case SRC : return "src"
143
- case LANG: return "class"
144
- case CHECKED: return "checked"
145
- case START: return "start"
146
- }
147
- }
148
-
149
- /**
150
- * @param {number} level
151
- * @returns {Token } */
152
- export const level_to_heading = (level) => {
153
- switch (level) {
154
- case 1: return HEADING_1
155
- case 2: return HEADING_2
156
- case 3: return HEADING_3
157
- case 4: return HEADING_4
158
- case 5: return HEADING_5
159
- default: return HEADING_6
160
- }
161
- }
162
- export const heading_from_level = level_to_heading
163
-
164
- /**
165
- * @param {Token} token
166
- * @returns {number} */
167
- export const heading_to_level = (token) => {
168
- switch (token) {
169
- case HEADING_1: return 1
170
- case HEADING_2: return 2
171
- case HEADING_3: return 3
172
- case HEADING_4: return 4
173
- case HEADING_5: return 5
174
- case HEADING_6: return 6
175
- default: return 0
176
- }
177
- }
178
-
179
- /**
180
- * @typedef {object } Parser
181
- * @property {Any_Renderer} renderer - {@link Renderer} interface
182
- * @property {string } text - Text to be added to the last token in the next flush
183
- * @property {string } pending - Characters for identifying tokens
184
- * @property {Uint32Array } tokens - Current token and it's parents (a slice of a tree)
185
- * @property {number } len - Number of tokens in types without root
186
- * @property {number } token - Last token in the tree
187
- * @property {Uint8Array } spaces
188
- * @property {string } indent
189
- * @property {number } indent_len
190
- * @property {number } fence_end - For {@link Token.Code_Fence} parsing
191
- * @property {number } fence_start
192
- * @property {number } blockquote_idx - For Blockquote parsing
193
- * @property {string } hr_char - For horizontal rule parsing
194
- * @property {number } hr_chars - For horizontal rule parsing
195
- * @property {number } table_state
196
- */
197
-
198
- const TOKEN_ARRAY_CAP = 24
199
-
200
- /**
201
- * Makes a new Parser object.
202
- * @param {Any_Renderer} renderer
203
- * @returns {Parser } */
204
- export function parser(renderer) {
205
- const tokens = new Uint32Array(TOKEN_ARRAY_CAP)
206
- tokens[0] = DOCUMENT
207
- return {
208
- renderer : renderer,
209
- text : "",
210
- pending : "",
211
- tokens : tokens,
212
- len : 0,
213
- token : DOCUMENT,
214
- fence_end: 0,
215
- blockquote_idx: 0,
216
- hr_char : '',
217
- hr_chars : 0,
218
- fence_start: 0,
219
- spaces : new Uint8Array(TOKEN_ARRAY_CAP),
220
- indent : "",
221
- indent_len : 0,
222
- table_state: 0,
223
- }
224
- }
225
-
226
- /**
227
- * Finish rendering the markdown - flushes any remaining text.
228
- * @param {Parser} p
229
- * @returns {void } */
230
- export function parser_end(p) {
231
- if (p.pending.length > 0) {
232
- parser_write(p, "\n")
233
- }
234
- }
235
-
236
- /**
237
- * @param {Parser} p
238
- * @returns {void } */
239
- function add_text(p) {
240
- if (p.text.length === 0) return
241
- console.assert(p.len > 0, "Never adding text to root")
242
- p.renderer.add_text(p.renderer.data, p.text)
243
- p.text = ""
244
- }
245
-
246
- /**
247
- * @param {Parser} p
248
- * @returns {void } */
249
- function ensure_paragraph(p) {
250
- switch (p.token) {
251
- case LINE_BREAK:
252
- case DOCUMENT:
253
- case BLOCKQUOTE:
254
- case LIST_ORDERED:
255
- case LIST_UNORDERED:
256
- add_token(p, PARAGRAPH)
257
- }
258
- }
259
-
260
- /**
261
- * @param {Parser} p
262
- * @param {string} text
263
- * @returns {void } */
264
- function push_text(p, text) {
265
- ensure_paragraph(p)
266
- p.text += text
267
- }
268
-
269
- /**
270
- * @param {Parser} p
271
- * @returns {void } */
272
- function end_token(p) {
273
- console.assert(p.len > 0, "No nodes to end")
274
- p.len -= 1
275
- p.token = /** @type {Token} */ (p.tokens[p.len])
276
- p.renderer.end_token(p.renderer.data)
277
- }
278
-
279
- /**
280
- * @param {Parser} p
281
- * @param {Token } token
282
- * @returns {void } */
283
- function add_token(p, token) {
284
- /*
285
- If a list doesn't start with a list item
286
- it means that there was a newline after the list:
287
-
288
- 1. foo
289
- 2. bar
290
- <empty line>
291
- <not_a_list_item> <- new token
292
- */
293
- if ((p.tokens[p.len] === LIST_ORDERED || p.tokens[p.len] === LIST_UNORDERED) &&
294
- token !== LIST_ITEM
295
- ) {
296
- end_token(p)
297
- }
298
-
299
- p.len += 1
300
- p.tokens[p.len] = token
301
- p.token = token
302
- p.renderer.add_token(p.renderer.data, token)
303
- }
304
-
305
- /**
306
- * @param {Parser} p
307
- * @param {number} token
308
- * @param {number} start_idx
309
- * @returns {number} */
310
- function idx_of_token(p, token, start_idx) {
311
- while (start_idx <= p.len) {
312
- if (p.tokens[start_idx] === token) {
313
- return start_idx
314
- }
315
- start_idx += 1
316
- }
317
- return -1
318
- }
319
-
320
- /**
321
- * End tokens until the parser has the given length.
322
- * @param {Parser} p
323
- * @param {number} len
324
- * @returns {void } */
325
- function end_tokens_to_len(p, len) {
326
-
327
- // TODO: specific token state should be reset only when the token ends
328
- p.fence_start = 0
329
-
330
- while (p.len > len) {
331
- end_token(p)
332
- }
333
- }
334
-
335
- /**
336
- * @param {Parser} p
337
- * @param {number} indent
338
- * @returns {number} */
339
- function end_tokens_to_indent(p, indent) {
340
-
341
- let idx = 0
342
- for (let i = 0; i <= p.len; i += 1) {
343
- indent -= p.spaces[i]
344
- if (indent < 0) {
345
- break
346
- }
347
- switch (p.tokens[i]) {
348
- case CODE_BLOCK:
349
- case CODE_FENCE:
350
- case BLOCKQUOTE:
351
- case LIST_ITEM:
352
- idx = i
353
- break
354
- }
355
- }
356
-
357
- while (p.len > idx) {end_token(p)}
358
-
359
- return indent
360
- }
361
-
362
- /**
363
- * @param {Parser } p
364
- * @param {Token } list_token
365
- * @returns {boolean} added a new list */
366
- function continue_or_add_list(p, list_token) {
367
- /* will create a new list inside the last item
368
- if the amount of spaces is greater than the last one (with prefix)
369
- 1. foo
370
- - bar <- new nested ul
371
- - baz <- new nested ul
372
- 12. qux <- cannot be nested in "baz" or "bar",
373
- so it's a new list in "foo"
374
- */
375
- let list_idx = -1
376
- let item_idx = -1
377
-
378
- for (let i = p.blockquote_idx+1; i <= p.len; i += 1) {
379
- if (p.tokens[i] === LIST_ITEM) {
380
- if (p.indent_len < p.spaces[i]) {
381
- item_idx = -1
382
- break
383
- }
384
- item_idx = i
385
- } else if (p.tokens[i] === list_token) {
386
- list_idx = i
387
- }
388
- }
389
-
390
- if (item_idx === -1) {
391
- if (list_idx === -1) {
392
- end_tokens_to_len(p, p.blockquote_idx)
393
- add_token(p, list_token)
394
- return true
395
- }
396
- end_tokens_to_len(p, list_idx)
397
- return false
398
- }
399
- end_tokens_to_len(p, item_idx)
400
- add_token(p, list_token)
401
- return true
402
- }
403
-
404
- /**
405
- * Create a new list
406
- * or continue the last one
407
- * @param {Parser } p
408
- * @param {number } prefix_length
409
- * @returns {void } */
410
- function add_list_item(p, prefix_length) {
411
- add_token(p, LIST_ITEM)
412
- p.spaces[p.len] = p.indent_len + prefix_length
413
- clear_root_pending(p)
414
- p.token = MAYBE_TASK
415
- }
416
-
417
- /**
418
- * @param {Parser} p
419
- * @returns {void } */
420
- function clear_root_pending(p) {
421
- p.indent = ""
422
- p.indent_len = 0
423
- p.pending = ""
424
- }
425
-
426
- /**
427
- * @param {number} charcode
428
- * @returns {boolean} */
429
- function is_digit(charcode) {
430
- switch (charcode) {
431
- case 48: case 49: case 50: case 51: case 52:
432
- case 53: case 54: case 55: case 56: case 57:
433
- return true
434
- default:
435
- return false
436
- }
437
- }
438
-
439
- /**
440
- * @param {number} charcode
441
- * @returns {boolean} */
442
- function is_delimeter(charcode) {
443
- switch (charcode) {
444
- // " " ":" ";" ")" "," "!" "." "?" "]" "\n"
445
- case 32: case 58: case 59: case 41: case 44: case 33: case 46: case 63: case 93: case 10:
446
- return true
447
- default:
448
- return false
449
- }
450
- }
451
-
452
- /**
453
- * @param {number} charcode
454
- * @returns {boolean} */
455
- function is_delimeter_or_number(charcode) {
456
- return is_digit(charcode) || is_delimeter(charcode)
457
- }
458
-
459
- /**
460
- * @param {number} charcode
461
- * @returns {boolean} */
462
- function is_alnum(charcode) {
463
- return is_digit(charcode) || // 0-9
464
- (charcode >= 65 && charcode <= 90) || // A-Z
465
- (charcode >= 97 && charcode <= 122) // a-z
466
- }
467
-
468
- /**
469
- * Parse and render another chunk of markdown.
470
- * @param {Parser} p
471
- * @param {string} chunk
472
- * @returns {void } */
473
- export function parser_write(p, chunk) {
474
- for (const char of chunk) {
475
-
476
- /*
477
- Handle newlines
478
- */
479
- if (p.token === NEWLINE) {
480
- switch (char) {
481
- case ' ':
482
- p.indent_len += 1
483
- continue
484
- case '\t':
485
- p.indent_len += 4
486
- continue
487
- }
488
-
489
- let indent = end_tokens_to_indent(p, p.indent_len)
490
-
491
- p.indent_len = 0
492
- p.token = p.tokens[p.len]
493
-
494
- if (indent > 0) {
495
- parser_write(p, " ".repeat(indent))
496
- }
497
- }
498
-
499
- const pending_with_char = p.pending + char
500
-
501
- /*
502
- Token specific checks
503
- */
504
- switch (p.token) {
505
- case LINE_BREAK:
506
- case DOCUMENT:
507
- case BLOCKQUOTE:
508
- case LIST_ORDERED:
509
- case LIST_UNORDERED:
510
- console.assert(p.text.length === 0, "Root should not have any text")
511
-
512
- switch (p.pending[0]) {
513
- case undefined:
514
- p.pending = char
515
- continue
516
- case ' ':
517
- console.assert(p.pending.length === 1)
518
- p.pending = char
519
- p.indent += ' '
520
- p.indent_len += 1
521
- continue
522
- case '\t':
523
- console.assert(p.pending.length === 1)
524
- p.pending = char
525
- p.indent += '\t'
526
- p.indent_len += 4
527
- continue
528
- case '\n':
529
- console.assert(p.pending.length === 1)
530
- /*
531
- Lists can have an empty line in between items:
532
- 1. foo
533
- <empty>
534
- 2. bar
535
- */
536
- if (p.tokens[p.len] === LIST_ITEM && p.token === LINE_BREAK) {
537
- end_token(p)
538
- clear_root_pending(p)
539
- p.pending = char
540
- continue
541
- }
542
- /*
543
- Exit out of tokens
544
- And ignore newlines in root
545
- */
546
- end_tokens_to_len(p, p.blockquote_idx)
547
- clear_root_pending(p)
548
- p.blockquote_idx = 0
549
- p.fence_start = 0
550
- p.pending = char
551
- continue
552
- /* Heading */
553
- case '#':
554
- switch (char) {
555
- case '#':
556
- if (p.pending.length < 6) {
557
- p.pending = pending_with_char
558
- continue
559
- }
560
- break // fail
561
- case ' ':
562
- end_tokens_to_indent(p, p.indent_len)
563
- add_token(p, heading_from_level(p.pending.length))
564
- clear_root_pending(p)
565
- continue
566
- }
567
- break // fail
568
- /* Blockquote */
569
- case '>': {
570
- const next_blockquote_idx = idx_of_token(p, BLOCKQUOTE, p.blockquote_idx+1)
571
-
572
- /*
573
- Only when there is no blockquote to the right of blockquote_idx
574
- a new blockquote can be created
575
- */
576
- if (next_blockquote_idx === -1) {
577
- end_tokens_to_len(p, p.blockquote_idx)
578
- p.blockquote_idx += 1
579
- p.fence_start = 0
580
- add_token(p, BLOCKQUOTE)
581
- } else {
582
- p.blockquote_idx = next_blockquote_idx
583
- }
584
-
585
- clear_root_pending(p)
586
- p.pending = char
587
- continue
588
- }
589
- /* Horizontal Rule
590
- "-- - --- - --"
591
- */
592
- case '-':
593
- case '*':
594
- case '_':
595
- if (p.hr_chars === 0) {
596
- console.assert(p.pending.length === 1, "Pending should be one character")
597
- p.hr_chars = 1
598
- p.hr_char = p.pending
599
- }
600
-
601
- if (p.hr_chars > 0) {
602
- switch (char) {
603
- case p.hr_char:
604
- p.hr_chars += 1
605
- p.pending = pending_with_char
606
- continue
607
- case ' ':
608
- p.pending = pending_with_char
609
- continue
610
- case '\n':
611
- if (p.hr_chars < 3) break
612
- end_tokens_to_indent(p, p.indent_len)
613
- p.renderer.add_token(p.renderer.data, RULE)
614
- p.renderer.end_token(p.renderer.data)
615
- clear_root_pending(p)
616
- p.hr_chars = 0
617
- continue
618
- }
619
-
620
- p.hr_chars = 0
621
- }
622
-
623
- /* Unordered list
624
- / * foo
625
- / * *bar*
626
- / * **baz**
627
- /*/
628
- if ('_' !== p.pending[0] &&
629
- ' ' === p.pending[1]
630
- ) {
631
- continue_or_add_list(p, LIST_UNORDERED)
632
- add_list_item(p, 2)
633
- parser_write(p, pending_with_char.slice(2))
634
- continue
635
- }
636
-
637
- break // fail
638
- /* Code Fence */
639
- case '`':
640
- /* ``?
641
- ^
642
- */
643
- if (p.pending.length < 3) {
644
- if ('`' === char) {
645
- p.pending = pending_with_char
646
- p.fence_start = pending_with_char.length
647
- continue
648
- }
649
- p.fence_start = 0
650
- break // fail
651
- }
652
-
653
- switch (char) {
654
- case '`':
655
- /* ````?
656
- ^
657
- */
658
- if (p.pending.length === p.fence_start) {
659
- p.pending = pending_with_char
660
- p.fence_start = pending_with_char.length
661
- }
662
- /* ```code`
663
- ^
664
- */
665
- else {
666
- add_token(p, PARAGRAPH)
667
- clear_root_pending(p)
668
- p.fence_start = 0
669
- parser_write(p, pending_with_char)
670
- }
671
- continue
672
- case '\n': {
673
- /* ```lang\n
674
- ^
675
- */
676
- end_tokens_to_indent(p, p.indent_len)
677
-
678
- add_token(p, CODE_FENCE)
679
- if (p.pending.length > p.fence_start) {
680
- p.renderer.set_attr(p.renderer.data, LANG, p.pending.slice(p.fence_start))
681
- }
682
- clear_root_pending(p)
683
- p.token = NEWLINE
684
- continue
685
- }
686
- default:
687
- /* ```lang\n
688
- ^
689
- */
690
- p.pending = pending_with_char
691
- continue
692
- }
693
- /*
694
- List Unordered for '+'
695
- The other list types are handled with HORIZONTAL_RULE
696
- */
697
- case '+':
698
- if (' ' !== char) break // fail
699
-
700
- continue_or_add_list(p, LIST_UNORDERED)
701
- add_list_item(p, 2)
702
- continue
703
- /* List Ordered */
704
- case '0': case '1': case '2': case '3': case '4':
705
- case '5': case '6': case '7': case '8': case '9':
706
- /*
707
- 12. foo
708
- ^
709
- */
710
- if ('.' === p.pending[p.pending.length-1]) {
711
- if (' ' !== char) break // fail
712
-
713
- if (continue_or_add_list(p, LIST_ORDERED) && p.pending !== "1.") {
714
- p.renderer.set_attr(p.renderer.data, START, p.pending.slice(0, -1))
715
- }
716
- add_list_item(p, p.pending.length+1)
717
- continue
718
- } else {
719
- const char_code = char.charCodeAt(0)
720
- if (46 === char_code || // '.'
721
- is_digit(char_code) // 0-9
722
- ) {
723
- p.pending = pending_with_char
724
- continue
725
- }
726
- }
727
- break // fail
728
- /* Table */
729
- case '|':
730
- end_tokens_to_len(p, p.blockquote_idx)
731
-
732
- add_token(p, TABLE)
733
- add_token(p, TABLE_ROW)
734
-
735
- p.pending = ""
736
- parser_write(p, char)
737
-
738
- continue
739
- }
740
-
741
- let to_write = pending_with_char
742
-
743
- /* Add a line break and continue in previous token */
744
- if (p.token === LINE_BREAK) {
745
- p.token = p.tokens[p.len]
746
- p.renderer.add_token(p.renderer.data, LINE_BREAK)
747
- p.renderer.end_token(p.renderer.data)
748
- }
749
- /* Code Block */
750
- else if (p.indent_len >= 4) {
751
- /*
752
- Case where there are additional spaces
753
- after the indent that makes the code block
754
- _________________________
755
- code
756
- ^^^^----indent
757
- ^^^-part of code
758
- _________________________
759
- \t code
760
- ^^-----indent
761
- ^^^-part of code
762
- */
763
- let code_start = 0
764
- for (; code_start < 4; code_start += 1) {
765
- if (p.indent[code_start] === '\t') {
766
- code_start = code_start+1
767
- break
768
- }
769
- }
770
- to_write = p.indent.slice(code_start) + pending_with_char
771
- add_token(p, CODE_BLOCK)
772
- }
773
- /* Paragraph */
774
- else {
775
- add_token(p, PARAGRAPH)
776
- }
777
-
778
- clear_root_pending(p)
779
- parser_write(p, to_write)
780
- continue
781
- case TABLE:
782
- if (p.table_state === 1) {
783
- switch (char) {
784
- case '-':
785
- case ' ':
786
- case '|':
787
- case ':':
788
- p.pending = pending_with_char
789
- continue
790
- case '\n':
791
- p.table_state = 2
792
- p.pending = ""
793
- continue
794
- default:
795
- end_token(p)
796
- p.table_state = 0
797
- break
798
- }
799
- } else {
800
- switch (p.pending) {
801
- case "|":
802
- add_token(p, TABLE_ROW)
803
- p.pending = ""
804
- parser_write(p, char)
805
- continue
806
- case "\n":
807
- end_token(p)
808
- p.pending = ""
809
- p.table_state = 0
810
- parser_write(p, char)
811
- continue
812
- }
813
- }
814
- break
815
- case TABLE_ROW:
816
- switch (p.pending) {
817
- case "":
818
- break
819
- case "|":
820
- add_token(p, TABLE_CELL)
821
- end_token(p)
822
- p.pending = ""
823
- parser_write(p, char)
824
- continue
825
- case "\n":
826
- end_token(p)
827
- p.table_state = Math.min(p.table_state+1, 2)
828
- p.pending = ""
829
- parser_write(p, char)
830
- continue
831
- default:
832
- add_token(p, TABLE_CELL)
833
- parser_write(p, char)
834
- continue
835
- }
836
- break
837
- case TABLE_CELL:
838
- if (p.pending === "|") {
839
- add_text(p)
840
- end_token(p)
841
- p.pending = ""
842
- parser_write(p, char)
843
- continue
844
- }
845
- break
846
- case CODE_BLOCK:
847
- switch (pending_with_char) {
848
- case "\n ":
849
- case "\n \t":
850
- case "\n \t":
851
- case "\n \t":
852
- case "\n\t":
853
- p.text += "\n"
854
- p.pending = ""
855
- continue
856
- case "\n":
857
- case "\n ":
858
- case "\n ":
859
- case "\n ":
860
- p.pending = pending_with_char
861
- continue
862
- default:
863
- if (p.pending.length !== 0) {
864
- add_text(p)
865
- end_token(p)
866
- p.pending = char
867
- } else {
868
- p.text += char
869
- }
870
- continue
871
- }
872
- case CODE_FENCE:
873
- switch (char) {
874
- case '`':
875
- /* ```\n<code>\n``??
876
- | ^
877
- */
878
- p.pending = pending_with_char
879
- continue
880
- case '\n':
881
- /* ```\n<code>\n```\n
882
- | ^
883
- */
884
- if (pending_with_char.length === p.fence_start + p.fence_end + 1) {
885
- add_text(p)
886
- end_token(p)
887
- p.pending = ""
888
- p.fence_start = 0
889
- p.fence_end = 0
890
- p.token = NEWLINE
891
- continue
892
- }
893
- p.token = NEWLINE
894
- break
895
- case ' ':
896
- /* ```\n<code>\n ??
897
- | ^ (space after newline is allowed)
898
- */
899
- if (p.pending[0] === '\n') {
900
- p.pending = pending_with_char
901
- p.fence_end += 1
902
- continue
903
- }
904
- break
905
- }
906
- // any other char
907
- p.text += p.pending
908
- p.pending = char
909
- p.fence_end = 1
910
- continue
911
- case CODE_INLINE:
912
- switch (char) {
913
- case '`':
914
- if (pending_with_char.length ===
915
- p.fence_start + Number(p.pending[0] === ' ') // 0 or 1 for space
916
- ) {
917
- add_text(p)
918
- end_token(p)
919
- p.pending = ""
920
- p.fence_start = 0
921
- } else {
922
- p.pending = pending_with_char
923
- }
924
- continue
925
- case '\n':
926
- p.text += p.pending
927
- p.pending = ""
928
- p.token = LINE_BREAK
929
- p.blockquote_idx = 0
930
- add_text(p)
931
- continue
932
- /* Trim space before ` */
933
- case ' ':
934
- p.text += p.pending
935
- p.pending = char
936
- continue
937
- default:
938
- p.text += pending_with_char
939
- p.pending = ""
940
- continue
941
- }
942
- /* Checkboxes */
943
- case MAYBE_TASK:
944
- switch (p.pending.length) {
945
- case 0:
946
- if ('[' !== char) break // fail
947
- p.pending = pending_with_char
948
- continue
949
- case 1:
950
- if (' ' !== char && 'x' !== char) break // fail
951
- p.pending = pending_with_char
952
- continue
953
- case 2:
954
- if (']' !== char) break // fail
955
- p.pending = pending_with_char
956
- continue
957
- case 3:
958
- if (' ' !== char) break // fail
959
- p.renderer.add_token(p.renderer.data, CHECKBOX)
960
- if ('x' === p.pending[1]) {
961
- p.renderer.set_attr(p.renderer.data, CHECKED, "")
962
- }
963
- p.renderer.end_token(p.renderer.data)
964
- p.pending = " "
965
- continue
966
- }
967
-
968
- p.token = p.tokens[p.len]
969
- p.pending = ""
970
- parser_write(p, pending_with_char)
971
- continue
972
- case STRONG_AST:
973
- case STRONG_UND: {
974
- /** @type {string} */ let symbol = '*'
975
- /** @type {Token } */ let italic = ITALIC_AST
976
- if (p.token === STRONG_UND) {
977
- symbol = '_'
978
- italic = ITALIC_UND
979
- }
980
-
981
- if (symbol === p.pending) {
982
- add_text(p)
983
- /* **Bold**
984
- ^
985
- */
986
- if (symbol === char) {
987
- end_token(p)
988
- p.pending = ""
989
- continue
990
- }
991
- /* **Bold*Bold->Em*
992
- ^
993
- */
994
- add_token(p, italic)
995
- p.pending = char
996
- continue
997
- }
998
-
999
- break
1000
- }
1001
- case ITALIC_AST:
1002
- case ITALIC_UND: {
1003
- /** @type {string} */ let symbol = '*'
1004
- /** @type {Token } */ let strong = STRONG_AST
1005
- if (p.token === ITALIC_UND) {
1006
- symbol = '_'
1007
- strong = STRONG_UND
1008
- }
1009
-
1010
- switch (p.pending) {
1011
- case symbol:
1012
- if (symbol === char) {
1013
- /* Decide between ***bold>em**em* and **bold*bold>em***
1014
- ^ ^
1015
- With the help of the next character
1016
- */
1017
- if (p.tokens[p.len-1] === strong) {
1018
- p.pending = pending_with_char
1019
- }
1020
- /* *em**bold
1021
- ^
1022
- */
1023
- else {
1024
- add_text(p)
1025
- add_token(p, strong)
1026
- p.pending = ""
1027
- }
1028
- }
1029
- /* *em*foo
1030
- ^
1031
- */
1032
- else {
1033
- add_text(p)
1034
- end_token(p)
1035
- p.pending = char
1036
- }
1037
- continue
1038
- case symbol+symbol:
1039
- const italic = p.token
1040
- add_text(p)
1041
- end_token(p)
1042
- end_token(p)
1043
- /* ***bold>em**em* or **bold*bold>em***
1044
- ^ ^
1045
- */
1046
- if (symbol !== char) {
1047
- add_token(p, italic)
1048
- p.pending = char
1049
- } else {
1050
- p.pending = ""
1051
- }
1052
- continue
1053
- }
1054
- break
1055
- }
1056
- case STRIKE:
1057
- if ("~~" === pending_with_char) {
1058
- add_text(p)
1059
- end_token(p)
1060
- p.pending = ""
1061
- continue
1062
- }
1063
- break
1064
- case MAYBE_EQ_BLOCK:
1065
- /*
1066
- \[? or $$?
1067
- ^ ^
1068
- */
1069
- if (char === '\n') {
1070
- add_text(p)
1071
- add_token(p, EQUATION_BLOCK)
1072
- p.pending = ""
1073
- } else {
1074
- p.token = p.tokens[p.len]
1075
- if (p.pending[0] === '\\') {
1076
- p.text += '['
1077
- } else {
1078
- p.text += '$$'
1079
- }
1080
- p.pending = ""
1081
- parser_write(p, char)
1082
- }
1083
- continue
1084
- case EQUATION_BLOCK:
1085
- if ("\\]" === pending_with_char || "$$" === pending_with_char) {
1086
- add_text(p)
1087
- end_token(p)
1088
- p.pending = ""
1089
- continue
1090
- }
1091
- break
1092
- case EQUATION_INLINE:
1093
- if ("\\)" === pending_with_char || "$" === p.pending[0]) {
1094
- add_text(p)
1095
- end_token(p)
1096
-
1097
- if(char === ')'){
1098
- p.pending = ""
1099
- } else {
1100
- p.pending = char
1101
- }
1102
- continue
1103
- }
1104
- break
1105
- /* Raw URLs */
1106
- case MAYBE_URL:
1107
- if ("http://" === pending_with_char ||
1108
- "https://" === pending_with_char
1109
- ) {
1110
- add_text(p)
1111
- add_token(p, RAW_URL)
1112
- p.pending = pending_with_char
1113
- p.text = pending_with_char
1114
- }
1115
- else
1116
- if ("http:/" [p.pending.length] === char ||
1117
- "https:/"[p.pending.length] === char
1118
- ) {
1119
- p.pending = pending_with_char
1120
- }
1121
- else {
1122
- p.token = p.tokens[p.len]
1123
- parser_write(p, char)
1124
- }
1125
- continue
1126
- case LINK:
1127
- case IMAGE:
1128
- if ("]" === p.pending) {
1129
- /*
1130
- [Link](url)
1131
- ^
1132
- */
1133
- add_text(p)
1134
- if ('(' === char) {
1135
- p.pending = pending_with_char
1136
- } else {
1137
- end_token(p)
1138
- p.pending = char
1139
- }
1140
- continue
1141
- }
1142
- if (']' === p.pending[0] &&
1143
- '(' === p.pending[1]
1144
- ) {
1145
- /*
1146
- [Link](url)
1147
- ^
1148
- */
1149
- if (')' === char) {
1150
- const type = p.token === LINK ? HREF : SRC
1151
- const url = p.pending.slice(2)
1152
- p.renderer.set_attr(p.renderer.data, type, url)
1153
- end_token(p)
1154
- p.pending = ""
1155
- } else {
1156
- p.pending += char
1157
- }
1158
- continue
1159
- }
1160
- break
1161
- case RAW_URL:
1162
- /* http://example.com?
1163
- ^
1164
- */
1165
- if (' ' === char ||
1166
- '\n'=== char ||
1167
- '\\'=== char
1168
- ) {
1169
- p.renderer.set_attr(p.renderer.data, HREF, p.pending)
1170
- add_text(p)
1171
- end_token(p)
1172
- p.pending = char
1173
- } else {
1174
- p.text += char
1175
- p.pending = pending_with_char
1176
- }
1177
- continue
1178
- case MAYBE_BR:
1179
- if (pending_with_char.startsWith("<br")) {
1180
- if (/* "<br" */
1181
- pending_with_char.length === 3 ||
1182
- /* "<br " */
1183
- char === ' ' ||
1184
- /* "<br/" | "<br /" */
1185
- char === '/' && (pending_with_char.length === 4 ||
1186
- p.pending[p.pending.length-1] === ' ')
1187
- ) {
1188
- p.pending = pending_with_char
1189
- continue
1190
- }
1191
-
1192
- /* "<br>" | "<br/>" */
1193
- if (char === '>') {
1194
- add_text(p)
1195
- p.token = p.tokens[p.len]
1196
- p.renderer.add_token(p.renderer.data, LINE_BREAK)
1197
- p.renderer.end_token(p.renderer.data)
1198
- p.pending = ""
1199
- continue
1200
- }
1201
- }
1202
- // Fail
1203
- p.token = p.tokens[p.len]
1204
- p.text += '<'
1205
- p.pending = p.pending.slice(1)
1206
- parser_write(p, char)
1207
- continue
1208
- }
1209
-
1210
- /*
1211
- Common checks
1212
- */
1213
- switch (p.pending[0]) {
1214
- /* Escape character */
1215
- case '\\':
1216
- if (p.token === IMAGE ||
1217
- p.token === EQUATION_BLOCK ||
1218
- p.token === EQUATION_INLINE)
1219
- break
1220
-
1221
- switch (char) {
1222
- case '(':
1223
- add_text(p)
1224
- add_token(p, EQUATION_INLINE)
1225
- p.pending = ""
1226
- continue
1227
- case '[':
1228
- p.token = MAYBE_EQ_BLOCK
1229
- p.pending = pending_with_char
1230
- continue
1231
- case '\n':
1232
- // Escaped newline has the same affect as unescaped one
1233
- p.pending = char
1234
- continue
1235
- default:
1236
- let charcode = char.charCodeAt(0)
1237
- p.pending = ""
1238
- p.text += is_digit(charcode) || // 0-9
1239
- (charcode >= 65 && charcode <= 90) || // A-Z
1240
- (charcode >= 97 && charcode <= 122) // a-z
1241
- ? pending_with_char
1242
- : char
1243
- continue
1244
- }
1245
- /* Newline */
1246
- case '\n':
1247
- switch (p.token) {
1248
- case IMAGE:
1249
- case EQUATION_BLOCK:
1250
- case EQUATION_INLINE:
1251
- break
1252
- case HEADING_1:
1253
- case HEADING_2:
1254
- case HEADING_3:
1255
- case HEADING_4:
1256
- case HEADING_5:
1257
- case HEADING_6:
1258
- add_text(p)
1259
- end_tokens_to_len(p, p.blockquote_idx)
1260
- p.blockquote_idx = 0
1261
- p.pending = char
1262
- continue
1263
- default:
1264
- add_text(p)
1265
- p.pending = char
1266
- p.token = LINE_BREAK
1267
- p.blockquote_idx = 0
1268
- continue
1269
- }
1270
- break
1271
- /* <br> */
1272
- case '<':
1273
- if (p.token !== IMAGE &&
1274
- p.token !== EQUATION_BLOCK &&
1275
- p.token !== EQUATION_INLINE
1276
- ) {
1277
- add_text(p)
1278
- p.pending = pending_with_char
1279
- p.token = MAYBE_BR
1280
- continue
1281
- }
1282
- break
1283
- /* `Code Inline` */
1284
- case '`':
1285
- if (p.token === IMAGE) break
1286
-
1287
- if ('`' === char) {
1288
- p.fence_start += 1
1289
- p.pending = pending_with_char
1290
- } else {
1291
- p.fence_start += 1 // started at 0, and first wasn't counted
1292
- add_text(p)
1293
- add_token(p, CODE_INLINE)
1294
- p.text = ' ' === char || '\n' === char ? "" : char // trim leading space
1295
- p.pending = ""
1296
- }
1297
- continue
1298
- case '_':
1299
- case '*': {
1300
- if (p.token === IMAGE ||
1301
- p.token === EQUATION_BLOCK ||
1302
- p.token === EQUATION_INLINE ||
1303
- p.token === STRONG_AST)
1304
- break
1305
-
1306
- /** @type {Token} */ let italic = ITALIC_AST
1307
- /** @type {Token} */ let strong = STRONG_AST
1308
- const symbol = p.pending[0]
1309
- if ('_' === symbol) {
1310
- italic = ITALIC_UND
1311
- strong = STRONG_UND
1312
- }
1313
-
1314
- if (p.pending.length === 1) {
1315
- /* **Strong**
1316
- ^
1317
- */
1318
- if (symbol === char) {
1319
- p.pending = pending_with_char
1320
- continue
1321
- }
1322
- /* *Em*
1323
- ^
1324
- */
1325
- if (' ' !== char && '\n' !== char) {
1326
- add_text(p)
1327
- add_token(p, italic)
1328
- p.pending = char
1329
- continue
1330
- }
1331
- } else {
1332
- /* ***Strong->Em***
1333
- ^
1334
- */
1335
- if (symbol === char) {
1336
- add_text(p)
1337
- add_token(p, strong)
1338
- add_token(p, italic)
1339
- p.pending = ""
1340
- continue
1341
- }
1342
- /* **Strong**
1343
- ^
1344
- */
1345
- if (' ' !== char && '\n' !== char) {
1346
- add_text(p)
1347
- add_token(p, strong)
1348
- p.pending = char
1349
- continue
1350
- }
1351
- }
1352
-
1353
- break
1354
- }
1355
- case '~':
1356
- if (p.token !== IMAGE &&
1357
- p.token !== STRIKE
1358
- ) {
1359
- if ("~" === p.pending) {
1360
- /* ~~Strike~~
1361
- ^
1362
- */
1363
- if ('~' === char) {
1364
- p.pending = pending_with_char
1365
- continue
1366
- }
1367
- } else {
1368
- /* ~~Strike~~
1369
- | ^
1370
- */
1371
- if (' ' !== char && '\n' !== char) {
1372
- add_text(p)
1373
- add_token(p, STRIKE)
1374
- p.pending = char
1375
- continue
1376
- }
1377
- }
1378
- }
1379
- break
1380
- /* $eq$ | $$eq$$ */
1381
- case '$':
1382
- if (p.token !== IMAGE &&
1383
- p.token !== STRIKE &&
1384
- "$" === p.pending
1385
- ) {
1386
- /* $$EQUATION_BLOCK$$
1387
- ^
1388
- */
1389
- if ('$' === char) {
1390
- p.token = MAYBE_EQ_BLOCK
1391
- p.pending = pending_with_char
1392
- continue
1393
- }
1394
- /* $123
1395
- ^
1396
- */
1397
- else if (is_delimeter_or_number(char.charCodeAt(0))) {
1398
- break
1399
- }
1400
- /* $EQUATION_INLINE$
1401
- ^
1402
- */
1403
- else {
1404
- add_text(p)
1405
- add_token(p, EQUATION_INLINE)
1406
- p.pending = char
1407
- continue
1408
- }
1409
- }
1410
- break
1411
- /* [Image](url) */
1412
- case '[':
1413
- if (p.token !== IMAGE &&
1414
- p.token !== LINK &&
1415
- p.token !== EQUATION_BLOCK &&
1416
- p.token !== EQUATION_INLINE &&
1417
- ']' !== char
1418
- ) {
1419
- add_text(p)
1420
- add_token(p, LINK)
1421
- p.pending = char
1422
- continue
1423
- }
1424
- break
1425
- /* ![Image](url) */
1426
- case '!':
1427
- if (!(p.token === IMAGE) &&
1428
- '[' === char
1429
- ) {
1430
- add_text(p)
1431
- add_token(p, IMAGE)
1432
- p.pending = ""
1433
- continue
1434
- }
1435
- break
1436
- /* Trim spaces */
1437
- case ' ':
1438
- if (p.pending.length === 1 && ' ' === char) {
1439
- continue
1440
- }
1441
- break
1442
- }
1443
-
1444
- /* foo http://...
1445
- | ^
1446
- */
1447
- if (p.token !== IMAGE &&
1448
- p.token !== LINK &&
1449
- p.token !== EQUATION_BLOCK &&
1450
- p.token !== EQUATION_INLINE &&
1451
- 'h' === char &&
1452
- (" " === p.pending ||
1453
- "" === p.pending)
1454
- ) {
1455
- p.text += p.pending
1456
- p.pending = char
1457
-
1458
- p.token = MAYBE_URL
1459
- continue
1460
- }
1461
-
1462
- /*
1463
- No check hit
1464
- */
1465
- p.text += p.pending
1466
- p.pending = char
1467
- }
1468
-
1469
- add_text(p)
1470
- }
1471
-
1472
-
1473
- /**
1474
- * @template T
1475
- * @callback Renderer_Add_Token
1476
- * @param {T } data
1477
- * @param {Token} type
1478
- * @returns {void } */
1479
-
1480
- /**
1481
- * @template T
1482
- * @callback Renderer_End_Token
1483
- * @param {T } data
1484
- * @returns {void } */
1485
-
1486
- /**
1487
- * @template T
1488
- * @callback Renderer_Add_Text
1489
- * @param {T } data
1490
- * @param {string} text
1491
- * @returns {void } */
1492
-
1493
- /**
1494
- * @template T
1495
- * @callback Renderer_Set_Attr
1496
- * @param {T } data
1497
- * @param {Attr } type
1498
- * @param {string} value
1499
- * @returns {void } */
1500
-
1501
- /**
1502
- * The renderer interface.
1503
- * @template T
1504
- * @typedef {object } Renderer
1505
- * @property {T } data User data object. Available as first param in callbacks.
1506
- * @property {Renderer_Add_Token<T>} add_token When the tokens starts.
1507
- * @property {Renderer_End_Token<T>} end_token When the token ends.
1508
- * @property {Renderer_Add_Text <T>} add_text To append text to current token. Can be called multiple times or none.
1509
- * @property {Renderer_Set_Attr <T>} set_attr Set additional attributes of current token eg. the link url.
1510
- */
1511
-
1512
- /** @typedef {Renderer<any>} Any_Renderer */
1513
-
1514
-
1515
- /**
1516
- * @typedef {object} Default_Renderer_Data
1517
- * @property {HTMLElement[]} nodes
1518
- * @property {number } index
1519
- *
1520
- * @typedef {Renderer <Default_Renderer_Data>} Default_Renderer
1521
- * @typedef {Renderer_Add_Token<Default_Renderer_Data>} Default_Renderer_Add_Token
1522
- * @typedef {Renderer_End_Token<Default_Renderer_Data>} Default_Renderer_End_Token
1523
- * @typedef {Renderer_Add_Text <Default_Renderer_Data>} Default_Renderer_Add_Text
1524
- * @typedef {Renderer_Set_Attr <Default_Renderer_Data>} Default_Renderer_Set_Attr
1525
- */
1526
-
1527
- /**
1528
- * @param {HTMLElement } root
1529
- * @returns {Default_Renderer} */
1530
- export function default_renderer(root) {
1531
- return {
1532
- add_token: default_add_token,
1533
- end_token: default_end_token,
1534
- add_text: default_add_text,
1535
- set_attr: default_set_attr,
1536
- data : {
1537
- nodes: /**@type {*}*/([root,,,,,]),
1538
- index: 0,
1539
- },
1540
- }
1541
- }
1542
-
1543
- /** @type {Default_Renderer_Add_Token} */
1544
- export function default_add_token(data, type) {
1545
-
1546
- /**@type {Element}*/ let parent = data.nodes[data.index]
1547
-
1548
- /**@type {HTMLElement}*/ let slot
1549
-
1550
- switch (type) {
1551
- case DOCUMENT: return // document is provided
1552
- case BLOCKQUOTE: slot = document.createElement("blockquote");break
1553
- case PARAGRAPH: slot = document.createElement("p") ;break
1554
- case LINE_BREAK: slot = document.createElement("br") ;break
1555
- case RULE: slot = document.createElement("hr") ;break
1556
- case HEADING_1: slot = document.createElement("h1") ;break
1557
- case HEADING_2: slot = document.createElement("h2") ;break
1558
- case HEADING_3: slot = document.createElement("h3") ;break
1559
- case HEADING_4: slot = document.createElement("h4") ;break
1560
- case HEADING_5: slot = document.createElement("h5") ;break
1561
- case HEADING_6: slot = document.createElement("h6") ;break
1562
- case ITALIC_AST:
1563
- case ITALIC_UND: slot = document.createElement("em") ;break
1564
- case STRONG_AST:
1565
- case STRONG_UND: slot = document.createElement("strong") ;break
1566
- case STRIKE: slot = document.createElement("s") ;break
1567
- case CODE_INLINE: slot = document.createElement("code") ;break
1568
- case RAW_URL:
1569
- case LINK: slot = document.createElement("a") ;break
1570
- case IMAGE: slot = document.createElement("img") ;break
1571
- case LIST_UNORDERED:slot = document.createElement("ul") ;break
1572
- case LIST_ORDERED: slot = document.createElement("ol") ;break
1573
- case LIST_ITEM: slot = document.createElement("li") ;break
1574
- case CHECKBOX:
1575
- let checkbox = slot = document.createElement("input")
1576
- checkbox.type = "checkbox"
1577
- checkbox.disabled = true
1578
- break
1579
- case CODE_BLOCK:
1580
- case CODE_FENCE:
1581
- parent = parent.appendChild(document.createElement("pre"))
1582
- slot = document.createElement("code")
1583
- break
1584
- case TABLE:
1585
- slot = document.createElement("table")
1586
- break
1587
- case TABLE_ROW:
1588
- switch (parent.children.length) {
1589
- case 0:
1590
- parent = parent.appendChild(document.createElement("thead"))
1591
- break
1592
- case 1:
1593
- parent = parent.appendChild(document.createElement("tbody"))
1594
- break
1595
- default:
1596
- parent = parent.children[1]
1597
- }
1598
- slot = document.createElement("tr")
1599
- break
1600
- case TABLE_CELL:
1601
- slot = document.createElement(parent.parentElement?.tagName === "THEAD" ? "th" : "td")
1602
- break
1603
- case EQUATION_BLOCK: slot = document.createElement("equation-block"); break
1604
- case EQUATION_INLINE: slot = document.createElement("equation-inline"); break
1605
- }
1606
-
1607
- data.nodes[++data.index] = parent.appendChild(slot)
1608
- }
1609
-
1610
- /** @type {Default_Renderer_End_Token} */
1611
- export function default_end_token(data) {
1612
- data.index -= 1
1613
- }
1614
-
1615
- /** @type {Default_Renderer_Add_Text} */
1616
- export function default_add_text(data, text) {
1617
- data.nodes[data.index].appendChild(document.createTextNode(text))
1618
- }
1619
-
1620
- /** @type {Default_Renderer_Set_Attr} */
1621
- export function default_set_attr(data, type, value) {
1622
- data.nodes[data.index].setAttribute(attr_to_html_attr(type), value)
1623
- }
1624
-
1625
-
1626
- /**
1627
- * @typedef {undefined} Logger_Renderer_Data
1628
- *
1629
- * @typedef {Renderer <Logger_Renderer_Data>} Logger_Renderer
1630
- * @typedef {Renderer_Add_Token<Logger_Renderer_Data>} Logger_Renderer_Add_Token
1631
- * @typedef {Renderer_End_Token<Logger_Renderer_Data>} Logger_Renderer_End_Token
1632
- * @typedef {Renderer_Add_Text <Logger_Renderer_Data>} Logger_Renderer_Add_Text
1633
- * @typedef {Renderer_Set_Attr <Logger_Renderer_Data>} Logger_Renderer_Set_Attr
1634
- */
1635
-
1636
- /** @returns {Logger_Renderer} */
1637
- export function logger_renderer() {
1638
- return {
1639
- data: undefined,
1640
- add_token: logger_add_token,
1641
- end_token: logger_end_token,
1642
- add_text: logger_add_text,
1643
- set_attr: logger_set_attr,
1644
- }
1645
- }
1646
-
1647
- /** @type {Logger_Renderer_Add_Token} */
1648
- export function logger_add_token(data, type) {
1649
- console.log("add_token:", token_to_string(type))
1650
- }
1651
-
1652
- /** @type {Logger_Renderer_End_Token} */
1653
- export function logger_end_token(data) {
1654
- console.log("end_token")
1655
- }
1656
-
1657
- /** @type {Logger_Renderer_Add_Text} */
1658
- export function logger_add_text(data, text) {
1659
- console.log('add_text: "%s"', text)
1660
- }
1661
-
1662
- /** @type {Logger_Renderer_Set_Attr} */
1663
- export function logger_set_attr(data, type, value) {
1664
- console.log('set_attr: %s="%s"', attr_to_html_attr(type), value)
1665
- }
1
+ export const DOCUMENT=1,PARAGRAPH=2,HEADING_1=3,HEADING_2=4,HEADING_3=5,HEADING_4=6,HEADING_5=7,HEADING_6=8,CODE_BLOCK=9,CODE_FENCE=10,CODE_INLINE=11,ITALIC_AST=12,ITALIC_UND=13,STRONG_AST=14,STRONG_UND=15,STRIKE=16,LINK=17,RAW_URL=18,IMAGE=19,BLOCKQUOTE=20,LINE_BREAK=21,RULE=22,LIST_UNORDERED=23,LIST_ORDERED=24,LIST_ITEM=25,CHECKBOX=26,TABLE=27,TABLE_ROW=28,TABLE_CELL=29,EQUATION_BLOCK=30,EQUATION_INLINE=31,NEWLINE=101,MAYBE_URL=102,MAYBE_TASK=103,MAYBE_BR=104,MAYBE_EQ_BLOCK=105,Token={Document:1,Blockquote:20,Paragraph:2,Heading_1:3,Heading_2:4,Heading_3:5,Heading_4:6,Heading_5:7,Heading_6:8,Code_Block:9,Code_Fence:10,Code_Inline:11,Italic_Ast:12,Italic_Und:13,Strong_Ast:14,Strong_Und:15,Strike:16,Link:17,Raw_URL:18,Image:19,Line_Break:21,Rule:22,List_Unordered:23,List_Ordered:24,List_Item:25,Checkbox:26,Table:27,Table_Row:28,Table_Cell:29,Equation_Block:30,Equation_Inline:31};export function token_to_string(e){switch(e){case 1:return"Document";case 20:return"Blockquote";case 2:return"Paragraph";case 3:return"Heading_1";case 4:return"Heading_2";case 5:return"Heading_3";case 6:return"Heading_4";case 7:return"Heading_5";case 8:return"Heading_6";case 9:return"Code_Block";case 10:return"Code_Fence";case 11:return"Code_Inline";case 12:return"Italic_Ast";case 13:return"Italic_Und";case 14:return"Strong_Ast";case 15:return"Strong_Und";case 16:return"Strike";case 17:return"Link";case 18:return"Raw URL";case 19:return"Image";case 21:return"Line_Break";case 22:return"Rule";case 23:return"List_Unordered";case 24:return"List_Ordered";case 25:return"List_Item";case 26:return"Checkbox";case 27:return"Table";case 28:return"Table_Row";case 29:return"Table_Cell";case 30:return"Equation_Block";case 31:return"Equation_Inline"}}export const HREF=1,SRC=2,LANG=4,CHECKED=8,START=16,Attr={Href:HREF,Src:SRC,Lang:LANG,Checked:CHECKED,Start:START};export function attr_to_html_attr(e){switch(e){case HREF:return"href";case SRC:return"src";case LANG:return"class";case CHECKED:return"checked";case START:return"start"}}export const level_to_heading=e=>{switch(e){case 1:return 3;case 2:return 4;case 3:return 5;case 4:return 6;case 5:return 7;default:return 8}},heading_from_level=level_to_heading,heading_to_level=e=>{switch(e){case 3:return 1;case 4:return 2;case 5:return 3;case 6:return 4;case 7:return 5;case 8:return 6;default:return 0}};const A=24;export function parser(e){const c=new Uint32Array(A);return c[0]=1,{renderer:e,text:"",pending:"",tokens:c,len:0,token:1,fence_end:0,blockquote_idx:0,hr_char:"",hr_chars:0,fence_start:0,spaces:new Uint8Array(A),indent:"",indent_len:0,table_state:0}}export function parser_end(e){e.pending.length>0&&parser_write(e,`
2
+ `)}function a(e){e.text.length!==0&&(console.assert(e.len>0,"Never adding text to root"),e.renderer.add_text(e.renderer.data,e.text),e.text="")}function N(e){switch(e.token){case 21:case 1:case 20:case 24:case 23:i(e,2)}}function O(e,c){N(e),e.text+=c}function o(e){console.assert(e.len>0,"No nodes to end"),e.len-=1,e.token=e.tokens[e.len],e.renderer.end_token(e.renderer.data)}function i(e,c){(e.tokens[e.len]===24||e.tokens[e.len]===23)&&c!==25&&o(e),e.len+=1,e.tokens[e.len]=c,e.token=c,e.renderer.add_token(e.renderer.data,c)}function L(e,c,n){for(;n<=e.len;){if(e.tokens[n]===c)return n;n+=1}return-1}function E(e,c){for(e.fence_start=0;e.len>c;)o(e)}function l(e,c){let n=0;for(let t=0;t<=e.len&&(c-=e.spaces[t],!(c<0));t+=1)switch(e.tokens[t]){case 9:case 10:case 20:case 25:n=t;break}for(;e.len>n;)o(e);return c}function I(e,c){let n=-1,t=-1;for(let s=e.blockquote_idx+1;s<=e.len;s+=1)if(e.tokens[s]===25){if(e.indent_len<e.spaces[s]){t=-1;break}t=s}else e.tokens[s]===c&&(n=s);return t===-1?n===-1?(E(e,e.blockquote_idx),i(e,c),!0):(E(e,n),!1):(E(e,t),i(e,c),!0)}function g(e,c){i(e,25),e.spaces[e.len]=e.indent_len+c,_(e),e.token=103}function _(e){e.indent="",e.indent_len=0,e.pending=""}function u(e){switch(e){case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return!0;default:return!1}}function f(e){switch(e){case 32:case 58:case 59:case 41:case 44:case 33:case 46:case 63:case 93:case 10:return!0;default:return!1}}function T(e){return u(e)||f(e)}function R(e){return u(e)||e>=65&&e<=90||e>=97&&e<=122}export function parser_write(e,c){for(const n of c){if(e.token===101){switch(n){case" ":e.indent_len+=1;continue;case" ":e.indent_len+=4;continue}let s=l(e,e.indent_len);e.indent_len=0,e.token=e.tokens[e.len],s>0&&parser_write(e," ".repeat(s))}const t=e.pending+n;switch(e.token){case 21:case 1:case 20:case 24:case 23:switch(console.assert(e.text.length===0,"Root should not have any text"),e.pending[0]){case void 0:e.pending=n;continue;case" ":console.assert(e.pending.length===1),e.pending=n,e.indent+=" ",e.indent_len+=1;continue;case" ":console.assert(e.pending.length===1),e.pending=n,e.indent+=" ",e.indent_len+=4;continue;case`
3
+ `:if(console.assert(e.pending.length===1),e.tokens[e.len]===25&&e.token===21){o(e),_(e),e.pending=n;continue}E(e,e.blockquote_idx),_(e),e.blockquote_idx=0,e.fence_start=0,e.pending=n;continue;case"#":switch(n){case"#":if(e.pending.length<6){e.pending=t;continue}break;case" ":l(e,e.indent_len),i(e,heading_from_level(e.pending.length)),_(e);continue}break;case">":{const r=L(e,20,e.blockquote_idx+1);r===-1?(E(e,e.blockquote_idx),e.blockquote_idx+=1,e.fence_start=0,i(e,20)):e.blockquote_idx=r,_(e),e.pending=n;continue}case"-":case"*":case"_":if(e.hr_chars===0&&(console.assert(e.pending.length===1,"Pending should be one character"),e.hr_chars=1,e.hr_char=e.pending),e.hr_chars>0){switch(n){case e.hr_char:e.hr_chars+=1,e.pending=t;continue;case" ":e.pending=t;continue;case`
4
+ `:if(e.hr_chars<3)break;l(e,e.indent_len),e.renderer.add_token(e.renderer.data,22),e.renderer.end_token(e.renderer.data),_(e),e.hr_chars=0;continue}e.hr_chars=0}if(e.pending[0]!=="_"&&e.pending[1]===" "){I(e,23),g(e,2),parser_write(e,t.slice(2));continue}break;case"`":if(e.pending.length<3){if(n==="`"){e.pending=t,e.fence_start=t.length;continue}e.fence_start=0;break}switch(n){case"`":e.pending.length===e.fence_start?(e.pending=t,e.fence_start=t.length):(i(e,2),_(e),e.fence_start=0,parser_write(e,t));continue;case`
5
+ `:{l(e,e.indent_len),i(e,10),e.pending.length>e.fence_start&&e.renderer.set_attr(e.renderer.data,LANG,e.pending.slice(e.fence_start)),_(e),e.token=101;continue}default:e.pending=t;continue}case"+":if(n!==" ")break;I(e,23),g(e,2);continue;case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":case"8":case"9":if(e.pending[e.pending.length-1]==="."){if(n!==" ")break;I(e,24)&&e.pending!=="1."&&e.renderer.set_attr(e.renderer.data,START,e.pending.slice(0,-1)),g(e,e.pending.length+1);continue}else{const r=n.charCodeAt(0);if(r===46||u(r)){e.pending=t;continue}}break;case"|":E(e,e.blockquote_idx),i(e,27),i(e,28),e.pending="",parser_write(e,n);continue}let s=t;if(e.token===21)e.token=e.tokens[e.len],e.renderer.add_token(e.renderer.data,21),e.renderer.end_token(e.renderer.data);else if(e.indent_len>=4){let r=0;for(;r<4;r+=1)if(e.indent[r]===" "){r=r+1;break}s=e.indent.slice(r)+t,i(e,9)}else i(e,2);_(e),parser_write(e,s);continue;case 27:if(e.table_state===1)switch(n){case"-":case" ":case"|":case":":e.pending=t;continue;case`
6
+ `:e.table_state=2,e.pending="";continue;default:o(e),e.table_state=0;break}else switch(e.pending){case"|":i(e,28),e.pending="",parser_write(e,n);continue;case`
7
+ `:o(e),e.pending="",e.table_state=0,parser_write(e,n);continue}break;case 28:switch(e.pending){case"":break;case"|":i(e,29),o(e),e.pending="",parser_write(e,n);continue;case`
8
+ `:o(e),e.table_state=Math.min(e.table_state+1,2),e.pending="",parser_write(e,n);continue;default:i(e,29),parser_write(e,n);continue}break;case 29:if(e.pending==="|"){a(e),o(e),e.pending="",parser_write(e,n);continue}break;case 9:switch(t){case`
9
+ `:case`
10
+ `:case`
11
+ `:case`
12
+ `:case`
13
+ `:e.text+=`
14
+ `,e.pending="";continue;case`
15
+ `:case`
16
+ `:case`
17
+ `:case`
18
+ `:e.pending=t;continue;default:e.pending.length!==0?(a(e),o(e),e.pending=n):e.text+=n;continue}case 10:switch(n){case"`":e.pending=t;continue;case`
19
+ `:if(t.length===e.fence_start+e.fence_end+1){a(e),o(e),e.pending="",e.fence_start=0,e.fence_end=0,e.token=101;continue}e.token=101;break;case" ":if(e.pending[0]===`
20
+ `){e.pending=t,e.fence_end+=1;continue}break}e.text+=e.pending,e.pending=n,e.fence_end=1;continue;case 11:switch(n){case"`":t.length===e.fence_start+ +(e.pending[0]===" ")?(a(e),o(e),e.pending="",e.fence_start=0):e.pending=t;continue;case`
21
+ `:e.text+=e.pending,e.pending="",e.token=21,e.blockquote_idx=0,a(e);continue;case" ":e.text+=e.pending,e.pending=n;continue;default:e.text+=t,e.pending="";continue}case 103:switch(e.pending.length){case 0:if(n!=="[")break;e.pending=t;continue;case 1:if(n!==" "&&n!=="x")break;e.pending=t;continue;case 2:if(n!=="]")break;e.pending=t;continue;case 3:if(n!==" ")break;e.renderer.add_token(e.renderer.data,26),e.pending[1]==="x"&&e.renderer.set_attr(e.renderer.data,CHECKED,""),e.renderer.end_token(e.renderer.data),e.pending=" ";continue}e.token=e.tokens[e.len],e.pending="",parser_write(e,t);continue;case 14:case 15:{let r="*",d=12;if(e.token===15&&(r="_",d=13),r===e.pending){if(a(e),r===n){o(e),e.pending="";continue}i(e,d),e.pending=n;continue}break}case 12:case 13:{let r="*",d=14;switch(e.token===13&&(r="_",d=15),e.pending){case r:r===n?e.tokens[e.len-1]===d?e.pending=t:(a(e),i(e,d),e.pending=""):(a(e),o(e),e.pending=n);continue;case r+r:const k=e.token;a(e),o(e),o(e),r!==n?(i(e,k),e.pending=n):e.pending="";continue}break}case 16:if(t==="~~"){a(e),o(e),e.pending="";continue}break;case 105:n===`
22
+ `?(a(e),i(e,30),e.pending=""):(e.token=e.tokens[e.len],e.pending[0]==="\\"?e.text+="[":e.text+="$$",e.pending="",parser_write(e,n));continue;case 30:if(t==="\\]"||t==="$$"){a(e),o(e),e.pending="";continue}break;case 31:if(t==="\\)"||e.pending[0]==="$"){a(e),o(e),n===")"?e.pending="":e.pending=n;continue}break;case 102:t==="http://"||t==="https://"?(a(e),i(e,18),e.pending=t,e.text=t):"http:/"[e.pending.length]===n||"https:/"[e.pending.length]===n?e.pending=t:(e.token=e.tokens[e.len],parser_write(e,n));continue;case 17:case 19:if(e.pending==="]"){a(e),n==="("?e.pending=t:(o(e),e.pending=n);continue}if(e.pending[0]==="]"&&e.pending[1]==="("){if(n===")"){const r=e.token===17?HREF:SRC,d=e.pending.slice(2);e.renderer.set_attr(e.renderer.data,r,d),o(e),e.pending=""}else e.pending+=n;continue}break;case 18:n===" "||n===`
23
+ `||n==="\\"?(e.renderer.set_attr(e.renderer.data,HREF,e.pending),a(e),o(e),e.pending=n):(e.text+=n,e.pending=t);continue;case 104:if(t.startsWith("<br")){if(t.length===3||n===" "||n==="/"&&(t.length===4||e.pending[e.pending.length-1]===" ")){e.pending=t;continue}if(n===">"){a(e),e.token=e.tokens[e.len],e.renderer.add_token(e.renderer.data,21),e.renderer.end_token(e.renderer.data),e.pending="";continue}}e.token=e.tokens[e.len],e.text+="<",e.pending=e.pending.slice(1),parser_write(e,n);continue}switch(e.pending[0]){case"\\":if(e.token===19||e.token===30||e.token===31)break;switch(n){case"(":a(e),i(e,31),e.pending="";continue;case"[":e.token=105,e.pending=t;continue;case`
24
+ `:e.pending=n;continue;default:let s=n.charCodeAt(0);e.pending="",e.text+=u(s)||s>=65&&s<=90||s>=97&&s<=122?t:n;continue}case`
25
+ `:switch(e.token){case 19:case 30:case 31:break;case 3:case 4:case 5:case 6:case 7:case 8:a(e),E(e,e.blockquote_idx),e.blockquote_idx=0,e.pending=n;continue;default:a(e),e.pending=n,e.token=21,e.blockquote_idx=0;continue}break;case"<":if(e.token!==19&&e.token!==30&&e.token!==31){a(e),e.pending=t,e.token=104;continue}break;case"`":if(e.token===19)break;n==="`"?(e.fence_start+=1,e.pending=t):(e.fence_start+=1,a(e),i(e,11),e.text=n===" "||n===`
26
+ `?"":n,e.pending="");continue;case"_":case"*":{if(e.token===19||e.token===30||e.token===31||e.token===14)break;let s=12,r=14;const d=e.pending[0];if(d==="_"&&(s=13,r=15),e.pending.length===1){if(d===n){e.pending=t;continue}if(n!==" "&&n!==`
27
+ `){a(e),i(e,s),e.pending=n;continue}}else{if(d===n){a(e),i(e,r),i(e,s),e.pending="";continue}if(n!==" "&&n!==`
28
+ `){a(e),i(e,r),e.pending=n;continue}}break}case"~":if(e.token!==19&&e.token!==16){if(e.pending==="~"){if(n==="~"){e.pending=t;continue}}else if(n!==" "&&n!==`
29
+ `){a(e),i(e,16),e.pending=n;continue}}break;case"$":if(e.token!==19&&e.token!==16&&e.pending==="$")if(n==="$"){e.token=105,e.pending=t;continue}else{if(T(n.charCodeAt(0)))break;a(e),i(e,31),e.pending=n;continue}break;case"[":if(e.token!==19&&e.token!==17&&e.token!==30&&e.token!==31&&n!=="]"){a(e),i(e,17),e.pending=n;continue}break;case"!":if(e.token!==19&&n==="["){a(e),i(e,19),e.pending="";continue}break;case" ":if(e.pending.length===1&&n===" ")continue;break}if(e.token!==19&&e.token!==17&&e.token!==30&&e.token!==31&&n==="h"&&(e.pending===" "||e.pending==="")){e.text+=e.pending,e.pending=n,e.token=102;continue}e.text+=e.pending,e.pending=n}a(e)}export function default_renderer(e){return{add_token:default_add_token,end_token:default_end_token,add_text:default_add_text,set_attr:default_set_attr,data:{nodes:[e,,,,,],index:0}}}export function default_add_token(e,c){let n=e.nodes[e.index],t;switch(c){case 1:return;case 20:t=document.createElement("blockquote");break;case 2:t=document.createElement("p");break;case 21:t=document.createElement("br");break;case 22:t=document.createElement("hr");break;case 3:t=document.createElement("h1");break;case 4:t=document.createElement("h2");break;case 5:t=document.createElement("h3");break;case 6:t=document.createElement("h4");break;case 7:t=document.createElement("h5");break;case 8:t=document.createElement("h6");break;case 12:case 13:t=document.createElement("em");break;case 14:case 15:t=document.createElement("strong");break;case 16:t=document.createElement("s");break;case 11:t=document.createElement("code");break;case 18:case 17:t=document.createElement("a");break;case 19:t=document.createElement("img");break;case 23:t=document.createElement("ul");break;case 24:t=document.createElement("ol");break;case 25:t=document.createElement("li");break;case 26:let s=t=document.createElement("input");s.type="checkbox",s.disabled=!0;break;case 9:case 10:n=n.appendChild(document.createElement("pre")),t=document.createElement("code");break;case 27:t=document.createElement("table");break;case 28:switch(n.children.length){case 0:n=n.appendChild(document.createElement("thead"));break;case 1:n=n.appendChild(document.createElement("tbody"));break;default:n=n.children[1]}t=document.createElement("tr");break;case 29:t=document.createElement(n.parentElement?.tagName==="THEAD"?"th":"td");break;case 30:t=document.createElement("equation-block");break;case 31:t=document.createElement("equation-inline");break}e.nodes[++e.index]=n.appendChild(t)}export function default_end_token(e){e.index-=1}export function default_add_text(e,c){e.nodes[e.index].appendChild(document.createTextNode(c))}export function default_set_attr(e,c,n){e.nodes[e.index].setAttribute(attr_to_html_attr(c),n)}export function logger_renderer(){return{data:void 0,add_token:logger_add_token,end_token:logger_end_token,add_text:logger_add_text,set_attr:logger_set_attr}}export function logger_add_token(e,c){console.log("add_token:",token_to_string(c))}export function logger_end_token(e){console.log("end_token")}export function logger_add_text(e,c){console.log('add_text: "%s"',c)}export function logger_set_attr(e,c,n){console.log('set_attr: %s="%s"',attr_to_html_attr(c),n)}