ref0-git 0.3.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/apps/github/.dev.vars.example +2 -1
- package/app/apps/github/generated/git-rest.ts +191 -25
- package/app/apps/github/generated/routeTree.gen.ts +21 -0
- package/app/apps/github/package.json +1 -1
- package/app/apps/github/src/branches/branch-list.tsx +8 -4
- package/app/apps/github/src/branches/branches.ts +10 -5
- package/app/apps/github/src/camo/addresses.ts +120 -0
- package/app/apps/github/src/camo/client.ts +125 -0
- package/app/apps/github/src/camo/fetch.ts +201 -0
- package/app/apps/github/src/camo/lookup.ts +23 -0
- package/app/apps/github/src/camo/node.ts +50 -0
- package/app/apps/github/src/camo/proxy.ts +116 -0
- package/app/apps/github/src/camo/sign-images.ts +307 -0
- package/app/apps/github/src/camo/signing.ts +80 -0
- package/app/apps/github/src/clients/allowed.ts +111 -18
- package/app/apps/github/src/clients/clients.ts +76 -61
- package/app/apps/github/src/clients/transport.ts +13 -0
- package/app/apps/github/src/code/code-tree.tsx +3 -1
- package/app/apps/github/src/code/file-view.tsx +15 -6
- package/app/apps/github/src/code/files.ts +26 -19
- package/app/apps/github/src/code/go-to-file.tsx +2 -1
- package/app/apps/github/src/code/load.ts +41 -23
- package/app/apps/github/src/code/markdown-files.ts +13 -2
- package/app/apps/github/src/code/ref-pages.ts +14 -5
- package/app/apps/github/src/code/ref-switcher.tsx +4 -3
- package/app/apps/github/src/code/refs.ts +42 -8
- package/app/apps/github/src/code/repo-home.tsx +6 -2
- package/app/apps/github/src/code/repo.ts +12 -6
- package/app/apps/github/src/code/saved.ts +5 -3
- package/app/apps/github/src/code/trees.ts +11 -9
- package/app/apps/github/src/components/markdown/markdown.css +6 -0
- package/app/apps/github/src/components/markdown/markdown.tsx +95 -10
- package/app/apps/github/src/components/owner-card.tsx +3 -1
- package/app/apps/github/src/components/repos/your-repos.tsx +3 -1
- package/app/apps/github/src/components/shell/owner-repos.tsx +7 -12
- package/app/apps/github/src/components/shell/repo-switcher.tsx +10 -16
- package/app/apps/github/src/components/signin/device-card.tsx +9 -2
- package/app/apps/github/src/components/user-avatar.tsx +2 -1
- package/app/apps/github/src/context/context.ts +13 -0
- package/app/apps/github/src/diffs/blobs.ts +37 -14
- package/app/apps/github/src/diffs/diff-job.ts +53 -0
- package/app/apps/github/src/diffs/diff-worker.ts +6 -0
- package/app/apps/github/src/diffs/diffing.ts +136 -0
- package/app/apps/github/src/diffs/files.ts +4 -3
- package/app/apps/github/src/diffs/hunks.tsx +101 -53
- package/app/apps/github/src/diffs/patch.ts +20 -11
- package/app/apps/github/src/history/commit-diff.ts +10 -5
- package/app/apps/github/src/history/commit-list.tsx +3 -1
- package/app/apps/github/src/history/commit-page.tsx +4 -2
- package/app/apps/github/src/history/history.ts +12 -10
- package/app/apps/github/src/hooks/allowed.ts +9 -9
- package/app/apps/github/src/hooks/avatars.ts +3 -3
- package/app/apps/github/src/hooks/open-counts.ts +5 -4
- package/app/apps/github/src/hooks/profiles.ts +26 -12
- package/app/apps/github/src/hooks/repos.ts +78 -25
- package/app/apps/github/src/inbox/count.ts +30 -29
- package/app/apps/github/src/inbox/inbox-item.tsx +3 -1
- package/app/apps/github/src/inbox/inbox-page.tsx +5 -2
- package/app/apps/github/src/inbox/marks.ts +26 -15
- package/app/apps/github/src/inbox/release-tags.ts +7 -6
- package/app/apps/github/src/inbox/subjects.ts +5 -4
- package/app/apps/github/src/inbox/subscription-controls.tsx +14 -5
- package/app/apps/github/src/inbox/subscriptions.ts +95 -35
- package/app/apps/github/src/inbox/threads.ts +6 -6
- package/app/apps/github/src/issues/changes.ts +40 -16
- package/app/apps/github/src/issues/counts.ts +8 -5
- package/app/apps/github/src/issues/issue-list.tsx +20 -6
- package/app/apps/github/src/issues/issue-page.tsx +30 -14
- package/app/apps/github/src/issues/issues.ts +195 -46
- package/app/apps/github/src/issues/label-dialog.tsx +8 -11
- package/app/apps/github/src/issues/labels-page.tsx +35 -3
- package/app/apps/github/src/issues/milestone-dialog.tsx +197 -0
- package/app/apps/github/src/issues/milestones-page.tsx +269 -0
- package/app/apps/github/src/issues/new-issue.tsx +3 -1
- package/app/apps/github/src/issues/sidebar.tsx +8 -6
- package/app/apps/github/src/markdown/README.md +22 -17
- package/app/apps/github/src/markdown/context.ts +83 -10
- package/app/apps/github/src/markdown/images.ts +3 -2
- package/app/apps/github/src/markdown/lookup.ts +9 -8
- package/app/apps/github/src/markdown/page-budget.ts +50 -0
- package/app/apps/github/src/markdown/patches.ts +74 -32
- package/app/apps/github/src/markdown/query.ts +7 -2
- package/app/apps/github/src/markdown/remote.ts +106 -0
- package/app/apps/github/src/markdown/render.ts +101 -24
- package/app/apps/github/src/markdown/texts.ts +43 -0
- package/app/apps/github/src/markdown/words-module.d.ts +5 -0
- package/app/apps/github/src/markdown/words-plugin.ts +135 -0
- package/app/apps/github/src/names/repos.ts +1 -1
- package/app/apps/github/src/palette/palette.tsx +7 -4
- package/app/apps/github/src/palette/rows.tsx +54 -33
- package/app/apps/github/src/palette/server.ts +5 -5
- package/app/apps/github/src/palette/targets.ts +1 -0
- package/app/apps/github/src/policy.ts +165 -66
- package/app/apps/github/src/private-repos.ts +2 -2
- package/app/apps/github/src/pulls/compare-page.tsx +6 -3
- package/app/apps/github/src/pulls/files-tab.tsx +480 -214
- package/app/apps/github/src/pulls/held-summary.ts +178 -0
- package/app/apps/github/src/pulls/live.ts +6 -4
- package/app/apps/github/src/pulls/merge-box.tsx +17 -10
- package/app/apps/github/src/pulls/pull-list.tsx +6 -3
- package/app/apps/github/src/pulls/pull-page.tsx +19 -10
- package/app/apps/github/src/pulls/pull.ts +68 -40
- package/app/apps/github/src/pulls/pulls.ts +39 -22
- package/app/apps/github/src/pulls/review-drafts.ts +131 -0
- package/app/apps/github/src/pulls/review.ts +374 -99
- package/app/apps/github/src/pulls/threads.tsx +152 -0
- package/app/apps/github/src/queue/queue-page.tsx +15 -8
- package/app/apps/github/src/queue/queue.ts +35 -20
- package/app/apps/github/src/releases/latest-release.tsx +3 -1
- package/app/apps/github/src/releases/overview.tsx +4 -2
- package/app/apps/github/src/releases/release-form.tsx +8 -5
- package/app/apps/github/src/releases/release-list.tsx +8 -5
- package/app/apps/github/src/releases/release-view.tsx +1 -0
- package/app/apps/github/src/releases/releases.ts +33 -23
- package/app/apps/github/src/releases/tag-list.tsx +3 -1
- package/app/apps/github/src/router.tsx +5 -2
- package/app/apps/github/src/routes/$owner/$repo/_code/blob/$.tsx +7 -1
- package/app/apps/github/src/routes/$owner/$repo/_code/tree/$.tsx +6 -1
- package/app/apps/github/src/routes/$owner/$repo/_releases/releases/index.tsx +2 -1
- package/app/apps/github/src/routes/$owner/$repo/_releases/tags.tsx +1 -1
- package/app/apps/github/src/routes/$owner/$repo/branches.tsx +5 -1
- package/app/apps/github/src/routes/$owner/$repo/commit/$sha.tsx +1 -1
- package/app/apps/github/src/routes/$owner/$repo/commits/$.tsx +6 -1
- package/app/apps/github/src/routes/$owner/$repo/index.tsx +1 -1
- package/app/apps/github/src/routes/$owner/$repo/issues/$number.tsx +4 -2
- package/app/apps/github/src/routes/$owner/$repo/issues/index.tsx +2 -2
- package/app/apps/github/src/routes/$owner/$repo/issues/new.tsx +5 -2
- package/app/apps/github/src/routes/$owner/$repo/labels.tsx +2 -1
- package/app/apps/github/src/routes/$owner/$repo/milestones.tsx +36 -0
- package/app/apps/github/src/routes/$owner/$repo/pull/$number/route.tsx +4 -2
- package/app/apps/github/src/routes/$owner/$repo/pulls/index.tsx +2 -2
- package/app/apps/github/src/routes/$owner/$repo/queue.tsx +2 -1
- package/app/apps/github/src/routes/$owner/$repo/releases/edit/$.tsx +5 -2
- package/app/apps/github/src/routes/$owner/$repo/releases/new.tsx +2 -1
- package/app/apps/github/src/routes/$owner/$repo/releases/tag/$.tsx +4 -2
- package/app/apps/github/src/routes/$owner/$repo/route.tsx +9 -5
- package/app/apps/github/src/routes/$owner/$repo/settings/hooks/index.tsx +3 -1
- package/app/apps/github/src/routes/$owner/$repo/settings/index.tsx +3 -1
- package/app/apps/github/src/routes/-nav.tsx +10 -2
- package/app/apps/github/src/routes/-root-testing.ts +29 -6
- package/app/apps/github/src/routes/__root.tsx +9 -4
- package/app/apps/github/src/routes/index.tsx +3 -1
- package/app/apps/github/src/routes/orgs/$owner/route.tsx +4 -1
- package/app/apps/github/src/routes/orgs/$owner/settings.tsx +4 -1
- package/app/apps/github/src/seed/acme.ts +1 -1
- package/app/apps/github/src/seed/seed.ts +199 -110
- package/app/apps/github/src/server.ts +24 -15
- package/app/apps/github/src/settings/account-avatar.tsx +4 -1
- package/app/apps/github/src/settings/general-settings.tsx +3 -1
- package/app/apps/github/src/settings/general.ts +4 -4
- package/app/apps/github/src/settings/hook-form.tsx +3 -1
- package/app/apps/github/src/settings/hook-list.tsx +5 -2
- package/app/apps/github/src/settings/hooks.ts +7 -7
- package/app/apps/github/src/settings/org-settings.tsx +3 -1
- package/app/apps/github/src/settings/orgs.ts +21 -11
- package/app/apps/github/src/settings/settings-layout.tsx +3 -1
- package/app/apps/github/src/signin/approval.ts +4 -3
- package/app/apps/github/src/signin/auth.ts +9 -2
- package/app/apps/github/src/signin/device-code.ts +8 -3
- package/app/apps/github/src/signin/device-prompt.ts +28 -3
- package/app/apps/github/src/signin/revoke.ts +36 -0
- package/app/apps/github/tsconfig.json +4 -1
- package/app/apps/github/vite.config.ts +8 -0
- package/app/pnpm-workspace.yaml +12 -5
- package/app/tsconfig.base.json +1 -0
- package/dist/cli/child.mjs +1 -1
- package/dist/cli/credentials.mjs +4 -4
- package/dist/cli/documents.mjs +32 -22
- package/dist/cli/events.mjs +1 -1
- package/dist/cli/failure.mjs +20 -7
- package/dist/cli/folders.mjs +2 -2
- package/dist/cli/git-remote-ref0.mjs +47 -22
- package/dist/cli/git.mjs +1 -1
- package/dist/cli/helpers.mjs +2 -2
- package/dist/cli/hosts.mjs +3 -3
- package/dist/cli/output.mjs +2 -2
- package/dist/cli/prompt.mjs +1 -1
- package/dist/cli/queue.mjs +7 -7
- package/dist/cli/ref0-git.mjs +2 -2
- package/dist/cli/repos.mjs +35 -18
- package/dist/cli/resources.mjs +5 -5
- package/dist/cli/retry-after.mjs +8 -0
- package/dist/cli/setup.mjs +172 -66
- package/dist/cli/sign-in.mjs +65 -23
- package/dist/cli/sign-ins.mjs +4 -4
- package/dist/cli/sqlite.mjs +2 -2
- package/dist/cli/tokens.mjs +3 -3
- package/dist/cli/vendor.mjs +1 -1
- package/dist/init/app.mjs +20 -21
- package/dist/init/choices.mjs +1 -1
- package/dist/init/cloudflare.mjs +15 -19
- package/dist/init/create.mjs +3 -2
- package/dist/init/detect.mjs +4 -4
- package/dist/init/files.mjs +120 -13
- package/dist/init/frameworks.mjs +16 -0
- package/dist/init/git-module.mjs +22 -7
- package/dist/init/jsonc.mjs +9 -7
- package/dist/init/metadata.mjs +9 -3
- package/dist/init/mounts.mjs +13 -10
- package/dist/init/next.mjs +2 -2
- package/dist/init/node-app.mjs +3 -3
- package/dist/init/node.mjs +42 -45
- package/dist/init/nuxt.mjs +153 -27
- package/dist/init/stores.mjs +14 -9
- package/dist/init/sveltekit.mjs +4 -2
- package/dist/init/template.mjs +9 -6
- package/dist/init/templates.mjs +63 -47
- package/dist/init/vercel.mjs +37 -37
- package/dist/init/versions.mjs +3 -3
- package/package.json +6 -5
|
@@ -8,23 +8,24 @@
|
|
|
8
8
|
|
|
9
9
|
Each patch in `patches.ts` does what words' own render doesn't. `markdown.test.ts`'s "each patch, which words alone still needs" runs a case for each patch twice: with every patch, it holds, and with every patch but that one, it fails. When a words release does a patch's work itself, that row fails: delete the patch and its row.
|
|
10
10
|
|
|
11
|
-
| Patch
|
|
12
|
-
|
|
|
13
|
-
| `githubHtml`
|
|
14
|
-
| `unwrap`
|
|
15
|
-
| `rawIds`
|
|
16
|
-
| `rawUrls`
|
|
17
|
-
| `repoImages`
|
|
18
|
-
| `
|
|
19
|
-
| `
|
|
20
|
-
| `
|
|
21
|
-
| `
|
|
22
|
-
| `
|
|
23
|
-
| `
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
| Patch | What it's for | The test's case |
|
|
12
|
+
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
|
|
13
|
+
| `githubHtml` | GitHub's raw tags, each with the attributes that act on it. GitHub's whole rule packs to 28,026 bytes, past words' 4,096; this fits in 759 (`allowed-html.ts`) | `<kbd>` kept |
|
|
14
|
+
| `unwrap` | `<form>`, `<button>`, `<center>`, `<font>` and `<u>` kept, then taken off, so their text shows: words drops a block-level tag it doesn't keep, text and all | `<center>`'s text shown |
|
|
15
|
+
| `rawIds` | A raw `id` or `name` with `user-content-`: `idPrefix` reaches only the ids words makes | `<a name="tern">` |
|
|
16
|
+
| `rawUrls` | A raw link's relative URL resolved in a file: `urlTransform` reaches only Markdown's | `<a href="deploy.md">` |
|
|
17
|
+
| `repoImages` | A file's image in its repo, and a comment's at the drop-in's raw route, named by `data-repo`, `data-ref` and `data-path`, for the browser to read as a blob (Q310) | `` |
|
|
18
|
+
| `remoteImages` | A remote image, and a `<source>`'s remote `srcset`, named by `data-remote` or `data-remote-srcset` with no `src`, for the browser to load through the image proxy once it's signed (Q860) | `` |
|
|
19
|
+
| `schemes` | `ftp:` and `tel:` dropped, as GitHub's rule drops them and words doesn't | `[Files](ftp://…)` and `<a href="tel:…">` |
|
|
20
|
+
| `alerts` | `> [!NOTE]` and the rest as GitHub's alerts, with their Octicons, but not a quote in a list | `> [!NOTE]` |
|
|
21
|
+
| `references` | `#12`, `GH-12`, `owner/repo#12`, `@login` and SHAs linked where they exist, by `references.ts`; a file links only `owner/repo#12` | `#12` |
|
|
22
|
+
| `lineBreaks` | A comment's soft breaks as `<br>` | `Kites\nfly` |
|
|
23
|
+
| `rawBreaks` | A comment's soft break between two raw tags as `<br>` too, which `lineBreaks` misses, as it needs text after the newline | `<kbd>Ctrl</kbd>\n<kbd>C</kbd>` |
|
|
24
|
+
| `anchors` | A file's headings' anchors, as GitHub's slugs, numbered across every level in the document's order, in a pass once words ends, but not on a raw heading that has an `id` | `## Setup` |
|
|
25
|
+
|
|
26
|
+
Words runs a tag's hooks in the order the patches first name that tag, each tag's over every element of it before the next tag's. So `alerts` marks a list's quotes before it reads quotes, and `anchors`, which needs the document's order across `h1` to `h6`, is a patch's `after`, a pass over the whole render once words ends. The same test file checks that the fitted list is 53 tags and 759 bytes, and that a raw heading with its own `id` keeps that one alone.
|
|
27
|
+
|
|
28
|
+
Words' hooks see serialized HTML, and words finds each element by its tags, which holds only while no `<`, `>` or `"` stands raw inside one. So every value a patch or `render.ts` takes from the text or a URL and writes into HTML goes through words' `escapeHtml`, the one escaper. The rest are fixed strings, or `references`' labels, whose patterns match only `[\w./#@-]`. `references` and `lineBreaks` also split with `htmlParts`, which reads a quoted value as part of its tag. "A file's URLs, as its hooks write them" in `markdown.test.ts` runs `references` after each hook that writes an attribute, and the fragment pass after `anchors`, which runs after `references`, and the fuzz test renders with every reference found.
|
|
28
29
|
|
|
29
30
|
Beside the patches, `render.ts` draws Mermaid with Pele, with no links or images and ids of its own. It also sends a fragment link to the prefixed id it means, as GitHub's page script does.
|
|
30
31
|
|
|
@@ -36,6 +37,10 @@ The user accepted these on 21 Nov. "The accepted gaps" in `markdown.test.ts` rec
|
|
|
36
37
|
- ``$`…`$`` keeps its backticks in the TeX.
|
|
37
38
|
- A block-level tag outside the unwrap list, such as `<aside>`, loses its text.
|
|
38
39
|
|
|
40
|
+
The drop-in EM accepted this one on 25 Nov, under next-phase row 429, as invalid HTML no realistic README writes:
|
|
41
|
+
|
|
42
|
+
- A raw heading that holds another heading inside an inline element, such as `<h3><span>Kite<h2>Fly</h2></span></h3>`, takes its anchor from its text up to the inner heading, `kite`, where a browser nests the inner one and GitHub's anchor reads `kitefly`.
|
|
43
|
+
|
|
39
44
|
## Against GitHub's render
|
|
40
45
|
|
|
41
46
|
`fixtures/` holds spike F's eight texts, each with GitHub's render in both contexts. "SF's fixtures, against GitHub's render" compares the facts a reader sees (`testing/facts.ts`).
|
|
@@ -68,6 +68,12 @@ export interface Linker {
|
|
|
68
68
|
readonly commit: (reference: CommitReference) => string | undefined;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** Whether `found` holds any issue, user or commit to link. */
|
|
72
|
+
export const hasFound = (found: Found): boolean =>
|
|
73
|
+
Object.keys(found.issues).length > 0 ||
|
|
74
|
+
found.users.length > 0 ||
|
|
75
|
+
Object.keys(found.commits).length > 0;
|
|
76
|
+
|
|
71
77
|
export const linkerOf = (found: Found): Linker => {
|
|
72
78
|
const users = new Set(found.users);
|
|
73
79
|
|
|
@@ -164,6 +170,15 @@ const decodedPath = (path: string) =>
|
|
|
164
170
|
})
|
|
165
171
|
.join('/');
|
|
166
172
|
|
|
173
|
+
/**
|
|
174
|
+
* What `resolveHref` writes before a relative link's own path in `context`: a file's repo, ref and
|
|
175
|
+
* folder, and nothing in a comment. The bound charges it at each link (#1098).
|
|
176
|
+
*/
|
|
177
|
+
export const linkPrefixOf = (context: MarkdownContext) =>
|
|
178
|
+
context.kind === 'comment'
|
|
179
|
+
? 0
|
|
180
|
+
: `/${context.repo}/blob/${encodedPath(context.ref)}/${encodedPath(context.dir)}/`.length;
|
|
181
|
+
|
|
167
182
|
/** A link's `href` as the context shows it: a file's relative ones in its repo, a comment's as written. */
|
|
168
183
|
export const resolveHref = (url: string, context: MarkdownContext) => {
|
|
169
184
|
if (context.kind === 'comment' || absolute(url)) return url;
|
|
@@ -172,25 +187,83 @@ export const resolveHref = (url: string, context: MarkdownContext) => {
|
|
|
172
187
|
return `/${context.repo}/blob/${encodedPath(context.ref)}/${path}${rest}`;
|
|
173
188
|
};
|
|
174
189
|
|
|
190
|
+
/** Where a comment's root-relative URLs resolve: a stand-in for the app's origin. */
|
|
191
|
+
const appOrigin = 'https://app.invalid';
|
|
192
|
+
|
|
175
193
|
/**
|
|
176
|
-
* The
|
|
177
|
-
*
|
|
194
|
+
* The drop-in's raw route, `/{owner}/{repo}/raw/{ref}/{path}`, its ref one segment with a
|
|
195
|
+
* branch's slashes encoded in it, as the route and its `download_url` write it.
|
|
178
196
|
*/
|
|
179
|
-
|
|
180
|
-
if (context.kind === 'comment' || absolute(url)) return undefined;
|
|
197
|
+
const rawRoute = /^\/([^/]+)\/([^/]+)\/raw\/([^/]+)\/(.+)$/;
|
|
181
198
|
|
|
182
|
-
|
|
199
|
+
/** An owner's or a repo's name, as GitHub allows one. */
|
|
200
|
+
const repoName = /^[\w.-]+$/;
|
|
201
|
+
|
|
202
|
+
/** A segment or a fragment as the name it encodes, or as written when it doesn't decode. */
|
|
203
|
+
const decodedSegment = (segment: string) => {
|
|
204
|
+
try {
|
|
205
|
+
return decodeURIComponent(segment);
|
|
206
|
+
} catch {
|
|
207
|
+
return segment;
|
|
208
|
+
}
|
|
183
209
|
};
|
|
184
210
|
|
|
185
|
-
/** A
|
|
186
|
-
const
|
|
211
|
+
/** A segment as the name it encodes, or `undefined` when it doesn't decode. */
|
|
212
|
+
const decodedOrNone = (segment: string) => {
|
|
187
213
|
try {
|
|
188
|
-
return decodeURIComponent(
|
|
214
|
+
return decodeURIComponent(segment);
|
|
189
215
|
} catch {
|
|
190
|
-
return
|
|
216
|
+
return undefined;
|
|
191
217
|
}
|
|
192
218
|
};
|
|
193
219
|
|
|
220
|
+
/**
|
|
221
|
+
* The raw route's ref and path as its router reads them: each segment decoded, and the path's
|
|
222
|
+
* joined, so a `%2F` in one is a folder's slash. `undefined` where the router answers 404, for a
|
|
223
|
+
* segment that doesn't decode or an empty one, and for a path holding `.` or `..` once decoded.
|
|
224
|
+
*/
|
|
225
|
+
const routed = (ref: string, path: string) => {
|
|
226
|
+
const names = path.split('/').map(decodedOrNone);
|
|
227
|
+
const file = names.every((name) => name !== undefined && name !== '') ? names.join('/') : '';
|
|
228
|
+
const name = decodedOrNone(ref);
|
|
229
|
+
if (name === undefined || name === '' || file === '') return undefined;
|
|
230
|
+
|
|
231
|
+
return file.split('/').some((part) => part === '.' || part === '..')
|
|
232
|
+
? undefined
|
|
233
|
+
: { ref: name, path: file };
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The file a comment's root-relative URL names at the drop-in's raw route, resolved as the browser
|
|
238
|
+
* resolves it, its `..`, `%2e` and backslashes too, its query and fragment aside, and read as the
|
|
239
|
+
* route reads it. A URL the browser would send to another host, as `//host/…` or `/\host/…`, names
|
|
240
|
+
* no file.
|
|
241
|
+
*/
|
|
242
|
+
const rawFileAt = (url: string): RepoImage | undefined => {
|
|
243
|
+
const at = url.startsWith('/') ? URL.parse(url, appOrigin) : null;
|
|
244
|
+
if (at?.origin !== appOrigin) return undefined;
|
|
245
|
+
|
|
246
|
+
const [, owner = '', repo = '', ref = '', path = ''] = rawRoute.exec(at.pathname) ?? [];
|
|
247
|
+
const names = [decodedSegment(owner), decodedSegment(repo)];
|
|
248
|
+
const file = routed(ref, path);
|
|
249
|
+
if (file === undefined || !names.every((name) => repoName.test(name))) return undefined;
|
|
250
|
+
|
|
251
|
+
return { repo: names.join('/'), ...file };
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The repo image an image's `src` names, which the browser reads as a blob: a file's relative
|
|
256
|
+
* image, and a comment's image at the drop-in's raw route, any repo's, which the viewer reads as
|
|
257
|
+
* they'd read the file (Q310). `undefined` for every other `src`, which stays as written.
|
|
258
|
+
*/
|
|
259
|
+
export const repoImageAt = (url: string, context: MarkdownContext): RepoImage | undefined => {
|
|
260
|
+
if (context.kind === 'comment') return rawFileAt(url);
|
|
261
|
+
|
|
262
|
+
if (absolute(url)) return undefined;
|
|
263
|
+
|
|
264
|
+
return { repo: context.repo, ref: context.ref, path: decodedPath(inRepo(url, context).path) };
|
|
265
|
+
};
|
|
266
|
+
|
|
194
267
|
/**
|
|
195
268
|
* The ids a fragment, as a link or a page's URL writes it, may reach in rendered text, in the
|
|
196
269
|
* order they're tried: as written, then with the prefix the sanitizer gives every id and name
|
|
@@ -198,7 +271,7 @@ const decodedFragment = (fragment: string) => {
|
|
|
198
271
|
* prefix again, for a name the text wrote with it, which the sanitizer prefixed once more.
|
|
199
272
|
*/
|
|
200
273
|
export const fragmentTargets = (fragment: string) => {
|
|
201
|
-
const decoded =
|
|
274
|
+
const decoded = decodedSegment(fragment);
|
|
202
275
|
const prefixed = decoded.startsWith('user-content-') ? decoded : `user-content-${decoded}`;
|
|
203
276
|
const again = `user-content-${decoded}`;
|
|
204
277
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Clients } from '../clients/clients.ts';
|
|
2
2
|
import type { RepoImage } from './context.ts';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -39,6 +39,7 @@ export const repoImageOf = (image: HTMLImageElement): RepoImage | undefined => {
|
|
|
39
39
|
* or `undefined` when it isn't there, isn't an image, or can't be read.
|
|
40
40
|
*/
|
|
41
41
|
export const repoImageBlob = async (
|
|
42
|
+
clients: Clients,
|
|
42
43
|
image: RepoImage,
|
|
43
44
|
signal: AbortSignal,
|
|
44
45
|
): Promise<Blob | undefined> => {
|
|
@@ -46,7 +47,7 @@ export const repoImageBlob = async (
|
|
|
46
47
|
const [owner = '', repo = ''] = image.repo.split('/');
|
|
47
48
|
if (type === undefined || image.path === '') return undefined;
|
|
48
49
|
|
|
49
|
-
const answer = await github.GET('/repos/{owner}/{repo}/contents/{path}', {
|
|
50
|
+
const answer = await clients.github.GET('/repos/{owner}/{repo}/contents/{path}', {
|
|
50
51
|
params: { path: { owner, repo, path: image.path }, query: { ref: image.ref } },
|
|
51
52
|
headers: { accept: 'application/vnd.github.raw' },
|
|
52
53
|
parseAs: 'blob',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as Schema from 'effect/Schema';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type { Clients } from '../clients/clients.ts';
|
|
4
4
|
import {
|
|
5
5
|
commitKey,
|
|
6
6
|
issueKey,
|
|
@@ -54,19 +54,20 @@ const shaOf = (commit: { readonly sha: string } | string | undefined) =>
|
|
|
54
54
|
const isText = Schema.is(Schema.String);
|
|
55
55
|
|
|
56
56
|
/** The drop-in's answers, through the REST client, as the viewer. */
|
|
57
|
-
export const githubLookups: Lookups
|
|
57
|
+
export const githubLookups = (clients: Clients): Lookups => ({
|
|
58
58
|
issue: async (reference) => {
|
|
59
59
|
const path = { ...ownerAndName(reference.repo), issue_number: Number(reference.number) };
|
|
60
60
|
const issue = existing(
|
|
61
|
-
await github.GET('/repos/{owner}/{repo}/issues/{issue_number}', { params: { path } }),
|
|
61
|
+
await clients.github.GET('/repos/{owner}/{repo}/issues/{issue_number}', { params: { path } }),
|
|
62
62
|
);
|
|
63
63
|
if (issue === undefined) return undefined;
|
|
64
64
|
|
|
65
65
|
return issue.pull_request === undefined ? 'issues' : 'pull';
|
|
66
66
|
},
|
|
67
67
|
user: async (login) =>
|
|
68
|
-
existing(
|
|
69
|
-
|
|
68
|
+
existing(
|
|
69
|
+
await clients.github.GET('/users/{username}', { params: { path: { username: login } } }),
|
|
70
|
+
) !== undefined,
|
|
70
71
|
commit: async ({ repo, sha }, signal) => {
|
|
71
72
|
const written = sha.toLowerCase();
|
|
72
73
|
const at = ownerAndName(repo);
|
|
@@ -74,14 +75,14 @@ export const githubLookups: Lookups = {
|
|
|
74
75
|
// starts answers, or a branch or tag of that name, which the prefix check refuses.
|
|
75
76
|
const full = fullSha.test(written)
|
|
76
77
|
? existing(
|
|
77
|
-
await github.GET('/repos/{owner}/{repo}/git/commits/{commit_sha}', {
|
|
78
|
+
await clients.github.GET('/repos/{owner}/{repo}/git/commits/{commit_sha}', {
|
|
78
79
|
params: { path: { ...at, commit_sha: written } },
|
|
79
80
|
signal: signal ?? null,
|
|
80
81
|
}),
|
|
81
82
|
)?.sha
|
|
82
83
|
: shaOf(
|
|
83
84
|
existing(
|
|
84
|
-
await github.GET('/repos/{owner}/{repo}/commits/{ref}', {
|
|
85
|
+
await clients.github.GET('/repos/{owner}/{repo}/commits/{ref}', {
|
|
85
86
|
params: { path: { ...at, ref: written } },
|
|
86
87
|
signal: signal ?? null,
|
|
87
88
|
}),
|
|
@@ -90,7 +91,7 @@ export const githubLookups: Lookups = {
|
|
|
90
91
|
|
|
91
92
|
return full?.startsWith(written) === true ? full : undefined;
|
|
92
93
|
},
|
|
93
|
-
};
|
|
94
|
+
});
|
|
94
95
|
|
|
95
96
|
/** What exists of `references`, each kind's first `lookedUpMost` looked up at once. */
|
|
96
97
|
export const lookUp = async (references: References, lookups: Lookups): Promise<Found> => {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { htmlMost } from '@ref0/github/markdown';
|
|
2
|
+
|
|
3
|
+
/** A text's share of its page's budget: the most it may render to, and the HTML it drew. */
|
|
4
|
+
export interface Taken {
|
|
5
|
+
readonly most: number;
|
|
6
|
+
readonly html: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What one page's texts may render to in all (#1098): `htmlMost`, shared in the order the page
|
|
11
|
+
* draws them, each text given what's left and charged what it drew. A text is charged once, by its
|
|
12
|
+
* key, however often the page draws it, so the browser's first draw, in the same order, makes the
|
|
13
|
+
* server's choices. Once closed, as the page settles in the browser, the HTML drawn is let go: a
|
|
14
|
+
* text keeps its share, drawn again when asked, and a text new to the page, such as a comment read
|
|
15
|
+
* later or a preview, gets a text's own limit.
|
|
16
|
+
*/
|
|
17
|
+
export interface PageBudget {
|
|
18
|
+
/** `key`'s share, drawing its HTML with `draw` within what's left the first time it's taken. */
|
|
19
|
+
readonly take: (key: string, draw: (most: number) => string) => Taken;
|
|
20
|
+
readonly close: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A page's budget, of `most` in all. */
|
|
24
|
+
export const pageBudget = (most = htmlMost): PageBudget => {
|
|
25
|
+
const shares = new Map<string, number>();
|
|
26
|
+
const taken = new Map<string, Taken>();
|
|
27
|
+
let left = most;
|
|
28
|
+
let open = true;
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
take: (key, draw) => {
|
|
32
|
+
const held = taken.get(key);
|
|
33
|
+
if (held !== undefined) return held;
|
|
34
|
+
|
|
35
|
+
const share = shares.get(key) ?? (open ? left : htmlMost);
|
|
36
|
+
if (!open) return { most: share, html: draw(share) };
|
|
37
|
+
|
|
38
|
+
const drawn = { most: share, html: draw(share) };
|
|
39
|
+
left = Math.max(0, left - drawn.html.length);
|
|
40
|
+
shares.set(key, share);
|
|
41
|
+
taken.set(key, drawn);
|
|
42
|
+
|
|
43
|
+
return drawn;
|
|
44
|
+
},
|
|
45
|
+
close: () => {
|
|
46
|
+
open = false;
|
|
47
|
+
taken.clear();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
};
|
|
@@ -4,6 +4,7 @@ import GithubSlugger from 'github-slugger';
|
|
|
4
4
|
import { githubHtml, unwrapped } from './allowed-html.ts';
|
|
5
5
|
import { repoImageAt, resolveHref, type Linker, type MarkdownContext } from './context.ts';
|
|
6
6
|
import { htmlParts, linkHtml } from './references.ts';
|
|
7
|
+
import { candidatesOf, remoteImageAt } from './remote.ts';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* A hook takes the HTML words made for one element, its children's hooks already run. Words finds
|
|
@@ -17,13 +18,16 @@ type Hook = (html: string) => string;
|
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* One thing words' own render lacks, which GitHub's has: raw tags for words to keep, hooks by tag,
|
|
20
|
-
*
|
|
21
|
-
*
|
|
21
|
+
* URLs to drop, and a pass over the whole render once words ends, for what needs the document's
|
|
22
|
+
* order, which words' hooks don't see: words runs each tag's hooks over every element of that
|
|
23
|
+
* tag before the next tag's. The README beside this file says what each is for and which test
|
|
24
|
+
* proves words still needs it.
|
|
22
25
|
*/
|
|
23
26
|
export interface Patch {
|
|
24
27
|
readonly allowed?: Readonly<Record<string, readonly string[]>>;
|
|
25
28
|
readonly hooks?: Readonly<Record<string, Hook>>;
|
|
26
29
|
readonly drops?: (url: string) => boolean;
|
|
30
|
+
readonly after?: Hook;
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
const entities = new Map([
|
|
@@ -56,6 +60,29 @@ const each = (tags: readonly string[], hook: Hook) =>
|
|
|
56
60
|
|
|
57
61
|
const headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as const;
|
|
58
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Each heading of a render given GitHub's anchor, its slug numbered across every level in the
|
|
65
|
+
* document's order, as GitHub's are: `## a`, `# a`, `### a` are `a`, `a-1`, `a-2`. A heading that
|
|
66
|
+
* holds an `id` keeps it and takes no slug: words writes none, so only a raw heading's own has one.
|
|
67
|
+
* No `<` or `>` stands raw in text, so each is found by its tags. A heading's text ends at the next
|
|
68
|
+
* heading's tag, opening or closing, of any level, as a browser's parser closes a heading there: a
|
|
69
|
+
* raw `<h1>` left open, or one holding another, keeps the headings after it their own.
|
|
70
|
+
*/
|
|
71
|
+
const anchored: Hook = (html) => {
|
|
72
|
+
const slugger = new GithubSlugger();
|
|
73
|
+
|
|
74
|
+
return html.replace(
|
|
75
|
+
/<(h[1-6])((?:[ /][^>]*)?)>([\s\S]*?)(?=<\/?h[1-6][ />]|$)/g,
|
|
76
|
+
(whole, tag: string, rest: string, inner: string) => {
|
|
77
|
+
if (/ id="/.test(rest)) return whole;
|
|
78
|
+
|
|
79
|
+
const id = escapeHtml(`user-content-${slugger.slug(textOf(inner))}`);
|
|
80
|
+
|
|
81
|
+
return `<${tag}${rest} id="${id}">${inner}`;
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
|
|
59
86
|
/** A tag GitHub drops while it shows its text: its text, without it. */
|
|
60
87
|
const unwrap: Hook = (html) => html.replace(/^<[^>]*>/, '').replace(/<\/[a-z]+>\s*$/, '');
|
|
61
88
|
|
|
@@ -153,7 +180,6 @@ export const dropped = (url: string) => /^\s*(?:ftp|tel):/i.test(textOf(url));
|
|
|
153
180
|
*/
|
|
154
181
|
export const patchesFor = (context: MarkdownContext, linker: Linker) => {
|
|
155
182
|
const file = context.kind === 'file';
|
|
156
|
-
const slugger = new GithubSlugger();
|
|
157
183
|
const linked: Hook = (html) => linkHtml(html, context, linker);
|
|
158
184
|
|
|
159
185
|
return {
|
|
@@ -191,22 +217,21 @@ export const patchesFor = (context: MarkdownContext, linker: Linker) => {
|
|
|
191
217
|
},
|
|
192
218
|
|
|
193
219
|
/**
|
|
194
|
-
* A file's image in its repo,
|
|
195
|
-
* `data-path`, which no text can write, as words
|
|
196
|
-
* reads it as a blob through the REST client
|
|
220
|
+
* A file's image in its repo, and a comment's at the drop-in's raw route, Markdown's or raw:
|
|
221
|
+
* no `src`, but `data-repo`, `data-ref` and `data-path`, which no text can write, as words
|
|
222
|
+
* keeps no `data-` attribute, and the browser reads it as a blob through the REST client
|
|
223
|
+
* (Q310), as the raw route serves every file as plain text.
|
|
197
224
|
*/
|
|
198
225
|
repoImages: {
|
|
199
|
-
hooks:
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (image === undefined) return whole;
|
|
226
|
+
hooks: {
|
|
227
|
+
img: (html) =>
|
|
228
|
+
html.replace(/ src="([^"]*)"/, (whole, url: string) => {
|
|
229
|
+
const image = repoImageAt(textOf(url), context);
|
|
230
|
+
if (image === undefined) return whole;
|
|
205
231
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
: {},
|
|
232
|
+
return ` data-repo="${escapeHtml(image.repo)}" data-ref="${escapeHtml(image.ref)}" data-path="${escapeHtml(image.path)}"`;
|
|
233
|
+
}),
|
|
234
|
+
},
|
|
210
235
|
},
|
|
211
236
|
|
|
212
237
|
/** `ftp:` and `tel:` dropped, in Markdown and raw HTML, as GitHub's rule drops them. */
|
|
@@ -221,6 +246,31 @@ export const patchesFor = (context: MarkdownContext, linker: Linker) => {
|
|
|
221
246
|
),
|
|
222
247
|
},
|
|
223
248
|
|
|
249
|
+
/**
|
|
250
|
+
* A remote image, Markdown's or raw, and a `<source>`'s remote `srcset`: no `src` or `srcset`,
|
|
251
|
+
* but `data-remote` or `data-remote-srcset`, which no text can write, for the browser to load
|
|
252
|
+
* through the image proxy once it's signed (Q860), as GitHub's camo loads them.
|
|
253
|
+
*/
|
|
254
|
+
remoteImages: {
|
|
255
|
+
hooks: {
|
|
256
|
+
img: (html) =>
|
|
257
|
+
html.replace(/ src="([^"]*)"/, (whole, url: string) => {
|
|
258
|
+
const remote = remoteImageAt(textOf(url));
|
|
259
|
+
|
|
260
|
+
return remote === undefined ? whole : ` data-remote="${escapeHtml(remote)}"`;
|
|
261
|
+
}),
|
|
262
|
+
source: (html) =>
|
|
263
|
+
html.replace(/ srcset="([^"]*)"/, (whole, srcset: string) => {
|
|
264
|
+
const written = textOf(srcset);
|
|
265
|
+
const remote = candidatesOf(written).some(
|
|
266
|
+
({ url }) => remoteImageAt(url) !== undefined,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return remote ? ` data-remote-srcset="${escapeHtml(written)}"` : whole;
|
|
270
|
+
}),
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
|
|
224
274
|
/** GitHub's alerts, `> [!NOTE]` and the rest, but not a quote in a list, as GitHub leaves it. */
|
|
225
275
|
alerts: {
|
|
226
276
|
hooks: {
|
|
@@ -244,21 +294,11 @@ export const patchesFor = (context: MarkdownContext, linker: Linker) => {
|
|
|
244
294
|
},
|
|
245
295
|
},
|
|
246
296
|
|
|
247
|
-
/**
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const id = escapeHtml(`user-content-${slugger.slug(textOf(html))}`);
|
|
254
|
-
|
|
255
|
-
return html.replace(
|
|
256
|
-
/^<(h[1-6])([^>]*)>/,
|
|
257
|
-
(_opening, tag: string, rest: string) => `<${tag}${rest} id="${id}">`,
|
|
258
|
-
);
|
|
259
|
-
})
|
|
260
|
-
: {},
|
|
261
|
-
},
|
|
297
|
+
/**
|
|
298
|
+
* A file's headings' anchors, GitHub's slugs in the document's order, once words ends, unless
|
|
299
|
+
* the text gave a raw heading an `id`.
|
|
300
|
+
*/
|
|
301
|
+
anchors: file ? { after: anchored } : {},
|
|
262
302
|
} satisfies Record<string, Patch>;
|
|
263
303
|
};
|
|
264
304
|
|
|
@@ -267,7 +307,8 @@ export type PatchName = keyof ReturnType<typeof patchesFor>;
|
|
|
267
307
|
/**
|
|
268
308
|
* What words takes from `patches`: their raw tags, each tag's hooks run one after another, and a
|
|
269
309
|
* `urlTransform` that drops their URLs and resolves a file's links against its repo, ref and folder.
|
|
270
|
-
*
|
|
310
|
+
* Every image keeps its URL for `repoImages`; a comment keeps its links as written. `after` is their
|
|
311
|
+
* passes over the whole render, one after another, for the render to run once words ends.
|
|
271
312
|
*/
|
|
272
313
|
export const compose = (context: MarkdownContext, patches: readonly Patch[]) => {
|
|
273
314
|
const hooks = new Map<string, Hook[]>();
|
|
@@ -295,5 +336,6 @@ export const compose = (context: MarkdownContext, patches: readonly Patch[]) =>
|
|
|
295
336
|
|
|
296
337
|
return key === 'href' ? resolveHref(url, context) : url;
|
|
297
338
|
},
|
|
339
|
+
after: (html: string) => patches.reduce((out, patch) => patch.after?.(out) ?? out, html),
|
|
298
340
|
};
|
|
299
341
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { queryOptions } from '@tanstack/react-query';
|
|
2
2
|
|
|
3
|
+
import type { Clients } from '../clients/clients.ts';
|
|
3
4
|
import type { MarkdownContext } from './context.ts';
|
|
4
5
|
import { githubLookups, lookUp } from './lookup.ts';
|
|
5
6
|
import { referencesIn, renderMarkdown } from './render.ts';
|
|
@@ -16,10 +17,14 @@ export const renderFresh = 60_000;
|
|
|
16
17
|
* same context draws again from the cache for `renderFresh`, so an issue opened again draws its
|
|
17
18
|
* body and comments with no lookups (F10).
|
|
18
19
|
*/
|
|
19
|
-
export const markdownQuery = (text: string, context: MarkdownContext) =>
|
|
20
|
+
export const markdownQuery = (clients: Clients, text: string, context: MarkdownContext) =>
|
|
20
21
|
queryOptions({
|
|
21
22
|
queryKey: ['markdown', context, text],
|
|
22
23
|
queryFn: async () =>
|
|
23
|
-
renderMarkdown(
|
|
24
|
+
renderMarkdown(
|
|
25
|
+
text,
|
|
26
|
+
context,
|
|
27
|
+
await lookUp(referencesIn(text, context), githubLookups(clients)),
|
|
28
|
+
),
|
|
24
29
|
staleTime: renderFresh,
|
|
25
30
|
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A text's remote images, which the image proxy loads in their place (Q860): `remoteImages` in
|
|
3
|
+
* `patches.ts` writes each as `data-remote` or `data-remote-srcset`, with no `src` or `srcset`, and
|
|
4
|
+
* the signer finds the same ones in the same render.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Where a URL with no origin resolves: a stand-in for the app's, which no image is on. */
|
|
8
|
+
const appOrigin = 'https://app.invalid';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The remote image a `src` or a `srcset` candidate names, as the browser would fetch it: `http:`
|
|
12
|
+
* or `https:` on another origin than the app's, `//host/…` and `/\host/…` too, written out whole,
|
|
13
|
+
* or `undefined` for every other URL.
|
|
14
|
+
*/
|
|
15
|
+
export const remoteImageAt = (url: string): string | undefined => {
|
|
16
|
+
const at = URL.parse(url.trim(), appOrigin);
|
|
17
|
+
|
|
18
|
+
return at !== null && at.origin !== appOrigin && /^https?:$/.test(at.protocol)
|
|
19
|
+
? at.href
|
|
20
|
+
: undefined;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** One `srcset` candidate: its URL, and its width or density as written, or `''`. */
|
|
24
|
+
export interface Candidate {
|
|
25
|
+
readonly url: string;
|
|
26
|
+
readonly descriptor: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A `srcset`'s candidates, as HTML's parser reads them: a URL up to whitespace, its trailing commas
|
|
31
|
+
* ending the candidate, else a descriptor up to the next comma outside parentheses.
|
|
32
|
+
*/
|
|
33
|
+
export const candidatesOf = (srcset: string): readonly Candidate[] => {
|
|
34
|
+
const candidates: Candidate[] = [];
|
|
35
|
+
let at = 0;
|
|
36
|
+
|
|
37
|
+
while (at < srcset.length) {
|
|
38
|
+
while (at < srcset.length && /[\s,]/.test(srcset[at] ?? '')) at += 1;
|
|
39
|
+
|
|
40
|
+
if (at >= srcset.length) break;
|
|
41
|
+
|
|
42
|
+
const start = at;
|
|
43
|
+
|
|
44
|
+
while (at < srcset.length && !/\s/.test(srcset[at] ?? '')) at += 1;
|
|
45
|
+
const written = srcset.slice(start, at);
|
|
46
|
+
const url = written.replace(/,+$/, '');
|
|
47
|
+
|
|
48
|
+
if (url !== written) {
|
|
49
|
+
candidates.push({ url, descriptor: '' });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let depth = 0;
|
|
54
|
+
const from = at;
|
|
55
|
+
|
|
56
|
+
for (; at < srcset.length; at += 1) {
|
|
57
|
+
const char = srcset[at];
|
|
58
|
+
if (char === '(') depth += 1;
|
|
59
|
+
else if (char === ')') depth = Math.max(0, depth - 1);
|
|
60
|
+
else if (char === ',' && depth === 0) break;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
candidates.push({ url, descriptor: srcset.slice(from, at).trim() });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return candidates;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** A `srcset` written back from its candidates. */
|
|
70
|
+
export const srcsetOf = (candidates: readonly Candidate[]) =>
|
|
71
|
+
candidates
|
|
72
|
+
.map(({ url, descriptor }) => (descriptor === '' ? url : `${url} ${descriptor}`))
|
|
73
|
+
.join(', ');
|
|
74
|
+
|
|
75
|
+
const entities = new Map([
|
|
76
|
+
['&', '&'],
|
|
77
|
+
['<', '<'],
|
|
78
|
+
['>', '>'],
|
|
79
|
+
['"', '"'],
|
|
80
|
+
[''', "'"],
|
|
81
|
+
[''', "'"],
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
/** An attribute's value as words escapes it, read back. */
|
|
85
|
+
const unescaped = (value: string) =>
|
|
86
|
+
value.replace(/&(?:amp|lt|gt|quot|#x27|#39);/g, (entity) => entities.get(entity) ?? entity);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The remote images a render marked, in the order it marked them: each `data-remote`, and each
|
|
90
|
+
* remote candidate of each `data-remote-srcset`.
|
|
91
|
+
*/
|
|
92
|
+
export const remoteImagesIn = (html: string): readonly string[] => {
|
|
93
|
+
const urls: string[] = [];
|
|
94
|
+
|
|
95
|
+
for (const [, key, value = ''] of html.matchAll(/ data-remote(-srcset)?="([^"]*)"/g)) {
|
|
96
|
+
const text = unescaped(value);
|
|
97
|
+
const found =
|
|
98
|
+
key === undefined
|
|
99
|
+
? [remoteImageAt(text)]
|
|
100
|
+
: candidatesOf(text).map(({ url }) => remoteImageAt(url));
|
|
101
|
+
|
|
102
|
+
urls.push(...found.filter((url) => url !== undefined));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return [...new Set(urls)];
|
|
106
|
+
};
|