@yaks/text 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 +202 -0
- package/README.md +82 -2
- package/_dist/format.d.ts +2 -0
- package/_dist/format.d.ts.map +1 -0
- package/_dist/mod.d.ts +39 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/possessive.d.ts +14 -0
- package/_dist/possessive.d.ts.map +1 -0
- package/_dist/safe.d.ts +3 -0
- package/_dist/safe.d.ts.map +1 -0
- package/_dist/tree.d.ts +105 -0
- package/_dist/tree.d.ts.map +1 -0
- package/deno.json +1 -0
- package/format.js +242 -0
- package/format.js.map +1 -0
- package/format.ts +253 -0
- package/mod.js +50 -0
- package/mod.js.map +1 -0
- package/mod.ts +85 -0
- package/package.json +21 -4
- package/possessive.js +14 -0
- package/possessive.js.map +1 -0
- package/possessive.ts +15 -0
- package/safe.js +10 -0
- package/safe.js.map +1 -0
- package/safe.ts +15 -0
- package/tree.js +103 -0
- package/tree.js.map +1 -0
- package/tree.ts +110 -0
package/possessive.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** A name made possessive: "Ada’s", but a name ending in s takes the bare
|
|
2
|
+
* apostrophe, "checks’" and "James’".
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { assertEquals } from '@std/assert'
|
|
6
|
+
* import { possessive } from '@yaks/text/possessive'
|
|
7
|
+
*
|
|
8
|
+
* assertEquals(possessive('Ada'), 'Ada’s')
|
|
9
|
+
* assertEquals(possessive('James'), 'James’')
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/ export let possessive = (name)=>/s$/i.test(name) ? `${name}’` : `${name}’s`;
|
|
14
|
+
//# sourceMappingURL=possessive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"possessive.js","sources":["./possessive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,aAAa,CAAC,OACvB,MAAM,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,CAAC,GAAG,GAAG,KAAK,EAAE,CAAC,CAAA"}
|
package/possessive.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A name made possessive: "Ada’s", but a name ending in s takes the bare
|
|
2
|
+
* apostrophe, "checks’" and "James’".
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { assertEquals } from '@std/assert'
|
|
6
|
+
* import { possessive } from '@yaks/text/possessive'
|
|
7
|
+
*
|
|
8
|
+
* assertEquals(possessive('Ada'), 'Ada’s')
|
|
9
|
+
* assertEquals(possessive('James'), 'James’')
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
export let possessive = (name: string): string =>
|
|
15
|
+
/s$/i.test(name) ? `${name}’` : `${name}’s`
|
package/safe.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This renderer's content boundary. Every text leaf and link destination loses
|
|
3
|
+
* its C0, DEL and C1 bytes before any formatting is added. Unlike the older
|
|
4
|
+
* terminal helper, this keeps no search-highlight markers, tabs or literal
|
|
5
|
+
* newlines; structural elements are the only source of output line breaks.
|
|
6
|
+
*/ // deno-lint-ignore no-control-regex -- the full control class is the boundary
|
|
7
|
+
let controls = /[\x00-\x1f\x7f-\x9f]/g;
|
|
8
|
+
/** Strip every C0/DEL/C1 byte from content, including tabs and newlines. */ export let safe = (text)=>text.replace(controls, '');
|
|
9
|
+
/** Strip the same bytes from a destination before it enters link syntax. */ export let safeHref = (href)=>safe(href);
|
|
10
|
+
//# sourceMappingURL=safe.js.map
|
package/safe.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe.js","sources":["./safe.ts"],"names":[],"mappings":"AAAA;;;;;CAKC,GAED,8EAA8E;AAC9E,IAAI,WAAW;AAEf,0EAA0E,GAC1E,OAAO,IAAI,OAAO,CAAC,OAAyB,KAAK,OAAO,CAAC,UAAU,IAAG;AAEtE,0EAA0E,GAC1E,OAAO,IAAI,WAAW,CAAC,OAAyB,KAAK,MAAK"}
|
package/safe.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This renderer's content boundary. Every text leaf and link destination loses
|
|
3
|
+
* its C0, DEL and C1 bytes before any formatting is added. Unlike the older
|
|
4
|
+
* terminal helper, this keeps no search-highlight markers, tabs or literal
|
|
5
|
+
* newlines; structural elements are the only source of output line breaks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// deno-lint-ignore no-control-regex -- the full control class is the boundary
|
|
9
|
+
let controls = /[\x00-\x1f\x7f-\x9f]/g
|
|
10
|
+
|
|
11
|
+
/** Strip every C0/DEL/C1 byte from content, including tabs and newlines. */
|
|
12
|
+
export let safe = (text: string): string => text.replace(controls, '')
|
|
13
|
+
|
|
14
|
+
/** Strip the same bytes from a destination before it enters link syntax. */
|
|
15
|
+
export let safeHref = (href: string): string => safe(href)
|
package/tree.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tree of recorded elements, kept until serialization. Keeping text leaves
|
|
3
|
+
* separate from the formatting this renderer adds lets the content boundary
|
|
4
|
+
* remove control bytes without erasing the line breaks that paragraphs, lists
|
|
5
|
+
* and `br` introduce.
|
|
6
|
+
*/ /**
|
|
7
|
+
* Record an element for markdown() or plain(). Text is escaped at serialization
|
|
8
|
+
* time, so code can keep its literal punctuation and every path goes through the
|
|
9
|
+
* same content boundary.
|
|
10
|
+
*
|
|
11
|
+
* Headings:
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { assertEquals } from '@std/assert'
|
|
14
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
15
|
+
* for (let level of [1, 2, 3, 4, 5, 6]) {
|
|
16
|
+
* let node = h(`h${level}`, null, 'A page')
|
|
17
|
+
* assertEquals(markdown(node), `${'#'.repeat(level)} A page`)
|
|
18
|
+
* assertEquals(plain(node), 'A page')
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Paragraphs and line breaks:
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { assertEquals } from '@std/assert'
|
|
25
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
26
|
+
* let node = h('div', null, h('p', null, 'One'),
|
|
27
|
+
* h('p', null, 'Two', h('br', null), 'Three'))
|
|
28
|
+
* assertEquals(markdown(node), 'One\n\nTwo \nThree')
|
|
29
|
+
* assertEquals(plain(node), 'One\n\nTwo\nThree')
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* Unordered lists:
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { assertEquals } from '@std/assert'
|
|
35
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
36
|
+
* let node = h('ul', null, h('li', null, 'One'), h('li', null, 'Two'))
|
|
37
|
+
* assertEquals(markdown(node), '- One\n- Two')
|
|
38
|
+
* assertEquals(plain(node), '- One\n- Two')
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* Ordered lists:
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { assertEquals } from '@std/assert'
|
|
44
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
45
|
+
* let node = h('ol', {start: 3}, h('li', null, 'One'), h('li', null, 'Two'))
|
|
46
|
+
* assertEquals(markdown(node), '3. One\n4. Two')
|
|
47
|
+
* assertEquals(plain(node), '3. One\n4. Two')
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* Links:
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { assertEquals } from '@std/assert'
|
|
53
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
54
|
+
* let node = h('a', {href: 'https://yaks.app'}, 'Open')
|
|
55
|
+
* assertEquals(markdown(node), '[Open](https://yaks.app)')
|
|
56
|
+
* assertEquals(plain(node), 'Open (https://yaks.app)')
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* Inline code:
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { assertEquals } from '@std/assert'
|
|
62
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
63
|
+
* let node = h('code', null, 'a * b')
|
|
64
|
+
* assertEquals(markdown(node), '`a * b`')
|
|
65
|
+
* assertEquals(plain(node), 'a * b')
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* Fenced code (br supplies the line break):
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { assertEquals } from '@std/assert'
|
|
71
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
72
|
+
* let node = h('pre', null, h('code', null, 'one', h('br', null), 'two'))
|
|
73
|
+
* let fence = '`'.repeat(3)
|
|
74
|
+
* assertEquals(markdown(node), `${fence}\none\ntwo\n${fence}`)
|
|
75
|
+
* assertEquals(plain(node), 'one\ntwo')
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* Emphasis:
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { assertEquals } from '@std/assert'
|
|
81
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
82
|
+
* for (let tag of ['strong', 'b', 'em', 'i']) {
|
|
83
|
+
* let node = h(tag, null, 'word')
|
|
84
|
+
* let marker = tag == 'strong' || tag == 'b' ? '**' : '*'
|
|
85
|
+
* assertEquals(markdown(node), `${marker}word${marker}`)
|
|
86
|
+
* assertEquals(plain(node), 'word')
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* Inline and unknown wrappers keep their children:
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { assertEquals } from '@std/assert'
|
|
93
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
94
|
+
* let node = h('custom', null, h('span', null, 'A'), [' ', 0, false, null])
|
|
95
|
+
* assertEquals(markdown(node), 'A 0')
|
|
96
|
+
* assertEquals(plain(node), 'A 0')
|
|
97
|
+
* ```
|
|
98
|
+
*/ export let h = (tag, props, ...children)=>({
|
|
99
|
+
tag,
|
|
100
|
+
props,
|
|
101
|
+
children
|
|
102
|
+
});
|
|
103
|
+
//# sourceMappingURL=tree.js.map
|
package/tree.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree.js","sources":["./tree.ts"],"names":[],"mappings":"AAAA;;;;;CAKC,GAWD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4FC,GACD,OAAO,IAAI,IAAa,CAAC,KAAK,OAAO,GAAG,WAAa,CAAC;IAAE;IAAK;IAAO;EAAS,CAAC,EAAC"}
|
package/tree.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tree of recorded elements, kept until serialization. Keeping text leaves
|
|
3
|
+
* separate from the formatting this renderer adds lets the content boundary
|
|
4
|
+
* remove control bytes without erasing the line breaks that paragraphs, lists
|
|
5
|
+
* and `br` introduce.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Child, H } from 'jsr:@yaks/render@0.2.36'
|
|
9
|
+
|
|
10
|
+
/** An element produced by this package's `h`; no DOM is needed. */
|
|
11
|
+
export type Node = {
|
|
12
|
+
tag: string
|
|
13
|
+
props: Record<string, unknown> | null
|
|
14
|
+
children: Child<Node>[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Record an element for markdown() or plain(). Text is escaped at serialization
|
|
19
|
+
* time, so code can keep its literal punctuation and every path goes through the
|
|
20
|
+
* same content boundary.
|
|
21
|
+
*
|
|
22
|
+
* Headings:
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { assertEquals } from '@std/assert'
|
|
25
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
26
|
+
* for (let level of [1, 2, 3, 4, 5, 6]) {
|
|
27
|
+
* let node = h(`h${level}`, null, 'A page')
|
|
28
|
+
* assertEquals(markdown(node), `${'#'.repeat(level)} A page`)
|
|
29
|
+
* assertEquals(plain(node), 'A page')
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Paragraphs and line breaks:
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { assertEquals } from '@std/assert'
|
|
36
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
37
|
+
* let node = h('div', null, h('p', null, 'One'),
|
|
38
|
+
* h('p', null, 'Two', h('br', null), 'Three'))
|
|
39
|
+
* assertEquals(markdown(node), 'One\n\nTwo \nThree')
|
|
40
|
+
* assertEquals(plain(node), 'One\n\nTwo\nThree')
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Unordered lists:
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { assertEquals } from '@std/assert'
|
|
46
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
47
|
+
* let node = h('ul', null, h('li', null, 'One'), h('li', null, 'Two'))
|
|
48
|
+
* assertEquals(markdown(node), '- One\n- Two')
|
|
49
|
+
* assertEquals(plain(node), '- One\n- Two')
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* Ordered lists:
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { assertEquals } from '@std/assert'
|
|
55
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
56
|
+
* let node = h('ol', {start: 3}, h('li', null, 'One'), h('li', null, 'Two'))
|
|
57
|
+
* assertEquals(markdown(node), '3. One\n4. Two')
|
|
58
|
+
* assertEquals(plain(node), '3. One\n4. Two')
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* Links:
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { assertEquals } from '@std/assert'
|
|
64
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
65
|
+
* let node = h('a', {href: 'https://yaks.app'}, 'Open')
|
|
66
|
+
* assertEquals(markdown(node), '[Open](https://yaks.app)')
|
|
67
|
+
* assertEquals(plain(node), 'Open (https://yaks.app)')
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* Inline code:
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { assertEquals } from '@std/assert'
|
|
73
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
74
|
+
* let node = h('code', null, 'a * b')
|
|
75
|
+
* assertEquals(markdown(node), '`a * b`')
|
|
76
|
+
* assertEquals(plain(node), 'a * b')
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* Fenced code (br supplies the line break):
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { assertEquals } from '@std/assert'
|
|
82
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
83
|
+
* let node = h('pre', null, h('code', null, 'one', h('br', null), 'two'))
|
|
84
|
+
* let fence = '`'.repeat(3)
|
|
85
|
+
* assertEquals(markdown(node), `${fence}\none\ntwo\n${fence}`)
|
|
86
|
+
* assertEquals(plain(node), 'one\ntwo')
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* Emphasis:
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { assertEquals } from '@std/assert'
|
|
92
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
93
|
+
* for (let tag of ['strong', 'b', 'em', 'i']) {
|
|
94
|
+
* let node = h(tag, null, 'word')
|
|
95
|
+
* let marker = tag == 'strong' || tag == 'b' ? '**' : '*'
|
|
96
|
+
* assertEquals(markdown(node), `${marker}word${marker}`)
|
|
97
|
+
* assertEquals(plain(node), 'word')
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* Inline and unknown wrappers keep their children:
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { assertEquals } from '@std/assert'
|
|
104
|
+
* import { h, markdown, plain } from '@yaks/text'
|
|
105
|
+
* let node = h('custom', null, h('span', null, 'A'), [' ', 0, false, null])
|
|
106
|
+
* assertEquals(markdown(node), 'A 0')
|
|
107
|
+
* assertEquals(plain(node), 'A 0')
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export let h: H<Node> = (tag, props, ...children) => ({ tag, props, children })
|