@mark1russell7/text 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Russell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,33 @@
1
- # Temporary Holding Version
1
+ # @mark1russell7/text
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Plain text helpers. The first group is the escapes of a text for four targets.
4
+
5
+ ```sh
6
+ pnpm add @mark1russell7/text
7
+ ```
8
+
9
+ | Function | Target | Changes |
10
+ | --- | --- | --- |
11
+ | `escapeHtml` | The content of an HTML element | `&`, `<` and `>`, to `&amp;`, `&lt;` and `&gt;` |
12
+ | `escapeAttribute` | An HTML attribute value in double or single quotes | `&`, `<`, `>`, `"` and `'`, to `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` |
13
+ | `escapeRegExp` | A pattern of `new RegExp`, outside a character class | A backslash before each syntax character: `^ $ \ . * + ? ( ) [ ] { } \|` |
14
+ | `escapeMarkdown` | One line of CommonMark or GitHub Markdown | A backslash before each ASCII punctuation character, and a space for each line end |
15
+
16
+ ```ts
17
+ import { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp } from "@mark1russell7/text";
18
+
19
+ escapeHtml("a < b & c"); // "a &lt; b &amp; c"
20
+ escapeAttribute(`say "hi"`); // "say &quot;hi&quot;"
21
+ new RegExp(`^${escapeRegExp("1+1=2?")}$`, "u").test("1+1=2?"); // true
22
+ escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
23
+ ```
24
+
25
+ ## The copies that this package replaces
26
+
27
+ The tests run 53 copies of these functions from other repositories of the owner. Each copy comes from a list of the duplication atlas, at a pinned commit. The file `test/copies.json` has each copy, and `test/differences.ts` has each difference from the function of this package, with its reason. A copy that is not in the list gives the same output on each of the 1000 test strings.
28
+
29
+ ## Limits
30
+
31
+ - `escapeHtml` does not escape quotes. For an attribute value, use `escapeAttribute`.
32
+ - `escapeRegExp` gives a pattern for use outside a character class. In a class, `-` also has a meaning.
33
+ - `escapeMarkdown` does not change spaces. Four spaces at the start of a line make a code block.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes,
3
+ * regular expressions and Markdown.
4
+ */
5
+ /**
6
+ * This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character
7
+ * references. The quotes stay, because they have no meaning in element content.
8
+ *
9
+ * @param text - The plain text.
10
+ * @returns The text with `&amp;`, `&lt;` and `&gt;` in place of the three characters.
11
+ */
12
+ export declare function escapeHtml(text: string): string;
13
+ /**
14
+ * This function escapes a text for an HTML attribute value in double quotes or in single quotes. It changes `&`, `<`,
15
+ * `>`, `"` and `'`. The result is also correct in element content.
16
+ *
17
+ * @param text - The plain text.
18
+ * @returns The text with `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` in place of the five characters.
19
+ */
20
+ export declare function escapeAttribute(text: string): string;
21
+ /**
22
+ * This function escapes a text for the pattern of a regular expression, outside a character class. It puts a backslash
23
+ * before each syntax character of ECMAScript: `^ $ \ . * + ? ( ) [ ] { } |`. The pattern matches only the text, with
24
+ * the flag `u`, with the flag `v` and with no flag.
25
+ *
26
+ * @param text - The plain text.
27
+ * @returns A pattern for `new RegExp` that matches the text.
28
+ */
29
+ export declare function escapeRegExp(text: string): string;
30
+ /**
31
+ * This function escapes a text for one line of CommonMark or GitHub Markdown: a paragraph, a heading or a table cell.
32
+ * It puts a backslash before each ASCII punctuation character, and changes each line end to a space.
33
+ *
34
+ * A backslash before an ASCII punctuation character always gives the character itself. Thus no character of the result
35
+ * starts emphasis, code, a link, HTML, an entity, a list or a table column. Four spaces at the start of a line still
36
+ * make a code block.
37
+ *
38
+ * @param text - The plain text.
39
+ * @returns The text for one line of Markdown.
40
+ */
41
+ export declare function escapeMarkdown(text: string): string;
42
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/escape.ts"],"mappings":";;;;;;;;;;;wBAegB,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe"}
package/build/index.js ADDED
@@ -0,0 +1,63 @@
1
+ // src/escape.ts
2
+ /**
3
+ * This module has the escapes of plain text for four targets. The targets are HTML content, an HTML attribute, a
4
+ * regular expression and one line of Markdown. Each function changes only the characters that have a meaning in its target.
5
+ */
6
+ const HTML = {
7
+ "&": "&amp;",
8
+ "<": "&lt;",
9
+ ">": "&gt;"
10
+ };
11
+ const ATTRIBUTE = {
12
+ ...HTML,
13
+ "\"": "&quot;",
14
+ "'": "&#39;"
15
+ };
16
+ /**
17
+ * This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character
18
+ * references. The quotes stay, because they have no meaning in element content.
19
+ *
20
+ * @param text - The plain text.
21
+ * @returns The text with `&amp;`, `&lt;` and `&gt;` in place of the three characters.
22
+ */
23
+ function escapeHtml(text) {
24
+ return text.replace(/[&<>]/g, (char) => HTML[char] ?? char);
25
+ }
26
+ /**
27
+ * This function escapes a text for an HTML attribute value in double quotes or in single quotes. It changes `&`, `<`,
28
+ * `>`, `"` and `'`. The result is also correct in element content.
29
+ *
30
+ * @param text - The plain text.
31
+ * @returns The text with `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` in place of the five characters.
32
+ */
33
+ function escapeAttribute(text) {
34
+ return text.replace(/[&<>"']/g, (char) => ATTRIBUTE[char] ?? char);
35
+ }
36
+ /**
37
+ * This function escapes a text for the pattern of a regular expression, outside a character class. It puts a backslash
38
+ * before each syntax character of ECMAScript: `^ $ \ . * + ? ( ) [ ] { } |`. The pattern matches only the text, with
39
+ * the flag `u`, with the flag `v` and with no flag.
40
+ *
41
+ * @param text - The plain text.
42
+ * @returns A pattern for `new RegExp` that matches the text.
43
+ */
44
+ function escapeRegExp(text) {
45
+ return text.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
46
+ }
47
+ /**
48
+ * This function escapes a text for one line of CommonMark or GitHub Markdown: a paragraph, a heading or a table cell.
49
+ * It puts a backslash before each ASCII punctuation character, and changes each line end to a space.
50
+ *
51
+ * A backslash before an ASCII punctuation character always gives the character itself. Thus no character of the result
52
+ * starts emphasis, code, a link, HTML, an entity, a list or a table column. Four spaces at the start of a line still
53
+ * make a code block.
54
+ *
55
+ * @param text - The plain text.
56
+ * @returns The text for one line of Markdown.
57
+ */
58
+ function escapeMarkdown(text) {
59
+ return text.replace(/\r\n?|\n/g, " ").replace(/[!-/:-@[-`{-~]/g, "\\$&");
60
+ }
61
+ export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp };
62
+
63
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/escape.ts"],"sourcesContent":["/**\n * This module has the escapes of plain text for four targets. The targets are HTML content, an HTML attribute, a\n * regular expression and one line of Markdown. Each function changes only the characters that have a meaning in its target.\n */\n\nconst HTML: Readonly<Record<string, string>> = { \"&\": \"&amp;\", \"<\": \"&lt;\", \">\": \"&gt;\" };\nconst ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '\"': \"&quot;\", \"'\": \"&#39;\" };\n\n/**\n * This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character\n * references. The quotes stay, because they have no meaning in element content.\n *\n * @param text - The plain text.\n * @returns The text with `&amp;`, `&lt;` and `&gt;` in place of the three characters.\n */\nexport function escapeHtml(text: string): string {\n return text.replace(/[&<>]/g, (char) => HTML[char] ?? char);\n}\n\n/**\n * This function escapes a text for an HTML attribute value in double quotes or in single quotes. It changes `&`, `<`,\n * `>`, `\"` and `'`. The result is also correct in element content.\n *\n * @param text - The plain text.\n * @returns The text with `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` in place of the five characters.\n */\nexport function escapeAttribute(text: string): string {\n return text.replace(/[&<>\"']/g, (char) => ATTRIBUTE[char] ?? char);\n}\n\n/**\n * This function escapes a text for the pattern of a regular expression, outside a character class. It puts a backslash\n * before each syntax character of ECMAScript: `^ $ \\ . * + ? ( ) [ ] { } |`. The pattern matches only the text, with\n * the flag `u`, with the flag `v` and with no flag.\n *\n * @param text - The plain text.\n * @returns A pattern for `new RegExp` that matches the text.\n */\nexport function escapeRegExp(text: string): string {\n return text.replace(/[\\\\^$.*+?()[\\]{}|]/g, \"\\\\$&\");\n}\n\n/**\n * This function escapes a text for one line of CommonMark or GitHub Markdown: a paragraph, a heading or a table cell.\n * It puts a backslash before each ASCII punctuation character, and changes each line end to a space.\n *\n * A backslash before an ASCII punctuation character always gives the character itself. Thus no character of the result\n * starts emphasis, code, a link, HTML, an entity, a list or a table column. Four spaces at the start of a line still\n * make a code block.\n *\n * @param text - The plain text.\n * @returns The text for one line of Markdown.\n */\nexport function escapeMarkdown(text: string): string {\n return text.replace(/\\r\\n?|\\n/g, \" \").replace(/[!-/:-@[-`{-~]/g, \"\\\\$&\");\n}\n"],"mappings":";;;;;AAKA,MAAM,OAAyC;CAAE,KAAK;CAAS,KAAK;CAAQ,KAAK;AAAO;AACxF,MAAM,YAA8C;CAAE,GAAG;CAAM,MAAK;CAAU,KAAK;AAAQ;;;;;;;;AAS3F,SAAgB,WAAW,MAAsB;CAC/C,OAAO,KAAK,QAAQ,WAAW,SAAS,KAAK,SAAS,IAAI;AAC5D;;;;;;;;AASA,SAAgB,gBAAgB,MAAsB;CACpD,OAAO,KAAK,QAAQ,aAAa,SAAS,UAAU,SAAS,IAAI;AACnE;;;;;;;;;AAUA,SAAgB,aAAa,MAAsB;CACjD,OAAO,KAAK,QAAQ,uBAAuB,MAAM;AACnD;;;;;;;;;;;;AAaA,SAAgB,eAAe,MAAsB;CACnD,OAAO,KAAK,QAAQ,aAAa,GAAG,CAAC,CAAC,QAAQ,mBAAmB,MAAM;AACzE"}
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package",
2
3
  "name": "@mark1russell7/text",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
4
+ "version": "0.0.1",
5
+ "description": "Plain text helpers. The first group is the escapes: escapeHtml, escapeAttribute, escapeRegExp and escapeMarkdown.",
6
+ "keywords": [
7
+ "text",
8
+ "escape",
9
+ "html",
10
+ "regexp",
11
+ "markdown",
12
+ "typescript"
13
+ ],
14
+ "homepage": "https://github.com/mark1russell7/text#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/mark1russell7/text/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/mark1russell7/text.git",
21
+ "directory": "packages/text"
22
+ },
23
+ "license": "MIT",
24
+ "author": "Mark Russell (https://github.com/mark1russell7)",
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./build/index.js",
28
+ "types": "./build/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./build/index.d.ts",
32
+ "default": "./build/index.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "files": [
37
+ "build",
38
+ "src",
39
+ "!src/**/*.test.ts"
40
+ ],
41
+ "engines": {
42
+ "node": ">=22.12"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "devDependencies": {
48
+ "@mark1russell7/cue": "^2.0.0",
49
+ "@mark1russell7/rigor-generators": "^0.1.8",
50
+ "@types/node": "^26.6.5",
51
+ "esbuild": "^0.28.2",
52
+ "tsdown": "^0.23.7",
53
+ "typescript": "^7.0.2",
54
+ "vitest": "^5.0.3"
55
+ },
56
+ "scripts": {
57
+ "build": "tsdown",
58
+ "test": "vitest run",
59
+ "test:watch": "vitest",
60
+ "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
61
+ }
6
62
  }
package/src/escape.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * This module has the escapes of plain text for four targets. The targets are HTML content, an HTML attribute, a
3
+ * regular expression and one line of Markdown. Each function changes only the characters that have a meaning in its target.
4
+ */
5
+
6
+ const HTML: Readonly<Record<string, string>> = { "&": "&amp;", "<": "&lt;", ">": "&gt;" };
7
+ const ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '"': "&quot;", "'": "&#39;" };
8
+
9
+ /**
10
+ * This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character
11
+ * references. The quotes stay, because they have no meaning in element content.
12
+ *
13
+ * @param text - The plain text.
14
+ * @returns The text with `&amp;`, `&lt;` and `&gt;` in place of the three characters.
15
+ */
16
+ export function escapeHtml(text: string): string {
17
+ return text.replace(/[&<>]/g, (char) => HTML[char] ?? char);
18
+ }
19
+
20
+ /**
21
+ * This function escapes a text for an HTML attribute value in double quotes or in single quotes. It changes `&`, `<`,
22
+ * `>`, `"` and `'`. The result is also correct in element content.
23
+ *
24
+ * @param text - The plain text.
25
+ * @returns The text with `&amp;`, `&lt;`, `&gt;`, `&quot;` and `&#39;` in place of the five characters.
26
+ */
27
+ export function escapeAttribute(text: string): string {
28
+ return text.replace(/[&<>"']/g, (char) => ATTRIBUTE[char] ?? char);
29
+ }
30
+
31
+ /**
32
+ * This function escapes a text for the pattern of a regular expression, outside a character class. It puts a backslash
33
+ * before each syntax character of ECMAScript: `^ $ \ . * + ? ( ) [ ] { } |`. The pattern matches only the text, with
34
+ * the flag `u`, with the flag `v` and with no flag.
35
+ *
36
+ * @param text - The plain text.
37
+ * @returns A pattern for `new RegExp` that matches the text.
38
+ */
39
+ export function escapeRegExp(text: string): string {
40
+ return text.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
41
+ }
42
+
43
+ /**
44
+ * This function escapes a text for one line of CommonMark or GitHub Markdown: a paragraph, a heading or a table cell.
45
+ * It puts a backslash before each ASCII punctuation character, and changes each line end to a space.
46
+ *
47
+ * A backslash before an ASCII punctuation character always gives the character itself. Thus no character of the result
48
+ * starts emphasis, code, a link, HTML, an entity, a list or a table column. Four spaces at the start of a line still
49
+ * make a code block.
50
+ *
51
+ * @param text - The plain text.
52
+ * @returns The text for one line of Markdown.
53
+ */
54
+ export function escapeMarkdown(text: string): string {
55
+ return text.replace(/\r\n?|\n/g, " ").replace(/[!-/:-@[-`{-~]/g, "\\$&");
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes,
3
+ * regular expressions and Markdown.
4
+ */
5
+ export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp } from "./escape.ts";