@tempoxyz/ds 0.3.16 → 0.3.17
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/dist/docs/AGENTS.md +4 -2
- package/dist/docs/marketing.md +699 -0
- package/dist/docs/product/documents.md +225 -0
- package/dist/docs/product/foundations.md +1 -1
- package/dist/document/Document.d.ts +46 -0
- package/dist/document/Document.d.ts.map +1 -0
- package/dist/document/Document.js +190 -0
- package/dist/document/Document.js.map +1 -0
- package/dist/document/Enhance.d.ts +13 -0
- package/dist/document/Enhance.d.ts.map +1 -0
- package/dist/document/Enhance.js +421 -0
- package/dist/document/Enhance.js.map +1 -0
- package/dist/document/bin.d.ts +3 -0
- package/dist/document/bin.d.ts.map +1 -0
- package/dist/document/bin.js +38 -0
- package/dist/document/bin.js.map +1 -0
- package/dist/document/document.css +373 -0
- package/dist/document/index.d.ts +3 -0
- package/dist/document/index.d.ts.map +1 -0
- package/dist/document/index.js +3 -0
- package/dist/document/index.js.map +1 -0
- package/dist/document/internal/Cli.d.ts +43 -0
- package/dist/document/internal/Cli.d.ts.map +1 -0
- package/dist/document/internal/Cli.js +87 -0
- package/dist/document/internal/Cli.js.map +1 -0
- package/dist/document/internal/Template.d.ts +21 -0
- package/dist/document/internal/Template.d.ts.map +1 -0
- package/dist/document/internal/Template.js +15 -0
- package/dist/document/internal/Template.js.map +1 -0
- package/dist/document/script.d.ts +2 -0
- package/dist/document/script.d.ts.map +1 -0
- package/dist/document/script.js +379 -0
- package/dist/document/templates/brief.html +96 -0
- package/dist/document/templates/essay.html +70 -0
- package/dist/document/templates/post.html +69 -0
- package/package.json +19 -1
- package/src/document/Document.ts +277 -0
- package/src/document/Enhance.ts +434 -0
- package/src/document/bin.ts +38 -0
- package/src/document/document.css +373 -0
- package/src/document/index.ts +2 -0
- package/src/document/internal/Cli.ts +132 -0
- package/src/document/internal/Template.ts +25 -0
- package/src/document/script.ts +8 -0
- package/src/document/templates/brief.html +96 -0
- package/src/document/templates/essay.html +70 -0
- package/src/document/templates/post.html +69 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document mode: the markup contract for agent-written HTML documents, and a lint that checks a
|
|
3
|
+
* document against it. Documents link `@tempoxyz/ds/document.css` and `@tempoxyz/ds/document.js`,
|
|
4
|
+
* or the same files at `dist/document/document.css` and `dist/document/script.js` on a CDN.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Classes an author may write. Anything else is outside the contract. */
|
|
9
|
+
export const classes = [
|
|
10
|
+
'brief',
|
|
11
|
+
'byline',
|
|
12
|
+
'chart',
|
|
13
|
+
'chart-key',
|
|
14
|
+
'chart-plot',
|
|
15
|
+
'chart-scroll',
|
|
16
|
+
'axis',
|
|
17
|
+
'chronology',
|
|
18
|
+
'dek',
|
|
19
|
+
'doc',
|
|
20
|
+
'facts',
|
|
21
|
+
'figures',
|
|
22
|
+
'fill-ink',
|
|
23
|
+
'fill-line',
|
|
24
|
+
'fill-quiet',
|
|
25
|
+
'input',
|
|
26
|
+
'kv',
|
|
27
|
+
'l-body',
|
|
28
|
+
'l-outset',
|
|
29
|
+
'l-page',
|
|
30
|
+
'l-screen',
|
|
31
|
+
'minor',
|
|
32
|
+
'n',
|
|
33
|
+
'note',
|
|
34
|
+
'outcome',
|
|
35
|
+
'phase',
|
|
36
|
+
'results',
|
|
37
|
+
'sn',
|
|
38
|
+
'steps',
|
|
39
|
+
'summary',
|
|
40
|
+
'swatch',
|
|
41
|
+
] as const
|
|
42
|
+
|
|
43
|
+
/** Classes allowed inside inline SVG diagrams and charts. */
|
|
44
|
+
export const svgClasses = [
|
|
45
|
+
'fill-ink',
|
|
46
|
+
'fill-line',
|
|
47
|
+
'fill-quiet',
|
|
48
|
+
'hair',
|
|
49
|
+
'ink',
|
|
50
|
+
'label',
|
|
51
|
+
'meta',
|
|
52
|
+
] as const
|
|
53
|
+
|
|
54
|
+
/** The templates an `article.doc` can name in `data-template`. */
|
|
55
|
+
export const templates = ['brief', 'essay', 'post'] as const
|
|
56
|
+
|
|
57
|
+
// Patterns that read as UI rather than a document. Matched against whole class names.
|
|
58
|
+
const banned = /^(?:badge|badges|callout|card|cards|chip|chips|pill|pills|tab|tabs|tag|tags)$/
|
|
59
|
+
|
|
60
|
+
// The built script and stylesheet, by package path or by the export names a copied file keeps.
|
|
61
|
+
const script = /(?:^|\/)(?:document\.js|document\/script\.js)(?:[?#]|$)/
|
|
62
|
+
const stylesheet = /(?:^|\/)document\.css(?:[?#]|$)/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Checks an HTML document against the document mode contract and returns its diagnostics in
|
|
66
|
+
* line order. An empty array means the document passes.
|
|
67
|
+
*
|
|
68
|
+
* The check reads tags with a small scanner rather than a full HTML parser: it skips comments, text,
|
|
69
|
+
* and the contents of `<script>` and `<style>`, and it pairs `<figure>` tags by nesting. Markup built
|
|
70
|
+
* at runtime is not seen.
|
|
71
|
+
*
|
|
72
|
+
* With `page`, the lint also checks that the tags before `<body>` link the document stylesheet and
|
|
73
|
+
* load the document script with `defer`, as one classic script.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* Document.lint('<article class="doc" data-template="post"><h4>Steps</h4></article>')
|
|
77
|
+
* // [{ line: 1, message: '<h4> is not allowed. Use h2 for sections and h3 below them.', rule: 'heading-depth' }]
|
|
78
|
+
*/
|
|
79
|
+
export function lint(html: string, options: lint.Options = {}): lint.Diagnostic[] {
|
|
80
|
+
const { page = false } = options
|
|
81
|
+
const diagnostics: lint.Diagnostic[] = []
|
|
82
|
+
const report = (index: number, rule: lint.Rule, message: string) =>
|
|
83
|
+
diagnostics.push({ line: html.slice(0, index).split('\n').length, message, rule })
|
|
84
|
+
const figures: {
|
|
85
|
+
caption: boolean
|
|
86
|
+
chart: boolean
|
|
87
|
+
index: number
|
|
88
|
+
key: boolean
|
|
89
|
+
series: number
|
|
90
|
+
}[] = []
|
|
91
|
+
const head = { index: 0, open: true, script: false, stylesheet: false }
|
|
92
|
+
let svg = 0
|
|
93
|
+
|
|
94
|
+
for (const tag of scan(html)) {
|
|
95
|
+
if (tag.name === 'body' || tag.name === 'article' || (tag.name === 'head' && tag.closing))
|
|
96
|
+
head.open = false
|
|
97
|
+
if (tag.name === 'head' && !tag.closing) head.index = tag.index
|
|
98
|
+
if (head.open && !tag.closing) {
|
|
99
|
+
const href = attribute(tag.attributes, 'href') ?? ''
|
|
100
|
+
const src = attribute(tag.attributes, 'src') ?? ''
|
|
101
|
+
if (
|
|
102
|
+
tag.name === 'link' &&
|
|
103
|
+
attribute(tag.attributes, 'rel')?.toLowerCase() === 'stylesheet' &&
|
|
104
|
+
stylesheet.test(href)
|
|
105
|
+
)
|
|
106
|
+
head.stylesheet = true
|
|
107
|
+
if (
|
|
108
|
+
tag.name === 'script' &&
|
|
109
|
+
attribute(tag.attributes, 'defer') !== undefined &&
|
|
110
|
+
attribute(tag.attributes, 'type') === undefined &&
|
|
111
|
+
script.test(src)
|
|
112
|
+
)
|
|
113
|
+
head.script = true
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (tag.name === 'svg' && !tag.selfClosing) svg += tag.closing ? -1 : 1
|
|
117
|
+
if (tag.closing) {
|
|
118
|
+
if (tag.name !== 'figure') continue
|
|
119
|
+
const figure = figures.pop()
|
|
120
|
+
if (!figure) continue
|
|
121
|
+
if (!figure.caption)
|
|
122
|
+
report(
|
|
123
|
+
figure.index,
|
|
124
|
+
'figure-caption',
|
|
125
|
+
'A <figure> needs a <figcaption> that states its takeaway.',
|
|
126
|
+
)
|
|
127
|
+
if (figure.chart && figure.series > 1 && !figure.key)
|
|
128
|
+
report(figure.index, 'chart-key', 'A chart with more than one series needs a ul.chart-key.')
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const names = (attribute(tag.attributes, 'class') ?? '').split(/\s+/).filter(Boolean)
|
|
133
|
+
const inSvg = svg > 0 || tag.name === 'svg'
|
|
134
|
+
|
|
135
|
+
if (/^h[4-6]$/.test(tag.name))
|
|
136
|
+
report(
|
|
137
|
+
tag.index,
|
|
138
|
+
'heading-depth',
|
|
139
|
+
`<${tag.name}> is not allowed. Use h2 for sections and h3 below them.`,
|
|
140
|
+
)
|
|
141
|
+
if (tag.name === 'details')
|
|
142
|
+
report(
|
|
143
|
+
tag.index,
|
|
144
|
+
'no-disclosure',
|
|
145
|
+
`<${tag.name}> is not allowed. A document shows everything it says.`,
|
|
146
|
+
)
|
|
147
|
+
const role = attribute(tag.attributes, 'role')
|
|
148
|
+
if (role === 'tab' || role === 'tablist')
|
|
149
|
+
report(tag.index, 'no-ui', `role="${role}" is not allowed. A document has no tabs.`)
|
|
150
|
+
|
|
151
|
+
for (const name of names) {
|
|
152
|
+
if (banned.test(name) || /^(?:badge|card|chip|pill|tab)-/.test(name))
|
|
153
|
+
report(tag.index, 'no-ui', `.${name} reads as UI. Write it as text, a table, or a list.`)
|
|
154
|
+
else if (!(inSvg ? svgClasses : classes).includes(name as never))
|
|
155
|
+
report(tag.index, 'unknown-class', `.${name} is not in the document mode contract.`)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (tag.name === 'article' && names.includes('doc')) {
|
|
159
|
+
const template = attribute(tag.attributes, 'data-template')
|
|
160
|
+
if (!templates.includes(template as never))
|
|
161
|
+
report(
|
|
162
|
+
tag.index,
|
|
163
|
+
'template',
|
|
164
|
+
'An article.doc needs data-template="essay", "brief", or "post".',
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (tag.name === 'figure') {
|
|
169
|
+
const chart = names.includes('chart')
|
|
170
|
+
if (chart && names.includes('l-screen'))
|
|
171
|
+
report(
|
|
172
|
+
tag.index,
|
|
173
|
+
'chart-width',
|
|
174
|
+
'A chart stays inside the page. Use l-body, l-outset, or l-page.',
|
|
175
|
+
)
|
|
176
|
+
figures.push({
|
|
177
|
+
caption: false,
|
|
178
|
+
chart,
|
|
179
|
+
index: tag.index,
|
|
180
|
+
key: false,
|
|
181
|
+
series: Number(attribute(tag.attributes, 'data-series') ?? 1),
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
const figure = figures.at(-1)
|
|
185
|
+
if (figure && tag.name === 'figcaption') figure.caption = true
|
|
186
|
+
if (figure && names.includes('chart-key')) figure.key = true
|
|
187
|
+
|
|
188
|
+
if (names.includes('chart-scroll')) {
|
|
189
|
+
const named =
|
|
190
|
+
attribute(tag.attributes, 'aria-label') ?? attribute(tag.attributes, 'aria-labelledby')
|
|
191
|
+
if (
|
|
192
|
+
attribute(tag.attributes, 'role') !== 'region' ||
|
|
193
|
+
attribute(tag.attributes, 'tabindex') !== '0' ||
|
|
194
|
+
!named
|
|
195
|
+
)
|
|
196
|
+
report(
|
|
197
|
+
tag.index,
|
|
198
|
+
'chart-scroll',
|
|
199
|
+
'.chart-scroll needs role="region", tabindex="0", and an accessible name.',
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (page && !head.stylesheet)
|
|
205
|
+
report(
|
|
206
|
+
head.index,
|
|
207
|
+
'stylesheet',
|
|
208
|
+
'The <head> needs <link rel="stylesheet"> to the document stylesheet, dist/document/document.css.',
|
|
209
|
+
)
|
|
210
|
+
if (page && !head.script)
|
|
211
|
+
report(
|
|
212
|
+
head.index,
|
|
213
|
+
'script',
|
|
214
|
+
'The <head> needs <script defer src> with the document script, dist/document/script.js, and no type.',
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
return diagnostics.sort((a, b) => a.line - b.line)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export declare namespace lint {
|
|
221
|
+
/** One problem in a document, at the line of the tag that causes it. */
|
|
222
|
+
type Diagnostic = { line: number; message: string; rule: Rule }
|
|
223
|
+
|
|
224
|
+
type Options = {
|
|
225
|
+
/**
|
|
226
|
+
* Whether the HTML is a whole page, whose head must link the document stylesheet and load the
|
|
227
|
+
* document script with `defer`. Defaults to false, which checks a fragment.
|
|
228
|
+
*/
|
|
229
|
+
page?: boolean | undefined
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The checks the lint runs. */
|
|
233
|
+
type Rule =
|
|
234
|
+
| 'chart-key'
|
|
235
|
+
| 'chart-scroll'
|
|
236
|
+
| 'chart-width'
|
|
237
|
+
| 'figure-caption'
|
|
238
|
+
| 'heading-depth'
|
|
239
|
+
| 'no-disclosure'
|
|
240
|
+
| 'no-ui'
|
|
241
|
+
| 'script'
|
|
242
|
+
| 'stylesheet'
|
|
243
|
+
| 'template'
|
|
244
|
+
| 'unknown-class'
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Returns an attribute's value, or undefined when the tag does not have it. */
|
|
248
|
+
function attribute(attributes: string, name: string): string | undefined {
|
|
249
|
+
const pattern = new RegExp(
|
|
250
|
+
`(?:^|\\s)${name}(?:\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+)))?(?=\\s|$)`,
|
|
251
|
+
'i',
|
|
252
|
+
)
|
|
253
|
+
const match = pattern.exec(attributes)
|
|
254
|
+
if (!match) return undefined
|
|
255
|
+
return match[1] ?? match[2] ?? match[3] ?? ''
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Yields each tag in source order, skipping comments. A raw text element, such as `<script>`, yields
|
|
260
|
+
* its start tag and skips its contents and end tag.
|
|
261
|
+
*/
|
|
262
|
+
function* scan(html: string) {
|
|
263
|
+
const pattern =
|
|
264
|
+
/<!--[\s\S]*?-->|<(script|style|textarea)\b((?:[^>"']|"[^"]*"|'[^']*')*)>[\s\S]*?<\/\1\s*>|<(\/?)([a-zA-Z][\w-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>/g
|
|
265
|
+
|
|
266
|
+
for (const match of html.matchAll(pattern)) {
|
|
267
|
+
if (match[0].startsWith('<!--')) continue
|
|
268
|
+
const attributes = (match[1] ? match[2] : match[5])?.trim() ?? ''
|
|
269
|
+
yield {
|
|
270
|
+
attributes: attributes.replace(/\/$/, ''),
|
|
271
|
+
closing: match[3] === '/',
|
|
272
|
+
index: match.index,
|
|
273
|
+
name: (match[1] ?? match[4] ?? '').toLowerCase(),
|
|
274
|
+
selfClosing: attributes.endsWith('/'),
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|