@mark1russell7/text 0.0.1 → 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/README.md +7 -3
- package/build/index.d.ts +17 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +23 -3
- package/build/index.js.map +1 -1
- package/package.json +3 -2
- package/src/escape.ts +24 -2
- package/src/index.ts +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @mark1russell7/text
|
|
2
2
|
|
|
3
|
-
Plain text helpers. The first group is the escapes of a text for
|
|
3
|
+
Plain text helpers. The first group is the escapes of a text for five targets.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
pnpm add @mark1russell7/text
|
|
@@ -12,19 +12,23 @@ pnpm add @mark1russell7/text
|
|
|
12
12
|
| `escapeAttribute` | An HTML attribute value in double or single quotes | `&`, `<`, `>`, `"` and `'`, to `&`, `<`, `>`, `"` and `'` |
|
|
13
13
|
| `escapeRegExp` | A pattern of `new RegExp`, outside a character class | A backslash before each syntax character: `^ $ \ . * + ? ( ) [ ] { } \|` |
|
|
14
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` |
|
|
15
17
|
|
|
16
18
|
```ts
|
|
17
|
-
import { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp } from "@mark1russell7/text";
|
|
19
|
+
import { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "@mark1russell7/text";
|
|
18
20
|
|
|
19
21
|
escapeHtml("a < b & c"); // "a < b & c"
|
|
20
22
|
escapeAttribute(`say "hi"`); // "say "hi""
|
|
21
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"
|
|
22
26
|
escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
|
|
23
27
|
```
|
|
24
28
|
|
|
25
29
|
## The copies that this package replaces
|
|
26
30
|
|
|
27
|
-
The tests run
|
|
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.
|
|
28
32
|
|
|
29
33
|
## Limits
|
|
30
34
|
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes,
|
|
3
|
-
* regular expressions and
|
|
3
|
+
* regular expressions, Markdown and GitHub Actions workflow commands.
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
6
|
* This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character
|
|
@@ -39,4 +39,20 @@ export declare function escapeRegExp(text: string): string;
|
|
|
39
39
|
* @returns The text for one line of Markdown.
|
|
40
40
|
*/
|
|
41
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;
|
|
42
58
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/escape.ts"],"mappings":";;;;;;;;;;;wBAegB,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe"}
|
|
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/escape.ts
|
|
2
2
|
/**
|
|
3
|
-
* This module has the escapes of plain text for
|
|
4
|
-
* regular expression
|
|
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
5
|
*/
|
|
6
6
|
const HTML = {
|
|
7
7
|
"&": "&",
|
|
@@ -58,6 +58,26 @@ function escapeRegExp(text) {
|
|
|
58
58
|
function escapeMarkdown(text) {
|
|
59
59
|
return text.replace(/\r\n?|\n/g, " ").replace(/[!-/:-@[-`{-~]/g, "\\$&");
|
|
60
60
|
}
|
|
61
|
-
|
|
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 };
|
|
62
82
|
|
|
63
83
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/escape.ts"],"sourcesContent":["/**\n * This module has the escapes of plain text for
|
|
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,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/text",
|
|
4
|
-
"version": "0.0.
|
|
5
|
-
"description": "Plain text helpers. The first group is the escapes: escapeHtml, escapeAttribute, escapeRegExp and
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"description": "Plain text helpers. The first group is the escapes: escapeHtml, escapeAttribute, escapeRegExp, escapeMarkdown, escapeWorkflowData and escapeWorkflowProperty.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"text",
|
|
8
8
|
"escape",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"@mark1russell7/cue": "^2.0.0",
|
|
49
49
|
"@mark1russell7/rigor-generators": "^0.1.8",
|
|
50
50
|
"@types/node": "^26.6.5",
|
|
51
|
+
"@vitest/coverage-v8": "5.1.0",
|
|
51
52
|
"esbuild": "^0.28.2",
|
|
52
53
|
"tsdown": "^0.23.7",
|
|
53
54
|
"typescript": "^7.0.2",
|
package/src/escape.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* This module has the escapes of plain text for
|
|
3
|
-
* regular expression
|
|
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
4
|
*/
|
|
5
5
|
|
|
6
6
|
const HTML: Readonly<Record<string, string>> = { "&": "&", "<": "<", ">": ">" };
|
|
@@ -54,3 +54,25 @@ export function escapeRegExp(text: string): string {
|
|
|
54
54
|
export function escapeMarkdown(text: string): string {
|
|
55
55
|
return text.replace(/\r\n?|\n/g, " ").replace(/[!-/:-@[-`{-~]/g, "\\$&");
|
|
56
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
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes,
|
|
3
|
-
* regular expressions and
|
|
3
|
+
* regular expressions, Markdown and GitHub Actions workflow commands.
|
|
4
4
|
*/
|
|
5
|
-
export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp } from "./escape.ts";
|
|
5
|
+
export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "./escape.ts";
|