From 306b143104d3e8a2260cc3529662ece1adf2a3d2 Mon Sep 17 00:00:00 2001 From: Weft packages Date: Thu, 19 Nov 2026 20:43:26 +0000 Subject: [PATCH] packages/ at markup-kit dd1e6eb5270e A snapshot of packages/ in the Weft repository; the official packages are published from these commits. Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc --- LICENSE-APACHE | 202 +++++ LICENSE-MIT | 21 + README.md | 48 + email/README.md | 132 +++ email/components/A.weft | 8 + email/components/Button.weft | 18 + email/components/Em.weft | 4 + email/components/Facts.weft | 9 + email/components/H1.weft | 4 + email/components/H2.weft | 4 + email/components/Hr.weft | 3 + email/components/Img.weft | 13 + email/components/Ol.weft | 8 + email/components/P.weft | 4 + email/components/Strong.weft | 4 + email/components/Ul.weft | 8 + email/email.weft | 227 +++++ email/mime.weft | 208 +++++ email/postmark.weft | 131 +++ email/resend.weft | 146 +++ email/scripts/check_mime.py | 49 + email/scripts/integration.mjs | 28 + email/template.weft | 45 + email/tests/Broken.weft | 3 + email/tests/Escapes.weft | 7 + email/tests/Everything.weft | 21 + email/tests/Mailto.weft | 5 + email/tests/Unsafe.weft | 8 + email/tests/email.test.weft | 451 ++++++++++ email/weft.toml | 11 + integration/.gitignore | 1 + integration/email/main.weft | 232 +++++ integration/email/weft.toml | 5 + integration/stripe/main.weft | 499 +++++++++++ integration/stripe/weft.toml | 5 + markdown/README.md | 102 +++ markdown/ast.weft | 83 ++ markdown/bench/bench.weft | 12 + markdown/lib/blocks.weft | 1145 +++++++++++++++++++++++ markdown/lib/chars.weft | 242 +++++ markdown/lib/entities.weft | 23 + markdown/lib/inlines.weft | 1397 +++++++++++++++++++++++++++++ markdown/lib/render.weft | 255 ++++++ markdown/lib/sanitize.weft | 403 +++++++++ markdown/lib/unicode.weft | 50 ++ markdown/markdown.weft | 156 ++++ markdown/scripts/bench.mjs | 49 + markdown/scripts/gen-spec.mjs | 69 ++ markdown/scripts/gen-tables.mjs | 73 ++ markdown/scripts/hostile.mjs | 39 + markdown/tests/cases.weft | 1333 +++++++++++++++++++++++++++ markdown/tests/hostile.weft | 47 + markdown/tests/markdown.test.weft | 220 +++++ markdown/tests/spec.weft | 54 ++ markdown/weft.toml | 10 + scripts/verify-local.mjs | 143 +++ stripe/README.md | 148 +++ stripe/billing.weft | 126 +++ stripe/checkout.weft | 213 +++++ stripe/customers.weft | 150 ++++ stripe/lib/form.weft | 110 +++ stripe/lib/sha256.weft | 195 ++++ stripe/payment_intents.weft | 235 +++++ stripe/prices.weft | 139 +++ stripe/products.weft | 125 +++ stripe/refunds.weft | 90 ++ stripe/scripts/integration.mjs | 31 + stripe/stripe.weft | 316 +++++++ stripe/subscriptions.weft | 215 +++++ stripe/tests/stripe.test.weft | 220 +++++ stripe/webhooks.weft | 114 +++ stripe/weft.toml | 11 + 72 files changed, 10915 insertions(+) create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 email/README.md create mode 100644 email/components/A.weft create mode 100644 email/components/Button.weft create mode 100644 email/components/Em.weft create mode 100644 email/components/Facts.weft create mode 100644 email/components/H1.weft create mode 100644 email/components/H2.weft create mode 100644 email/components/Hr.weft create mode 100644 email/components/Img.weft create mode 100644 email/components/Ol.weft create mode 100644 email/components/P.weft create mode 100644 email/components/Strong.weft create mode 100644 email/components/Ul.weft create mode 100644 email/email.weft create mode 100644 email/mime.weft create mode 100644 email/postmark.weft create mode 100644 email/resend.weft create mode 100644 email/scripts/check_mime.py create mode 100644 email/scripts/integration.mjs create mode 100644 email/template.weft create mode 100644 email/tests/Broken.weft create mode 100644 email/tests/Escapes.weft create mode 100644 email/tests/Everything.weft create mode 100644 email/tests/Mailto.weft create mode 100644 email/tests/Unsafe.weft create mode 100644 email/tests/email.test.weft create mode 100644 email/weft.toml create mode 100644 integration/.gitignore create mode 100644 integration/email/main.weft create mode 100644 integration/email/weft.toml create mode 100644 integration/stripe/main.weft create mode 100644 integration/stripe/weft.toml create mode 100644 markdown/README.md create mode 100644 markdown/ast.weft create mode 100644 markdown/bench/bench.weft create mode 100644 markdown/lib/blocks.weft create mode 100644 markdown/lib/chars.weft create mode 100644 markdown/lib/entities.weft create mode 100644 markdown/lib/inlines.weft create mode 100644 markdown/lib/render.weft create mode 100644 markdown/lib/sanitize.weft create mode 100644 markdown/lib/unicode.weft create mode 100644 markdown/markdown.weft create mode 100644 markdown/scripts/bench.mjs create mode 100644 markdown/scripts/gen-spec.mjs create mode 100644 markdown/scripts/gen-tables.mjs create mode 100644 markdown/scripts/hostile.mjs create mode 100644 markdown/tests/cases.weft create mode 100644 markdown/tests/hostile.weft create mode 100644 markdown/tests/markdown.test.weft create mode 100644 markdown/tests/spec.weft create mode 100644 markdown/weft.toml create mode 100644 scripts/verify-local.mjs create mode 100644 stripe/README.md create mode 100644 stripe/billing.weft create mode 100644 stripe/checkout.weft create mode 100644 stripe/customers.weft create mode 100644 stripe/lib/form.weft create mode 100644 stripe/lib/sha256.weft create mode 100644 stripe/payment_intents.weft create mode 100644 stripe/prices.weft create mode 100644 stripe/products.weft create mode 100644 stripe/refunds.weft create mode 100644 stripe/scripts/integration.mjs create mode 100644 stripe/stripe.weft create mode 100644 stripe/subscriptions.weft create mode 100644 stripe/tests/stripe.test.weft create mode 100644 stripe/webhooks.weft create mode 100644 stripe/weft.toml diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..6981081 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 The Weft authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2a028be --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# Official packages + +Packages published under the `weft` owner, written in Weft over HTTP APIs and `std`. There is no npm bridge +(`docs/packages.md`, Rulings): anything an app needs from the JS ecosystem is written here instead. + +| package | what | capabilities | +|---|---|---| +| [`weft/markdown`](markdown/README.md) | CommonMark and GFM: a syntax tree, HTML and a sanitiser | none | +| [`weft/stripe`](stripe/README.md) | Stripe's API: customers, Checkout, payments, subscriptions, prices, products, webhooks | `http.fetch@api.stripe.com` | +| [`weft/email`](email/README.md) | Resend and Postmark, MIME with attachments, templates rendered to HTML and text | `http.fetch@api.resend.com`, `http.fetch@api.postmarkapp.com`, `http.media` (its `Img` shows the image a template names) | + +Each folder is a Weft project with its own `weft.toml`. Its tests, benchmarks and scripts stay out of the published archive +(`include` in the manifest). + +## Trying them against a local registry + +`scripts/verify-local.mjs` runs the whole path on this machine: it starts `apps/registry`, makes a publish token and a +signing key for the owner `weft`, publishes each package signed, and then in a fresh app runs `weft add`, `check`, `run`, +`build --target js` and `install --frozen`. + +```sh +cargo build --release -p weft-cli # in core/ +node packages/scripts/verify-local.mjs # every package, or name some: markdown stripe email +``` + +## Publishing for real + +None of this has happened yet. In order: + +1. **Deploy the registry** at pkg.weft-lang.dev (`apps/registry/README.md`, What a deploy needs). +2. **Make the owner's token**: `POST /v1/tokens` with `{ "owners": ["weft"] }` and the operator's `REGISTRY_ADMIN_TOKEN`. `weft` is + a reserved owner, so only the operator can grant it. +3. **Make the signing key offline**: `weft publish --new-key`, keep the secret out of the repository, and register the public + key with `PUT /v1/owners/weft/key`. From then on the registry takes only versions signed by it. +4. **Set each manifest's `repository`** to the public source. It is a placeholder today + (`https://git.weft-lang.dev/weft/packages`), and a signed statement records it for good. +5. **Publish from a clean checkout**, with the commit recorded: `WEFT_TOKEN=… WEFT_SIGNING_KEY=… WEFT_COMMIT=$(git rev-parse HEAD) + weft publish --sign --dir packages/`. +6. **Look at the generated pages** (`/packages/weft/`) before announcing a version: published versions can be yanked but + never changed. + +Before each package's first real release: + +| package | still to do | +|---|---| +| `weft/markdown` | nothing beyond the steps above | +| `weft/stripe` | run its calls against a Stripe test-mode key: so far they have met only stripe-mock and a scripted server | +| `weft/email` | send a message through Resend and Postmark with test keys: so far they have met only a scripted server | diff --git a/email/README.md b/email/README.md new file mode 100644 index 0000000..a599ad1 --- /dev/null +++ b/email/README.md @@ -0,0 +1,132 @@ +# @weft/email + +Email for Weft: sending through Resend and Postmark, MIME messages with attachments, and templates that render to HTML and +text. Its only capabilities are `http.fetch@api.resend.com`, `http.fetch@api.postmarkapp.com` and `http.media`, because its +`Img` component shows the image a template names. + +```sh +weft add weft/email +``` + +```weft +import { send } from "@weft/email" +import { resend } from "@weft/email/resend" +import { document } from "@weft/email/template" +import { render } from "std/app" +import { env } from "std/process" +import Receipt from "./Receipt" + +let mail = resend({ apiKey: env("RESEND_API_KEY")! }) + +let body = document(render(Receipt, { name: "Ana", total: "19,99 €" }), { + title: "Your receipt", + preheader: "Thanks for your order", + footer: "Example d.o.o., Ljubljana", +}) +let sent = try await send(mail, { + from: { email: "info@example.com", name: "Example" }, + to: ["ana@example.com"], + subject: "Your receipt", + html: body.html, + text: body.text, +}) +``` + +```weft file=Receipt.weft +import Button from "@weft/email/components/Button" +import Facts from "@weft/email/components/Facts" +import H1 from "@weft/email/components/H1" +import P from "@weft/email/components/P" +import Strong from "@weft/email/components/Strong" + +prop name: string +prop total: string +--- +

