@discord/intl 0.5.2 → 0.6.0
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/dist/format.d.ts +3 -3
- package/dist/format.js +76 -107
- package/dist/formatters/ast.js +30 -77
- package/dist/formatters/markdown.js +18 -65
- package/dist/formatters/react.js +26 -72
- package/dist/formatters/string.js +15 -58
- package/dist/hash.js +11 -11
- package/dist/intl-manager.js +38 -89
- package/dist/message-loader.d.ts +8 -2
- package/dist/message-loader.js +93 -225
- package/dist/message.d.ts +3 -3
- package/dist/message.js +76 -135
- package/package.json +3 -2
- package/src/format.ts +39 -30
- package/src/intl-manager.ts +4 -2
- package/src/message-loader.ts +8 -2
- package/src/message.ts +48 -39
- package/dist/keyless-json.d.ts +0 -68
- package/dist/keyless-json.js +0 -107
- package/src/keyless-json.ts +0 -161
package/dist/format.d.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* - adding extra context for hook and link functions to provide a `key`
|
|
17
17
|
* parameter, important to have React treat the resulting elements nicely.
|
|
18
18
|
*/
|
|
19
|
-
import {
|
|
19
|
+
import { AstNode } from '@discord/intl-ast';
|
|
20
20
|
import { Formatters } from 'intl-messageformat';
|
|
21
21
|
import type { FormatConfig } from './format-config';
|
|
22
22
|
import type { RichTextTagNames } from './types';
|
|
@@ -27,5 +27,5 @@ export declare abstract class FormatBuilder<Result> {
|
|
|
27
27
|
abstract finish(): Result[];
|
|
28
28
|
}
|
|
29
29
|
export type FormatBuilderConstructor<Result> = new () => FormatBuilder<Result>;
|
|
30
|
-
export declare function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>(builder: Builder, nodes:
|
|
31
|
-
export declare function bindFormatValues<Result>(Builder: FormatBuilderConstructor<Result>, nodes:
|
|
30
|
+
export declare function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>(builder: Builder, nodes: AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number, originalMessage?: string): void;
|
|
31
|
+
export declare function bindFormatValues<Result>(Builder: FormatBuilderConstructor<Result>, nodes: AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number): Result[];
|
package/dist/format.js
CHANGED
|
@@ -17,24 +17,13 @@
|
|
|
17
17
|
* - adding extra context for hook and link functions to provide a `key`
|
|
18
18
|
* parameter, important to have React treat the resulting elements nicely.
|
|
19
19
|
*/
|
|
20
|
-
var __values = (this && this.__values) || function(o) {
|
|
21
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
22
|
-
if (m) return m.call(o);
|
|
23
|
-
if (o && typeof o.length === "number") return {
|
|
24
|
-
next: function () {
|
|
25
|
-
if (o && i >= o.length) o = void 0;
|
|
26
|
-
return { value: o && o[i++], done: !o };
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
30
|
-
};
|
|
31
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
21
|
exports.FormatBuilder = void 0;
|
|
33
22
|
exports.bindFormatValuesWithBuilder = bindFormatValuesWithBuilder;
|
|
34
23
|
exports.bindFormatValues = bindFormatValues;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
const icu_skeleton_parser_1 = require("@formatjs/icu-skeleton-parser");
|
|
25
|
+
const intl_ast_1 = require("@discord/intl-ast");
|
|
26
|
+
const intl_messageformat_1 = require("intl-messageformat");
|
|
38
27
|
/**
|
|
39
28
|
* Returns true if the tag name should be considered a rich text tag that
|
|
40
29
|
* applies formatting to a message, rather than being a user-supplied value.
|
|
@@ -42,46 +31,41 @@ var intl_messageformat_1 = require("intl-messageformat");
|
|
|
42
31
|
function isRichTextTag(name) {
|
|
43
32
|
return name[0] === '$';
|
|
44
33
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
return FormatBuilder;
|
|
49
|
-
}());
|
|
34
|
+
class FormatBuilder {
|
|
35
|
+
}
|
|
50
36
|
exports.FormatBuilder = FormatBuilder;
|
|
51
|
-
function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, formatConfig, values, currentPluralValue, originalMessage) {
|
|
52
|
-
var
|
|
53
|
-
var _b, _c;
|
|
54
|
-
if (values === void 0) { values = {}; }
|
|
37
|
+
function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, formatConfig, values = {}, currentPluralValue, originalMessage) {
|
|
38
|
+
var _a, _b;
|
|
55
39
|
// Hot path for static messages that are just parsed as a single string element.
|
|
56
|
-
if (nodes.length === 1 && nodes[0].
|
|
57
|
-
builder.pushLiteralText(nodes[0].
|
|
40
|
+
if (nodes.length === 1 && nodes[0][intl_ast_1.AstNodeIndices.Type] === intl_ast_1.FormatJsNodeType.Literal) {
|
|
41
|
+
builder.pushLiteralText(nodes[0][intl_ast_1.AstNodeIndices.Value]);
|
|
58
42
|
return;
|
|
59
43
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
switch (
|
|
63
|
-
case
|
|
64
|
-
builder.pushLiteralText(node.
|
|
65
|
-
|
|
66
|
-
case
|
|
44
|
+
for (const node of nodes) {
|
|
45
|
+
const nodeType = node[intl_ast_1.AstNodeIndices.Type];
|
|
46
|
+
switch (nodeType) {
|
|
47
|
+
case intl_ast_1.FormatJsNodeType.Literal:
|
|
48
|
+
builder.pushLiteralText(node[intl_ast_1.AstNodeIndices.Value]);
|
|
49
|
+
continue;
|
|
50
|
+
case intl_ast_1.FormatJsNodeType.Pound:
|
|
67
51
|
// Replace `#` in plural rules with the actual numeric value. Only
|
|
68
52
|
// numeric values are replaced, otherwise the value is completed ignored?
|
|
69
53
|
// Behavior copied from FormatJS directly.
|
|
70
54
|
if (typeof currentPluralValue === 'number') {
|
|
71
|
-
|
|
72
|
-
builder.pushLiteralText(
|
|
55
|
+
const value = formatters.getNumberFormat(locales).format(currentPluralValue);
|
|
56
|
+
builder.pushLiteralText(value);
|
|
73
57
|
}
|
|
74
|
-
|
|
58
|
+
continue;
|
|
75
59
|
}
|
|
76
|
-
|
|
60
|
+
const variableName = node[intl_ast_1.AstNodeIndices.Value];
|
|
77
61
|
// Enforce that all required values are provided by the caller, even if the
|
|
78
62
|
// actual value is falsy/undefined.
|
|
79
63
|
if (!(variableName in values) && !isRichTextTag(variableName)) {
|
|
80
64
|
throw new intl_messageformat_1.MissingValueError(variableName, originalMessage);
|
|
81
65
|
}
|
|
82
|
-
|
|
83
|
-
switch (
|
|
84
|
-
case
|
|
66
|
+
const value = values[variableName];
|
|
67
|
+
switch (nodeType) {
|
|
68
|
+
case intl_ast_1.FormatJsNodeType.Argument:
|
|
85
69
|
// Empty values don't need to be added at all, they are purely for AST representation.
|
|
86
70
|
if (variableName == '$_')
|
|
87
71
|
break;
|
|
@@ -94,125 +78,110 @@ function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, format
|
|
|
94
78
|
builder.pushLiteralText(String(value));
|
|
95
79
|
}
|
|
96
80
|
break;
|
|
97
|
-
case
|
|
81
|
+
case intl_ast_1.FormatJsNodeType.Date: {
|
|
82
|
+
const nodeStyle = node[intl_ast_1.AstNodeIndices.Style];
|
|
98
83
|
// Distinct from FormatJS: We don't currently parse the skeleton ahead of time in the AST,
|
|
99
84
|
// so this manages parsing the skeleton as well before passing it onto the date formatter.
|
|
100
|
-
|
|
101
|
-
? formatConfig.date[
|
|
102
|
-
:
|
|
103
|
-
? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(
|
|
85
|
+
const style = nodeStyle in formatConfig.date
|
|
86
|
+
? formatConfig.date[nodeStyle]
|
|
87
|
+
: nodeStyle != null
|
|
88
|
+
? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(nodeStyle)
|
|
104
89
|
: formatConfig.time.medium;
|
|
105
90
|
// @ts-expect-error Cast string values to dates properly.
|
|
106
91
|
builder.pushLiteralText(formatters.getDateTimeFormat(locales, style).format(value));
|
|
107
92
|
break;
|
|
108
93
|
}
|
|
109
|
-
case
|
|
94
|
+
case intl_ast_1.FormatJsNodeType.Time: {
|
|
95
|
+
const nodeStyle = node[intl_ast_1.AstNodeIndices.Style];
|
|
110
96
|
// Distinct from FormatJS: We don't currently parse the skeleton ahead of time in the AST,
|
|
111
97
|
// so this manages parsing the skeleton as well before passing it onto the date formatter.
|
|
112
|
-
|
|
113
|
-
? formatConfig.time[
|
|
114
|
-
:
|
|
115
|
-
? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(
|
|
98
|
+
const style = nodeStyle in formatConfig.time
|
|
99
|
+
? formatConfig.time[nodeStyle]
|
|
100
|
+
: nodeStyle != null
|
|
101
|
+
? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(nodeStyle)
|
|
116
102
|
: undefined; // TODO: parseSkeleton();
|
|
117
103
|
builder.pushLiteralText(
|
|
118
104
|
// @ts-expect-error Cast string values to dates properly.
|
|
119
105
|
formatters.getDateTimeFormat(locales, style).format(value));
|
|
120
106
|
break;
|
|
121
107
|
}
|
|
122
|
-
case
|
|
108
|
+
case intl_ast_1.FormatJsNodeType.Number: {
|
|
109
|
+
const nodeStyle = node[intl_ast_1.AstNodeIndices.Style];
|
|
123
110
|
// Distinct from FormatJS: We don't currently parse the skeleton ahead of time in the AST,
|
|
124
111
|
// so this manages parsing the skeleton as well before passing it onto the date formatter.
|
|
125
|
-
|
|
126
|
-
? formatConfig.number[
|
|
127
|
-
:
|
|
128
|
-
? (0, icu_skeleton_parser_1.parseNumberSkeleton)((0, icu_skeleton_parser_1.parseNumberSkeletonFromString)(
|
|
112
|
+
const style = nodeStyle in formatConfig.number
|
|
113
|
+
? formatConfig.number[nodeStyle]
|
|
114
|
+
: nodeStyle != null
|
|
115
|
+
? (0, icu_skeleton_parser_1.parseNumberSkeleton)((0, icu_skeleton_parser_1.parseNumberSkeletonFromString)(nodeStyle))
|
|
129
116
|
: undefined;
|
|
130
|
-
|
|
117
|
+
const scaledValue =
|
|
131
118
|
// @ts-expect-error This is a weird cast that's not accurate, but works in the short term.
|
|
132
|
-
typeof value !== 'number' ? value : value * ((
|
|
119
|
+
typeof value !== 'number' ? value : value * ((_a = style === null || style === void 0 ? void 0 : style.scale) !== null && _a !== void 0 ? _a : 1);
|
|
133
120
|
builder.pushLiteralText(formatters.getNumberFormat(locales, style).format(scaledValue));
|
|
134
121
|
break;
|
|
135
122
|
}
|
|
136
|
-
case
|
|
137
|
-
|
|
138
|
-
|
|
123
|
+
case intl_ast_1.FormatJsNodeType.Tag: {
|
|
124
|
+
const children = node[intl_ast_1.AstNodeIndices.Children];
|
|
125
|
+
const appliedChildren = bindFormatValues(builder.constructor, children, locales, formatters, formatConfig, values, currentPluralValue);
|
|
139
126
|
if (isRichTextTag(variableName)) {
|
|
140
127
|
builder.pushRichTextTag(variableName, appliedChildren);
|
|
141
128
|
}
|
|
142
129
|
else {
|
|
143
130
|
if (typeof value !== 'function') {
|
|
144
|
-
throw
|
|
131
|
+
throw `expected a function type for a Tag formatting value, ${variableName}. got ${typeof value}: ${value}`;
|
|
145
132
|
}
|
|
146
|
-
|
|
133
|
+
let chunks = value(appliedChildren);
|
|
147
134
|
chunks = Array.isArray(chunks) ? chunks : [chunks];
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (typeof chunk === 'string') {
|
|
152
|
-
builder.pushLiteralText(chunk);
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
builder.pushObject(chunk);
|
|
156
|
-
}
|
|
135
|
+
for (const chunk of chunks) {
|
|
136
|
+
if (typeof chunk === 'string') {
|
|
137
|
+
builder.pushLiteralText(chunk);
|
|
157
138
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
finally {
|
|
161
|
-
try {
|
|
162
|
-
if (chunks_1_1 && !chunks_1_1.done && (_d = chunks_1.return)) _d.call(chunks_1);
|
|
139
|
+
else {
|
|
140
|
+
builder.pushObject(chunk);
|
|
163
141
|
}
|
|
164
|
-
finally { if (e_2) throw e_2.error; }
|
|
165
142
|
}
|
|
166
143
|
}
|
|
167
144
|
break;
|
|
168
145
|
}
|
|
169
|
-
case
|
|
170
|
-
|
|
146
|
+
case intl_ast_1.FormatJsNodeType.Select: {
|
|
147
|
+
const value = node[intl_ast_1.AstNodeIndices.Value];
|
|
148
|
+
const options = node[intl_ast_1.AstNodeIndices.Options];
|
|
149
|
+
const option = value in options ? options[value] : options.other;
|
|
171
150
|
if (option == null) {
|
|
172
|
-
throw
|
|
151
|
+
throw `${value} is not a known option for select value ${variableName}. Valid options are ${Object.keys(options).join(', ')}`;
|
|
173
152
|
}
|
|
174
153
|
bindFormatValuesWithBuilder(builder, option.value, locales, formatters, formatConfig, values);
|
|
175
154
|
break;
|
|
176
155
|
}
|
|
177
|
-
case
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
var
|
|
184
|
-
|
|
156
|
+
case intl_ast_1.FormatJsNodeType.Plural: {
|
|
157
|
+
const value = node[intl_ast_1.AstNodeIndices.Value];
|
|
158
|
+
const options = node[intl_ast_1.AstNodeIndices.Options];
|
|
159
|
+
const offset = node[intl_ast_1.AstNodeIndices.Offset];
|
|
160
|
+
const pluralType = node[intl_ast_1.AstNodeIndices.PluralType];
|
|
161
|
+
const option = (() => {
|
|
162
|
+
var _a;
|
|
163
|
+
const exactSelector = `=${value}`;
|
|
164
|
+
if (exactSelector in options)
|
|
165
|
+
return options[exactSelector];
|
|
166
|
+
const rule = formatters
|
|
167
|
+
.getPluralRules(locales, { type: pluralType })
|
|
185
168
|
// @ts-expect-error Assert this `as number` properly.
|
|
186
|
-
.select(value - (
|
|
187
|
-
return (
|
|
169
|
+
.select(value - (offset !== null && offset !== void 0 ? offset : 0));
|
|
170
|
+
return (_a = options[rule]) !== null && _a !== void 0 ? _a : options.other;
|
|
188
171
|
})();
|
|
189
172
|
if (option == null) {
|
|
190
|
-
throw
|
|
173
|
+
throw `${value} is not a known option for plural value ${variableName}. Valid options are ${Object.keys(options).join(', ')}`;
|
|
191
174
|
}
|
|
192
175
|
bindFormatValuesWithBuilder(builder, option.value, locales, formatters, formatConfig, values,
|
|
193
176
|
// @ts-expect-error assert this `as number` properly.
|
|
194
|
-
value - ((
|
|
177
|
+
value - ((_b = node.offset) !== null && _b !== void 0 ? _b : 0));
|
|
195
178
|
break;
|
|
196
179
|
}
|
|
197
180
|
}
|
|
198
|
-
};
|
|
199
|
-
try {
|
|
200
|
-
for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
|
|
201
|
-
var node = nodes_1_1.value;
|
|
202
|
-
_loop_1(node);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
206
|
-
finally {
|
|
207
|
-
try {
|
|
208
|
-
if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
|
|
209
|
-
}
|
|
210
|
-
finally { if (e_1) throw e_1.error; }
|
|
211
181
|
}
|
|
212
182
|
}
|
|
213
|
-
function bindFormatValues(Builder, nodes, locales, formatters, formatConfig, values, currentPluralValue) {
|
|
214
|
-
|
|
215
|
-
var builder = new Builder();
|
|
183
|
+
function bindFormatValues(Builder, nodes, locales, formatters, formatConfig, values = {}, currentPluralValue) {
|
|
184
|
+
const builder = new Builder();
|
|
216
185
|
bindFormatValuesWithBuilder(builder, nodes, locales, formatters, formatConfig, values, currentPluralValue);
|
|
217
186
|
return builder.finish();
|
|
218
187
|
}
|
package/dist/formatters/ast.js
CHANGED
|
@@ -1,48 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
-
if (!m) return o;
|
|
20
|
-
var i = m.call(o), r, ar = [], e;
|
|
21
|
-
try {
|
|
22
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
-
}
|
|
24
|
-
catch (error) { e = { error: error }; }
|
|
25
|
-
finally {
|
|
26
|
-
try {
|
|
27
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
-
}
|
|
29
|
-
finally { if (e) throw e.error; }
|
|
30
|
-
}
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
-
if (ar || !(i in from)) {
|
|
36
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
-
ar[i] = from[i];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
-
};
|
|
42
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
3
|
exports.astFormatter = exports.RichTextNodeType = void 0;
|
|
44
4
|
exports.formatToAst = formatToAst;
|
|
45
|
-
|
|
5
|
+
const format_1 = require("../format");
|
|
46
6
|
// This structure aims to match `simple-markdown`'s AST, but that type is very
|
|
47
7
|
// loosely defined as just "type" and "arbitrary map of content". This is more
|
|
48
8
|
// explicit and type-safe.
|
|
@@ -66,59 +26,52 @@ var RichTextNodeType;
|
|
|
66
26
|
*/
|
|
67
27
|
RichTextNodeType["Object"] = "object";
|
|
68
28
|
})(RichTextNodeType || (exports.RichTextNodeType = RichTextNodeType = {}));
|
|
69
|
-
|
|
70
|
-
$_:
|
|
71
|
-
$b:
|
|
72
|
-
$i:
|
|
73
|
-
$del:
|
|
74
|
-
$code:
|
|
75
|
-
$link:
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
},
|
|
83
|
-
$p: function (content) { return ({ type: RichTextNodeType.Paragraph, content: content }); },
|
|
29
|
+
const AST_RICH_TEXT_ELEMENTS = {
|
|
30
|
+
$_: () => ({ type: RichTextNodeType.Text, content: '' }),
|
|
31
|
+
$b: (content) => ({ type: RichTextNodeType.Strong, content }),
|
|
32
|
+
$i: (content) => ({ type: RichTextNodeType.Emphasis, content }),
|
|
33
|
+
$del: (content) => ({ type: RichTextNodeType.Strikethrough, content }),
|
|
34
|
+
$code: (content) => ({ type: RichTextNodeType.Code, content }),
|
|
35
|
+
$link: ([target, ...content]) => ({
|
|
36
|
+
type: RichTextNodeType.Link,
|
|
37
|
+
target: target.content,
|
|
38
|
+
content,
|
|
39
|
+
}),
|
|
40
|
+
$p: (content) => ({ type: RichTextNodeType.Paragraph, content }),
|
|
84
41
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
_this.result = [];
|
|
90
|
-
return _this;
|
|
42
|
+
class AstBuilder extends format_1.FormatBuilder {
|
|
43
|
+
constructor() {
|
|
44
|
+
super(...arguments);
|
|
45
|
+
this.result = [];
|
|
91
46
|
}
|
|
92
|
-
|
|
93
|
-
var _a;
|
|
47
|
+
pushRichTextTag(tag, children) {
|
|
94
48
|
if (!(tag in AST_RICH_TEXT_ELEMENTS)) {
|
|
95
|
-
throw
|
|
49
|
+
throw `${tag} is not a known rich text formatting tag`;
|
|
96
50
|
}
|
|
97
|
-
|
|
51
|
+
const result = AST_RICH_TEXT_ELEMENTS[tag](children, '');
|
|
98
52
|
if (Array.isArray(result)) {
|
|
99
|
-
|
|
53
|
+
this.result.push(...result);
|
|
100
54
|
}
|
|
101
55
|
else {
|
|
102
56
|
this.result.push(result);
|
|
103
57
|
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
58
|
+
}
|
|
59
|
+
pushLiteralText(text) {
|
|
60
|
+
const last = this.result[this.result.length - 1];
|
|
107
61
|
if (last != null && last.type === RichTextNodeType.Text) {
|
|
108
62
|
last.content += text;
|
|
109
63
|
}
|
|
110
64
|
else {
|
|
111
65
|
this.result.push({ type: RichTextNodeType.Text, content: text });
|
|
112
66
|
}
|
|
113
|
-
}
|
|
114
|
-
|
|
67
|
+
}
|
|
68
|
+
pushObject(value) {
|
|
115
69
|
this.result.push({ type: RichTextNodeType.Object, content: value });
|
|
116
|
-
}
|
|
117
|
-
|
|
70
|
+
}
|
|
71
|
+
finish() {
|
|
118
72
|
return this.result;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
}(format_1.FormatBuilder));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
122
75
|
/**
|
|
123
76
|
* Format the given message in the current locale with the provided values.
|
|
124
77
|
* The returned values is _always_ an Array of parts, even if the message is
|
|
@@ -1,77 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
-
if (!m) return o;
|
|
20
|
-
var i = m.call(o), r, ar = [], e;
|
|
21
|
-
try {
|
|
22
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
-
}
|
|
24
|
-
catch (error) { e = { error: error }; }
|
|
25
|
-
finally {
|
|
26
|
-
try {
|
|
27
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
-
}
|
|
29
|
-
finally { if (e) throw e.error; }
|
|
30
|
-
}
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
-
if (ar || !(i in from)) {
|
|
36
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
-
ar[i] = from[i];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
-
};
|
|
42
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
3
|
exports.markdownFormatter = void 0;
|
|
44
4
|
exports.formatToMarkdownString = formatToMarkdownString;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
$_:
|
|
48
|
-
$b:
|
|
49
|
-
$i:
|
|
50
|
-
$del:
|
|
51
|
-
$code:
|
|
52
|
-
$link:
|
|
53
|
-
|
|
54
|
-
return '[' + content.join('') + '](' + target + ')';
|
|
55
|
-
},
|
|
56
|
-
$p: function (content) { return content.join('') + '\n\n'; },
|
|
5
|
+
const string_1 = require("./string");
|
|
6
|
+
const MARKDOWN_RICH_TEXT_ELEMENTS = {
|
|
7
|
+
$_: () => '',
|
|
8
|
+
$b: (content) => '**' + content.join('') + '**',
|
|
9
|
+
$i: (content) => '*' + content.join('') + '*',
|
|
10
|
+
$del: (content) => '~~' + content.join('') + '~~',
|
|
11
|
+
$code: (content) => '`' + content.join('') + '`',
|
|
12
|
+
$link: ([target, ...content]) => '[' + content.join('') + '](' + target + ')',
|
|
13
|
+
$p: (content) => content.join('') + '\n\n',
|
|
57
14
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
_this.result = '';
|
|
63
|
-
return _this;
|
|
15
|
+
class MarkdownBuilder extends string_1.StringBuilder {
|
|
16
|
+
constructor() {
|
|
17
|
+
super(...arguments);
|
|
18
|
+
this.result = '';
|
|
64
19
|
}
|
|
65
|
-
|
|
20
|
+
pushRichTextTag(tag, children) {
|
|
66
21
|
this.result += MARKDOWN_RICH_TEXT_ELEMENTS[tag](children, '');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
function formatToMarkdownString(message, values, Builder) {
|
|
71
|
-
if (Builder === void 0) { Builder = MarkdownBuilder; }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function formatToMarkdownString(message, values, Builder = MarkdownBuilder) {
|
|
72
25
|
if (typeof message === 'string')
|
|
73
26
|
return message;
|
|
74
|
-
|
|
27
|
+
const result = this.bindFormatValues(Builder, message, values);
|
|
75
28
|
// MarkdownBuilder always creates a single-element array with the string value.
|
|
76
29
|
return result[0];
|
|
77
30
|
}
|