shelving 1.50.2 → 1.51.3
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/markup/index.d.ts +0 -1
- package/markup/index.js +0 -1
- package/markup/render.d.ts +3 -2
- package/markup/render.js +9 -9
- package/markup/rules.d.ts +3 -7
- package/markup/rules.js +30 -45
- package/markup/types.d.ts +6 -25
- package/package.json +5 -5
- package/react/useQuery.d.ts +2 -2
- package/util/index.d.ts +1 -0
- package/util/index.js +1 -0
- package/util/jsx.d.ts +35 -0
- package/util/jsx.js +35 -0
- package/util/search.d.ts +37 -24
- package/util/search.js +51 -36
- package/util/string.d.ts +0 -9
- package/util/string.js +0 -10
- package/markup/helpers.d.ts +0 -25
- package/markup/helpers.js +0 -54
package/markup/index.d.ts
CHANGED
package/markup/index.js
CHANGED
package/markup/render.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { JSXNode } from "../util/index.js";
|
|
2
|
+
import type { MarkupOptions } from "./types.js";
|
|
2
3
|
/**
|
|
3
4
|
* Parse a text string as Markdownish syntax and render it as a JSX node.
|
|
4
5
|
* - Syntax is not defined by this code, but by the rules supplied to it.
|
|
@@ -32,4 +33,4 @@ import type { MarkupOptions, MarkupNode } from "./types.js";
|
|
|
32
33
|
*
|
|
33
34
|
* @returns ReactNode, i.e. either a complete `ReactElement`, `null`, `undefined`, `string`, or an array of zero or more of those.
|
|
34
35
|
*/
|
|
35
|
-
export declare function renderMarkup(content: string, options?: Partial<MarkupOptions>):
|
|
36
|
+
export declare function renderMarkup(content: string, options?: Partial<MarkupOptions>): JSXNode;
|
package/markup/render.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
|
-
import { sanitizeLines } from "../index.js";
|
|
2
|
+
import { sanitizeLines } from "../util/index.js";
|
|
3
3
|
import { MARKUP_RULES } from "./rules.js";
|
|
4
4
|
/** Convert a string into an array of React nodes using a set of rules. */
|
|
5
5
|
function renderString(content, options) {
|
|
@@ -15,12 +15,12 @@ function renderString(content, options) {
|
|
|
15
15
|
let matchedRule = undefined;
|
|
16
16
|
let matchedResult = undefined;
|
|
17
17
|
for (const rule of options.rules) {
|
|
18
|
-
const { priority = 0, match, contexts } = rule;
|
|
18
|
+
const { priority = 0, match, regexp, contexts } = rule;
|
|
19
19
|
// Only apply this rule if both:
|
|
20
20
|
// 1. The priority is equal or higher to the current priority.
|
|
21
21
|
// 2. The rule is allowed in the current context.
|
|
22
22
|
if (priority >= matchedPriority && contexts.includes(options.context)) {
|
|
23
|
-
const result = match(content, options);
|
|
23
|
+
const result = match ? match(content, options) : regexp ? content.match(regexp) : null;
|
|
24
24
|
// If this matched and has an index (it might not if it's a `/g` global RegExp, which would be a mistake).
|
|
25
25
|
if (result && typeof result.index === "number") {
|
|
26
26
|
const index = result.index;
|
|
@@ -46,13 +46,12 @@ function renderString(content, options) {
|
|
|
46
46
|
// Call the rule's `render()` function to generate the node.
|
|
47
47
|
const childOptions = { ...options, context: matchedRule.childContext };
|
|
48
48
|
const element = matchedRule.render(matchedResult, childOptions);
|
|
49
|
-
element.rule = matchedRule;
|
|
50
49
|
appendNode(nodes, renderNode(element, childOptions));
|
|
51
50
|
// Decrement the content.
|
|
52
51
|
content = content.slice(matchedIndex + matchedResult[0].length);
|
|
53
52
|
}
|
|
54
53
|
else {
|
|
55
|
-
// If nothing else matched add the rest of the string as a node
|
|
54
|
+
// If nothing else matched add the rest of the string as a node.
|
|
56
55
|
// Don't need to push the string through `renderNode()` because we already know it doesn't match any rules in the current context.
|
|
57
56
|
nodes.push(content);
|
|
58
57
|
content = "";
|
|
@@ -103,10 +102,11 @@ function renderNode(node, options) {
|
|
|
103
102
|
if (node instanceof Array)
|
|
104
103
|
return node.map(n => renderNode(n, options));
|
|
105
104
|
if (typeof node === "object" && node) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
return {
|
|
106
|
+
...node,
|
|
107
|
+
$$typeof: REACT_SECURITY_SYMBOL,
|
|
108
|
+
props: node.props.children ? { ...node.props, children: renderNode(node.props.children, options) } : node.props,
|
|
109
|
+
};
|
|
110
110
|
}
|
|
111
111
|
return node;
|
|
112
112
|
}
|
package/markup/rules.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type { MarkupRule
|
|
2
|
-
export declare const getMarkupMatcher: (regexp: RegExp) => MarkupRuleMatcher;
|
|
3
|
-
export declare const getMarkupBlockMatcher: (middle?: string, end?: string, start?: string) => MarkupRuleMatcher;
|
|
4
|
-
export declare const getMarkupLineMatcher: (middle?: string, end?: string, start?: string) => MarkupRuleMatcher;
|
|
5
|
-
export declare const getMarkupWrapMatcher: (chars: string, middle?: string) => MarkupRuleMatcher;
|
|
1
|
+
import type { MarkupRule } from "./types.js";
|
|
6
2
|
/**
|
|
7
3
|
* Headings are single line only (don't allow multiline).
|
|
8
4
|
* - 1-6 hashes then 1+ spaces, then the title.
|
|
@@ -48,7 +44,7 @@ export declare const PARAGRAPH_RULE: MarkupRule;
|
|
|
48
44
|
* - If link is not valid (using `new URL(url)` then unparsed text will be returned.
|
|
49
45
|
* - For security only `http://` or `https://` links will work (if invalid the unparsed text will be returned).
|
|
50
46
|
*/
|
|
51
|
-
export declare const
|
|
47
|
+
export declare const LINK_RULE: MarkupRule;
|
|
52
48
|
/**
|
|
53
49
|
* Autolinked URL starts with `http:` or `https:` and matches an unlimited number of non-space characters.
|
|
54
50
|
* - If followed by space and then text in `()` round or `[]` square brackets that will be used as the title, e.g. `http://google.com/maps (Google Maps)` or `http://google.com/maps [Google Maps]` (this syntax is from Todoist and maybe other things too).
|
|
@@ -73,7 +69,7 @@ export declare const CODE_RULE: MarkupRule;
|
|
|
73
69
|
* - Closing characters must exactly match opening characters.
|
|
74
70
|
* - Different to Markdown: strong is always surrounded by `*asterisks*` and emphasis is always surrounded by `_underscores_` (strong isn't 'double emphasis').
|
|
75
71
|
*/
|
|
76
|
-
export declare const
|
|
72
|
+
export declare const STRONG_RULE: MarkupRule;
|
|
77
73
|
/**
|
|
78
74
|
* Inline emphasis.
|
|
79
75
|
* - Inline text wrapped in one or more `_` underscore symbols.
|
package/markup/rules.js
CHANGED
|
@@ -1,23 +1,8 @@
|
|
|
1
|
-
import { formatUrl, toURL } from "../util/index.js";
|
|
1
|
+
import { formatUrl, toURL, getLineRegExp, getBlockRegExp, MATCH_LINE, MATCH_BLOCK, getWrapRegExp } from "../util/index.js";
|
|
2
2
|
// Regular expression partials (`\` slashes must be escaped as `\\`).
|
|
3
|
-
const LINE = "[^\\n]*"; // Match line of content (anything that's not a newline).
|
|
4
|
-
const LINE_START = "^\\n*|\\n+"; // Starts at start of line (one or more linebreak or start of string).
|
|
5
|
-
const LINE_END = "\\n+|$"; // Ends at end of line (one or more linebreak or end of string).
|
|
6
|
-
const BLOCK = "[\\s\\S]*?"; // Match block of content (including newlines so don't be greedy).
|
|
7
|
-
const BLOCK_START = "^\\n*|\\n+"; // Starts at start of a block (one or more linebreak or start of string).
|
|
8
|
-
const BLOCK_END = "\\n*$|\\n\\n+"; // End of a block (two or more linebreaks or end of string).
|
|
9
3
|
const BULLETS = "-*•+"; // Anything that can be a bullet (used for unordered lists and horizontal rules).
|
|
10
|
-
const WORDS = `\\S(?:[\\s\\S]*?\\S)?`; // Run of text that starts and ends with non-space characters (possibly multi-line).
|
|
11
4
|
// Regular expressions.
|
|
12
|
-
const
|
|
13
|
-
// Regular expression makers.
|
|
14
|
-
export const getMarkupMatcher = regexp => content => content.match(regexp);
|
|
15
|
-
export const getMarkupBlockMatcher = (middle = BLOCK, end = BLOCK_END, start = BLOCK_START) => getMarkupMatcher(new RegExp(`(?:${start})${middle}(?:${end})`));
|
|
16
|
-
export const getMarkupLineMatcher = (middle = LINE, end = LINE_END, start = LINE_START) => getMarkupMatcher(new RegExp(`(?:${start})${middle}(?:${end})`));
|
|
17
|
-
export const getMarkupWrapMatcher = (chars, middle = WORDS) => {
|
|
18
|
-
const regexp = new RegExp(`(${chars})(${middle})\\1`);
|
|
19
|
-
return content => content.match(regexp);
|
|
20
|
-
};
|
|
5
|
+
const MATCH_INDENT = /^ {1,2}/gm;
|
|
21
6
|
/**
|
|
22
7
|
* Headings are single line only (don't allow multiline).
|
|
23
8
|
* - 1-6 hashes then 1+ spaces, then the title.
|
|
@@ -25,7 +10,7 @@ export const getMarkupWrapMatcher = (chars, middle = WORDS) => {
|
|
|
25
10
|
* - Markdown's underline syntax is not supported (for simplification).
|
|
26
11
|
*/
|
|
27
12
|
export const HEADING_RULE = {
|
|
28
|
-
|
|
13
|
+
regexp: getLineRegExp(`(#{1,6}) +(${MATCH_LINE.source})`),
|
|
29
14
|
render: ([, prefix = "", children = ""]) => ({ type: `h${prefix.length}`, key: null, props: { children } }),
|
|
30
15
|
contexts: ["block"],
|
|
31
16
|
childContext: "inline",
|
|
@@ -38,7 +23,7 @@ export const HEADING_RULE = {
|
|
|
38
23
|
* - Might have infinite number of spaces between the characters.
|
|
39
24
|
*/
|
|
40
25
|
export const HORIZONTAL_RULE = {
|
|
41
|
-
|
|
26
|
+
regexp: getLineRegExp(`([${BULLETS}])(?: *\\1){2,}`),
|
|
42
27
|
render: () => ({ type: "hr", key: null, props: {} }),
|
|
43
28
|
contexts: ["block"],
|
|
44
29
|
};
|
|
@@ -51,7 +36,7 @@ export const HORIZONTAL_RULE = {
|
|
|
51
36
|
*/
|
|
52
37
|
const UNORDERED = `[${BULLETS}] +`; // Anything that can be a bullet (used for unordered lists and horizontal rules).
|
|
53
38
|
export const UNORDERED_LIST_RULE = {
|
|
54
|
-
|
|
39
|
+
regexp: getBlockRegExp(`${UNORDERED}(${MATCH_BLOCK.source})`),
|
|
55
40
|
render: ([, list = ""]) => {
|
|
56
41
|
const children = list.split(SPLIT_UL_ITEMS).map(mapUnorderedItem);
|
|
57
42
|
return { type: "ul", key: null, props: { children } };
|
|
@@ -61,7 +46,7 @@ export const UNORDERED_LIST_RULE = {
|
|
|
61
46
|
};
|
|
62
47
|
const SPLIT_UL_ITEMS = new RegExp(`\\n+${UNORDERED}`, "g");
|
|
63
48
|
const mapUnorderedItem = (item, key) => {
|
|
64
|
-
const children = item.replace(
|
|
49
|
+
const children = item.replace(MATCH_INDENT, "");
|
|
65
50
|
return { type: "li", key, props: { children } };
|
|
66
51
|
};
|
|
67
52
|
/**
|
|
@@ -71,7 +56,7 @@ const mapUnorderedItem = (item, key) => {
|
|
|
71
56
|
*/
|
|
72
57
|
const ORDERED = "[0-9]+[.):] +"; // Number for a numbered list (e.g. `1.` or `2)` or `3:`)
|
|
73
58
|
export const ORDERED_LIST_RULE = {
|
|
74
|
-
|
|
59
|
+
regexp: getBlockRegExp(`(${ORDERED}${MATCH_BLOCK.source})`),
|
|
75
60
|
render: ([, list = ""]) => {
|
|
76
61
|
const children = list.split(SPLIT_OL_ITEMS).map(mapOrderedItem);
|
|
77
62
|
return { type: "ol", key: null, props: { children } };
|
|
@@ -86,7 +71,7 @@ const mapOrderedItem = (item, key) => {
|
|
|
86
71
|
const children = item
|
|
87
72
|
.slice(firstSpace + 1)
|
|
88
73
|
.trimStart()
|
|
89
|
-
.replace(
|
|
74
|
+
.replace(MATCH_INDENT, "");
|
|
90
75
|
return { type: "li", key, props: { value, children } };
|
|
91
76
|
};
|
|
92
77
|
/**
|
|
@@ -96,7 +81,7 @@ const mapOrderedItem = (item, key) => {
|
|
|
96
81
|
* - Quote indent symbol can be followed by zero or more spaces.
|
|
97
82
|
*/
|
|
98
83
|
export const BLOCKQUOTE_RULE = {
|
|
99
|
-
|
|
84
|
+
regexp: getLineRegExp(`(>${MATCH_LINE.source}(?:\\n>${MATCH_LINE.source})*)`),
|
|
100
85
|
render: ([, quote = ""]) => ({
|
|
101
86
|
type: "blockquote",
|
|
102
87
|
key: null,
|
|
@@ -115,7 +100,7 @@ const BLOCKQUOTE_LINES = /^>/gm;
|
|
|
115
100
|
*/
|
|
116
101
|
export const FENCED_CODE_RULE = {
|
|
117
102
|
// Matcher has its own end that only stops when it reaches a matching closing fence or the end of the string.
|
|
118
|
-
|
|
103
|
+
regexp: getBlockRegExp(`(\`{3,}|~{3,}) *(${MATCH_LINE.source})\\n(${MATCH_BLOCK.source})`, `\\n\\1\\n+|\\n\\1$|$`),
|
|
119
104
|
render: ([, , file, children]) => ({
|
|
120
105
|
type: "pre",
|
|
121
106
|
key: null,
|
|
@@ -134,7 +119,7 @@ export const FENCED_CODE_RULE = {
|
|
|
134
119
|
* - When ordering rules, paragraph should go after other "block" context elements (because it has a very generous capture).
|
|
135
120
|
*/
|
|
136
121
|
export const PARAGRAPH_RULE = {
|
|
137
|
-
|
|
122
|
+
regexp: getBlockRegExp(` *(${MATCH_BLOCK.source})`),
|
|
138
123
|
render: ([, children]) => ({ type: `p`, key: null, props: { children } }),
|
|
139
124
|
contexts: ["block"],
|
|
140
125
|
childContext: "inline",
|
|
@@ -148,10 +133,11 @@ export const PARAGRAPH_RULE = {
|
|
|
148
133
|
* - If link is not valid (using `new URL(url)` then unparsed text will be returned.
|
|
149
134
|
* - For security only `http://` or `https://` links will work (if invalid the unparsed text will be returned).
|
|
150
135
|
*/
|
|
151
|
-
export const
|
|
136
|
+
export const LINK_RULE = {
|
|
152
137
|
// Custom matcher to check the URL against the allowed schemes.
|
|
138
|
+
regexp: /\[([^\]]*?)\]\(([^)]*?)\)/,
|
|
153
139
|
match: (content, { schemes, url: base }) => {
|
|
154
|
-
const matches = content.match(
|
|
140
|
+
const matches = content.match(LINK_RULE.regexp);
|
|
155
141
|
if (matches && typeof matches.index === "number") {
|
|
156
142
|
const [, title = "", href = ""] = matches;
|
|
157
143
|
const url = toURL(href, base);
|
|
@@ -170,7 +156,6 @@ export const LINK_MARKUP = {
|
|
|
170
156
|
contexts: ["inline", "list"],
|
|
171
157
|
childContext: "link",
|
|
172
158
|
};
|
|
173
|
-
const MATCH_LINK = /\[([^\]]*?)\]\(([^)]*?)\)/;
|
|
174
159
|
/**
|
|
175
160
|
* Autolinked URL starts with `http:` or `https:` and matches an unlimited number of non-space characters.
|
|
176
161
|
* - If followed by space and then text in `()` round or `[]` square brackets that will be used as the title, e.g. `http://google.com/maps (Google Maps)` or `http://google.com/maps [Google Maps]` (this syntax is from Todoist and maybe other things too).
|
|
@@ -180,8 +165,9 @@ const MATCH_LINK = /\[([^\]]*?)\]\(([^)]*?)\)/;
|
|
|
180
165
|
*/
|
|
181
166
|
export const AUTOLINK_RULE = {
|
|
182
167
|
// Custom matcher to check the URL against the allowed schemes.
|
|
168
|
+
regexp: /([a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9]:\S+)(?: +(?:\(([^)]*?)\)|\[([^\]]*?)\]))?/,
|
|
183
169
|
match: (content, { schemes, url: base }) => {
|
|
184
|
-
const matches = content.match(
|
|
170
|
+
const matches = content.match(AUTOLINK_RULE.regexp);
|
|
185
171
|
if (matches && typeof matches.index === "number") {
|
|
186
172
|
const [, href = "", title1 = "", title2 = ""] = matches;
|
|
187
173
|
const url = toURL(href, base);
|
|
@@ -192,11 +178,10 @@ export const AUTOLINK_RULE = {
|
|
|
192
178
|
}
|
|
193
179
|
}
|
|
194
180
|
},
|
|
195
|
-
render:
|
|
181
|
+
render: LINK_RULE.render,
|
|
196
182
|
contexts: ["inline", "list"],
|
|
197
183
|
childContext: "link",
|
|
198
184
|
};
|
|
199
|
-
const MATCH_AUTOLINK = /([a-z][a-z0-9-]*[a-z0-9]:\S+)(?: +(?:\(([^)]*?)\)|\[([^\]]*?)\]))?/i;
|
|
200
185
|
/**
|
|
201
186
|
* Inline code.
|
|
202
187
|
* - Text surrounded by one or more "`" backtick tilde characters.
|
|
@@ -205,7 +190,7 @@ const MATCH_AUTOLINK = /([a-z][a-z0-9-]*[a-z0-9]:\S+)(?: +(?:\(([^)]*?)\)|\[([^\
|
|
|
205
190
|
* - Same as Markdown syntax.
|
|
206
191
|
*/
|
|
207
192
|
export const CODE_RULE = {
|
|
208
|
-
|
|
193
|
+
regexp: getWrapRegExp("`+", MATCH_BLOCK.source),
|
|
209
194
|
render: ([, , children]) => ({ type: "code", key: null, props: { children } }),
|
|
210
195
|
contexts: ["inline", "list"],
|
|
211
196
|
priority: 10, // Higher priority than other inlines so it matches first before e.g. `strong` or `em` (from CommonMark spec: "Code span backticks have higher precedence than any other inline constructs except HTML tags and autolinks.")
|
|
@@ -218,8 +203,8 @@ export const CODE_RULE = {
|
|
|
218
203
|
* - Closing characters must exactly match opening characters.
|
|
219
204
|
* - Different to Markdown: strong is always surrounded by `*asterisks*` and emphasis is always surrounded by `_underscores_` (strong isn't 'double emphasis').
|
|
220
205
|
*/
|
|
221
|
-
export const
|
|
222
|
-
|
|
206
|
+
export const STRONG_RULE = {
|
|
207
|
+
regexp: getWrapRegExp("\\*+"),
|
|
223
208
|
render: ([, , children]) => ({ type: "strong", key: null, props: { children } }),
|
|
224
209
|
contexts: ["inline", "list", "link"],
|
|
225
210
|
childContext: "inline",
|
|
@@ -233,7 +218,7 @@ export const STRONG_MARKUP = {
|
|
|
233
218
|
* - Different to Markdown: strong is always surrounded by `*asterisks*` and emphasis is always surrounded by `_underscores_` (strong isn't 'double emphasis').
|
|
234
219
|
*/
|
|
235
220
|
export const EMPHASIS_RULE = {
|
|
236
|
-
|
|
221
|
+
regexp: getWrapRegExp("_+"),
|
|
237
222
|
render: ([, , children]) => ({ type: "em", key: null, props: { children } }),
|
|
238
223
|
contexts: ["inline", "list", "link"],
|
|
239
224
|
childContext: "inline",
|
|
@@ -247,7 +232,7 @@ export const EMPHASIS_RULE = {
|
|
|
247
232
|
* - Markdown doesn't have this.
|
|
248
233
|
*/
|
|
249
234
|
export const INSERT_RULE = {
|
|
250
|
-
|
|
235
|
+
regexp: getWrapRegExp("\\+\\++"),
|
|
251
236
|
render: ([, , children]) => ({ type: "ins", key: null, props: { children } }),
|
|
252
237
|
contexts: ["inline", "list", "link"],
|
|
253
238
|
childContext: "inline",
|
|
@@ -261,7 +246,7 @@ export const INSERT_RULE = {
|
|
|
261
246
|
* - Markdown doesn't have this.
|
|
262
247
|
*/
|
|
263
248
|
export const DELETE_RULE = {
|
|
264
|
-
|
|
249
|
+
regexp: getWrapRegExp("--+|~~+"),
|
|
265
250
|
render: ([, , children]) => ({ type: "del", key: null, props: { children } }),
|
|
266
251
|
contexts: ["inline", "list", "link"],
|
|
267
252
|
childContext: "inline",
|
|
@@ -275,7 +260,7 @@ export const DELETE_RULE = {
|
|
|
275
260
|
* - This works better with textareas that wrap text (since manually breaking up long lines is no longer necessary).
|
|
276
261
|
*/
|
|
277
262
|
export const LINEBREAK_RULE = {
|
|
278
|
-
|
|
263
|
+
regexp: /\n/,
|
|
279
264
|
render: () => ({ type: "br", key: null, props: {} }),
|
|
280
265
|
contexts: ["inline", "list", "link"],
|
|
281
266
|
childContext: "inline",
|
|
@@ -297,10 +282,10 @@ export const MARKUP_RULES = [
|
|
|
297
282
|
BLOCKQUOTE_RULE,
|
|
298
283
|
FENCED_CODE_RULE,
|
|
299
284
|
PARAGRAPH_RULE,
|
|
300
|
-
|
|
285
|
+
LINK_RULE,
|
|
301
286
|
AUTOLINK_RULE,
|
|
302
287
|
CODE_RULE,
|
|
303
|
-
|
|
288
|
+
STRONG_RULE,
|
|
304
289
|
EMPHASIS_RULE,
|
|
305
290
|
INSERT_RULE,
|
|
306
291
|
DELETE_RULE,
|
|
@@ -318,10 +303,10 @@ export const MARKUP_RULES_BLOCK = [
|
|
|
318
303
|
];
|
|
319
304
|
/** Subset of markup rules that work in an inline context. */
|
|
320
305
|
export const MARKUP_RULES_INLINE = [
|
|
321
|
-
|
|
306
|
+
LINK_RULE,
|
|
322
307
|
AUTOLINK_RULE,
|
|
323
308
|
CODE_RULE,
|
|
324
|
-
|
|
309
|
+
STRONG_RULE,
|
|
325
310
|
EMPHASIS_RULE,
|
|
326
311
|
INSERT_RULE,
|
|
327
312
|
DELETE_RULE,
|
|
@@ -332,10 +317,10 @@ export const MARKUP_RULES_SHORTFORM = [
|
|
|
332
317
|
UNORDERED_LIST_RULE,
|
|
333
318
|
ORDERED_LIST_RULE,
|
|
334
319
|
PARAGRAPH_RULE,
|
|
335
|
-
|
|
320
|
+
LINK_RULE,
|
|
336
321
|
AUTOLINK_RULE,
|
|
337
322
|
CODE_RULE,
|
|
338
|
-
|
|
323
|
+
STRONG_RULE,
|
|
339
324
|
EMPHASIS_RULE,
|
|
340
325
|
INSERT_RULE,
|
|
341
326
|
DELETE_RULE,
|
package/markup/types.d.ts
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
* JSX element.
|
|
3
|
-
* - Compatible with but _slightly_ more flexible than `React.ReactElement`
|
|
4
|
-
*/
|
|
5
|
-
export declare type MarkupElement = {
|
|
6
|
-
type: string;
|
|
7
|
-
key: string | number | null;
|
|
8
|
-
props: MarkupElementProps;
|
|
9
|
-
$$typeof?: symbol;
|
|
10
|
-
/** This specifies the rule that created this element. */
|
|
11
|
-
rule?: MarkupRule;
|
|
12
|
-
};
|
|
13
|
-
export declare type MarkupElementProps = {
|
|
14
|
-
[prop: string]: unknown;
|
|
15
|
-
children?: MarkupNode;
|
|
16
|
-
};
|
|
17
|
-
/** JSX node (compatible with but slightly more flexible than `React.ReactNode`) */
|
|
18
|
-
export declare type MarkupNode = undefined | null | string | MarkupElement | MarkupNode[];
|
|
1
|
+
import { JSXElement } from "../util/index.js";
|
|
19
2
|
/** A single markup parsing rule. */
|
|
20
3
|
export declare type MarkupRule = {
|
|
21
|
-
/** RegExp
|
|
22
|
-
readonly
|
|
4
|
+
/** RegExp for matching this rule. */
|
|
5
|
+
readonly regexp: RegExp;
|
|
6
|
+
/** Custom matching function for this rule (overrides `regexp` if present. */
|
|
7
|
+
readonly match?: (content: string, options: MarkupOptions) => RegExpMatchArray | null | undefined | void;
|
|
23
8
|
/**
|
|
24
9
|
* Return a corresponding JSX element for the match.
|
|
25
10
|
* @param ...matches The matches from `match` RegExp (without the `0` zeroeth "whole match").
|
|
@@ -27,7 +12,7 @@ export declare type MarkupRule = {
|
|
|
27
12
|
* - The `key` property is not required (will be set automatically).
|
|
28
13
|
* - e.g. `{ type: "a", props: { href: "/example.html", className: "strong", children: "Children *can* include _syntax_" } }`
|
|
29
14
|
*/
|
|
30
|
-
readonly render:
|
|
15
|
+
readonly render: (matches: RegExpMatchArray, options: MarkupOptions) => JSXElement;
|
|
31
16
|
/** Apply the rule only when in certain contexts, e.g. `["block", "inline", "list"]` */
|
|
32
17
|
readonly contexts: string[];
|
|
33
18
|
/** Context any string children returned from `render()` should be rendered with. */
|
|
@@ -42,8 +27,6 @@ export declare type MarkupRule = {
|
|
|
42
27
|
*/
|
|
43
28
|
readonly priority?: number;
|
|
44
29
|
};
|
|
45
|
-
export declare type MarkupRuleMatcher = (content: string, options: MarkupOptions) => RegExpMatchArray | null | undefined | void;
|
|
46
|
-
export declare type MarkupRuleRenderer = (matches: RegExpMatchArray, options: MarkupOptions) => MarkupElement;
|
|
47
30
|
/** A set of parse rules (as an object or array). */
|
|
48
31
|
export declare type MarkupRules = Iterable<MarkupRule>;
|
|
49
32
|
/** The current parsing options (represents the current state of the parsing). */
|
|
@@ -59,5 +42,3 @@ export declare type MarkupOptions = {
|
|
|
59
42
|
/** Valid URL schemes/protocols for links (including trailing commas), defaults to `[`http:`, `https:`]` */
|
|
60
43
|
readonly schemes: string[];
|
|
61
44
|
};
|
|
62
|
-
/** The create element function. */
|
|
63
|
-
export declare type MarkupElementCreator = (type: string, props?: MarkupElementProps | null) => MarkupElement;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"state-management",
|
|
12
12
|
"query-builder"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.51.3",
|
|
15
15
|
"repository": "https://github.com/dhoulb/shelving",
|
|
16
16
|
"author": "Dave Houlbrooke <dave@shax.com>",
|
|
17
17
|
"license": "0BSD",
|
|
@@ -63,14 +63,14 @@
|
|
|
63
63
|
"@types/jest": "^27.4.0",
|
|
64
64
|
"@types/react": "^17.0.39",
|
|
65
65
|
"@types/react-dom": "^17.0.11",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
67
|
-
"@typescript-eslint/parser": "^5.
|
|
68
|
-
"eslint": "^8.
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^5.11.0",
|
|
67
|
+
"@typescript-eslint/parser": "^5.11.0",
|
|
68
|
+
"eslint": "^8.9.0",
|
|
69
69
|
"eslint-config-prettier": "^8.3.0",
|
|
70
70
|
"eslint-plugin-import": "^2.25.4",
|
|
71
71
|
"eslint-plugin-prettier": "^4.0.0",
|
|
72
72
|
"firebase": "^9.6.6",
|
|
73
|
-
"jest": "^27.5.
|
|
73
|
+
"jest": "^27.5.1",
|
|
74
74
|
"jest-ts-webcompat-resolver": "^1.0.0",
|
|
75
75
|
"prettier": "^2.5.1",
|
|
76
76
|
"react": "^17.0.2",
|
package/react/useQuery.d.ts
CHANGED
|
@@ -34,8 +34,8 @@ export declare function useAsyncQuery<T extends Data>(ref: DatabaseQuery<T> | un
|
|
|
34
34
|
export declare function useQuery<T extends Data>(ref: DatabaseQuery<T>, maxAge?: number | true): Results<T>;
|
|
35
35
|
export declare function useQuery<T extends Data>(ref: DatabaseQuery<T> | undefined, maxAge?: number | true): Results<T> | undefined;
|
|
36
36
|
/** Use the first result of a query or `undefined` if the query has no matching results (or a promise indicating the result is loading). */
|
|
37
|
-
export declare function useAsyncQueryResult<T extends Data>(ref: DatabaseQuery<T>, maxAge?: number | true): DocumentResult<T> |
|
|
38
|
-
export declare function useAsyncQueryResult<T extends Data>(ref: DatabaseQuery<T> | undefined, maxAge?: number | true): DocumentResult<T> |
|
|
37
|
+
export declare function useAsyncQueryResult<T extends Data>(ref: DatabaseQuery<T>, maxAge?: number | true): DocumentResult<T> | PromiseLike<DocumentResult<T>>;
|
|
38
|
+
export declare function useAsyncQueryResult<T extends Data>(ref: DatabaseQuery<T> | undefined, maxAge?: number | true): DocumentResult<T> | PromiseLike<DocumentResult<T>> | undefined;
|
|
39
39
|
/** Use the first result of a query or `undefined` if the query has no matching results */
|
|
40
40
|
export declare function useQueryResult<T extends Data>(ref: DatabaseQuery<T>, maxAge?: number | true): DocumentResult<T>;
|
|
41
41
|
export declare function useQueryResult<T extends Data>(ref: DatabaseQuery<T> | undefined, maxAge?: number | true): DocumentResult<T> | undefined;
|
package/util/index.d.ts
CHANGED
package/util/index.js
CHANGED
package/util/jsx.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Data } from "./data.js";
|
|
2
|
+
/** Function that creates a JSX element. */
|
|
3
|
+
export declare type JSXElementCreator<P extends Data = Data> = (props: P) => JSXElement | null;
|
|
4
|
+
/** Set of valid props for a JSX element. */
|
|
5
|
+
export declare type JSXProps = {
|
|
6
|
+
readonly [key: string]: unknown;
|
|
7
|
+
readonly children?: JSXNode;
|
|
8
|
+
};
|
|
9
|
+
/** JSX element (similar to `React.ReactElement`) */
|
|
10
|
+
export declare type JSXElement<P extends JSXProps = JSXProps, T extends string | JSXElementCreator<P> = string | JSXElementCreator<P>> = {
|
|
11
|
+
type: T;
|
|
12
|
+
props: P;
|
|
13
|
+
key: string | number | null;
|
|
14
|
+
$$typeof?: symbol;
|
|
15
|
+
};
|
|
16
|
+
/** JSX node (similar to `React.ReactNode`) */
|
|
17
|
+
export declare type JSXNode = undefined | null | string | JSXElement | JSXNode[];
|
|
18
|
+
/** Is an unknown value a JSX element? */
|
|
19
|
+
export declare const isElement: <T extends JSXNode>(el: unknown) => el is T;
|
|
20
|
+
/** Is an unknown value a JSX node? */
|
|
21
|
+
export declare const isNode: <T extends JSXNode>(node: unknown) => node is T;
|
|
22
|
+
/**
|
|
23
|
+
* Take a Markup JSX node and strip all tags from it to produce a plain text string.
|
|
24
|
+
*
|
|
25
|
+
* @param node A JsxNode, e.g. either a JSX element, a plain string, or null/undefined (or an array of those things).
|
|
26
|
+
* @returns The combined string made from the JSX node.
|
|
27
|
+
*
|
|
28
|
+
* @example `- Item with *strong*\n- Item with _em_` becomes `Item with strong Item with em`
|
|
29
|
+
*/
|
|
30
|
+
export declare function nodeToText(node?: JSXNode): string;
|
|
31
|
+
/**
|
|
32
|
+
* Iterate through all elements in a node.
|
|
33
|
+
* - This is useful if you, e.g. want to apply a `className` to all `<h1>` elements, or make a list of all URLs found in a Node.
|
|
34
|
+
*/
|
|
35
|
+
export declare function yieldElements(node: JSXNode): Generator<JSXElement, void>;
|
package/util/jsx.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Is an unknown value a JSX element? */
|
|
2
|
+
export const isElement = (el) => typeof el === "object" && el !== null && "type" in el;
|
|
3
|
+
/** Is an unknown value a JSX node? */
|
|
4
|
+
export const isNode = (node) => node === null || typeof node === "string" || isElement(node) || node instanceof Array;
|
|
5
|
+
/**
|
|
6
|
+
* Take a Markup JSX node and strip all tags from it to produce a plain text string.
|
|
7
|
+
*
|
|
8
|
+
* @param node A JsxNode, e.g. either a JSX element, a plain string, or null/undefined (or an array of those things).
|
|
9
|
+
* @returns The combined string made from the JSX node.
|
|
10
|
+
*
|
|
11
|
+
* @example `- Item with *strong*\n- Item with _em_` becomes `Item with strong Item with em`
|
|
12
|
+
*/
|
|
13
|
+
export function nodeToText(node) {
|
|
14
|
+
if (typeof node === "string")
|
|
15
|
+
return node;
|
|
16
|
+
if (node instanceof Array)
|
|
17
|
+
return node.map(nodeToText).join(" ");
|
|
18
|
+
if (typeof node === "object" && node)
|
|
19
|
+
return nodeToText(node.props.children);
|
|
20
|
+
return "";
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Iterate through all elements in a node.
|
|
24
|
+
* - This is useful if you, e.g. want to apply a `className` to all `<h1>` elements, or make a list of all URLs found in a Node.
|
|
25
|
+
*/
|
|
26
|
+
export function* yieldElements(node) {
|
|
27
|
+
if (node instanceof Array)
|
|
28
|
+
for (const n of node)
|
|
29
|
+
yield* yieldElements(n);
|
|
30
|
+
else if (typeof node === "object" && node) {
|
|
31
|
+
yield node;
|
|
32
|
+
if (isNode(node.props.children))
|
|
33
|
+
yield* yieldElements(node.props.children);
|
|
34
|
+
}
|
|
35
|
+
}
|
package/util/search.d.ts
CHANGED
|
@@ -1,25 +1,41 @@
|
|
|
1
1
|
import type { ImmutableArray } from "./array.js";
|
|
2
2
|
import { Matchable } from "./filter.js";
|
|
3
|
+
export declare const MATCH_SPACE: RegExp;
|
|
4
|
+
export declare const MATCH_SPACES: RegExp;
|
|
5
|
+
export declare const MATCH_LINEBREAK: RegExp;
|
|
6
|
+
export declare const MATCH_LINEBREAKS: RegExp;
|
|
7
|
+
export declare const MATCH_LINE: RegExp;
|
|
8
|
+
export declare const MATCH_LINE_START: RegExp;
|
|
9
|
+
export declare const MATCH_LINE_END: RegExp;
|
|
10
|
+
export declare const MATCH_BLOCK: RegExp;
|
|
11
|
+
export declare const MATCH_BLOCK_START: RegExp;
|
|
12
|
+
export declare const MATCH_BLOCK_END: RegExp;
|
|
13
|
+
export declare const MATCH_WORDS: RegExp;
|
|
3
14
|
/**
|
|
4
|
-
*
|
|
15
|
+
* Convert a string to a regular expression that matches that string.
|
|
5
16
|
*
|
|
6
|
-
* @param
|
|
7
|
-
*
|
|
8
|
-
* - Item is an object: recurse into each property of the object to look for strings that match.
|
|
9
|
-
* - Item is string: match the string against the regular expression.
|
|
10
|
-
* - Item is anything else: return false (can't be matched).
|
|
11
|
-
*
|
|
12
|
-
* @param regexp The `RegExp` instance to match the
|
|
17
|
+
* @param str The input string.
|
|
18
|
+
* @param flags RegExp flags that are passed into the created RegExp.
|
|
13
19
|
*/
|
|
14
|
-
export declare
|
|
20
|
+
export declare const toRegExp: (str: string, flags?: string) => RegExp;
|
|
21
|
+
/** Escape special characters in a string regular expression. */
|
|
22
|
+
export declare const escapeRegExp: (str: string) => string;
|
|
23
|
+
/** Create regular expression that matches a block of content. */
|
|
24
|
+
export declare const getBlockRegExp: (middle?: string, end?: string, start?: string) => RegExp;
|
|
25
|
+
/** Create regular expression that matches a line of content. */
|
|
26
|
+
export declare const getLineRegExp: (middle?: string, end?: string, start?: string) => RegExp;
|
|
27
|
+
/** Create regular expression that matches piece of text wrapped by a set of characters. */
|
|
28
|
+
export declare const getWrapRegExp: (chars: string, middle?: string) => RegExp;
|
|
15
29
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* -
|
|
30
|
+
* Convert a string query to the corresponding set of case-insensitive regular expressions.
|
|
31
|
+
* - Splies the query into words (respecting "quoted phrases").
|
|
32
|
+
* - Return a regex for each word or quoted phrase.
|
|
33
|
+
* - Unquoted words match partially (starting with a word boundary).
|
|
34
|
+
* - Quoted phrases match fully (starting and ending with a word boundary).
|
|
21
35
|
*/
|
|
22
|
-
export declare
|
|
36
|
+
export declare const toWordRegExps: (query: string) => ImmutableArray<RegExp>;
|
|
37
|
+
/** Convert a string to a regular expression matching the start of a word boundary. */
|
|
38
|
+
export declare const toWordRegExp: (word: string) => RegExp;
|
|
23
39
|
/**
|
|
24
40
|
* Match an item matching all words in a query.
|
|
25
41
|
*
|
|
@@ -27,17 +43,14 @@ export declare function MATCHES_ALL(item: unknown, regexps: ImmutableArray<RegEx
|
|
|
27
43
|
* @param query The query string to search for.
|
|
28
44
|
* - Supports `"compound queries"` with quotes.
|
|
29
45
|
*/
|
|
30
|
-
export declare function
|
|
46
|
+
export declare function matchesAll(item: unknown, regexps: Iterable<RegExp>): boolean;
|
|
31
47
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* - Quoted phrases match fully (starting and ending with a word boundary).
|
|
48
|
+
* Match an item any of several regular expressions.
|
|
49
|
+
*
|
|
50
|
+
* @param item The item to search for the query in.
|
|
51
|
+
* @param regexps An iterable set of regular expressions.
|
|
37
52
|
*/
|
|
38
|
-
export declare
|
|
39
|
-
/** Convert a string to a regular expression matching the start of a word boundary. */
|
|
40
|
-
export declare const toWordRegExp: (word: string) => RegExp;
|
|
53
|
+
export declare function matchesAny(item: unknown, regexps: Iterable<RegExp>): boolean;
|
|
41
54
|
/** Matcher that matches any words in a string. */
|
|
42
55
|
export declare class MatchAnyWord implements Matchable<unknown, void> {
|
|
43
56
|
private _regexps;
|
package/util/search.js
CHANGED
|
@@ -1,18 +1,42 @@
|
|
|
1
|
-
import { toWords,
|
|
1
|
+
import { toWords, normalizeString } from "./string.js";
|
|
2
|
+
// Regular expressions.
|
|
3
|
+
export const MATCH_SPACE = /\s+/; // Match the first run of one or more space characters.
|
|
4
|
+
export const MATCH_SPACES = /\s+/g; // Match all runs of one or more space characters.
|
|
5
|
+
export const MATCH_LINEBREAK = /\n+/; // Match the first run of one or more linebreak characters.
|
|
6
|
+
export const MATCH_LINEBREAKS = /\n+/; // Match all runs of one or more linebreak characters.
|
|
7
|
+
export const MATCH_LINE = /[^\n]*/; // Match line of content (anything that's not a newline).
|
|
8
|
+
export const MATCH_LINE_START = /^\n*|\n+/; // Starts at start of line (one or more linebreak or start of string).
|
|
9
|
+
export const MATCH_LINE_END = /\n+|$/; // Ends at end of line (one or more linebreak or end of string).
|
|
10
|
+
export const MATCH_BLOCK = /[\s\S]*?/; // Match block of content (including newlines so don't be greedy).
|
|
11
|
+
export const MATCH_BLOCK_START = /^\n*|\n+/; // Starts at start of a block (one or more linebreak or start of string).
|
|
12
|
+
export const MATCH_BLOCK_END = /\n*$|\n\n+/; // End of a block (two or more linebreaks or end of string).
|
|
13
|
+
export const MATCH_WORDS = /\S(?:[\s\S]*?\S)?/; // Run of text that starts and ends with non-space characters (possibly multi-line).
|
|
2
14
|
/**
|
|
3
|
-
*
|
|
15
|
+
* Convert a string to a regular expression that matches that string.
|
|
4
16
|
*
|
|
5
|
-
* @param
|
|
6
|
-
*
|
|
7
|
-
* - Item is an object: recurse into each property of the object to look for strings that match.
|
|
8
|
-
* - Item is string: match the string against the regular expression.
|
|
9
|
-
* - Item is anything else: return false (can't be matched).
|
|
10
|
-
*
|
|
11
|
-
* @param regexp The `RegExp` instance to match the
|
|
17
|
+
* @param str The input string.
|
|
18
|
+
* @param flags RegExp flags that are passed into the created RegExp.
|
|
12
19
|
*/
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
export const toRegExp = (str, flags = "") => new RegExp(escapeRegExp(str), flags);
|
|
21
|
+
/** Escape special characters in a string regular expression. */
|
|
22
|
+
export const escapeRegExp = (str) => str.replace(REPLACE_ESCAPED, "\\$&");
|
|
23
|
+
const REPLACE_ESCAPED = /[-[\]/{}()*+?.\\^$|]/g;
|
|
24
|
+
/** Create regular expression that matches a block of content. */
|
|
25
|
+
export const getBlockRegExp = (middle = MATCH_BLOCK.source, end = MATCH_BLOCK_END.source, start = MATCH_BLOCK_START.source) => new RegExp(`(?:${start})${middle}(?:${end})`);
|
|
26
|
+
/** Create regular expression that matches a line of content. */
|
|
27
|
+
export const getLineRegExp = (middle = MATCH_LINE.source, end = MATCH_LINE_END.source, start = MATCH_LINE_START.source) => new RegExp(`(?:${start})${middle}(?:${end})`);
|
|
28
|
+
/** Create regular expression that matches piece of text wrapped by a set of characters. */
|
|
29
|
+
export const getWrapRegExp = (chars, middle = MATCH_WORDS.source) => new RegExp(`(${chars})(${middle})\\1`);
|
|
30
|
+
/**
|
|
31
|
+
* Convert a string query to the corresponding set of case-insensitive regular expressions.
|
|
32
|
+
* - Splies the query into words (respecting "quoted phrases").
|
|
33
|
+
* - Return a regex for each word or quoted phrase.
|
|
34
|
+
* - Unquoted words match partially (starting with a word boundary).
|
|
35
|
+
* - Quoted phrases match fully (starting and ending with a word boundary).
|
|
36
|
+
*/
|
|
37
|
+
export const toWordRegExps = (query) => toWords(query).map(toWordRegExp);
|
|
38
|
+
/** Convert a string to a regular expression matching the start of a word boundary. */
|
|
39
|
+
export const toWordRegExp = (word) => new RegExp(`\\b${escapeRegExp(normalizeString(word))}`, "i");
|
|
16
40
|
/**
|
|
17
41
|
* Match an item matching all words in a query.
|
|
18
42
|
*
|
|
@@ -20,46 +44,37 @@ export function MATCHES(item, regexp) {
|
|
|
20
44
|
* @param query The query string to search for.
|
|
21
45
|
* - Supports `"compound queries"` with quotes.
|
|
22
46
|
*/
|
|
23
|
-
export function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
47
|
+
export function matchesAll(item, regexps) {
|
|
48
|
+
let count = 0;
|
|
49
|
+
if (typeof item === "string") {
|
|
50
|
+
for (const regexp of regexps) {
|
|
51
|
+
count++;
|
|
52
|
+
if (!regexp.test(item))
|
|
27
53
|
return false;
|
|
28
|
-
|
|
54
|
+
}
|
|
29
55
|
}
|
|
30
|
-
return false;
|
|
56
|
+
return count ? true : false;
|
|
31
57
|
}
|
|
32
58
|
/**
|
|
33
|
-
* Match an item
|
|
59
|
+
* Match an item any of several regular expressions.
|
|
34
60
|
*
|
|
35
61
|
* @param item The item to search for the query in.
|
|
36
|
-
* @param
|
|
37
|
-
* - Supports `"compound queries"` with quotes.
|
|
62
|
+
* @param regexps An iterable set of regular expressions.
|
|
38
63
|
*/
|
|
39
|
-
export function
|
|
64
|
+
export function matchesAny(item, regexps) {
|
|
40
65
|
if (typeof item === "string")
|
|
41
66
|
for (const regexp of regexps)
|
|
42
|
-
if (
|
|
67
|
+
if (regexp.test(item))
|
|
43
68
|
return true;
|
|
44
69
|
return false;
|
|
45
70
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Convert a string query to the corresponding set of case-insensitive regular expressions.
|
|
48
|
-
* - Splies the query into words (respecting "quoted phrases").
|
|
49
|
-
* - Return a regex for each word or quoted phrase.
|
|
50
|
-
* - Unquoted words match partially (starting with a word boundary).
|
|
51
|
-
* - Quoted phrases match fully (starting and ending with a word boundary).
|
|
52
|
-
*/
|
|
53
|
-
export const toWordRegExps = (query) => toWords(query).map(toWordRegExp);
|
|
54
|
-
/** Convert a string to a regular expression matching the start of a word boundary. */
|
|
55
|
-
export const toWordRegExp = (word) => new RegExp(`\\b${escapeRegExp(normalizeString(word))}`, "i");
|
|
56
71
|
/** Matcher that matches any words in a string. */
|
|
57
72
|
export class MatchAnyWord {
|
|
58
73
|
constructor(words) {
|
|
59
74
|
this._regexps = toWordRegExps(words);
|
|
60
75
|
}
|
|
61
76
|
match(value) {
|
|
62
|
-
return
|
|
77
|
+
return matchesAny(value, this._regexps);
|
|
63
78
|
}
|
|
64
79
|
}
|
|
65
80
|
/** Matcher that matches all words in a string. */
|
|
@@ -68,7 +83,7 @@ export class MatchAllWords {
|
|
|
68
83
|
this._regexps = toWordRegExps(words);
|
|
69
84
|
}
|
|
70
85
|
match(value) {
|
|
71
|
-
return
|
|
86
|
+
return matchesAll(value, this._regexps);
|
|
72
87
|
}
|
|
73
88
|
}
|
|
74
89
|
/** Matcher that matches an exact phrase. */
|
|
@@ -77,6 +92,6 @@ export class MatchWord {
|
|
|
77
92
|
this._regexp = toWordRegExp(phrase);
|
|
78
93
|
}
|
|
79
94
|
match(value) {
|
|
80
|
-
return
|
|
95
|
+
return this._regexp.test(value);
|
|
81
96
|
}
|
|
82
97
|
}
|
package/util/string.d.ts
CHANGED
|
@@ -77,15 +77,6 @@ export declare const toSlug: (str: string) => string;
|
|
|
77
77
|
export declare const toWords: (str: string) => ImmutableArray<string>;
|
|
78
78
|
/** Find and iterate over the words in a string. */
|
|
79
79
|
export declare function yieldWords(value: string): Generator<string, void, void>;
|
|
80
|
-
/**
|
|
81
|
-
* Convert a string to a regular expression that matches that string.
|
|
82
|
-
*
|
|
83
|
-
* @param str The input string.
|
|
84
|
-
* @param flags RegExp flags that are passed into the created RegExp.
|
|
85
|
-
*/
|
|
86
|
-
export declare const toRegExp: (str: string, flags?: string) => RegExp;
|
|
87
|
-
/** Escape special characters in a string regular expression. */
|
|
88
|
-
export declare const escapeRegExp: (str: string) => string;
|
|
89
80
|
/** Is the first character of a string an uppercase letter? */
|
|
90
81
|
export declare const isUppercaseLetter: (str: string) => boolean;
|
|
91
82
|
/** Is the first character of a string a lowercase letter? */
|
package/util/string.js
CHANGED
|
@@ -135,16 +135,6 @@ export function* yieldWords(value) {
|
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
const MATCH_WORD = /[^\s"]+|"([^"]*)"/g; // Runs of characters without spaces, or "quoted phrases"
|
|
138
|
-
/**
|
|
139
|
-
* Convert a string to a regular expression that matches that string.
|
|
140
|
-
*
|
|
141
|
-
* @param str The input string.
|
|
142
|
-
* @param flags RegExp flags that are passed into the created RegExp.
|
|
143
|
-
*/
|
|
144
|
-
export const toRegExp = (str, flags = "") => new RegExp(escapeRegExp(str), flags);
|
|
145
|
-
/** Escape special characters in a string regular expression. */
|
|
146
|
-
export const escapeRegExp = (str) => str.replace(REPLACE_ESCAPED, "\\$&");
|
|
147
|
-
const REPLACE_ESCAPED = /[-[\]/{}()*+?.\\^$|]/g;
|
|
148
138
|
/** Is the first character of a string an uppercase letter? */
|
|
149
139
|
export const isUppercaseLetter = (str) => isBetween(str.charCodeAt(0), 65, 90);
|
|
150
140
|
/** Is the first character of a string a lowercase letter? */
|
package/markup/helpers.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { MarkupElement, MarkupNode } from "./types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Take a Markup JSX node and strip all tags from it to produce a plain text string.
|
|
4
|
-
*
|
|
5
|
-
* @param node A JsxNode, e.g. either a JSX element, a plain string, or null/undefined (or an array of those things).
|
|
6
|
-
* @returns The combined string made from the JSX node.
|
|
7
|
-
*
|
|
8
|
-
* @example `- Item with *strong*\n- Item with _em_` becomes `Item with strong Item with em`
|
|
9
|
-
*/
|
|
10
|
-
export declare function nodeToText(node: MarkupNode): string;
|
|
11
|
-
/**
|
|
12
|
-
* Take a Markup JSX node and convert it to an HTML string.
|
|
13
|
-
*
|
|
14
|
-
* @param node Any `MarkupNode`, i.e. a string, `MarkupElement`, or array of those.
|
|
15
|
-
* - Any props in the node will be rendered if they are strings or numbers or `true`. All other props are skipped.
|
|
16
|
-
* @returns The HTML generated from the node.
|
|
17
|
-
*
|
|
18
|
-
* @example `- Item with *strong*\n- Item with _em_` becomes `<ul><li>Item with <strong>strong</strong></li><li>Item with <em>em</em></ul>`
|
|
19
|
-
*/
|
|
20
|
-
export declare function nodeToHtml(node: MarkupNode): string;
|
|
21
|
-
/**
|
|
22
|
-
* Iterate through all elements in a node.
|
|
23
|
-
* - This is useful if you, e.g. want to apply a `className` to all `<h1>` elements, or make a list of all URLs found in a Node.
|
|
24
|
-
*/
|
|
25
|
-
export declare function yieldElements(node: MarkupNode): Generator<MarkupElement, void>;
|
package/markup/helpers.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { serialise } from "../util/index.js";
|
|
2
|
-
/**
|
|
3
|
-
* Take a Markup JSX node and strip all tags from it to produce a plain text string.
|
|
4
|
-
*
|
|
5
|
-
* @param node A JsxNode, e.g. either a JSX element, a plain string, or null/undefined (or an array of those things).
|
|
6
|
-
* @returns The combined string made from the JSX node.
|
|
7
|
-
*
|
|
8
|
-
* @example `- Item with *strong*\n- Item with _em_` becomes `Item with strong Item with em`
|
|
9
|
-
*/
|
|
10
|
-
export function nodeToText(node) {
|
|
11
|
-
if (typeof node === "string")
|
|
12
|
-
return node;
|
|
13
|
-
if (node instanceof Array)
|
|
14
|
-
return node.map(nodeToText).join(" ");
|
|
15
|
-
if (typeof node === "object" && node)
|
|
16
|
-
return nodeToText(node.props.children);
|
|
17
|
-
return "";
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Take a Markup JSX node and convert it to an HTML string.
|
|
21
|
-
*
|
|
22
|
-
* @param node Any `MarkupNode`, i.e. a string, `MarkupElement`, or array of those.
|
|
23
|
-
* - Any props in the node will be rendered if they are strings or numbers or `true`. All other props are skipped.
|
|
24
|
-
* @returns The HTML generated from the node.
|
|
25
|
-
*
|
|
26
|
-
* @example `- Item with *strong*\n- Item with _em_` becomes `<ul><li>Item with <strong>strong</strong></li><li>Item with <em>em</em></ul>`
|
|
27
|
-
*/
|
|
28
|
-
export function nodeToHtml(node) {
|
|
29
|
-
if (typeof node === "string")
|
|
30
|
-
return node;
|
|
31
|
-
if (node instanceof Array)
|
|
32
|
-
return node.map(nodeToHtml).join("");
|
|
33
|
-
if (typeof node === "object" && node) {
|
|
34
|
-
const { type, props: { children, ...props }, } = node;
|
|
35
|
-
const strings = Object.entries(props).map(propToString).filter(Boolean);
|
|
36
|
-
return `<${type}${strings.length ? ` ${strings.join(" ")}` : ""}>${nodeToHtml(children)}</${type}>`;
|
|
37
|
-
}
|
|
38
|
-
return "";
|
|
39
|
-
}
|
|
40
|
-
const propToString = ([key, value]) => (value === true ? key : typeof value === "number" && Number.isFinite(value) ? `${key}="${value.toString()}"` : typeof value === "string" ? `${key}=${serialise(value)}` : "");
|
|
41
|
-
/**
|
|
42
|
-
* Iterate through all elements in a node.
|
|
43
|
-
* - This is useful if you, e.g. want to apply a `className` to all `<h1>` elements, or make a list of all URLs found in a Node.
|
|
44
|
-
*/
|
|
45
|
-
export function* yieldElements(node) {
|
|
46
|
-
if (node instanceof Array)
|
|
47
|
-
for (const n of node)
|
|
48
|
-
yield* yieldElements(n);
|
|
49
|
-
else if (typeof node === "object" && node) {
|
|
50
|
-
yield node;
|
|
51
|
-
if (node.props.children)
|
|
52
|
-
yield* yieldElements(node.props.children);
|
|
53
|
-
}
|
|
54
|
-
}
|