@sekiso/markdown 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -5
- package/src/index.test.ts +191 -0
- package/src/index.ts +373 -0
- package/src/text.ts +309 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sekiso/markdown",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Sekiso renderer adapter for Markdown: the same views as text, with no framework and no DOM",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/vercel-labs/sekiso.git"
|
|
8
|
+
"url": "git+https://github.com/vercel-labs/sekiso.git",
|
|
9
|
+
"directory": "packages/markdown"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.ts"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@sekiso/core": "0.0.1",
|
|
17
|
+
"@ui-sdk/core": "0.1.0-beta.17"
|
|
9
18
|
}
|
|
10
|
-
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { defineView } from '@sekiso/core/view'
|
|
3
|
+
import { definePrimitive } from '@ui-sdk/core'
|
|
4
|
+
import { describe, expect, it } from 'vitest'
|
|
5
|
+
import { createMeterApp, meter, meterView, readout, readoutView } from '../../../test/fixtures'
|
|
6
|
+
import { createMarkdown, renderToMarkdown, Scope, Sekiso, View } from './index'
|
|
7
|
+
|
|
8
|
+
describe('markdown adapter', () => {
|
|
9
|
+
it('renders a view as text, with no DOM, and fills its outlet', () => {
|
|
10
|
+
const app = createMeterApp()
|
|
11
|
+
const text = renderToMarkdown(Sekiso({
|
|
12
|
+
app,
|
|
13
|
+
outlets: {
|
|
14
|
+
chart: ({ value }) => `value ${String(value)}`,
|
|
15
|
+
},
|
|
16
|
+
children: View({ of: meterView }),
|
|
17
|
+
}))
|
|
18
|
+
expect(text).toBe('[add] value 0')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('keeps state between renders, and hears every change', () => {
|
|
22
|
+
const app = createMeterApp({ step: 5 })
|
|
23
|
+
let heard = 0
|
|
24
|
+
let api: { add: () => void } | undefined
|
|
25
|
+
const view = defineView(meter, {
|
|
26
|
+
tags: { root: 'p', add: 'button' },
|
|
27
|
+
}, (context, s) => {
|
|
28
|
+
api = context.api
|
|
29
|
+
return s.Root({
|
|
30
|
+
children: String(context.state.value),
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
const markdown = createMarkdown(() => Sekiso({
|
|
34
|
+
app,
|
|
35
|
+
children: View({ of: view }),
|
|
36
|
+
}))
|
|
37
|
+
markdown.subscribe(() => heard++)
|
|
38
|
+
expect(markdown.render()).toBe('0')
|
|
39
|
+
api!.add()
|
|
40
|
+
expect(heard).toBe(1)
|
|
41
|
+
expect(markdown.render()).toBe('5')
|
|
42
|
+
markdown.destroy()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('lets the renderer options win over the theme hints, and gives a primitive the one it needs', () => {
|
|
46
|
+
const app = createMeterApp({ step: 5 })
|
|
47
|
+
let bump: (() => void) | undefined
|
|
48
|
+
const view = defineView(readout, {
|
|
49
|
+
tags: { root: 'p' },
|
|
50
|
+
uses: { meter },
|
|
51
|
+
}, (context, s) => {
|
|
52
|
+
bump = context.api.bump
|
|
53
|
+
return s.Root({ children: `${context.uses.meter.state.value} after ${context.state.presses}` })
|
|
54
|
+
})
|
|
55
|
+
const markdown = createMarkdown(() => Sekiso({
|
|
56
|
+
app,
|
|
57
|
+
children: Scope({
|
|
58
|
+
of: meter,
|
|
59
|
+
options: { step: 2 },
|
|
60
|
+
children: View({ of: view }),
|
|
61
|
+
}),
|
|
62
|
+
}))
|
|
63
|
+
expect(markdown.render()).toBe('0 after 0')
|
|
64
|
+
bump!()
|
|
65
|
+
expect(markdown.render()).toBe('2 after 1')
|
|
66
|
+
markdown.destroy()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('sets the parts of a paragraph on one line', () => {
|
|
70
|
+
const text = renderToMarkdown(Sekiso({
|
|
71
|
+
app: createMeterApp(),
|
|
72
|
+
children: Scope({
|
|
73
|
+
of: meter,
|
|
74
|
+
children: View({
|
|
75
|
+
of: readoutView,
|
|
76
|
+
input: { unit: 'kg' },
|
|
77
|
+
}),
|
|
78
|
+
}),
|
|
79
|
+
}))
|
|
80
|
+
expect(text).toBe('0 kg after 0 [bump]')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('tells no one of a change its own render made, so a page that paints on every change stops', () => {
|
|
84
|
+
const place = definePrimitive({
|
|
85
|
+
name: 'place',
|
|
86
|
+
options: { current: 'value' },
|
|
87
|
+
state: (options: { current?: { path: string } }) => ({ current: options.current }),
|
|
88
|
+
sync: options => ({ current: options.current }),
|
|
89
|
+
api: () => ({}),
|
|
90
|
+
slots: slot => ({
|
|
91
|
+
root: slot({}),
|
|
92
|
+
}),
|
|
93
|
+
})
|
|
94
|
+
const view = defineView(place, {
|
|
95
|
+
tags: { root: 'p' },
|
|
96
|
+
}, ({ state }, s) => s.Root({ children: state.current?.path }))
|
|
97
|
+
let path = '/'
|
|
98
|
+
const markdown = createMarkdown(() => Sekiso({
|
|
99
|
+
app: createMeterApp(),
|
|
100
|
+
// A new object on every render, which a careless renderer passes.
|
|
101
|
+
children: Scope({
|
|
102
|
+
of: place,
|
|
103
|
+
options: {
|
|
104
|
+
current: { path },
|
|
105
|
+
},
|
|
106
|
+
children: View({ of: view }),
|
|
107
|
+
}),
|
|
108
|
+
}))
|
|
109
|
+
let heard = 0
|
|
110
|
+
markdown.subscribe(() => heard++)
|
|
111
|
+
expect(markdown.render()).toBe('/')
|
|
112
|
+
path = '/issues'
|
|
113
|
+
expect(markdown.render()).toBe('/issues')
|
|
114
|
+
expect(markdown.render()).toBe('/issues')
|
|
115
|
+
expect(heard).toBe(0)
|
|
116
|
+
markdown.destroy()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('says which scope is missing', () => {
|
|
120
|
+
const app = createMeterApp()
|
|
121
|
+
expect(() => renderToMarkdown(Sekiso({
|
|
122
|
+
app,
|
|
123
|
+
children: View({
|
|
124
|
+
of: readoutView,
|
|
125
|
+
input: { unit: 'kg' },
|
|
126
|
+
}),
|
|
127
|
+
})))
|
|
128
|
+
.toThrow('"readout" depends on "meter"')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('writes each element as Markdown', () => {
|
|
132
|
+
const page = definePrimitive({
|
|
133
|
+
name: 'page',
|
|
134
|
+
state: () => ({}),
|
|
135
|
+
api: () => ({}),
|
|
136
|
+
slots: slot => ({
|
|
137
|
+
root: slot({}),
|
|
138
|
+
link: slot<{ value: string }>({
|
|
139
|
+
props: { value: 'key' },
|
|
140
|
+
attrs: (_state, part) => ({ href: `/items/${part.props.value}` }),
|
|
141
|
+
}),
|
|
142
|
+
toggle: slot({
|
|
143
|
+
attrs: () => ({ 'aria-pressed': 'true' }),
|
|
144
|
+
}),
|
|
145
|
+
field: slot({
|
|
146
|
+
attrs: () => ({ placeholder: 'Search' }),
|
|
147
|
+
}),
|
|
148
|
+
}),
|
|
149
|
+
})
|
|
150
|
+
const view = defineView(page, {
|
|
151
|
+
tags: {
|
|
152
|
+
root: 'article',
|
|
153
|
+
link: 'a',
|
|
154
|
+
toggle: 'button',
|
|
155
|
+
field: 'input',
|
|
156
|
+
},
|
|
157
|
+
layout: {
|
|
158
|
+
title: 'h1',
|
|
159
|
+
items: 'ul',
|
|
160
|
+
item: 'li',
|
|
161
|
+
note: 'p',
|
|
162
|
+
dot: 'span',
|
|
163
|
+
},
|
|
164
|
+
}, (_context, s) => s.Root({
|
|
165
|
+
children: [
|
|
166
|
+
s.Title({ children: 'A *bold* title' }),
|
|
167
|
+
s.Items({
|
|
168
|
+
children: ['one', 'two'].map(value => s.Item({
|
|
169
|
+
key: value,
|
|
170
|
+
children: s.Link({ value, children: value }),
|
|
171
|
+
})),
|
|
172
|
+
}),
|
|
173
|
+
s.Note({
|
|
174
|
+
children: [
|
|
175
|
+
s.Toggle({ children: 'Open' }),
|
|
176
|
+
s.Field({}),
|
|
177
|
+
s.Dot({ label: 'Online' }),
|
|
178
|
+
],
|
|
179
|
+
}),
|
|
180
|
+
],
|
|
181
|
+
}))
|
|
182
|
+
expect(renderToMarkdown(Sekiso({
|
|
183
|
+
app: createMeterApp(),
|
|
184
|
+
children: View({ of: view }),
|
|
185
|
+
}))).toBe([
|
|
186
|
+
'# A \\*bold\\* title',
|
|
187
|
+
'- [one](/items/one)\n- [two](/items/two)',
|
|
188
|
+
'[x] Open _Search_ Online',
|
|
189
|
+
].join('\n\n'))
|
|
190
|
+
})
|
|
191
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import type { App, AppState } from '@sekiso/core/app'
|
|
2
|
+
import type { AnyView, KElement, KNode, Used, Uses, ViewContext, ViewDefinition } from '@sekiso/core/view'
|
|
3
|
+
import type { OptionsOf, Primitive } from '@ui-sdk/core'
|
|
4
|
+
import type { Flow } from './text'
|
|
5
|
+
import { isElement, needsOf, viewKey } from '@sekiso/core/renderer'
|
|
6
|
+
// What a part renders, read with no DOM. UI SDK marks these stable, and ships them for tests.
|
|
7
|
+
import { partAttrs, partPresent } from '@ui-sdk/core/testing'
|
|
8
|
+
import { escape, flowOfTag, serialize } from './text'
|
|
9
|
+
|
|
10
|
+
type Instance = ReturnType<Primitive>
|
|
11
|
+
/** What a render reads from, an instance or the app. */
|
|
12
|
+
interface Source {
|
|
13
|
+
get: () => object
|
|
14
|
+
subscribe: (listener: () => void) => () => void
|
|
15
|
+
}
|
|
16
|
+
/** Each source a render read, with the state it read. */
|
|
17
|
+
type Read = Map<Source, object>
|
|
18
|
+
type Scopes = ReadonlyMap<Primitive, Instance>
|
|
19
|
+
|
|
20
|
+
/** A framework-native piece for a named outlet: here, a function that returns more of the tree, or text. */
|
|
21
|
+
export type Outlet = (props: Record<string, unknown>) => Child
|
|
22
|
+
type Outlets = Readonly<Record<string, Outlet>>
|
|
23
|
+
|
|
24
|
+
/** What this renderer renders: the same three pieces every adapter has, as plain objects. */
|
|
25
|
+
export type Child = SekisoNode | ScopeNode | ViewNode | string | null | undefined | false | readonly Child[]
|
|
26
|
+
|
|
27
|
+
interface SekisoNode {
|
|
28
|
+
readonly type: 'sekiso'
|
|
29
|
+
readonly app: App
|
|
30
|
+
readonly outlets: Outlets
|
|
31
|
+
readonly children: Child
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface ScopeNode {
|
|
35
|
+
readonly type: 'scope'
|
|
36
|
+
readonly of: Primitive
|
|
37
|
+
readonly options: object | undefined
|
|
38
|
+
readonly children: Child
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface ViewNode {
|
|
42
|
+
readonly type: 'view'
|
|
43
|
+
readonly of: AnyView
|
|
44
|
+
readonly input: unknown
|
|
45
|
+
readonly options: object | undefined
|
|
46
|
+
readonly key: string | number | undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface SekisoProps {
|
|
50
|
+
app: App
|
|
51
|
+
/** What each of the views' named outlets renders. */
|
|
52
|
+
outlets?: Outlets
|
|
53
|
+
children?: Child
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Gives the views below it the app's theme and i18n, and this renderer's outlets. */
|
|
57
|
+
export function Sekiso({ app, outlets, children }: SekisoProps): SekisoNode {
|
|
58
|
+
return {
|
|
59
|
+
type: 'sekiso',
|
|
60
|
+
app,
|
|
61
|
+
outlets: outlets ?? {},
|
|
62
|
+
children,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ScopeProps<P extends Primitive> {
|
|
67
|
+
of: P
|
|
68
|
+
/** The primitive's options, which is where the renderer passes its ports. */
|
|
69
|
+
options?: OptionsOf<P>
|
|
70
|
+
children?: Child
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* One instance of a primitive, shared with every view below. It lives as long
|
|
75
|
+
* as the scope stays at its place in the tree, across renders.
|
|
76
|
+
*/
|
|
77
|
+
export function Scope<P extends Primitive>({ of, options, children }: ScopeProps<P>): ScopeNode {
|
|
78
|
+
return { type: 'scope', of, options, children }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type ViewProps<P extends Primitive, Input, U extends Uses = Uses> = {
|
|
82
|
+
of: ViewDefinition<P, Input, U>
|
|
83
|
+
/** Used only when no scope above holds the view's primitive. */
|
|
84
|
+
options?: OptionsOf<P>
|
|
85
|
+
/** A view with another key starts over, like a route with other params. */
|
|
86
|
+
key?: string | number
|
|
87
|
+
} & (undefined extends Input ? { input?: Input } : {
|
|
88
|
+
/** What this view takes from the renderer, like a route's params. */
|
|
89
|
+
input: Input
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
/** A neutral view, against the nearest scope of its primitive, or its own. */
|
|
93
|
+
export function View<P extends Primitive, Input = undefined, U extends Uses = Uses>(props: ViewProps<P, Input, U>): ViewNode {
|
|
94
|
+
const { of, options, input, key } = props as ViewProps<P, Input, U> & { input?: Input }
|
|
95
|
+
return {
|
|
96
|
+
type: 'view',
|
|
97
|
+
of: of as AnyView,
|
|
98
|
+
input,
|
|
99
|
+
options,
|
|
100
|
+
key,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface Markdown {
|
|
105
|
+
/** The tree as Markdown, from the state it is in now. */
|
|
106
|
+
render: () => string
|
|
107
|
+
/** Hears every change that could change the text: the app's, and every instance's the last render read. */
|
|
108
|
+
subscribe: (listener: () => void) => () => void
|
|
109
|
+
/** Destroys every instance the renderer made. */
|
|
110
|
+
destroy: () => void
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface Pass {
|
|
114
|
+
app: AppState
|
|
115
|
+
outlets: Outlets
|
|
116
|
+
scopes: Scopes
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface Made {
|
|
120
|
+
of: Primitive
|
|
121
|
+
instance: Instance
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let made = 0
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A renderer that keeps its instances between renders, so state lives as long
|
|
128
|
+
* as a scope stays where it is. `tree` runs on every render, so the options it
|
|
129
|
+
* passes, like where the app is, can change. A part's `bind` never runs, since
|
|
130
|
+
* there is no element: a primitive takes its data as an option instead.
|
|
131
|
+
*/
|
|
132
|
+
export function createMarkdown(tree: () => Child): Markdown {
|
|
133
|
+
const instances = new Map<string, Made>()
|
|
134
|
+
const listeners = new Set<() => void>()
|
|
135
|
+
const heard = new Map<Source, () => void>()
|
|
136
|
+
let seen = new Set<string>()
|
|
137
|
+
let rendering = false
|
|
138
|
+
|
|
139
|
+
const tell = (): void => listeners.forEach(listener => listener())
|
|
140
|
+
// A render passes options, and an option can change a state that the render then reads. That
|
|
141
|
+
// change is in the text already. Told, a page that paints on every change would never stop.
|
|
142
|
+
const changed = (): void => {
|
|
143
|
+
if (!rendering)
|
|
144
|
+
tell()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Hears exactly the instances and apps this render read. */
|
|
148
|
+
function hear(sources: Read): void {
|
|
149
|
+
for (const [source, stop] of heard) {
|
|
150
|
+
if (!sources.has(source)) {
|
|
151
|
+
stop()
|
|
152
|
+
heard.delete(source)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const source of sources.keys()) {
|
|
156
|
+
if (!heard.has(source))
|
|
157
|
+
heard.set(source, source.subscribe(changed))
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function instanceAt(path: string, of: Primitive, options: object): Instance {
|
|
162
|
+
seen.add(path)
|
|
163
|
+
const kept = instances.get(path)
|
|
164
|
+
if (kept?.of === of) {
|
|
165
|
+
kept.instance.setOptions(options)
|
|
166
|
+
return kept.instance
|
|
167
|
+
}
|
|
168
|
+
kept?.instance.destroy()
|
|
169
|
+
const instance = of({ id: `md${++made}`, ...options })
|
|
170
|
+
instances.set(path, { of, instance })
|
|
171
|
+
return instance
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function scopeOf(scopes: Scopes, primitive: Primitive, dependent: string): Instance {
|
|
175
|
+
const instance = scopes.get(primitive)
|
|
176
|
+
if (!instance)
|
|
177
|
+
throw new Error(`[sekiso] "${dependent}" depends on "${primitive.name}". Put a Scope({ of: ${primitive.name} }) above it.`)
|
|
178
|
+
return instance
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function optionsFor(pass: Pass, of: Primitive, options: object | undefined): object {
|
|
182
|
+
const needed = Object.fromEntries(
|
|
183
|
+
Object.entries(needsOf(of)).map(([name, primitive]) => [
|
|
184
|
+
name,
|
|
185
|
+
scopeOf(pass.scopes, primitive, of.name),
|
|
186
|
+
]),
|
|
187
|
+
)
|
|
188
|
+
return {
|
|
189
|
+
...pass.app.theme.hints[of.name],
|
|
190
|
+
...needed,
|
|
191
|
+
...options,
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function viewFlow(view: AnyView, input: unknown, options: object | undefined, path: string, pass: Pass, read: Read): Flow {
|
|
196
|
+
const scoped = pass.scopes.get(view.primitive)
|
|
197
|
+
const instance = scoped ?? instanceAt(`${path}/${viewKey(view)}`, view.primitive, optionsFor(pass, view.primitive, options))
|
|
198
|
+
const used = Object.entries(view.uses).map(([name, primitive]) => [
|
|
199
|
+
name,
|
|
200
|
+
scopeOf(pass.scopes, primitive, view.primitive.name),
|
|
201
|
+
] as const)
|
|
202
|
+
const stateOf = (source: Instance): object => {
|
|
203
|
+
const state = source.get()
|
|
204
|
+
read.set(source, state)
|
|
205
|
+
return state
|
|
206
|
+
}
|
|
207
|
+
const context: ViewContext<Primitive, unknown> = {
|
|
208
|
+
state: stateOf(instance),
|
|
209
|
+
api: instance.api,
|
|
210
|
+
uses: Object.fromEntries(used.map(([name, other]): [string, Used<Primitive>] => [
|
|
211
|
+
name,
|
|
212
|
+
{
|
|
213
|
+
state: stateOf(other),
|
|
214
|
+
api: other.api,
|
|
215
|
+
},
|
|
216
|
+
])),
|
|
217
|
+
t: pass.app.i18n.t,
|
|
218
|
+
locale: pass.app.i18n.locale,
|
|
219
|
+
dir: pass.app.i18n.dir,
|
|
220
|
+
input,
|
|
221
|
+
}
|
|
222
|
+
const scopes = scoped
|
|
223
|
+
? pass.scopes
|
|
224
|
+
: new Map(pass.scopes).set(view.primitive, instance)
|
|
225
|
+
return nodeFlow(view.render(context as never), {
|
|
226
|
+
view,
|
|
227
|
+
instance,
|
|
228
|
+
path,
|
|
229
|
+
pass: { ...pass, scopes },
|
|
230
|
+
read,
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
interface Place {
|
|
235
|
+
view: AnyView
|
|
236
|
+
instance: Instance
|
|
237
|
+
path: string
|
|
238
|
+
pass: Pass
|
|
239
|
+
read: Read
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function elementFlow(element: KElement, index: number, place: Place): Flow {
|
|
243
|
+
const { view, instance, path, pass, read } = place
|
|
244
|
+
const at = `${path}/${element.name}:${element.key ?? index}`
|
|
245
|
+
switch (element.kind) {
|
|
246
|
+
case 'part': {
|
|
247
|
+
const part = instance.part(element.name, element.props)
|
|
248
|
+
if (!partPresent(part, element.props))
|
|
249
|
+
return []
|
|
250
|
+
const attributes = partAttrs(part, element.props) as Record<string, unknown>
|
|
251
|
+
return flowOfTag(view.tags[element.name]!, attributes, nodeFlow(element.children, { ...place, path: at }))
|
|
252
|
+
}
|
|
253
|
+
case 'layout': {
|
|
254
|
+
const attributes = element.label === undefined
|
|
255
|
+
? {}
|
|
256
|
+
: { 'aria-label': element.label }
|
|
257
|
+
return flowOfTag(view.tags[element.name]!, attributes, nodeFlow(element.children, { ...place, path: at }))
|
|
258
|
+
}
|
|
259
|
+
// An icon's artwork is the theme's, which text has no use for. The words beside it say the same.
|
|
260
|
+
case 'icon':
|
|
261
|
+
return []
|
|
262
|
+
case 'outlet': {
|
|
263
|
+
const outlet = pass.outlets[element.name]
|
|
264
|
+
return outlet
|
|
265
|
+
? childFlow(outlet({ ...element.props }), at, pass, read)
|
|
266
|
+
: []
|
|
267
|
+
}
|
|
268
|
+
case 'view':
|
|
269
|
+
return viewFlow(element.props.view as AnyView, element.props.input, undefined, at, pass, read)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function nodeFlow(node: KNode, place: Place, index = 0): Flow {
|
|
274
|
+
if (Array.isArray(node))
|
|
275
|
+
return node.flatMap((child: KNode, position) => nodeFlow(child, place, position))
|
|
276
|
+
if (isElement(node))
|
|
277
|
+
return elementFlow(node, index, place)
|
|
278
|
+
if (typeof node === 'string' || typeof node === 'number') {
|
|
279
|
+
return [
|
|
280
|
+
{
|
|
281
|
+
text: escape(String(node)),
|
|
282
|
+
element: false,
|
|
283
|
+
},
|
|
284
|
+
]
|
|
285
|
+
}
|
|
286
|
+
return []
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function childFlow(child: Child, path: string, pass: Pass | undefined, read: Read, index = 0): Flow {
|
|
290
|
+
if (Array.isArray(child))
|
|
291
|
+
return child.flatMap((each: Child, position) => childFlow(each, path, pass, read, position))
|
|
292
|
+
if (!child)
|
|
293
|
+
return []
|
|
294
|
+
if (typeof child === 'string') {
|
|
295
|
+
return [
|
|
296
|
+
{
|
|
297
|
+
text: escape(child),
|
|
298
|
+
element: false,
|
|
299
|
+
},
|
|
300
|
+
]
|
|
301
|
+
}
|
|
302
|
+
const node = child as SekisoNode | ScopeNode | ViewNode
|
|
303
|
+
if (node.type === 'sekiso') {
|
|
304
|
+
const app = node.app.get()
|
|
305
|
+
read.set(node.app, app)
|
|
306
|
+
return childFlow(node.children, `${path}/sekiso`, {
|
|
307
|
+
app,
|
|
308
|
+
outlets: node.outlets,
|
|
309
|
+
scopes: new Map(),
|
|
310
|
+
}, read)
|
|
311
|
+
}
|
|
312
|
+
if (!pass)
|
|
313
|
+
throw new Error('[sekiso] View and Scope must be inside Sekiso.')
|
|
314
|
+
if (node.type === 'scope') {
|
|
315
|
+
const at = `${path}/${index}:${node.of.name}`
|
|
316
|
+
const instance = instanceAt(at, node.of, optionsFor(pass, node.of, node.options))
|
|
317
|
+
read.set(instance, instance.get())
|
|
318
|
+
return childFlow(node.children, at, {
|
|
319
|
+
...pass,
|
|
320
|
+
scopes: new Map(pass.scopes).set(node.of, instance),
|
|
321
|
+
}, read)
|
|
322
|
+
}
|
|
323
|
+
return viewFlow(node.of, node.input, node.options, `${path}/${index}:${node.key ?? ''}`, pass, read)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
render: () => {
|
|
328
|
+
seen = new Set()
|
|
329
|
+
const read: Read = new Map()
|
|
330
|
+
rendering = true
|
|
331
|
+
let blocks: string[]
|
|
332
|
+
try {
|
|
333
|
+
blocks = serialize(childFlow(tree(), '', undefined, read))
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
rendering = false
|
|
337
|
+
}
|
|
338
|
+
// An instance the tree no longer reaches is gone, like a component that unmounted.
|
|
339
|
+
for (const [path, { instance }] of instances) {
|
|
340
|
+
if (!seen.has(path)) {
|
|
341
|
+
instance.destroy()
|
|
342
|
+
instances.delete(path)
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
hear(read)
|
|
346
|
+
// A state that changed after the render read it is not in the text yet.
|
|
347
|
+
if ([...read].some(([source, state]) => source.get() !== state))
|
|
348
|
+
queueMicrotask(tell)
|
|
349
|
+
return blocks.join('\n\n')
|
|
350
|
+
},
|
|
351
|
+
subscribe: (listener) => {
|
|
352
|
+
listeners.add(listener)
|
|
353
|
+
return () => listeners.delete(listener)
|
|
354
|
+
},
|
|
355
|
+
destroy: () => {
|
|
356
|
+
hear(new Map())
|
|
357
|
+
for (const { instance } of instances.values())
|
|
358
|
+
instance.destroy()
|
|
359
|
+
instances.clear()
|
|
360
|
+
},
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Renders a tree once, as Markdown, and destroys what it made. For a server answering one request. */
|
|
365
|
+
export function renderToMarkdown(tree: Child): string {
|
|
366
|
+
const markdown = createMarkdown(() => tree)
|
|
367
|
+
try {
|
|
368
|
+
return markdown.render()
|
|
369
|
+
}
|
|
370
|
+
finally {
|
|
371
|
+
markdown.destroy()
|
|
372
|
+
}
|
|
373
|
+
}
|
package/src/text.ts
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// How elements become Markdown. A view's tree says each part's element, so the
|
|
2
|
+
// element decides: a heading, a paragraph, a list, a link, a button, a field.
|
|
3
|
+
|
|
4
|
+
/** A run of inline text, already in Markdown. `element` says it came from an element, so a space sets it apart from its neighbors. */
|
|
5
|
+
interface Piece {
|
|
6
|
+
readonly text: string
|
|
7
|
+
readonly element: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Block
|
|
11
|
+
= | { readonly kind: 'paragraph', readonly text: string }
|
|
12
|
+
| { readonly kind: 'heading', readonly level: number, readonly text: string }
|
|
13
|
+
| { readonly kind: 'list', readonly ordered: boolean, readonly items: readonly (readonly Block[])[] }
|
|
14
|
+
| { readonly kind: 'item', readonly blocks: readonly Block[] }
|
|
15
|
+
| { readonly kind: 'rule' }
|
|
16
|
+
|
|
17
|
+
/** What a node renders: inline pieces and blocks, in order. */
|
|
18
|
+
export type Flow = readonly (Piece | Block)[]
|
|
19
|
+
|
|
20
|
+
const HEADINGS: Readonly<Record<string, number>> = {
|
|
21
|
+
h1: 1,
|
|
22
|
+
h2: 2,
|
|
23
|
+
h3: 3,
|
|
24
|
+
h4: 4,
|
|
25
|
+
h5: 5,
|
|
26
|
+
h6: 6,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Elements that only hold text. Every element not named here, or below, is a block. */
|
|
30
|
+
const INLINE = new Set([
|
|
31
|
+
'abbr',
|
|
32
|
+
'b',
|
|
33
|
+
'bdi',
|
|
34
|
+
'bdo',
|
|
35
|
+
'cite',
|
|
36
|
+
'data',
|
|
37
|
+
'dfn',
|
|
38
|
+
'kbd',
|
|
39
|
+
'label',
|
|
40
|
+
'mark',
|
|
41
|
+
'output',
|
|
42
|
+
'q',
|
|
43
|
+
's',
|
|
44
|
+
'samp',
|
|
45
|
+
'small',
|
|
46
|
+
'span',
|
|
47
|
+
'sub',
|
|
48
|
+
'sup',
|
|
49
|
+
'time',
|
|
50
|
+
'u',
|
|
51
|
+
'var',
|
|
52
|
+
])
|
|
53
|
+
|
|
54
|
+
const EMPHASIS: Readonly<Record<string, string>> = {
|
|
55
|
+
strong: '**',
|
|
56
|
+
b: '**',
|
|
57
|
+
em: '_',
|
|
58
|
+
i: '_',
|
|
59
|
+
del: '~~',
|
|
60
|
+
s: '~~',
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isPiece(entry: Piece | Block): entry is Piece {
|
|
64
|
+
return 'text' in entry && !('kind' in entry)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Text a person reads, with the characters Markdown would take as syntax escaped. */
|
|
68
|
+
export function escape(text: string): string {
|
|
69
|
+
return text.replace(/[\\`*_[\]<>]/g, '\\$&')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function piece(text: string): Flow {
|
|
73
|
+
return text
|
|
74
|
+
? [
|
|
75
|
+
{ text, element: true },
|
|
76
|
+
]
|
|
77
|
+
: []
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Whether a space goes between two runs: only around an element's, and not before punctuation or after an opening bracket. */
|
|
81
|
+
function spaced(before: string, after: string): boolean {
|
|
82
|
+
return !/\s$/.test(before) && !/^\s/.test(after) && !/^[,.;:!?)\]]/.test(after) && !/[([]$/.test(before)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** One block on one line, for text that must stay on one, like a link's. */
|
|
86
|
+
function flat(block: Block): string {
|
|
87
|
+
switch (block.kind) {
|
|
88
|
+
case 'paragraph':
|
|
89
|
+
case 'heading':
|
|
90
|
+
return block.text
|
|
91
|
+
case 'item':
|
|
92
|
+
return block.blocks.map(flat).join(' ')
|
|
93
|
+
case 'list':
|
|
94
|
+
return block.items.map(item => item.map(flat).join(' ')).join(' ')
|
|
95
|
+
case 'rule':
|
|
96
|
+
return ''
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The text of a flow, as inline Markdown. Blocks inside it are set on its line. */
|
|
101
|
+
function inline(flow: Flow): string {
|
|
102
|
+
let text = ''
|
|
103
|
+
let last: Piece | undefined
|
|
104
|
+
for (const entry of flow) {
|
|
105
|
+
const next: Piece = isPiece(entry)
|
|
106
|
+
? entry
|
|
107
|
+
: {
|
|
108
|
+
text: flat(entry),
|
|
109
|
+
element: true,
|
|
110
|
+
}
|
|
111
|
+
if (!next.text)
|
|
112
|
+
continue
|
|
113
|
+
if (last && (last.element || next.element) && spaced(text, next.text))
|
|
114
|
+
text += ' '
|
|
115
|
+
text += next.text
|
|
116
|
+
last = next
|
|
117
|
+
}
|
|
118
|
+
// Spaces fold into one, as on a page. A line break stays: only text a person wrote has one.
|
|
119
|
+
return text.replace(/[ \t\f\r]+/g, ' ').replace(/ ?\n ?/g, '\n').trim()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function oneLine(flow: Flow): string {
|
|
123
|
+
return inline(flow).replace(/\s+/g, ' ')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** A flow as blocks: each run of inline pieces becomes a paragraph. */
|
|
127
|
+
function blocks(flow: Flow): Block[] {
|
|
128
|
+
const result: Block[] = []
|
|
129
|
+
let run: Piece[] = []
|
|
130
|
+
const close = (): void => {
|
|
131
|
+
const text = inline(run)
|
|
132
|
+
if (text)
|
|
133
|
+
result.push({ kind: 'paragraph', text })
|
|
134
|
+
run = []
|
|
135
|
+
}
|
|
136
|
+
for (const entry of flow) {
|
|
137
|
+
if (isPiece(entry)) {
|
|
138
|
+
run.push(entry)
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
close()
|
|
142
|
+
result.push(entry)
|
|
143
|
+
}
|
|
144
|
+
close()
|
|
145
|
+
return result
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function attribute(attributes: Readonly<Record<string, unknown>>, name: string): string | undefined {
|
|
149
|
+
const value = attributes[name]
|
|
150
|
+
return value === undefined || value === null || value === false
|
|
151
|
+
? undefined
|
|
152
|
+
: String(value)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A field's value as code, or its placeholder in italics, so text a person types stands apart from the page's own. */
|
|
156
|
+
function field(attributes: Readonly<Record<string, unknown>>, children: Flow): Flow {
|
|
157
|
+
const type = attribute(attributes, 'type')
|
|
158
|
+
if (type === 'hidden')
|
|
159
|
+
return []
|
|
160
|
+
if (type === 'checkbox' || type === 'radio') {
|
|
161
|
+
const checked = attributes.checked === true || attribute(attributes, 'aria-checked') === 'true'
|
|
162
|
+
return piece(checked
|
|
163
|
+
? '[x]'
|
|
164
|
+
: '[ ]')
|
|
165
|
+
}
|
|
166
|
+
const value = attribute(attributes, 'value') ?? inline(children)
|
|
167
|
+
if (value)
|
|
168
|
+
return piece(`\`${value.replace(/`/g, '\'').replace(/\s+/g, ' ')}\``)
|
|
169
|
+
const hint = attribute(attributes, 'placeholder') ?? attribute(attributes, 'aria-label')
|
|
170
|
+
return piece(hint
|
|
171
|
+
? `_${escape(hint)}_`
|
|
172
|
+
: '')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function link(attributes: Readonly<Record<string, unknown>>, text: string): Flow {
|
|
176
|
+
const href = attribute(attributes, 'href')
|
|
177
|
+
return href
|
|
178
|
+
? piece(`[${text || escape(href)}](${href.replace(/[\s()]/g, encodeURIComponent)})`)
|
|
179
|
+
: piece(text)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A button in brackets, and one that is pressed or not as a box that is checked or not. */
|
|
183
|
+
function button(attributes: Readonly<Record<string, unknown>>, text: string): Flow {
|
|
184
|
+
const pressed = attribute(attributes, 'aria-pressed') ?? attribute(attributes, 'aria-selected')
|
|
185
|
+
if (pressed === 'true' || pressed === 'false') {
|
|
186
|
+
return piece(`${pressed === 'true'
|
|
187
|
+
? '[x]'
|
|
188
|
+
: '[ ]'} ${text}`)
|
|
189
|
+
}
|
|
190
|
+
return piece(text && `[${text}]`)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function list(ordered: boolean, children: Flow): Flow {
|
|
194
|
+
const items = blocks(children).map(block => block.kind === 'item'
|
|
195
|
+
? block.blocks
|
|
196
|
+
: [block]).filter(item => item.length)
|
|
197
|
+
return items.length
|
|
198
|
+
? [
|
|
199
|
+
{ kind: 'list', ordered, items },
|
|
200
|
+
]
|
|
201
|
+
: []
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** What an element with its attributes and its children's flow renders. */
|
|
205
|
+
export function flowOfTag(tag: string, attributes: Readonly<Record<string, unknown>>, children: Flow): Flow {
|
|
206
|
+
if (attributes.hidden === true || attributes.hidden === '' || attribute(attributes, 'aria-hidden') === 'true')
|
|
207
|
+
return []
|
|
208
|
+
const label = attribute(attributes, 'aria-label')
|
|
209
|
+
const text = (): string => oneLine(children) || (label
|
|
210
|
+
? escape(label)
|
|
211
|
+
: '')
|
|
212
|
+
const level = HEADINGS[tag]
|
|
213
|
+
if (level) {
|
|
214
|
+
return [
|
|
215
|
+
{
|
|
216
|
+
kind: 'heading',
|
|
217
|
+
level,
|
|
218
|
+
text: text(),
|
|
219
|
+
},
|
|
220
|
+
]
|
|
221
|
+
}
|
|
222
|
+
switch (tag) {
|
|
223
|
+
case 'a':
|
|
224
|
+
return link(attributes, text())
|
|
225
|
+
case 'button':
|
|
226
|
+
return button(attributes, text())
|
|
227
|
+
case 'input':
|
|
228
|
+
case 'textarea':
|
|
229
|
+
case 'select':
|
|
230
|
+
return field(attributes, children)
|
|
231
|
+
case 'img': {
|
|
232
|
+
const src = attribute(attributes, 'src')
|
|
233
|
+
return piece(src
|
|
234
|
+
? ``
|
|
235
|
+
: '')
|
|
236
|
+
}
|
|
237
|
+
case 'code':
|
|
238
|
+
return piece(`\`${oneLine(children)}\``)
|
|
239
|
+
case 'br':
|
|
240
|
+
return [
|
|
241
|
+
{ text: '\n', element: false },
|
|
242
|
+
]
|
|
243
|
+
case 'hr':
|
|
244
|
+
return [
|
|
245
|
+
{ kind: 'rule' },
|
|
246
|
+
]
|
|
247
|
+
case 'p':
|
|
248
|
+
return blocks(children)
|
|
249
|
+
case 'li':
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
kind: 'item',
|
|
253
|
+
blocks: blocks(children),
|
|
254
|
+
},
|
|
255
|
+
]
|
|
256
|
+
case 'ul':
|
|
257
|
+
case 'ol':
|
|
258
|
+
case 'menu':
|
|
259
|
+
return list(tag === 'ol', children)
|
|
260
|
+
}
|
|
261
|
+
const mark = EMPHASIS[tag]
|
|
262
|
+
if (mark) {
|
|
263
|
+
const words = oneLine(children)
|
|
264
|
+
return piece(words && `${mark}${words}${mark}`)
|
|
265
|
+
}
|
|
266
|
+
if (INLINE.has(tag)) {
|
|
267
|
+
// An element with no text of its own says what it is through its label, like a dot that shows presence.
|
|
268
|
+
const words = inline(children) || (label
|
|
269
|
+
? escape(label)
|
|
270
|
+
: '')
|
|
271
|
+
return piece(words)
|
|
272
|
+
}
|
|
273
|
+
return blocks(children)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function indent(text: string, by: string): string {
|
|
277
|
+
return text.split('\n').map((line, index) => index && line
|
|
278
|
+
? `${by}${line}`
|
|
279
|
+
: line).join('\n')
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function written(block: Block): string {
|
|
283
|
+
switch (block.kind) {
|
|
284
|
+
case 'paragraph':
|
|
285
|
+
return block.text
|
|
286
|
+
case 'heading':
|
|
287
|
+
return `${'#'.repeat(block.level)} ${block.text}`
|
|
288
|
+
case 'rule':
|
|
289
|
+
return '---'
|
|
290
|
+
case 'item':
|
|
291
|
+
return written({
|
|
292
|
+
kind: 'list',
|
|
293
|
+
ordered: false,
|
|
294
|
+
items: [block.blocks],
|
|
295
|
+
})
|
|
296
|
+
case 'list':
|
|
297
|
+
return block.items.map((item, index) => {
|
|
298
|
+
const marker = block.ordered
|
|
299
|
+
? `${index + 1}.`
|
|
300
|
+
: '-'
|
|
301
|
+
return `${marker} ${indent(item.map(written).join('\n'), ' '.repeat(marker.length + 1))}`
|
|
302
|
+
}).join('\n')
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** A flow as Markdown blocks, each one ready to be set apart from the next by a blank line. */
|
|
307
|
+
export function serialize(flow: Flow): string[] {
|
|
308
|
+
return blocks(flow).map(written).filter(Boolean)
|
|
309
|
+
}
|
package/README.md
DELETED