@yaks/markdown 0.0.0-stage → 0.2.36

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Jeff Peterson
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,95 @@
1
- # Temporary Holding Version
1
+ # @yaks/markdown
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Parses GitHub Flavored Markdown (GFM) and renders it as element nodes for Preact
4
+ in a browser or `@yaks/tui` in a terminal. It uses Marked 18.2.0 for parsing,
5
+ never generates HTML strings, and does not store or fetch documents.
6
+
7
+ ```ts
8
+ import { createElement as h } from 'react'
9
+ import { Bold, Heading, Markdown } from '@yaks/markdown'
10
+
11
+ h(Markdown, { source: '# Hello\n\n**Shared** rendering.' })
12
+ h(Heading, { level: 2 }, h(Bold, null, 'Hello'))
13
+ ```
14
+
15
+ `@yaks/markdown/structural` exports `parse`, `render`, `blocks`, `slugs`,
16
+ `headings`, `safeHref`, and the `Token` type without importing Preact. These
17
+ exports are also available from `@yaks/markdown`.
18
+
19
+ `parse(source, { breaks = true } = {})` returns the Markdown tokens. The default
20
+ turns single newlines into line breaks, suitable for chat messages. Use
21
+ `parse(source, { breaks: false })` for documents whose prose wraps across source
22
+ lines. `render(tokens, h)` targets the portable `@yaks/render` element
23
+ vocabulary, in a `div.Markdown` of its own; `blocks(tokens, h)` is the same
24
+ blocks without it, for a caller that holds them in its own element. `Markdown`
25
+ supplies Preact's `h` and uses the default parsing behavior. `Token` is
26
+ re-exported as a type. `safeHref(href)` permits relative URLs and `http`,
27
+ `https`, or `mailto` URLs, rejecting other explicit schemes and control
28
+ characters/ASCII spaces. It is also used by `Link` and Markdown rendering.
29
+ `Bold`, `Italic`, `Heading`, `Code`, `CodeBlock`, and `Link` are ordinary
30
+ semantic Preact components. Creating a node does not mount it: the application
31
+ passes it to Preact's browser renderer or a `@yaks/tui` screen. Terminal escape
32
+ sequences are generated by the terminal renderer, not this package.
33
+
34
+ A link to another site opens in a new tab. `@yaks/markdown/away` decides it once
35
+ for every link drawn here, in @yaks/ui's parts and in a page's hand-written
36
+ anchors (@yaks/web/away): `leaves(href, site)` is whether an http or https
37
+ address is on a host other than the page's own `site` (`www.` aside), and
38
+ `away(props, site)` adds `target="_blank" rel="noopener"` to such a link that
39
+ names no target. The renderer takes the site as `{ site }` and the components
40
+ from the `Site` context (`@yaks/markdown/site`); unset, it is the location's
41
+ host in a browser and none on a server, where every address leaves.
42
+
43
+ Includes headings, emphasis, strike, inline/fenced code, links, lists/task
44
+ lists, quotes, rules, tables, and explicit/soft line breaks. HTML is literal
45
+ text; unsafe URL schemes are omitted; images become alt-text links rather than
46
+ remote loads. The harness applies Markdown to message prose through
47
+ query-matched transcript renderers. Tool calls/results stay literal (results
48
+ stay dim).
49
+
50
+ The terminal lays out tables with aligned columns, wrapping, and subtle borders.
51
+ Narrow tables switch to stacked header/value records. Markdown column alignment
52
+ is preserved in the shared semantic cells. Rendering does not fetch images or
53
+ other remote content.
54
+
55
+ Entity ids in prose are part of the grammar: a bare `T-123`, `#a1b2c3`, UUID or
56
+ whole SHA parses as a `ref` token, and `[words](T-123)` is a link to one. The
57
+ host says where each leads through `Refs`, `(id) => href | undefined`, passed as
58
+ `{ refs }` to `render`/`blocks` or, for `Markdown`, given by the `Host` context.
59
+ An id the host can open is an `a.Body_Ref` carrying `data-ref`; one it cannot
60
+ open stays its words, and with no host every id is words.
61
+
62
+ An eid (a UUID) in prose is never drawn whole. The host's `Ids`,
63
+ `(eid) => id | undefined`, passed as `{ ids }`, names it: the entity's id
64
+ (`P-1`) where the host already holds the entity or keeps its series in memory.
65
+ Otherwise it is drawn as its short handle from @yaks/id (`#a3f19c024b`). `Refs`
66
+ is asked about the drawn form, so a link never carries a whole uuid, and
67
+ `data-ref` keeps the eid. `drawn(source, ids)` does the same to the text itself,
68
+ for a reader of text such as a model or a printout: each eid written bare or as
69
+ a link's target becomes its drawn form, and code keeps its eids.
70
+ `shown(id, ids)` is the form one id is drawn as. A code span holding a hex word
71
+ (`` `1a2b3c4` ``) is asked about too, the way a commit is quoted. Code stays
72
+ literal. `mentions(tokens)` lists what a document points at, entities and
73
+ addresses, read as it is drawn. `flat` draws links, images and ids as words, for
74
+ a title (`MarkdownInline` takes it too). The same options take
75
+ `code(text, language)`, which colours a fenced block as runs of
76
+ `{ text, classes }`; without it code is plain.
77
+
78
+ The other way round, `typed(source)` lists the ids typed in prose that a store
79
+ would name an entity by, outside code: a human id (`T-12`) or a short handle of
80
+ ten or more hex characters, bare or as a link's target. `stored(source, eids)`
81
+ replaces each one `eids` answers with that eid, and leaves the rest as typed.
82
+ Given the text it replaces, `stored(source, eids, was)` swaps only the ids the
83
+ new text adds: one standing where `was` kept the same id as typed stays typed,
84
+ so `P-1 is very useful` is never bound to whatever later holds P-1, and one
85
+ standing where `was` held the eid it names takes that eid again.
86
+
87
+ `@yaks/markdown/graph` is the graph plugin built on them (@yaks/graph `Prose`):
88
+ a store composing it keeps each id typed in a property declaring `prose`
89
+ (@yaks/vocab) as the eid it names, so the text names the entity in any store and
90
+ under any number. A human id is swapped only where its letter is one the entity
91
+ prints with or carries, so `K-12` is not taken for whatever holds the number 12.
92
+ An id naming nothing stays as typed, and nothing is refused.
93
+
94
+ Run `deno task test packages/markdown` from the repository root for parser,
95
+ structural rendering and URL-safety tests.
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Whether a link leaves the site its page is on, decided once for every link
3
+ * drawn: Markdown (`render` and `blocks`), every @yaks/ui part given an `href`
4
+ * (`el`), and the anchors a page writes by hand, through its one document
5
+ * script (@yaks/web/away). A link that leaves opens in a new tab with
6
+ * `rel="noopener"`, so the person's own tab stays where it was.
7
+ *
8
+ * A page's {@link Site} is its own host and the hosts that count as the same
9
+ * app, such as the yaks.app platform's apex for its pages; `www.` is the same
10
+ * host. A link to an http or https address on any other host leaves, a space's
11
+ * site under yaks.app included. A relative link, mailto:, tel: and sms: stay.
12
+ *
13
+ * ```ts
14
+ * import { leaves } from '@yaks/markdown/away'
15
+ * import { equal } from '@yaks/testing'
16
+ *
17
+ * let site = ['yaks.app']
18
+ * equal(leaves('https://discord.com/x', site), true)
19
+ * equal(leaves('https://ada.yaks.app/notes/', site), true)
20
+ * equal(leaves('https://www.yaks.app/pricing', site), false)
21
+ * equal(leaves('/manage/ada/apps', site), false)
22
+ * equal(leaves('mailto:ada@example.com', site), false)
23
+ * ```
24
+ *
25
+ * @module
26
+ */ /** The hosts that are a page's own site. Preact draws it from the `Site`
27
+ * context (@yaks/markdown/site). */ export type Site = readonly string[];
28
+ /** Where the page is, as far as this process can see: its location's host in
29
+ * a browser, and none in a server drawing a page with no `Site` given, so
30
+ * there every address leaves. */ export declare let here: () => Site;
31
+ /** Whether `href` leads off `site` to another site's http or https address. */ export declare let leaves: (href: unknown, site?: Site) => boolean;
32
+ /** What a link that leaves carries. */ export declare const AWAY: {
33
+ readonly target: '_blank';
34
+ readonly rel: 'noopener';
35
+ };
36
+ /** A link's own props, with {@link AWAY} where it leaves and names no target
37
+ * of its own; a rel it names is kept. */ export declare let away: <P extends Record<string, unknown>>(props: P, site?: Site) => P;
38
+ //# sourceMappingURL=away.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"away.d.ts","sources":["../away.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;CAyBC,GAED;kCACkC,GAClC,YAAY,gBAAgB,MAAM;AAElC;;+BAE+B,GAC/B,OAAO,YAAI,YAAW,KAGrB;AAID,6EAA6E,GAC7E,OAAO,YAAI,SAAU,MAAM,OAAO,EAAE,OAAM,SAAgB,OAAO,CAKhE;AAED,qCAAqC,GACrC,OAAO,cAAM;WAAS,QAAQ;WAAU,KAAK;EAAqB;AAElE;uCACuC,GACvC,OAAO,YAAI,OAAQ,UAAU,OAAO,MAAM,EAAE,OAAO,GACjD,OAAO,GACP,OAAO,SACN,EAGQ"}
@@ -0,0 +1,27 @@
1
+ import type { Plugin, Prose } from "@yaks/graph";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ /**
4
+ * How prose names entities in a store with this vocabulary.
5
+ *
6
+ * ```ts
7
+ * import { assertEquals } from '@std/assert'
8
+ * import { loadVocab } from '@yaks/vocab'
9
+ * import { idKeywords } from '@yaks/id'
10
+ * let vocab = loadVocab({
11
+ * $defs: {
12
+ * task: { component: true, kind: true, prefix: 'T', properties: {} },
13
+ * },
14
+ * }, [idKeywords])
15
+ * let eid = '0e513227-de00-4000-8000-000000000001'
16
+ * let tx = { get: () => [{ entity: { eid, num: 12 }, task: {} }] }
17
+ * let at = new Map([['T-12', eid], ['K-12', eid]])
18
+ * assertEquals(
19
+ * await prose(vocab).stored(['T-12, not K-12 or `T-12`'], at, tx as never),
20
+ * [`${eid}, not K-12 or \`T-12\``],
21
+ * )
22
+ * ```
23
+ */ export declare let prose: (vocab: Vocab) => Prose;
24
+ /** Prose stored as it names entities, as a graph plugin. */ export declare let plugins: (host: {
25
+ vocab: Vocab;
26
+ }) => Plugin[];
27
+ //# sourceMappingURL=graph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.d.ts","sources":["../graph.ts"],"names":[],"mappings":"AAgBA,cAA2B,MAAM,EAAE,KAAK,2BAAwC;AAGhF,cAAc,KAAK,2BAAgC;AAenD;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,YAAI,QAAS,OAAO,UAAQ,MA2BlC;AAED,0DAA0D,GAC1D,OAAO,YAAI,UAAW;EAAQ,OAAO;MAAU,SAE9C"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { type Context, type ReactElement, type ReactNode } from "@types/react";
2
+ import type { H } from "@yaks/render";
3
+ import { type Options } from "./structural.js";
4
+ import { Site } from "./site.js";
5
+ export * from "./structural.js";
6
+ export { Site };
7
+ type Props = {
8
+ children?: ReactNode;
9
+ };
10
+ export declare let Bold: ({}: Props) => ReactElement<{
11
+ className?: string;
12
+ }>;
13
+ export declare let Italic: ({}: Props) => ReactElement<{
14
+ className?: string;
15
+ }>;
16
+ export declare let Heading: ({}: Props & {
17
+ level?: number;
18
+ }) => ReactElement<{
19
+ className?: string;
20
+ }>;
21
+ export declare let Code: ({}: Props) => ReactElement<{
22
+ className?: string;
23
+ }>;
24
+ export declare let CodeBlock: ({}: Props & {
25
+ language?: string;
26
+ }) => ReactElement<{
27
+ className?: string;
28
+ }>;
29
+ export declare let Link: ({}: Props & {
30
+ href: string;
31
+ }) => ReactElement<{
32
+ href: string | undefined;
33
+ className: string;
34
+ }>;
35
+ /** What the host gives every `Markdown` beneath it: where an entity id leads
36
+ * for this reader, and how code is coloured. The browse app gives its own
37
+ * routes; a page whose reader cannot open entities gives none, and ids stay
38
+ * words. */ export declare let Host: Context<Options>;
39
+ /** The host's options as a component beneath it draws with them: `Host`'s,
40
+ * with the page's `Site` where one is given. */ export declare let useHost: () => Options;
41
+ /** Draw a node from the structural renderer with React DOM property names. */ export declare let reactNode: H<ReactElement>;
42
+ /** `breaks: false` reads a document whose prose wraps across source lines. */ export declare let Markdown: ({}: {
43
+ source: string;
44
+ breaks?: boolean;
45
+ }) => ReactElement;
46
+ /** A line of Markdown in the line it stands in, such as a label in a button:
47
+ * its inline markup, in a `span`. Its ids stay words, since the line it
48
+ * stands in is usually itself the thing pressed; `flat` makes its links and
49
+ * images words too, for a line that is itself a link, such as a title. */ export declare let MarkdownInline: ({}: {
50
+ source: string;
51
+ flat?: boolean;
52
+ }) => ReactElement;
53
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAC0C,SACxC,KAAK,OAAO,EAGZ,KAAK,YAAY,EACjB,KAAK,SAAS,uBAES;AACzB,cAAc,CAAC,4BAAiC;AAChD,SAEE,KAAK,OAAO,0BAKU;AAExB,SAAS,IAAI,oBAAmB;AAChC,gCAA+B;AAC/B,SAAS,IAAI,GAAE;KAEV;EAAU,WAAW;;AAC1B,OAAO,YAAI,OAAQ,IAAc,KAAK,KAAG;EAAe,YAAY,MAAM;GACG;AAC7E,OAAO,YAAI,SACT,IAAc,KAAK,KAClB;EAAe,YAAY,MAAM;GACuC;AAC3E,OAAO,YAAI,UACT,IAAyB;EAAU,QAAQ,MAAM;CAAE,KAClD;EAAe,YAAY,MAAM;GAKtB;AACd,OAAO,YAAI,OAAQ,IAAc,KAAK,KAAG;EAAe,YAAY,MAAM;GACD;AACzE,OAAO,YAAI,YACT,IAAwB;EAAU,WAAW,MAAM;CAAE,KACpD;EAAe,YAAY,MAAM;GASjC;AACH,OAAO,YAAI,OACT,IAAoB;EAAU,MAAM,MAAM;CAAE,KAC3C;EAAe,MAAM,MAAM,GAAG,SAAS;EAAE,WAAW,MAAM;GAK1D;AAEH;;;UAGU,GACV,OAAO,YAAI,MAAM,QAAQ,SAAqC;AAE9D;8CAC8C,GAC9C,OAAO,YAAI,eAAc,QAIxB;AAED,4EAA4E,GAC5E,OAAO,YAAI,WAAW,EAAE,cAQvB;AAED,4EAA4E,GAC5E,OAAO,YAAI,WACT;EAAsB,QAAQ,MAAM;EAAE,SAAS,OAAO;CAAE,KACvD,aAKA;AAEH;;;wEAGwE,GACxE,OAAO,YAAI,iBACT;EAAoB,QAAQ,MAAM;EAAE,OAAO,OAAO;CAAE,KACnD,aAQA"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The site the page React is drawing is on (@yaks/markdown/away `Site`), for
3
+ * a page drawn where there is no `location` to read it from: a server gives
4
+ * it once at the page's edge, and every Markdown link and kit part beneath
5
+ * opens another site's address in a new tab by it.
6
+ *
7
+ * @module
8
+ */ import { type Context } from "@types/react";
9
+ import type { Site as Hosts } from "./away.js";
10
+ /** The hosts that are the page's own site; unset, the location's. */ export declare let Site: Context<Hosts | undefined>;
11
+ //# sourceMappingURL=site.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"site.d.ts","sources":["../site.ts"],"names":[],"mappings":"AAAA;;;;;;;CAOC,GACyC,SAAS,KAAK,OAAO,uBAAyC;AACxG,cAAc,QAAQ,KAAK,oBAAmB;AAE9C,mEAAmE,GACnE,OAAO,YAAI,MAAM,QAAQ,QAAQ,SAAS,EAE9B"}
@@ -0,0 +1,100 @@
1
+ /** Markdown as structural nodes, never generated HTML or terminal escapes.
2
+ * Uses the same GFM parser/version as the older canvas markdown renderer.
3
+ */ import { type Token } from "marked";
4
+ import type { Child, H } from "@yaks/render";
5
+ import { type Site } from "./away.js";
6
+ export { type Token } from "marked";
7
+ /** Where an entity id named in prose leads: an href for a reader who can open
8
+ * the entity, or nothing, and then the id stays words. The host gives it, since
9
+ * only the host knows its routes and its reader. It is asked for an id written
10
+ * bare (T-123, #a1b2c3, a 40- or 64-hex SHA), for a link written to one
11
+ * ([the plan](T-123)), and for a code span holding a hex word, the way a commit
12
+ * is quoted (`1a2b3c4`), where a bare word like "decafed" never is. An eid
13
+ * (a UUID) is asked about as it is drawn ({@link Ids}), so a link never
14
+ * carries a whole uuid. */ export type Refs = (id: string) => string | undefined;
15
+ /** What an entity is called where prose holds its eid: its id (`P-1`), from
16
+ * an entity the host has already read, a series it keeps in memory, or the
17
+ * number and letter it read for the eids the prose holds ({@link eids}), or
18
+ * nothing, and then the eid is drawn as its short handle (`#a3f19c024b`). It
19
+ * is asked as the prose is drawn, so the host reads before, never during. */ export type Ids = (eid: string) => string | undefined;
20
+ /** Whether a link's target is an entity id rather than an address. */ export declare let named: (href: string) => boolean;
21
+ /** An id as prose draws it: an eid as the host calls it ({@link Ids}), or its
22
+ * short handle; any other id as written. */ export declare let shown: (id: string, ids?: Ids) => string;
23
+ /** Parse GFM. `breaks` turns a single newline into a line break, which is what
24
+ * a chat message or a terminal line wants and what a document does not: prose
25
+ * wrapped at 80 columns would keep every one of those wraps. An entity id in
26
+ * the prose is a `ref` token of its own; code keeps its ids literal. */ export declare let parse: (source: string, _dts_1: never) => Token[];
27
+ /** Parse a line of GFM, such as a label or a title: its inline markup only,
28
+ * with no paragraph around it. */ export declare let parseInline: (source: string) => Token[];
29
+ /** Something a document points at: an entity, by the id it was written as,
30
+ * or an address. */ export type Mention = {
31
+ kind: 'entity';
32
+ id: string;
33
+ } | {
34
+ kind: 'link';
35
+ href: string;
36
+ };
37
+ /** What a document points at, in reading order, read the way it is drawn: an
38
+ * id written bare, a link written to an id, and a hex word quoted in code
39
+ * (which the host may know as a commit) are entities; any other link or image
40
+ * is its address, unless the renderer would refuse it or it carries a quote
41
+ * or a backslash, the debris of prose around a bare URL. Code keeps its ids
42
+ * literal. */ export declare let mentions: (tokens: Token[]) => Mention[];
43
+ /** Prose as its reader reads it, still as text: each eid it holds, written
44
+ * bare or as a link's target, drawn as {@link shown} draws it, so a model or a
45
+ * printout never reads a whole uuid. Code keeps its ids literal, and a link
46
+ * keeps its own words. What the renderer does to the same text, for a reader
47
+ * of text rather than nodes. */ export declare let drawn: (source: string, ids?: Ids) => string;
48
+ /** The eids prose holds, written bare or as a link's target, in reading
49
+ * order, once each and in lower case: what {@link drawn} and the renderer ask
50
+ * a host's {@link Ids} about. Code keeps its ids literal, so none in it is
51
+ * listed. */ export declare let eids: (source: string) => string[];
52
+ /** The ids typed in prose that a store would name an entity by: each human id
53
+ * (`T-12`) and short handle (`#a3f19c024b`) written bare or as a link's
54
+ * target, in reading order, once each. Code keeps its ids literal, so none in
55
+ * it is listed. What {@link stored} is asked to swap. */ export declare let typed: (source: string) => string[];
56
+ /** Prose as a store keeps it: each id {@link typed} lists that `eids` names
57
+ * replaced by that eid, bare or as a link's target, so the text names the
58
+ * entity in any store and under any number. An id it does not name stays as
59
+ * typed, and so does everything in code and a link's own words.
60
+ *
61
+ * Given the text it replaces (`was`), only the ids the new text adds are
62
+ * swapped: one standing where `was` kept the same id as typed stays typed, so
63
+ * words left alone (`P-1 is very useful`) are never bound to whatever entity
64
+ * comes to hold that id later. One standing where `was` held the eid it names
65
+ * (a body written back as its reader saw it drawn) takes that eid again. */ export declare let stored: (source: string, eids: (id: string) => string | undefined, was?: string) => string;
66
+ /** A document's anchor names, handed out in reading order: lowercase words
67
+ * joined by hyphens, everything that is not a letter or a number dropped — so
68
+ * a name can never carry a quote or an angle bracket into an attribute — and
69
+ * a number on any name the document says twice. */ export declare let slugs: () => (text: string) => string;
70
+ /** Every heading of a document in reading order, each under the id `render`
71
+ * gives it — which is how a contents list links what is on the page without
72
+ * drawing the page a second time. */ export declare let headings: (tokens: Token[]) => {
73
+ depth: number;
74
+ text: string;
75
+ id: string;
76
+ }[];
77
+ /** Only navigable links: no scripting, data, or control-byte URL schemes. */ export declare let safeHref: (href: string) => string | undefined;
78
+ /** What a host gives the renderer: where an entity id leads (`Refs`), what an
79
+ * eid is called (`Ids`), how a fenced block's code is coloured, as runs of
80
+ * text each with its classes, and the page's own site, off which a link
81
+ * opens in a new tab (./away.ts; unset, the location's). */ export type Options = {
82
+ refs?: Refs;
83
+ ids?: Ids;
84
+ code?: (text: string, language?: string) => Run[];
85
+ site?: Site;
86
+ /** Links, images and ids drawn as their words, for a line that is itself
87
+ * the thing pressed, such as a title. */ flat?: boolean;
88
+ };
89
+ /** A run of code text and the classes it is coloured with. */ export type Run = {
90
+ text: string;
91
+ classes: string[];
92
+ };
93
+ /** The parsed tree's blocks through any @yaks/render host (including
94
+ * Preact), for a caller that holds them in an element of its own, such as a
95
+ * page's `Body`. HTML tokens remain text. Images are descriptive links, never
96
+ * remote fetches.
97
+ */ export declare let blocks: <Node>(tokens: Token[], host: H<Node>, _dts_2: never) => Child<Node>[];
98
+ /** Render the parsed tree through any @yaks/render host: its blocks, in the
99
+ * document's own element. */ export declare let render: <Node>(tokens: Token[], host: H<Node>, options?: Options) => Node;
100
+ //# sourceMappingURL=structural.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structural.d.ts","sources":["../structural.ts"],"names":[],"mappings":"AAAA;;CAEC,GACD,SAAwB,KAAK,KAAK,iBAAmD;AAErF,cAAc,KAAK,EAAE,CAAC,4BAAiC;AACvD,SAAe,KAAK,IAAI,oBAAmB;AAE3C,SAAS,KAAK,KAAK,iBAA2B;AAE9C;;;;;;;yBAOyB,GACzB,YAAY,QAAQ,IAAI,MAAM,KAAK,MAAM,GAAG,SAAS;AAErD;;;;2EAI2E,GAC3E,YAAY,OAAO,KAAK,MAAM,KAAK,MAAM,GAAG,SAAS;AAiBrD,oEAAoE,GACpE,OAAO,YAAI,QAAS,MAAM,MAAM,KAAG,OAAO,CACJ;AAEtC;0CAC0C,GAC1C,OAAO,YAAI,QAAS,IAAI,MAAM,EAAE,MAAM,QAAM,MAAM,CACQ;AAkG1D;;;sEAGsE,GACtE,OAAO,YAAI,QACT,QAAQ,MAAM,EACd,kBACC,QAA6D;AAEhE;gCACgC,GAChC,OAAO,YAAI,cAAe,QAAQ,MAAM,KAAG,QACI;AAE/C;kBACkB,GAClB,YAAY;EACN,MAAM;EAAU,IAAI,MAAM;;EAC1B,MAAM;EAAQ,MAAM,MAAM;;AAEhC;;;;;YAKY,GACZ,OAAO,YAAI,WAAY,QAAQ,YAAU,UAgBxC;AA4DD;;;;8BAI8B,GAC9B,OAAO,YAAI,QAAS,QAAQ,MAAM,EAAE,MAAM,QAAM,MAAM,CAG1C;AAEZ;;;WAGW,GACX,OAAO,YAAI,OAAQ,QAAQ,MAAM,KAAG,MAAM,GAQzC;AAQD;;;uDAGuD,GACvD,OAAO,YAAI,QAAS,QAAQ,MAAM,KAAG,MAAM,GAQ1C;AAED;;;;;;;;;0EAS0E,GAC1E,OAAO,YAAI,SACT,QAAQ,MAAM,EACd,OAAO,IAAI,MAAM,KAAK,MAAM,GAAG,SAAS,EACxC,MAAM,MAAM,KACX,MAAM,CAQR;AA6ED;;;iDAGiD,GACjD,OAAO,YAAI,cAAa,MAAM,MAAM,KAAK,MAAM,CAS9C;AAED;;mCAEmC,GACnC,OAAO,YAAI,WACT,QAAQ;EACL,OAAO,MAAM;EAAE,MAAM,MAAM;EAAE,IAAI,MAAM;IAc3C;AAED,2EAA2E,GAC3E,OAAO,YAAI,WAAY,MAAM,MAAM,KAAG,MAAM,GAAG,SAAS,CAQvD;AAkCD;;;0DAG0D,GAC1D,YAAY;EACV,OAAO;EACP,MAAM;EACN,QAAQ,MAAM,MAAM,EAAE,WAAW,MAAM,KAAK;EAC5C,OAAO;EACP;yCACuC,GACvC,OAAO,OAAO;;AAGhB,4DAA4D,GAC5D,YAAY;EAAQ,MAAM,MAAM;EAAE,SAAS,MAAM;;AAEjD;;;;CAIC,GACD,OAAO,YAAI,SAAU,MACnB,QAAQ,SACR,MAAM,EAAE,OACR,kBACC,MAAM,QA0MR;AAED;2BAC2B,GAC3B,OAAO,YAAI,SAAU,MACnB,QAAQ,SACR,MAAM,EAAE,OACR,UAAU,YACT,KAKA"}
package/away.js ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Whether a link leaves the site its page is on, decided once for every link
3
+ * drawn: Markdown (`render` and `blocks`), every @yaks/ui part given an `href`
4
+ * (`el`), and the anchors a page writes by hand, through its one document
5
+ * script (@yaks/web/away). A link that leaves opens in a new tab with
6
+ * `rel="noopener"`, so the person's own tab stays where it was.
7
+ *
8
+ * A page's {@link Site} is its own host and the hosts that count as the same
9
+ * app, such as the yaks.app platform's apex for its pages; `www.` is the same
10
+ * host. A link to an http or https address on any other host leaves, a space's
11
+ * site under yaks.app included. A relative link, mailto:, tel: and sms: stay.
12
+ *
13
+ * ```ts
14
+ * import { leaves } from '@yaks/markdown/away'
15
+ * import { equal } from '@yaks/testing'
16
+ *
17
+ * let site = ['yaks.app']
18
+ * equal(leaves('https://discord.com/x', site), true)
19
+ * equal(leaves('https://ada.yaks.app/notes/', site), true)
20
+ * equal(leaves('https://www.yaks.app/pricing', site), false)
21
+ * equal(leaves('/manage/ada/apps', site), false)
22
+ * equal(leaves('mailto:ada@example.com', site), false)
23
+ * ```
24
+ *
25
+ * @module
26
+ */ /** The hosts that are a page's own site. Preact draws it from the `Site`
27
+ * context (@yaks/markdown/site). */ /** Where the page is, as far as this process can see: its location's host in
28
+ * a browser, and none in a server drawing a page with no `Site` given, so
29
+ * there every address leaves. */ export let here = ()=>{
30
+ let host = globalThis.location?.hostname;
31
+ return host ? [
32
+ host
33
+ ] : [];
34
+ };
35
+ let bare = (host)=>host.toLowerCase().replace(/^www\./, '');
36
+ /** Whether `href` leads off `site` to another site's http or https address. */ export let leaves = (href, site = here())=>{
37
+ if (typeof href != 'string') return false;
38
+ let to = URL.parse(href.startsWith('//') ? `https:${href}` : href);
39
+ return !!to && /^https?:$/.test(to.protocol) && !site.some((host)=>bare(host) == bare(to.hostname));
40
+ };
41
+ /** What a link that leaves carries. */ export const AWAY = {
42
+ target: '_blank',
43
+ rel: 'noopener'
44
+ };
45
+ /** A link's own props, with {@link AWAY} where it leaves and names no target
46
+ * of its own; a rel it names is kept. */ export let away = (props, site)=>props.target == null && leaves(props.href, site) ? {
47
+ ...props,
48
+ ...AWAY,
49
+ ...props.rel ? {
50
+ rel: props.rel
51
+ } : {}
52
+ } : props;
53
+ //# sourceMappingURL=away.js.map
package/away.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"away.js","sources":["./away.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;CAyBC,GAED;kCACkC,GAGlC;;+BAE+B,GAC/B,OAAO,IAAI,OAAO;EAChB,IAAI,OAAO,WAAW,QAAQ,EAAE;EAChC,OAAO,OAAO;IAAC;GAAK,GAAG,EAAE;AAC3B,EAAC;AAED,IAAI,OAAO,CAAC,OAAiB,KAAK,WAAW,GAAG,OAAO,CAAC,UAAU;AAElE,6EAA6E,GAC7E,OAAO,IAAI,SAAS,CAAC,MAAe,OAAa,MAAM;EACrD,IAAI,OAAO,QAAQ,UAAU,OAAO;EACpC,IAAI,KAAK,IAAI,KAAK,CAAC,KAAK,UAAU,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG;EAC7D,OAAO,CAAC,CAAC,MAAM,YAAY,IAAI,CAAC,GAAG,QAAQ,KACzC,CAAC,KAAK,IAAI,CAAC,CAAC,OAAS,KAAK,SAAS,KAAK,GAAG,QAAQ;AACvD,EAAC;AAED,qCAAqC,GACrC,OAAO,MAAM,OAAO;EAAE,QAAQ;EAAU,KAAK;AAAW,EAAU;AAElE;uCACuC,GACvC,OAAO,IAAI,OAAO,CAChB,OACA,OAEA,MAAM,MAAM,IAAI,QAAQ,OAAO,MAAM,IAAI,EAAE,QACvC;IAAE,GAAG,KAAK;IAAE,GAAG,IAAI;IAAE,GAAG,MAAM,GAAG,GAAG;MAAE,KAAK,MAAM,GAAG;IAAC,IAAI,CAAC,CAAC;EAAC,IAC5D,MAAK"}