Thank you!

+

Hi {name}, your payment came through.

+ +"), "go") +} + +test "sanitize keeps only a disabled checkbox" { + assertEq(sanitize(""), "") + assertEq(sanitize(""), "") + assertEq( + sanitize(""), + "", + ) +} + +test "sanitize gives well-formed output" { + assertEq(sanitize("xy"), "xy") + assertEq(sanitize("x"), "x") + assertEq(sanitize("

a"), "

a

") + assertEq(sanitize("1 < 2 & 3 > 2 & ok"), "1 < 2 & 3 > 2 & ok") + assertEq(sanitize("
x"), "x") + assertEq( + sanitize("x"), + "x", + ) + assertEq(sanitize("x"), "x") +} + +test "a policy narrows what is kept" { + assertEq(sanitize("xy", { tags: ["b"] }), "xy") + assertEq(sanitize("y", { schemes: ["ftp"] }), "y") + assertEq(sanitize("y", { schemes: ["ftp"] }), "y") + let attributes = Map([["b", ["title"]]]) + assertEq( + sanitize("x", { tags: ["b"], attributes }), + "x", + ) +} + +test "html: sanitize keeps safe raw HTML in Markdown" { + assertEq( + toHtml("
\n*hi*\n
", { html: "sanitize" }), + "
\n*hi*\n
\n", + ) + assertEq( + toHtml("a b ", { html: "sanitize" }), + "

