shelving 1.51.1 → 1.51.2

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 CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from "./types.js";
2
- export * from "./helpers.js";
3
2
  export * from "./rules.js";
4
3
  export * from "./render.js";
package/markup/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from "./types.js";
2
- export * from "./helpers.js";
3
2
  export * from "./rules.js";
4
3
  export * from "./render.js";
@@ -1,4 +1,5 @@
1
- import type { MarkupOptions, MarkupNode } from "./types.js";
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>): MarkupNode;
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) {
@@ -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 (presumably it doesn't have any punctuation).
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
- node.$$typeof = REACT_SECURITY_SYMBOL; // Inject React security type. See https://github.com/facebook/react/pull/4832
107
- if (node.props.children)
108
- node.props.children = renderNode(node.props.children, options);
109
- return node;
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
@@ -44,7 +44,7 @@ export declare const PARAGRAPH_RULE: MarkupRule;
44
44
  * - If link is not valid (using `new URL(url)` then unparsed text will be returned.
45
45
  * - For security only `http://` or `https://` links will work (if invalid the unparsed text will be returned).
46
46
  */
47
- export declare const LINK_MARKUP: MarkupRule;
47
+ export declare const LINK_RULE: MarkupRule;
48
48
  /**
49
49
  * Autolinked URL starts with `http:` or `https:` and matches an unlimited number of non-space characters.
50
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).
@@ -69,7 +69,7 @@ export declare const CODE_RULE: MarkupRule;
69
69
  * - Closing characters must exactly match opening characters.
70
70
  * - Different to Markdown: strong is always surrounded by `*asterisks*` and emphasis is always surrounded by `_underscores_` (strong isn't 'double emphasis').
71
71
  */
72
- export declare const STRONG_MARKUP: MarkupRule;
72
+ export declare const STRONG_RULE: MarkupRule;
73
73
  /**
74
74
  * Inline emphasis.
75
75
  * - Inline text wrapped in one or more `_` underscore symbols.
package/markup/rules.js CHANGED
@@ -1,8 +1,8 @@
1
- import { formatUrl, toURL, getLineRegExp, MATCH_LINE, getBlockRegExp, MATCH_BLOCK, getWrapRegExp } 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
3
  const BULLETS = "-*•+"; // Anything that can be a bullet (used for unordered lists and horizontal rules).
4
4
  // Regular expressions.
5
- const REPLACE_INDENT = /^ {1,2}/gm;
5
+ const MATCH_INDENT = /^ {1,2}/gm;
6
6
  /**
7
7
  * Headings are single line only (don't allow multiline).
8
8
  * - 1-6 hashes then 1+ spaces, then the title.
@@ -10,7 +10,7 @@ const REPLACE_INDENT = /^ {1,2}/gm;
10
10
  * - Markdown's underline syntax is not supported (for simplification).
11
11
  */
12
12
  export const HEADING_RULE = {
13
- regexp: getLineRegExp(`(#{1,6}) +(${MATCH_LINE})`),
13
+ regexp: getLineRegExp(`(#{1,6}) +(${MATCH_LINE.source})`),
14
14
  render: ([, prefix = "", children = ""]) => ({ type: `h${prefix.length}`, key: null, props: { children } }),
15
15
  contexts: ["block"],
16
16
  childContext: "inline",
@@ -36,7 +36,7 @@ export const HORIZONTAL_RULE = {
36
36
  */
37
37
  const UNORDERED = `[${BULLETS}] +`; // Anything that can be a bullet (used for unordered lists and horizontal rules).
38
38
  export const UNORDERED_LIST_RULE = {
39
- regexp: getBlockRegExp(`${UNORDERED}(${MATCH_BLOCK})`),
39
+ regexp: getBlockRegExp(`${UNORDERED}(${MATCH_BLOCK.source})`),
40
40
  render: ([, list = ""]) => {
41
41
  const children = list.split(SPLIT_UL_ITEMS).map(mapUnorderedItem);
42
42
  return { type: "ul", key: null, props: { children } };
@@ -46,7 +46,7 @@ export const UNORDERED_LIST_RULE = {
46
46
  };
47
47
  const SPLIT_UL_ITEMS = new RegExp(`\\n+${UNORDERED}`, "g");
48
48
  const mapUnorderedItem = (item, key) => {
49
- const children = item.replace(REPLACE_INDENT, "");
49
+ const children = item.replace(MATCH_INDENT, "");
50
50
  return { type: "li", key, props: { children } };
51
51
  };
52
52
  /**
@@ -56,7 +56,7 @@ const mapUnorderedItem = (item, key) => {
56
56
  */
57
57
  const ORDERED = "[0-9]+[.):] +"; // Number for a numbered list (e.g. `1.` or `2)` or `3:`)
58
58
  export const ORDERED_LIST_RULE = {
59
- regexp: getBlockRegExp(`(${ORDERED}${MATCH_BLOCK})`),
59
+ regexp: getBlockRegExp(`(${ORDERED}${MATCH_BLOCK.source})`),
60
60
  render: ([, list = ""]) => {
61
61
  const children = list.split(SPLIT_OL_ITEMS).map(mapOrderedItem);
62
62
  return { type: "ol", key: null, props: { children } };
@@ -71,7 +71,7 @@ const mapOrderedItem = (item, key) => {
71
71
  const children = item
72
72
  .slice(firstSpace + 1)
73
73
  .trimStart()
74
- .replace(REPLACE_INDENT, "");
74
+ .replace(MATCH_INDENT, "");
75
75
  return { type: "li", key, props: { value, children } };
76
76
  };
77
77
  /**
@@ -81,7 +81,7 @@ const mapOrderedItem = (item, key) => {
81
81
  * - Quote indent symbol can be followed by zero or more spaces.
82
82
  */
83
83
  export const BLOCKQUOTE_RULE = {
84
- regexp: getLineRegExp(`(>${MATCH_LINE}(?:\\n>${MATCH_LINE})*)`),
84
+ regexp: getLineRegExp(`(>${MATCH_LINE.source}(?:\\n>${MATCH_LINE.source})*)`),
85
85
  render: ([, quote = ""]) => ({
86
86
  type: "blockquote",
87
87
  key: null,
@@ -100,7 +100,7 @@ const BLOCKQUOTE_LINES = /^>/gm;
100
100
  */
101
101
  export const FENCED_CODE_RULE = {
102
102
  // Matcher has its own end that only stops when it reaches a matching closing fence or the end of the string.
103
- regexp: getBlockRegExp(`(\`{3,}|~{3,}) *(${MATCH_LINE})\\n(${MATCH_BLOCK})`, `\\n\\1\\n+|\\n\\1$|$`),
103
+ regexp: getBlockRegExp(`(\`{3,}|~{3,}) *(${MATCH_LINE.source})\\n(${MATCH_BLOCK.source})`, `\\n\\1\\n+|\\n\\1$|$`),
104
104
  render: ([, , file, children]) => ({
105
105
  type: "pre",
106
106
  key: null,
@@ -119,7 +119,7 @@ export const FENCED_CODE_RULE = {
119
119
  * - When ordering rules, paragraph should go after other "block" context elements (because it has a very generous capture).
120
120
  */
121
121
  export const PARAGRAPH_RULE = {
122
- regexp: getBlockRegExp(` *(${MATCH_BLOCK})`),
122
+ regexp: getBlockRegExp(` *(${MATCH_BLOCK.source})`),
123
123
  render: ([, children]) => ({ type: `p`, key: null, props: { children } }),
124
124
  contexts: ["block"],
125
125
  childContext: "inline",
@@ -133,11 +133,11 @@ export const PARAGRAPH_RULE = {
133
133
  * - If link is not valid (using `new URL(url)` then unparsed text will be returned.
134
134
  * - For security only `http://` or `https://` links will work (if invalid the unparsed text will be returned).
135
135
  */
136
- export const LINK_MARKUP = {
136
+ export const LINK_RULE = {
137
137
  // Custom matcher to check the URL against the allowed schemes.
138
138
  regexp: /\[([^\]]*?)\]\(([^)]*?)\)/,
139
139
  match: (content, { schemes, url: base }) => {
140
- const matches = content.match(LINK_MARKUP.regexp);
140
+ const matches = content.match(LINK_RULE.regexp);
141
141
  if (matches && typeof matches.index === "number") {
142
142
  const [, title = "", href = ""] = matches;
143
143
  const url = toURL(href, base);
@@ -178,7 +178,7 @@ export const AUTOLINK_RULE = {
178
178
  }
179
179
  }
180
180
  },
181
- render: LINK_MARKUP.render,
181
+ render: LINK_RULE.render,
182
182
  contexts: ["inline", "list"],
183
183
  childContext: "link",
184
184
  };
@@ -190,7 +190,7 @@ export const AUTOLINK_RULE = {
190
190
  * - Same as Markdown syntax.
191
191
  */
192
192
  export const CODE_RULE = {
193
- regexp: getWrapRegExp("`+", MATCH_BLOCK),
193
+ regexp: getWrapRegExp("`+", MATCH_BLOCK.source),
194
194
  render: ([, , children]) => ({ type: "code", key: null, props: { children } }),
195
195
  contexts: ["inline", "list"],
196
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.")
@@ -203,7 +203,7 @@ export const CODE_RULE = {
203
203
  * - Closing characters must exactly match opening characters.
204
204
  * - Different to Markdown: strong is always surrounded by `*asterisks*` and emphasis is always surrounded by `_underscores_` (strong isn't 'double emphasis').
205
205
  */
206
- export const STRONG_MARKUP = {
206
+ export const STRONG_RULE = {
207
207
  regexp: getWrapRegExp("\\*+"),
208
208
  render: ([, , children]) => ({ type: "strong", key: null, props: { children } }),
209
209
  contexts: ["inline", "list", "link"],
@@ -282,10 +282,10 @@ export const MARKUP_RULES = [
282
282
  BLOCKQUOTE_RULE,
283
283
  FENCED_CODE_RULE,
284
284
  PARAGRAPH_RULE,
285
- LINK_MARKUP,
285
+ LINK_RULE,
286
286
  AUTOLINK_RULE,
287
287
  CODE_RULE,
288
- STRONG_MARKUP,
288
+ STRONG_RULE,
289
289
  EMPHASIS_RULE,
290
290
  INSERT_RULE,
291
291
  DELETE_RULE,
@@ -303,10 +303,10 @@ export const MARKUP_RULES_BLOCK = [
303
303
  ];
304
304
  /** Subset of markup rules that work in an inline context. */
305
305
  export const MARKUP_RULES_INLINE = [
306
- LINK_MARKUP,
306
+ LINK_RULE,
307
307
  AUTOLINK_RULE,
308
308
  CODE_RULE,
309
- STRONG_MARKUP,
309
+ STRONG_RULE,
310
310
  EMPHASIS_RULE,
311
311
  INSERT_RULE,
312
312
  DELETE_RULE,
@@ -317,10 +317,10 @@ export const MARKUP_RULES_SHORTFORM = [
317
317
  UNORDERED_LIST_RULE,
318
318
  ORDERED_LIST_RULE,
319
319
  PARAGRAPH_RULE,
320
- LINK_MARKUP,
320
+ LINK_RULE,
321
321
  AUTOLINK_RULE,
322
322
  CODE_RULE,
323
- STRONG_MARKUP,
323
+ STRONG_RULE,
324
324
  EMPHASIS_RULE,
325
325
  INSERT_RULE,
326
326
  DELETE_RULE,
package/markup/types.d.ts CHANGED
@@ -1,21 +1,4 @@
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
4
  /** RegExp for matching this rule. */
@@ -29,7 +12,7 @@ export declare type MarkupRule = {
29
12
  * - The `key` property is not required (will be set automatically).
30
13
  * - e.g. `{ type: "a", props: { href: "/example.html", className: "strong", children: "Children *can* include _syntax_" } }`
31
14
  */
32
- readonly render: (matches: RegExpMatchArray, options: MarkupOptions) => MarkupElement;
15
+ readonly render: (matches: RegExpMatchArray, options: MarkupOptions) => JSXElement;
33
16
  /** Apply the rule only when in certain contexts, e.g. `["block", "inline", "list"]` */
34
17
  readonly contexts: string[];
35
18
  /** Context any string children returned from `render()` should be rendered with. */
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.51.1",
14
+ "version": "1.51.2",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
package/util/index.d.ts CHANGED
@@ -17,6 +17,7 @@ export * from "./filter.js";
17
17
  export * from "./function.js";
18
18
  export * from "./hydrate.js";
19
19
  export * from "./iterate.js";
20
+ export * from "./jsx.js";
20
21
  export * from "./lazy.js";
21
22
  export * from "./map.js";
22
23
  export * from "./merge.js";
package/util/index.js CHANGED
@@ -17,6 +17,7 @@ export * from "./filter.js";
17
17
  export * from "./function.js";
18
18
  export * from "./hydrate.js";
19
19
  export * from "./iterate.js";
20
+ export * from "./jsx.js";
20
21
  export * from "./lazy.js";
21
22
  export * from "./map.js";
22
23
  export * from "./merge.js";
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,5 +1,16 @@
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
  *
@@ -9,13 +20,6 @@ import { Matchable } from "./filter.js";
9
20
  export declare const toRegExp: (str: string, flags?: string) => RegExp;
10
21
  /** Escape special characters in a string regular expression. */
11
22
  export declare const escapeRegExp: (str: string) => string;
12
- export declare const MATCH_LINE = "[^\\n]*";
13
- export declare const MATCH_LINE_START = "^\\n*|\\n+";
14
- export declare const MATCH_LINE_END = "\\n+|$";
15
- export declare const MATCH_BLOCK = "[\\s\\S]*?";
16
- export declare const MATCH_BLOCK_START = "^\\n*|\\n+";
17
- export declare const MATCH_BLOCK_END = "\\n*$|\\n\\n+";
18
- export declare const MATCH_WORDS = "\\S(?:[\\s\\S]*?\\S)?";
19
23
  /** Create regular expression that matches a block of content. */
20
24
  export declare const getBlockRegExp: (middle?: string, end?: string, start?: string) => RegExp;
21
25
  /** Create regular expression that matches a line of content. */
package/util/search.js CHANGED
@@ -1,4 +1,16 @@
1
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
  *
@@ -9,20 +21,12 @@ export const toRegExp = (str, flags = "") => new RegExp(escapeRegExp(str), flags
9
21
  /** Escape special characters in a string regular expression. */
10
22
  export const escapeRegExp = (str) => str.replace(REPLACE_ESCAPED, "\\$&");
11
23
  const REPLACE_ESCAPED = /[-[\]/{}()*+?.\\^$|]/g;
12
- // Regular expression partials (`\` slashes must be escaped as `\\`).
13
- export const MATCH_LINE = "[^\\n]*"; // Match line of content (anything that's not a newline).
14
- export const MATCH_LINE_START = "^\\n*|\\n+"; // Starts at start of line (one or more linebreak or start of string).
15
- export const MATCH_LINE_END = "\\n+|$"; // Ends at end of line (one or more linebreak or end of string).
16
- export const MATCH_BLOCK = "[\\s\\S]*?"; // Match block of content (including newlines so don't be greedy).
17
- export const MATCH_BLOCK_START = "^\\n*|\\n+"; // Starts at start of a block (one or more linebreak or start of string).
18
- export const MATCH_BLOCK_END = "\\n*$|\\n\\n+"; // End of a block (two or more linebreaks or end of string).
19
- export const MATCH_WORDS = `\\S(?:[\\s\\S]*?\\S)?`; // Run of text that starts and ends with non-space characters (possibly multi-line).
20
24
  /** Create regular expression that matches a block of content. */
21
- export const getBlockRegExp = (middle = MATCH_BLOCK, end = MATCH_BLOCK_END, start = MATCH_BLOCK_START) => new RegExp(`(?:${start})${middle}(?:${end})`);
25
+ export const getBlockRegExp = (middle = MATCH_BLOCK.source, end = MATCH_BLOCK_END.source, start = MATCH_BLOCK_START.source) => new RegExp(`(?:${start})${middle}(?:${end})`);
22
26
  /** Create regular expression that matches a line of content. */
23
- export const getLineRegExp = (middle = MATCH_LINE, end = MATCH_LINE_END, start = MATCH_LINE_START) => new RegExp(`(?:${start})${middle}(?:${end})`);
27
+ export const getLineRegExp = (middle = MATCH_LINE.source, end = MATCH_LINE_END.source, start = MATCH_LINE_START.source) => new RegExp(`(?:${start})${middle}(?:${end})`);
24
28
  /** Create regular expression that matches piece of text wrapped by a set of characters. */
25
- export const getWrapRegExp = (chars, middle = MATCH_WORDS) => new RegExp(`(${chars})(${middle})\\1`);
29
+ export const getWrapRegExp = (chars, middle = MATCH_WORDS.source) => new RegExp(`(${chars})(${middle})\\1`);
26
30
  /**
27
31
  * Convert a string query to the corresponding set of case-insensitive regular expressions.
28
32
  * - Splies the query into words (respecting "quoted phrases").
@@ -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
- }