@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/away.ts ADDED
@@ -0,0 +1,61 @@
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
+ */
27
+
28
+ /** The hosts that are a page's own site. Preact draws it from the `Site`
29
+ * context (@yaks/markdown/site). */
30
+ export type Site = readonly string[]
31
+
32
+ /** Where the page is, as far as this process can see: its location's host in
33
+ * a browser, and none in a server drawing a page with no `Site` given, so
34
+ * there every address leaves. */
35
+ export let here = (): Site => {
36
+ let host = globalThis.location?.hostname
37
+ return host ? [host] : []
38
+ }
39
+
40
+ let bare = (host: string) => host.toLowerCase().replace(/^www\./, '')
41
+
42
+ /** Whether `href` leads off `site` to another site's http or https address. */
43
+ export let leaves = (href: unknown, site: Site = here()): boolean => {
44
+ if (typeof href != 'string') return false
45
+ let to = URL.parse(href.startsWith('//') ? `https:${href}` : href)
46
+ return !!to && /^https?:$/.test(to.protocol) &&
47
+ !site.some((host) => bare(host) == bare(to.hostname))
48
+ }
49
+
50
+ /** What a link that leaves carries. */
51
+ export const AWAY = { target: '_blank', rel: 'noopener' } as const
52
+
53
+ /** A link's own props, with {@link AWAY} where it leaves and names no target
54
+ * of its own; a rel it names is kept. */
55
+ export let away = <P extends Record<string, unknown>>(
56
+ props: P,
57
+ site?: Site,
58
+ ): P =>
59
+ props.target == null && leaves(props.href, site)
60
+ ? { ...props, ...AWAY, ...props.rel ? { rel: props.rel } : {} }
61
+ : props
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/markdown","version":"0.2.36","license":"Apache-2.0","description":"Parses GitHub Flavored Markdown into structural renderer nodes rather than HTML strings or terminal escape sequences.","exports":{".":"./mod.ts","./structural":"./structural.ts","./graph":"./graph.ts","./away":"./away.ts","./site":"./site.ts"},"imports":{"@yaks/markdown":"./mod.ts","@yaks/markdown/structural":"./structural.ts","@yaks/markdown/graph":"./graph.ts","@yaks/markdown/away":"./away.ts","@yaks/markdown/site":"./site.ts"}}
package/graph.js ADDED
@@ -0,0 +1,82 @@
1
+ // The graph plugin this package contributes, at `@yaks/markdown/graph`: prose
2
+ // stored as it names entities (@yaks/graph `Prose`). A person types `T-12`, a
3
+ // model writes back the `#a3f19c024b` it was shown, and the store keeps the
4
+ // eid each names, so the text names the entity in any store and under any
5
+ // number, and is drawn short for its reader (./structural.ts `drawn`).
6
+ //
7
+ // Finding the ids is this package's grammar (./structural.ts `typed`): a
8
+ // human id or a handle written bare or as a link's target, never one in code.
9
+ // Which entity each names is the graph's one read of them all. What is left is
10
+ // being sure the id was meant: a human id's letter must be one the entity it
11
+ // names prints with or carries, so `K-12` in a sentence about schools is not
12
+ // taken for whatever entity holds the number 12.
13
+ //
14
+ // A store composes it or does not. An app's store does not, since an app's
15
+ // code reads its own text as it wrote it.
16
+ import { human, parse, prefixes } from "@yaks/id";
17
+ import { after } from "@yaks/fp";
18
+ import { stored, typed } from "./structural.js";
19
+ /** Every letter an entity answers to: the one it prints with and each its
20
+ * components declare. */ let letters = (vocab)=>{
21
+ let id = human(vocab);
22
+ let declared = prefixes(vocab);
23
+ return (b)=>new Set([
24
+ id(b).split('-')[0],
25
+ ...Object.keys(b).flatMap((c)=>declared[c] ?? [])
26
+ ]);
27
+ };
28
+ /**
29
+ * How prose names entities in a store with this vocabulary.
30
+ *
31
+ * ```ts
32
+ * import { assertEquals } from '@std/assert'
33
+ * import { loadVocab } from '@yaks/vocab'
34
+ * import { idKeywords } from '@yaks/id'
35
+ * let vocab = loadVocab({
36
+ * $defs: {
37
+ * task: { component: true, kind: true, prefix: 'T', properties: {} },
38
+ * },
39
+ * }, [idKeywords])
40
+ * let eid = '0e513227-de00-4000-8000-000000000001'
41
+ * let tx = { get: () => [{ entity: { eid, num: 12 }, task: {} }] }
42
+ * let at = new Map([['T-12', eid], ['K-12', eid]])
43
+ * assertEquals(
44
+ * await prose(vocab).stored(['T-12, not K-12 or `T-12`'], at, tx as never),
45
+ * [`${eid}, not K-12 or \`T-12\``],
46
+ * )
47
+ * ```
48
+ */ export let prose = (vocab)=>{
49
+ let answers = letters(vocab);
50
+ return {
51
+ typed,
52
+ stored: (texts, at, tx, was)=>{
53
+ let lettered = [
54
+ ...at
55
+ ].filter(([id])=>parse(id)?.prefix);
56
+ let eids = [
57
+ ...new Set(lettered.map(([, eid])=>eid))
58
+ ];
59
+ return after(eids.length ? tx.get(eids) : [], (rows)=>{
60
+ let live = new Map(rows.filter((b)=>!b.tombstone).map((b)=>[
61
+ b.entity.eid,
62
+ b
63
+ ]));
64
+ let meant = (id)=>{
65
+ let eid = at.get(id);
66
+ let letter = parse(id)?.prefix;
67
+ if (!eid || !letter) return eid;
68
+ let b = live.get(eid);
69
+ return b && answers(b).has(letter) ? eid : undefined;
70
+ };
71
+ return texts.map((text, i)=>stored(text, meant, was?.[i]));
72
+ });
73
+ }
74
+ };
75
+ };
76
+ /** Prose stored as it names entities, as a graph plugin. */ export let plugins = (host)=>[
77
+ {
78
+ name: '@yaks/markdown',
79
+ prose: prose(host.vocab)
80
+ }
81
+ ];
82
+ //# sourceMappingURL=graph.js.map
package/graph.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph.js","sources":["./graph.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8EAA8E;AAC9E,4EAA4E;AAC5E,0EAA0E;AAC1E,uEAAuE;AACvE,EAAE;AACF,yEAAyE;AACzE,8EAA8E;AAC9E,+EAA+E;AAC/E,6EAA6E;AAC7E,6EAA6E;AAC7E,iDAAiD;AACjD,EAAE;AACF,2EAA2E;AAC3E,0CAA0C;AAG1C,SAAS,KAAK,EAAE,KAAK,EAAE,QAAQ,wBAA6B;AAC5D,SAAS,KAAK,wBAA6B;AAE3C,SAAS,MAAM,EAAE,KAAK,0BAAyB;AAE/C;uBACuB,GACvB,IAAI,UAAU,CAAC;EACb,IAAI,KAAK,MAAM;EACf,IAAI,WAAW,SAAS;EACxB,OAAO,CAAC,IACN,IAAI,IAAI;MACN,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE;SAChB,OAAO,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,IAAM,QAAQ,CAAC,EAAE,IAAI,EAAE;KACnD;AACL;AAEA;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,UAAU,QAAQ;EACtB,OAAO;IACL;IACA,QAAQ,CACN,OACA,IACA,IACA;MAEA,IAAI,WAAW;WAAI;OAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,GAAK,MAAM,KAAK;MACnD,IAAI,OAAO;WAAI,IAAI,IAAI,SAAS,GAAG,CAAC,CAAC,GAAG,IAAI,GAAK;OAAM;MACvD,OAAO,MAAM,KAAK,MAAM,GAAG,GAAG,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC;QAC7C,IAAI,OAAO,IAAI,IACb,KAAK,MAAM,CAAC,CAAC,IAAM,CAAC,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC,IAAM;YAAC,EAAE,MAAM,CAAC,GAAG;YAAE;WAAE;QAE/D,IAAI,QAAQ,CAAC;UACX,IAAI,MAAM,GAAG,GAAG,CAAC;UACjB,IAAI,SAAS,MAAM,KAAK;UACxB,IAAI,CAAC,OAAO,CAAC,QAAQ,OAAO;UAC5B,IAAI,IAAI,KAAK,GAAG,CAAC;UACjB,OAAO,KAAK,QAAQ,GAAG,GAAG,CAAC,UAAU,MAAM;QAC7C;QACA,OAAO,MAAM,GAAG,CAAC,CAAC,MAAM,IAAM,OAAO,MAAM,OAAO,KAAK,CAAC,EAAE;MAC5D;IACF;EACF;AACF,EAAC;AAED,0DAA0D,GAC1D,OAAO,IAAI,UAAU,CAAC,OAAqC;IACzD;MAAE,MAAM;MAAkB,OAAO,MAAM,KAAK,KAAK;IAAE;GACpD,CAAA"}
package/graph.ts ADDED
@@ -0,0 +1,88 @@
1
+ // The graph plugin this package contributes, at `@yaks/markdown/graph`: prose
2
+ // stored as it names entities (@yaks/graph `Prose`). A person types `T-12`, a
3
+ // model writes back the `#a3f19c024b` it was shown, and the store keeps the
4
+ // eid each names, so the text names the entity in any store and under any
5
+ // number, and is drawn short for its reader (./structural.ts `drawn`).
6
+ //
7
+ // Finding the ids is this package's grammar (./structural.ts `typed`): a
8
+ // human id or a handle written bare or as a link's target, never one in code.
9
+ // Which entity each names is the graph's one read of them all. What is left is
10
+ // being sure the id was meant: a human id's letter must be one the entity it
11
+ // names prints with or carries, so `K-12` in a sentence about schools is not
12
+ // taken for whatever entity holds the number 12.
13
+ //
14
+ // A store composes it or does not. An app's store does not, since an app's
15
+ // code reads its own text as it wrote it.
16
+
17
+ import type { Bundle, Eid, Plugin, Prose, ReadTx } from 'jsr:@yaks/graph@0.2.36'
18
+ import { human, parse, prefixes } from '@yaks/id'
19
+ import { after } from '@yaks/fp'
20
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
21
+ import { stored, typed } from './structural.js'
22
+
23
+ /** Every letter an entity answers to: the one it prints with and each its
24
+ * components declare. */
25
+ let letters = (vocab: Vocab) => {
26
+ let id = human(vocab)
27
+ let declared = prefixes(vocab)
28
+ return (b: Bundle): Set<string> =>
29
+ new Set([
30
+ id(b).split('-')[0],
31
+ ...Object.keys(b).flatMap((c) => declared[c] ?? []),
32
+ ])
33
+ }
34
+
35
+ /**
36
+ * How prose names entities in a store with this vocabulary.
37
+ *
38
+ * ```ts
39
+ * import { assertEquals } from '@std/assert'
40
+ * import { loadVocab } from '@yaks/vocab'
41
+ * import { idKeywords } from '@yaks/id'
42
+ * let vocab = loadVocab({
43
+ * $defs: {
44
+ * task: { component: true, kind: true, prefix: 'T', properties: {} },
45
+ * },
46
+ * }, [idKeywords])
47
+ * let eid = '0e513227-de00-4000-8000-000000000001'
48
+ * let tx = { get: () => [{ entity: { eid, num: 12 }, task: {} }] }
49
+ * let at = new Map([['T-12', eid], ['K-12', eid]])
50
+ * assertEquals(
51
+ * await prose(vocab).stored(['T-12, not K-12 or `T-12`'], at, tx as never),
52
+ * [`${eid}, not K-12 or \`T-12\``],
53
+ * )
54
+ * ```
55
+ */
56
+ export let prose = (vocab: Vocab): Prose => {
57
+ let answers = letters(vocab)
58
+ return {
59
+ typed,
60
+ stored: (
61
+ texts: string[],
62
+ at: Map<string, Eid>,
63
+ tx: ReadTx,
64
+ was?: (string | undefined)[],
65
+ ) => {
66
+ let lettered = [...at].filter(([id]) => parse(id)?.prefix)
67
+ let eids = [...new Set(lettered.map(([, eid]) => eid))]
68
+ return after(eids.length ? tx.get(eids) : [], (rows: Bundle[]) => {
69
+ let live = new Map(
70
+ rows.filter((b) => !b.tombstone).map((b) => [b.entity.eid, b]),
71
+ )
72
+ let meant = (id: string): Eid | undefined => {
73
+ let eid = at.get(id)
74
+ let letter = parse(id)?.prefix
75
+ if (!eid || !letter) return eid
76
+ let b = live.get(eid)
77
+ return b && answers(b).has(letter) ? eid : undefined
78
+ }
79
+ return texts.map((text, i) => stored(text, meant, was?.[i]))
80
+ })
81
+ },
82
+ }
83
+ }
84
+
85
+ /** Prose stored as it names entities, as a graph plugin. */
86
+ export let plugins = (host: { vocab: Vocab }): Plugin[] => [
87
+ { name: '@yaks/markdown', prose: prose(host.vocab) },
88
+ ]
package/mod.js ADDED
@@ -0,0 +1,60 @@
1
+ // React components over the shared structural Markdown renderer.
2
+ /* @ts-types="npm:@types/react@19.3.0" */ import { createContext, createElement as h, useContext } from "react";
3
+ import { blocks, parse, parseInline, render, safeHref } from "./structural.js";
4
+ import { away } from "./away.js";
5
+ import { Site } from "./site.js";
6
+ export * from "./structural.js";
7
+ export { Site };
8
+ export let Bold = ({ children })=>/*#__PURE__*/ h('strong', {
9
+ className: 'Body_Strong'
10
+ }, children);
11
+ export let Italic = ({ children })=>/*#__PURE__*/ h('em', {
12
+ className: 'Body_Emphasis'
13
+ }, children);
14
+ export let Heading = ({ level = 1, children })=>/*#__PURE__*/ h('h' + Math.max(1, Math.min(6, Math.trunc(level))), {
15
+ className: `Body_Heading Body_Heading-${Math.max(1, Math.min(6, Math.trunc(level)))}`
16
+ }, children);
17
+ export let Code = ({ children })=>/*#__PURE__*/ h('code', {
18
+ className: 'Body_Code'
19
+ }, children);
20
+ export let CodeBlock = ({ children, language })=>/*#__PURE__*/ h('pre', {
21
+ className: 'Body_Source'
22
+ }, /*#__PURE__*/ h('code', {
23
+ className: 'Body_Code' + (language ? ' language-' + language : '')
24
+ }, children));
25
+ export let Link = ({ href, children })=>/*#__PURE__*/ h('a', away({
26
+ href: safeHref(href),
27
+ className: 'Body_Link'
28
+ }, useHost().site), children);
29
+ /** What the host gives every `Markdown` beneath it: where an entity id leads
30
+ * for this reader, and how code is coloured. The browse app gives its own
31
+ * routes; a page whose reader cannot open entities gives none, and ids stay
32
+ * words. */ export let Host = /*#__PURE__*/ createContext({});
33
+ /** The host's options as a component beneath it draws with them: `Host`'s,
34
+ * with the page's `Site` where one is given. */ export let useHost = ()=>{
35
+ let host = useContext(Host);
36
+ let site = useContext(Site) ?? host.site;
37
+ return site ? {
38
+ ...host,
39
+ site
40
+ } : host;
41
+ };
42
+ /** Draw a node from the structural renderer with React DOM property names. */ export let reactNode = (tag, props, ...children)=>{
43
+ if (!props || !('class' in props)) return /*#__PURE__*/ h(tag, props, ...children);
44
+ let { class: className, ...attributes } = props;
45
+ return /*#__PURE__*/ h(tag, {
46
+ ...attributes,
47
+ className
48
+ }, ...children);
49
+ };
50
+ /** `breaks: false` reads a document whose prose wraps across source lines. */ export let Markdown = ({ source, breaks })=>render(parse(source, {
51
+ breaks
52
+ }), reactNode, useHost());
53
+ /** A line of Markdown in the line it stands in, such as a label in a button:
54
+ * its inline markup, in a `span`. Its ids stay words, since the line it
55
+ * stands in is usually itself the thing pressed; `flat` makes its links and
56
+ * images words too, for a line that is itself a link, such as a title. */ export let MarkdownInline = ({ source, flat })=>/*#__PURE__*/ h('span', null, ...blocks(parseInline(source), reactNode, {
57
+ flat,
58
+ site: useHost().site
59
+ }));
60
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,uCAAuC,GAAG,SAExC,aAAa,EACb,iBAAiB,CAAC,EAGlB,UAAU,gBACa;AAEzB,SACE,MAAM,EAEN,KAAK,EACL,WAAW,EACX,MAAM,EACN,QAAQ,0BACc;AACxB,SAAS,IAAI,oBAAmB;AAChC,SAAS,IAAI,oBAAmB;AAChC,gCAA+B;AAC/B,SAAS,IAAI,GAAE;AAGf,OAAO,IAAI,OAAO,CAAC,EAAE,QAAQ,EAAS,iBACpC,EAA0B,UAAU;IAAE,WAAW;EAAc,GAAG,UAAS;AAC7E,OAAO,IAAI,SAAS,CAClB,EAAE,QAAQ,EAAS,iBAEnB,EAA0B,MAAM;IAAE,WAAW;EAAgB,GAAG,UAAS;AAC3E,OAAO,IAAI,UAAU,CACnB,EAAE,QAAQ,CAAC,EAAE,QAAQ,EAA8B,iBAEnD,EAA0B,MAAM,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,KAAK,KAAK,CAAC,UAAU;IAC3E,WAAW,CAAC,0BAA0B,EACpC,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,KAAK,KAAK,CAAC,UACnC;EACJ,GAAG,UAAS;AACd,OAAO,IAAI,OAAO,CAAC,EAAE,QAAQ,EAAS,iBACpC,EAA0B,QAAQ;IAAE,WAAW;EAAY,GAAG,UAAS;AACzE,OAAO,IAAI,YAAY,CACrB,EAAE,QAAQ,EAAE,QAAQ,EAAiC,iBAErD,EACE,OACA;IAAE,WAAW;EAAc,iBAC3B,EACE,QACA;IAAE,WAAW,cAAc,CAAC,WAAW,eAAe,WAAW,EAAE;EAAE,GACrE,WAEH;AACH,OAAO,IAAI,OAAO,CAChB,EAAE,IAAI,EAAE,QAAQ,EAA4B,iBAE5C,EACE,KACA,KAAK;IAAE,MAAM,SAAS;IAAO,WAAW;EAAY,GAAG,UAAU,IAAI,GACrE,UACD;AAEH;;;UAGU,GACV,OAAO,IAAI,qBAAyB,cAAuB,CAAC,GAAE;AAE9D;8CAC8C,GAC9C,OAAO,IAAI,UAAU;EACnB,IAAI,OAAO,WAAW;EACtB,IAAI,OAAO,WAAW,SAAS,KAAK,IAAI;EACxC,OAAO,OAAO;IAAE,GAAG,IAAI;IAAE;EAAK,IAAI;AACpC,EAAC;AAED,4EAA4E,GAC5E,OAAO,IAAI,YAA6B,CAAC,KAAK,OAAO,GAAG;EACtD,IAAI,CAAC,SAAS,CAAC,CAAC,WAAW,KAAK,GAAG,qBAAO,EAAE,KAAK,UAAU;EAC3D,IAAI,EAAE,OAAO,SAAS,EAAE,GAAG,YAAY,GAAG;EAC1C,qBAAO,EACL,KACA;IAAE,GAAG,UAAU;IAAE;EAAU,MACxB;AAEP,EAAC;AAED,4EAA4E,GAC5E,OAAO,IAAI,WAAW,CACpB,EAAE,MAAM,EAAE,MAAM,EAAwC,GAExD,OACE,MAAM,QAAQ;IAAE;EAAO,IACvB,WACA,WACD;AAEH;;;wEAGwE,GACxE,OAAO,IAAI,iBAAiB,CAC1B,EAAE,MAAM,EAAE,IAAI,EAAsC,iBAEpD,EACE,QACA,SACG,OAAO,YAAY,SAAS,WAAW;IACxC;IACA,MAAM,UAAU,IAAI;EACtB,IACD"}
package/mod.ts ADDED
@@ -0,0 +1,111 @@
1
+ // React components over the shared structural Markdown renderer.
2
+ /* @ts-types="npm:@types/react@19.3.0" */ import {
3
+ type Context,
4
+ createContext,
5
+ createElement as h,
6
+ type ReactElement,
7
+ type ReactNode,
8
+ useContext,
9
+ } from 'react'
10
+ import type { H } from 'jsr:@yaks/render@0.2.36'
11
+ import {
12
+ blocks,
13
+ type Options,
14
+ parse,
15
+ parseInline,
16
+ render,
17
+ safeHref,
18
+ } from './structural.js'
19
+ import { away } from './away.js'
20
+ import { Site } from './site.js'
21
+ export * from './structural.js'
22
+ export { Site }
23
+
24
+ type Props = { children?: ReactNode }
25
+ export let Bold = ({ children }: Props): ReactElement<{ className?: string }> =>
26
+ h<{ className?: string }>('strong', { className: 'Body_Strong' }, children)
27
+ export let Italic = (
28
+ { children }: Props,
29
+ ): ReactElement<{ className?: string }> =>
30
+ h<{ className?: string }>('em', { className: 'Body_Emphasis' }, children)
31
+ export let Heading = (
32
+ { level = 1, children }: Props & { level?: number },
33
+ ): ReactElement<{ className?: string }> =>
34
+ h<{ className?: string }>('h' + Math.max(1, Math.min(6, Math.trunc(level))), {
35
+ className: `Body_Heading Body_Heading-${
36
+ Math.max(1, Math.min(6, Math.trunc(level)))
37
+ }`,
38
+ }, children)
39
+ export let Code = ({ children }: Props): ReactElement<{ className?: string }> =>
40
+ h<{ className?: string }>('code', { className: 'Body_Code' }, children)
41
+ export let CodeBlock = (
42
+ { children, language }: Props & { language?: string },
43
+ ): ReactElement<{ className?: string }> =>
44
+ h<{ className?: string }>(
45
+ 'pre',
46
+ { className: 'Body_Source' },
47
+ h(
48
+ 'code',
49
+ { className: 'Body_Code' + (language ? ' language-' + language : '') },
50
+ children,
51
+ ),
52
+ )
53
+ export let Link = (
54
+ { href, children }: Props & { href: string },
55
+ ): ReactElement<{ href: string | undefined; className: string }> =>
56
+ h(
57
+ 'a',
58
+ away({ href: safeHref(href), className: 'Body_Link' }, useHost().site),
59
+ children,
60
+ )
61
+
62
+ /** What the host gives every `Markdown` beneath it: where an entity id leads
63
+ * for this reader, and how code is coloured. The browse app gives its own
64
+ * routes; a page whose reader cannot open entities gives none, and ids stay
65
+ * words. */
66
+ export let Host: Context<Options> = createContext<Options>({})
67
+
68
+ /** The host's options as a component beneath it draws with them: `Host`'s,
69
+ * with the page's `Site` where one is given. */
70
+ export let useHost = (): Options => {
71
+ let host = useContext(Host)
72
+ let site = useContext(Site) ?? host.site
73
+ return site ? { ...host, site } : host
74
+ }
75
+
76
+ /** Draw a node from the structural renderer with React DOM property names. */
77
+ export let reactNode: H<ReactElement> = (tag, props, ...children) => {
78
+ if (!props || !('class' in props)) return h(tag, props, ...children)
79
+ let { class: className, ...attributes } = props
80
+ return h<{ className?: unknown }>(
81
+ tag,
82
+ { ...attributes, className },
83
+ ...children,
84
+ )
85
+ }
86
+
87
+ /** `breaks: false` reads a document whose prose wraps across source lines. */
88
+ export let Markdown = (
89
+ { source, breaks }: { source: string; breaks?: boolean },
90
+ ): ReactElement =>
91
+ render(
92
+ parse(source, { breaks }),
93
+ reactNode,
94
+ useHost(),
95
+ )
96
+
97
+ /** A line of Markdown in the line it stands in, such as a label in a button:
98
+ * its inline markup, in a `span`. Its ids stay words, since the line it
99
+ * stands in is usually itself the thing pressed; `flat` makes its links and
100
+ * images words too, for a line that is itself a link, such as a title. */
101
+ export let MarkdownInline = (
102
+ { source, flat }: { source: string; flat?: boolean },
103
+ ): ReactElement =>
104
+ h(
105
+ 'span',
106
+ null,
107
+ ...blocks(parseInline(source), reactNode, {
108
+ flat,
109
+ site: useHost().site,
110
+ }),
111
+ )
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@yaks/markdown",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/markdown",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./structural": {
12
+ "types": "./_dist/structural.d.ts",
13
+ "default": "./structural.js"
14
+ },
15
+ "./graph": {
16
+ "types": "./_dist/graph.d.ts",
17
+ "default": "./graph.js"
18
+ },
19
+ "./away": {
20
+ "types": "./_dist/away.d.ts",
21
+ "default": "./away.js"
22
+ },
23
+ "./site": {
24
+ "types": "./_dist/site.d.ts",
25
+ "default": "./site.js"
26
+ }
27
+ },
28
+ "license": "Apache-2.0",
29
+ "dependencies": {
30
+ "@yaks/fp": "0.2.36",
31
+ "@yaks/graph": "0.2.36",
32
+ "@yaks/id": "0.2.36",
33
+ "@yaks/render": "0.2.36",
34
+ "@yaks/vocab": "0.2.36",
35
+ "@types/react": "19.3.0",
36
+ "marked": "18.2.0",
37
+ "react": "19.3.0"
38
+ },
39
+ "description": "Parses GitHub Flavored Markdown into structural renderer nodes rather than HTML strings or terminal escape sequences.",
40
+ "repository": "https://github.com/yak-sh/yak.git"
41
+ }
package/site.js ADDED
@@ -0,0 +1,10 @@
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
+ */ /* @ts-types="npm:@types/react@19.3.0" */ import { createContext } from "react";
9
+ /** The hosts that are the page's own site; unset, the location's. */ export let Site = /*#__PURE__*/ createContext(undefined);
10
+ //# sourceMappingURL=site.js.map
package/site.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"site.js","sources":["./site.ts"],"names":[],"mappings":"AAAA;;;;;;;CAOC,GACD,uCAAuC,GAAG,SAAuB,aAAa,gBAA0B;AAGxG,mEAAmE,GACnE,OAAO,IAAI,qBAAmC,cAE5C,WAAU"}
package/site.ts ADDED
@@ -0,0 +1,15 @@
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
+ */
9
+ /* @ts-types="npm:@types/react@19.3.0" */ import { type Context, createContext } from 'react'
10
+ import type { Site as Hosts } from './away.ts'
11
+
12
+ /** The hosts that are the page's own site; unset, the location's. */
13
+ export let Site: Context<Hosts | undefined> = createContext<
14
+ Hosts | undefined
15
+ >(undefined)