a b

\n", + ) + assertEq(toHtml("\n\nok", { html: "sanitize" }), "\n

ok

\n") + assertEq( + toHtml("- [x] done", { html: "sanitize" }), + "
    \n
  • done
  • \n
\n", + ) +} + +test "html: trust passes raw HTML through" { + assertEq( + toHtml("
a
", { html: "trust" }), + "
a
\n", + ) +} + +// ---- GFM ---- +test "tables" { + let html = toHtml("| a | b |\n|:--|--:|\n| 1 | `x\\|y` |") + assertEq( + html, + "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
1x|y
\n", + ) +} + +test "task lists" { + let doc = parse("- [ ] todo\n- [x] done\n- plain") + match doc.children[0] { + BulletList { items } => { + let checked: boolean?[] = items.map(i => i.checked) + let expected: boolean?[] = [false, true, null] + assertEq(checked, expected) + }, + _ => assert(false, "a list"), + } +} + +test "strikethrough and bare links" { + assertEq(toHtml("~~gone~~"), "

gone

\n") + assertEq( + toHtml("see https://x.dev/a_b."), + "

see https://x.dev/a_b.

\n", + ) + assertEq(toHtml("www.x.dev"), "

www.x.dev

\n") + assertEq(toHtml("me@x.dev"), "

