@mark1russell7/text 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +36 -2
- package/build/index.d.ts +58 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +83 -0
- package/build/index.js.map +1 -0
- package/package.json +60 -3
- package/src/escape.ts +78 -0
- package/src/index.ts +5 -0
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,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mark1russell7/text
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Plain text helpers. The first group is the escapes of a text for five 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 `&`, `<` and `>` |
|
|
12
|
+
| `escapeAttribute` | An HTML attribute value in double or single quotes | `&`, `<`, `>`, `"` and `'`, to `&`, `<`, `>`, `"` and `'` |
|
|
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
|
+
| `escapeWorkflowData` | The data of a GitHub Actions workflow command | `%`, CR and LF, to `%25`, `%0D` and `%0A` |
|
|
16
|
+
| `escapeWorkflowProperty` | A property value of a workflow command | The characters of `escapeWorkflowData`, and `:` and `,` to `%3A` and `%2C` |
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "@mark1russell7/text";
|
|
20
|
+
|
|
21
|
+
escapeHtml("a < b & c"); // "a < b & c"
|
|
22
|
+
escapeAttribute(`say "hi"`); // "say "hi""
|
|
23
|
+
new RegExp(`^${escapeRegExp("1+1=2?")}$`, "u").test("1+1=2?"); // true
|
|
24
|
+
console.log(`::warning title=${escapeWorkflowProperty("a: b")}::${escapeWorkflowData("50%
|
|
25
|
+
next")}`); // "::warning title=a%3A b::50%25%0Anext"
|
|
26
|
+
escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## The copies that this package replaces
|
|
30
|
+
|
|
31
|
+
The tests run 58 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.
|
|
32
|
+
|
|
33
|
+
## Limits
|
|
34
|
+
|
|
35
|
+
- `escapeHtml` does not escape quotes. For an attribute value, use `escapeAttribute`.
|
|
36
|
+
- `escapeRegExp` gives a pattern for use outside a character class. In a class, `-` also has a meaning.
|
|
37
|
+
- `escapeMarkdown` does not change spaces. Four spaces at the start of a line make a code block.
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes,
|
|
3
|
+
* regular expressions, Markdown and GitHub Actions workflow commands.
|
|
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 `&`, `<` and `>` 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 `&`, `<`, `>`, `"` and `'` 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
|
+
/**
|
|
43
|
+
* This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,
|
|
44
|
+
* the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.
|
|
45
|
+
*
|
|
46
|
+
* @param text - The plain text.
|
|
47
|
+
* @returns The data of the command. The runner decodes it to the text.
|
|
48
|
+
*/
|
|
49
|
+
export declare function escapeWorkflowData(text: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It
|
|
52
|
+
* changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.
|
|
53
|
+
*
|
|
54
|
+
* @param text - The plain text.
|
|
55
|
+
* @returns The property value. The runner decodes it to the text.
|
|
56
|
+
*/
|
|
57
|
+
export declare function escapeWorkflowProperty(text: string): string;
|
|
58
|
+
//# 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;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/escape.ts
|
|
2
|
+
/**
|
|
3
|
+
* This module has the escapes of plain text for five targets. The targets are HTML content, an HTML attribute, a
|
|
4
|
+
* regular expression, one line of Markdown and a GitHub Actions workflow command. Each function changes only the characters that have a meaning in its target.
|
|
5
|
+
*/
|
|
6
|
+
const HTML = {
|
|
7
|
+
"&": "&",
|
|
8
|
+
"<": "<",
|
|
9
|
+
">": ">"
|
|
10
|
+
};
|
|
11
|
+
const ATTRIBUTE = {
|
|
12
|
+
...HTML,
|
|
13
|
+
"\"": """,
|
|
14
|
+
"'": "'"
|
|
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 `&`, `<` and `>` 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 `&`, `<`, `>`, `"` and `'` 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
|
+
/**
|
|
62
|
+
* This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,
|
|
63
|
+
* the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.
|
|
64
|
+
*
|
|
65
|
+
* @param text - The plain text.
|
|
66
|
+
* @returns The data of the command. The runner decodes it to the text.
|
|
67
|
+
*/
|
|
68
|
+
function escapeWorkflowData(text) {
|
|
69
|
+
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It
|
|
73
|
+
* changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.
|
|
74
|
+
*
|
|
75
|
+
* @param text - The plain text.
|
|
76
|
+
* @returns The property value. The runner decodes it to the text.
|
|
77
|
+
*/
|
|
78
|
+
function escapeWorkflowProperty(text) {
|
|
79
|
+
return escapeWorkflowData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
|
|
80
|
+
}
|
|
81
|
+
export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty };
|
|
82
|
+
|
|
83
|
+
//# 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 five targets. The targets are HTML content, an HTML attribute, a\n * regular expression, one line of Markdown and a GitHub Actions workflow command. Each function changes only the characters that have a meaning in its target.\n */\n\nconst HTML: Readonly<Record<string, string>> = { \"&\": \"&\", \"<\": \"<\", \">\": \">\" };\nconst ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '\"': \""\", \"'\": \"'\" };\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 `&`, `<` and `>` 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 `&`, `<`, `>`, `"` and `'` 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\n/**\n * This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,\n * the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.\n *\n * @param text - The plain text.\n * @returns The data of the command. The runner decodes it to the text.\n */\nexport function escapeWorkflowData(text: string): string {\n return text.replaceAll(\"%\", \"%25\").replaceAll(\"\\r\", \"%0D\").replaceAll(\"\\n\", \"%0A\");\n}\n\n/**\n * This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It\n * changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.\n *\n * @param text - The plain text.\n * @returns The property value. The runner decodes it to the text.\n */\nexport function escapeWorkflowProperty(text: string): string {\n return escapeWorkflowData(text).replaceAll(\":\", \"%3A\").replaceAll(\",\", \"%2C\");\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;;;;;;;;AASA,SAAgB,mBAAmB,MAAsB;CACvD,OAAO,KAAK,WAAW,KAAK,KAAK,CAAC,CAAC,WAAW,MAAM,KAAK,CAAC,CAAC,WAAW,MAAM,KAAK;AACnF;;;;;;;;AASA,SAAgB,uBAAuB,MAAsB;CAC3D,OAAO,mBAAmB,IAAI,CAAC,CAAC,WAAW,KAAK,KAAK,CAAC,CAAC,WAAW,KAAK,KAAK;AAC9E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package",
|
|
2
3
|
"name": "@mark1russell7/text",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"description": "Plain text helpers. The first group is the escapes: escapeHtml, escapeAttribute, escapeRegExp, escapeMarkdown, escapeWorkflowData and escapeWorkflowProperty.",
|
|
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
|
+
"@vitest/coverage-v8": "5.1.0",
|
|
52
|
+
"esbuild": "^0.28.2",
|
|
53
|
+
"tsdown": "^0.23.7",
|
|
54
|
+
"typescript": "^7.0.2",
|
|
55
|
+
"vitest": "^5.0.3"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"test:watch": "vitest",
|
|
61
|
+
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
|
|
62
|
+
}
|
|
6
63
|
}
|
package/src/escape.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module has the escapes of plain text for five targets. The targets are HTML content, an HTML attribute, a
|
|
3
|
+
* regular expression, one line of Markdown and a GitHub Actions workflow command. Each function changes only the characters that have a meaning in its target.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const HTML: Readonly<Record<string, string>> = { "&": "&", "<": "<", ">": ">" };
|
|
7
|
+
const ATTRIBUTE: Readonly<Record<string, string>> = { ...HTML, '"': """, "'": "'" };
|
|
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 `&`, `<` and `>` 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 `&`, `<`, `>`, `"` and `'` 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
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This function escapes a text for the data of a GitHub Actions workflow command, the part after `::`. It changes `%`,
|
|
60
|
+
* the carriage return and the line feed to `%25`, `%0D` and `%0A`, so the command stays on one line.
|
|
61
|
+
*
|
|
62
|
+
* @param text - The plain text.
|
|
63
|
+
* @returns The data of the command. The runner decodes it to the text.
|
|
64
|
+
*/
|
|
65
|
+
export function escapeWorkflowData(text: string): string {
|
|
66
|
+
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This function escapes a text for a property value of a GitHub Actions workflow command, for example `title=`. It
|
|
71
|
+
* changes the characters of `escapeWorkflowData`, and also `:` and `,` to `%3A` and `%2C`.
|
|
72
|
+
*
|
|
73
|
+
* @param text - The plain text.
|
|
74
|
+
* @returns The property value. The runner decodes it to the text.
|
|
75
|
+
*/
|
|
76
|
+
export function escapeWorkflowProperty(text: string): string {
|
|
77
|
+
return escapeWorkflowData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
|
|
78
|
+
}
|
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, Markdown and GitHub Actions workflow commands.
|
|
4
|
+
*/
|
|
5
|
+
export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "./escape.ts";
|