me@x.dev

\n") +} + +test "gfm: false is plain CommonMark" { + assertEq(toHtml("~~a~~ https://x.dev", { gfm: false }), "

~~a~~ https://x.dev

\n") + assertEq(toHtml("| a |\n|---|", { gfm: false }), "

| a |\n|---|

\n") +} + +// ---- what Vidni's briefs need ---- +test "heading offset, link target and URL rewriting" { + assertEq(toHtml("# T", { headingOffset: 1 }), "

T

\n") + assertEq(toHtml("###### T", { headingOffset: 3 }), "
T
\n") + assertEq( + toHtml("[a](https://x.dev)", { linkTarget: "_blank" }), + "

a

\n", + ) + let resolve = (url: string, kind: "link" | "image") => url.startsWith("upload:") + ? `https://cdn.x.dev/${url.slice(7)}` + : null + assertEq( + toHtml("![c](upload:abc)", { rewriteUrl: resolve }), + "

\"c\"

\n", + ) + // Not resolved: `upload:` isn't an allowed scheme, so the caption stays. + assertEq(toHtml("![c](upload:abc)"), "

c

\n") +} + +test "toText for previews and emails" { + assertEq( + toText(parse("# Hi\n\nRead **this** [guide](https://x.dev).\n\n![cat](a.png)\n\n- one\n- two")), + "Hi\n\nRead this guide (https://x.dev).\n\n[image: cat]\n\n- one\n\n- two", + ) +} + +test "visit finds the images of a document" { + let mut urls: string[] = [] + visit(parse("![a](upload:1)\n\n> *![b](upload:2)*\n\n| ![c](upload:3) |\n|---|"), n => { + if let Picture { url } = n { + urls.push(url) + } + }) + assertEq(urls, ["upload:1", "upload:2", "upload:3"]) +} + +test "render uses a tree that was changed" { + let doc = parse("hello") + assertEq(render({ children: [...doc.children, ...doc.children] }), "

hello

\n

hello

\n") +} + +test "strange characters survive" { + assertEq(toHtml("a\u{0}b"), "

a\u{fffd}b

\n") + assertEq(toHtml("line\r\nnext\rlast"), "

line\nnext\nlast

\n") + assertEq(toHtml("😀 *é*"), "

😀 é

\n") + assertEq(toHtml(""), "") +} diff --git a/markdown/tests/spec.weft b/markdown/tests/spec.weft new file mode 100644 index 0000000..8378ec0 --- /dev/null +++ b/markdown/tests/spec.weft @@ -0,0 +1,54 @@ +// Runs the spec's examples: `weft run tests/spec.weft [commonmark|gfm] [-v]` prints every failure and a summary. +import { args, exit } from "std/process" +import { log } from "std/log" +import { toHtml } from "../markdown" +import { COMMONMARK, GFM } from "./cases" +import type { Case } from "./cases" + +/// Where the GFM spec (written against CommonMark 0.29) and CommonMark 0.31.2 give different HTML for the same text, +/// CommonMark wins: cmark-gfm's renderer folds nested ``, which CommonMark's examples keep. +fn followsCommonMark(c: Case, got: string): boolean { + return COMMONMARK.some(o => o.markdown == c.markdown && o.html == got) +} + +fn runAll(name: string, cases: Case[], verbose: boolean): int { + let mut failed = 0 + let mut disagreements = 0 + let mut bySection = Map() + for c of cases { + let got = toHtml(c.markdown, { gfm: c.gfm, html: "trust" }) + if got != c.html && followsCommonMark(c, got) { + disagreements += 1 + } else if got != c.html { + failed += 1 + bySection.set(c.section, (bySection.get(c.section) ?? 0) + 1) + if verbose { + log(`--- ${name} example ${c.id} (${c.section})`) + log(`markdown: ${c.markdown}`) + log(`expected: ${c.html}`) + log(`got: ${got}`) + } + } + } + log( + `${name}: ${cases.length - failed - disagreements} of ${cases.length} pass, ${disagreements} follow CommonMark 0.31.2 where the specs disagree, ${failed} fail`, + ) + for [section, n] of bySection { + log(` ${n} failing in ${section}`) + } + return failed +} + +let argv = args() +let verbose = argv.includes("-v") +let only = argv.filter(a => a != "-v").at(0) +let mut failures = 0 +if only == null || only == "commonmark" { + failures += runAll("CommonMark", COMMONMARK, verbose) +} +if only == null || only == "gfm" { + failures += runAll("GFM", GFM, verbose) +} +if failures > 0 { + exit(1) +} diff --git a/markdown/weft.toml b/markdown/weft.toml new file mode 100644 index 0000000..062b3e0 --- /dev/null +++ b/markdown/weft.toml @@ -0,0 +1,10 @@ +[package] +name = "weft/markdown" +version = "0.1.0" +description = "CommonMark and GitHub Flavored Markdown: an AST, HTML and a sanitiser, safe by default" +license = "MIT OR Apache-2.0" +# A placeholder until the public repository exists (packages/README.md, Publishing for real). +repository = "https://git.weft-lang.dev/weft/packages" +exports = ["markdown", "ast"] +main = "markdown" +include = ["markdown.weft", "ast.weft", "lib", "README.md"] diff --git a/scripts/verify-local.mjs b/scripts/verify-local.mjs new file mode 100644 index 0000000..f2dbf55 --- /dev/null +++ b/scripts/verify-local.mjs @@ -0,0 +1,143 @@ +// Proves the official packages end to end against the registry app running locally: starts `apps/registry`, makes a +// publish token and a signing key for the owner `weft`, publishes each package signed, then in a fresh app runs +// `weft add`, checks it, runs it on the VM and builds it for the browser. Nothing leaves this machine. +// +// node packages/scripts/verify-local.mjs [markdown] [stripe] [email] +import { spawn, spawnSync } from "node:child_process" +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs" +import { dirname, join } from "node:path" +import { fileURLToPath } from "node:url" + +const repo = join(dirname(fileURLToPath(import.meta.url)), "../..") +const weft = process.env.WEFT_BIN ?? join(repo, "core/target/release/weft") +const work = join(repo, "core/target/tools/verify") +const admin = "local-operator-token" +const port = 4873 + (process.pid % 1000) +const registry = `http://127.0.0.1:${port}` + +// What a fresh app does with each package, and the line it must print. No network: these run offline. +const apps = { + markdown: { + main: `import { toHtml } from "@weft/markdown" +import { log } from "std/log" +log(toHtml("# Hello\\n\\n- [x] **done** ")) +`, + expect: "

Hello

\n
    \n
  • done <script>bad()</script>
  • \n
", + capabilities: "capabilities none", + }, + stripe: { + main: `import { client } from "@weft/stripe" +import { signature, verify } from "@weft/stripe/webhooks" +import { log } from "std/log" +let payload = "{\\"id\\":\\"evt_1\\",\\"type\\":\\"checkout.session.completed\\",\\"created\\":1,\\"data\\":{\\"object\\":{}}}" +let event = try verify(payload, signature(payload, "whsec_local", 1700000000), "whsec_local", { now: 1700000000 }) +log(event.type, client("sk_test_local").baseUrl) +`, + expect: "checkout.session.completed https://api.stripe.com", + capabilities: "capabilities http.fetch", + }, + email: { + main: `import { memory, outbox, send } from "@weft/email" +import { toMime } from "@weft/email/mime" +import { document } from "@weft/email/template" +import { render } from "std/app" +import { log } from "std/log" +import Welcome from "./Welcome" +let body = document(render(Welcome), { title: "Hi" }) +let sent = try await send(memory(), { from: "a@example.com", to: ["b@example.com"], subject: "Hi", html: body.html, text: body.text }) +log(sent.provider, outbox().length, toMime(outbox()[0], { id: "x" }).includes("multipart/alternative")) +`, + files: { + "Welcome.weft": `import Button from "@weft/email/components/Button" +import H1 from "@weft/email/components/H1" +import P from "@weft/email/components/P" +--- +

Hi

+

Welcome

+