@khanacademy/pure-markdown 0.1.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/CHANGELOG.md +12 -0
- package/dist/es/index.js +322 -0
- package/dist/es/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +296 -0
- package/dist/index.js.flow +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -0
- package/src/__tests__/__snapshots__/index_test.js.snap +113 -0
- package/src/__tests__/index_test.js +552 -0
- package/src/index.js +319 -0
package/CHANGELOG.md
ADDED
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import SimpleMarkdown from '@khanacademy/simple-markdown';
|
|
2
|
+
|
|
3
|
+
function ownKeys(object, enumerableOnly) {
|
|
4
|
+
var keys = Object.keys(object);
|
|
5
|
+
|
|
6
|
+
if (Object.getOwnPropertySymbols) {
|
|
7
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
8
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
|
9
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
10
|
+
})), keys.push.apply(keys, symbols);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return keys;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function _objectSpread2(target) {
|
|
17
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
18
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
|
19
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
|
20
|
+
_defineProperty(target, key, source[key]);
|
|
21
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
|
22
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return target;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _defineProperty(obj, key, value) {
|
|
30
|
+
if (key in obj) {
|
|
31
|
+
Object.defineProperty(obj, key, {
|
|
32
|
+
value: value,
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
obj[key] = value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return obj;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* This match function matches math in `$`s, such as:
|
|
46
|
+
*
|
|
47
|
+
* $y = x + 1$
|
|
48
|
+
*
|
|
49
|
+
* It functions roughly like the following regex:
|
|
50
|
+
* /\$([^\$]*)\$/
|
|
51
|
+
*
|
|
52
|
+
* Unfortunately, math may have other `$`s inside it, as
|
|
53
|
+
* long as they are inside `{` braces `}`, mostly for
|
|
54
|
+
* `\text{ $math$ }`.
|
|
55
|
+
*
|
|
56
|
+
* To parse this, we can't use a regex, since we
|
|
57
|
+
* should support arbitrary nesting (even though
|
|
58
|
+
* MathJax actually only supports two levels of nesting
|
|
59
|
+
* here, which we *could* parse with a regex).
|
|
60
|
+
*
|
|
61
|
+
* Non-regex matchers like this are now a first-class
|
|
62
|
+
* concept in simple-markdown. Yay!
|
|
63
|
+
*
|
|
64
|
+
* This can also match block-math, which is math alone in a paragraph.
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
var rWidgetRule = /^\[\[\u2603 (([a-z-]+) ([0-9]+))\]\]/;
|
|
68
|
+
|
|
69
|
+
var mathMatcher = (source, state, isBlock) => {
|
|
70
|
+
var length = source.length;
|
|
71
|
+
var index = 0; // When looking for blocks, skip over leading spaces
|
|
72
|
+
|
|
73
|
+
if (isBlock) {
|
|
74
|
+
if (state.inline) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
while (index < length && source[index] === " ") {
|
|
79
|
+
index++;
|
|
80
|
+
}
|
|
81
|
+
} // Our source must start with a "$"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if (!(index < length && source[index] === "$")) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
index++;
|
|
89
|
+
var startIndex = index;
|
|
90
|
+
var braceLevel = 0; // Loop through the source, looking for a closing '$'
|
|
91
|
+
// closing '$'s only count if they are not escaped with
|
|
92
|
+
// a `\`, and we are not in nested `{}` braces.
|
|
93
|
+
|
|
94
|
+
while (index < length) {
|
|
95
|
+
var character = source[index];
|
|
96
|
+
|
|
97
|
+
if (character === "\\") {
|
|
98
|
+
// Consume both the `\` and the escaped char as a single
|
|
99
|
+
// token.
|
|
100
|
+
// This is so that the second `$` in `$\\$` closes
|
|
101
|
+
// the math expression, since the first `\` is escaping
|
|
102
|
+
// the second `\`, but the second `\` is not escaping
|
|
103
|
+
// the second `$`.
|
|
104
|
+
// This also handles the case of escaping `$`s or
|
|
105
|
+
// braces `\{`
|
|
106
|
+
index++;
|
|
107
|
+
} else if (braceLevel <= 0 && character === "$") {
|
|
108
|
+
var endIndex = index + 1;
|
|
109
|
+
|
|
110
|
+
if (isBlock) {
|
|
111
|
+
// Look for two trailing newlines after the closing `$`
|
|
112
|
+
var match = /^(?: *\n){2,}/.exec(source.slice(endIndex));
|
|
113
|
+
endIndex = match ? endIndex + match[0].length : null;
|
|
114
|
+
} // Return an array that looks like the results of a
|
|
115
|
+
// regex's .exec function:
|
|
116
|
+
// capture[0] is the whole string
|
|
117
|
+
// capture[1] is the first "paren" match, which is the
|
|
118
|
+
// content of the math here, as if we wrote the regex
|
|
119
|
+
// /\$([^\$]*)\$/
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
if (endIndex) {
|
|
123
|
+
return [source.substring(0, endIndex), source.substring(startIndex, index)];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return null;
|
|
127
|
+
} else if (character === "{") {
|
|
128
|
+
braceLevel++;
|
|
129
|
+
} else if (character === "}") {
|
|
130
|
+
braceLevel--;
|
|
131
|
+
} else if (character === "\n" && source[index - 1] === "\n") {
|
|
132
|
+
// This is a weird case we supported in the old
|
|
133
|
+
// math implementation--double newlines break
|
|
134
|
+
// math. I'm preserving it for now because content
|
|
135
|
+
// creators might have questions with single '$'s
|
|
136
|
+
// in paragraphs...
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
index++;
|
|
141
|
+
} // we didn't find a closing `$`
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
return null;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
var mathMatch = (source, state) => mathMatcher(source, state, false);
|
|
148
|
+
|
|
149
|
+
var blockMathMatch = (source, state) => mathMatcher(source, state, true);
|
|
150
|
+
|
|
151
|
+
var TITLED_TABLE_REGEX = new RegExp("^\\|\\| +(.*) +\\|\\| *\\n" + "(" + // The simple-markdown nptable regex, without
|
|
152
|
+
// the leading `^`
|
|
153
|
+
// $FlowFixMe[incompatible-use]
|
|
154
|
+
SimpleMarkdown.defaultRules.nptable.match.regex.source.substring(1) + ")");
|
|
155
|
+
var crowdinJiptMatcher = SimpleMarkdown.blockRegex(/^(crwdns.*)\n\s*\n/);
|
|
156
|
+
var pureMarkdownRules = _objectSpread2(_objectSpread2({}, SimpleMarkdown.defaultRules), {}, {
|
|
157
|
+
// NOTE: basically ignored by JIPT. wraps everything at the outer layer
|
|
158
|
+
columns: {
|
|
159
|
+
order: -2,
|
|
160
|
+
match: SimpleMarkdown.blockRegex(/^([\s\S]*\n\n)={5,}\n\n([\s\S]*)/),
|
|
161
|
+
parse: (capture, _parse, state) => {
|
|
162
|
+
return {
|
|
163
|
+
col1: _parse(capture[1], state),
|
|
164
|
+
col2: _parse(capture[2], state)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
crowdinId: {
|
|
169
|
+
order: -1,
|
|
170
|
+
match: (source, state, prevCapture) => {
|
|
171
|
+
// Only match on the just-in-place translation site
|
|
172
|
+
if (state.isJipt) {
|
|
173
|
+
return crowdinJiptMatcher(source, state, prevCapture);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return null;
|
|
177
|
+
},
|
|
178
|
+
parse: (capture, _parse2, state) => ({
|
|
179
|
+
id: capture[1]
|
|
180
|
+
})
|
|
181
|
+
},
|
|
182
|
+
// This is pretty much horrible, but we have a regex here to capture an
|
|
183
|
+
// entire table + a title. capture[1] is the title. capture[2] of the
|
|
184
|
+
// regex is a copy of the simple-markdown nptable regex. Then we turn
|
|
185
|
+
// our capture[2] into tableCapture[0], and any further captures in
|
|
186
|
+
// our table regex into tableCapture[1..], and we pass tableCapture to
|
|
187
|
+
// our nptable regex
|
|
188
|
+
titledTable: {
|
|
189
|
+
// process immediately before nptables
|
|
190
|
+
order: SimpleMarkdown.defaultRules.nptable.order - 0.5,
|
|
191
|
+
match: SimpleMarkdown.blockRegex(TITLED_TABLE_REGEX),
|
|
192
|
+
parse: (capture, _parse3, state) => {
|
|
193
|
+
var title = SimpleMarkdown.parseInline(_parse3, capture[1], state); // Remove our [0] and [1] captures, and pass the rest to
|
|
194
|
+
// the nptable parser
|
|
195
|
+
|
|
196
|
+
var tableCapture = capture.slice(2);
|
|
197
|
+
var table = SimpleMarkdown.defaultRules.nptable.parse(tableCapture, _parse3, state);
|
|
198
|
+
return {
|
|
199
|
+
title: title,
|
|
200
|
+
table: table
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
widget: {
|
|
205
|
+
order: SimpleMarkdown.defaultRules.link.order - 0.75,
|
|
206
|
+
match: SimpleMarkdown.inlineRegex(rWidgetRule),
|
|
207
|
+
parse: (capture, _parse4, state) => {
|
|
208
|
+
return {
|
|
209
|
+
id: capture[1],
|
|
210
|
+
widgetType: capture[2]
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
blockMath: {
|
|
215
|
+
order: SimpleMarkdown.defaultRules.codeBlock.order + 0.5,
|
|
216
|
+
match: blockMathMatch,
|
|
217
|
+
parse: (capture, _parse5, state) => {
|
|
218
|
+
return {
|
|
219
|
+
content: capture[1]
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
math: {
|
|
224
|
+
order: SimpleMarkdown.defaultRules.link.order - 0.25,
|
|
225
|
+
match: mathMatch,
|
|
226
|
+
parse: (capture, _parse6, state) => {
|
|
227
|
+
return {
|
|
228
|
+
content: capture[1]
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
unescapedDollar: {
|
|
233
|
+
order: SimpleMarkdown.defaultRules.link.order - 0.24,
|
|
234
|
+
match: SimpleMarkdown.inlineRegex(/^(?!\\)\$/),
|
|
235
|
+
parse: (capture, _parse7, state) => {
|
|
236
|
+
return {};
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
fence: _objectSpread2(_objectSpread2({}, SimpleMarkdown.defaultRules.fence), {}, {
|
|
240
|
+
parse: (capture, _parse8, state) => {
|
|
241
|
+
var node = SimpleMarkdown.defaultRules.fence.parse(capture, _parse8, state); // support screenreader-only text with ```alt
|
|
242
|
+
|
|
243
|
+
if (node.lang === "alt") {
|
|
244
|
+
return {
|
|
245
|
+
type: "codeBlock",
|
|
246
|
+
lang: "alt",
|
|
247
|
+
// default codeBlock parsing doesn't parse the contents.
|
|
248
|
+
// We need to parse the contents for things like table
|
|
249
|
+
// support :).
|
|
250
|
+
// The \n\n is because the inside of the codeblock might
|
|
251
|
+
// not end in double newlines for block rules, because
|
|
252
|
+
// ordinarily we don't parse this :).
|
|
253
|
+
content: _parse8(node.content + "\n\n", state)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return node;
|
|
258
|
+
}
|
|
259
|
+
}),
|
|
260
|
+
blockQuote: _objectSpread2(_objectSpread2({}, SimpleMarkdown.defaultRules.blockQuote), {}, {
|
|
261
|
+
// Replace the less restrictive blockquote regex from SimpleMarkdown
|
|
262
|
+
// with a more restrictive one. The only difference should be that
|
|
263
|
+
//
|
|
264
|
+
// > A blockquote
|
|
265
|
+
//
|
|
266
|
+
// > Another blockquote
|
|
267
|
+
//
|
|
268
|
+
// will now match as two different blockQuotes instead of a single
|
|
269
|
+
// blockquote with some paragraph breaks in it.
|
|
270
|
+
//
|
|
271
|
+
// The main motivation for doing this is to provide better support for
|
|
272
|
+
// translators translating blockquotes with multiple paragraphs in
|
|
273
|
+
// them. When translating articles, we split up paragraphs, translate
|
|
274
|
+
// them separately, and then recombine them. We do this so that
|
|
275
|
+
// translators don't have to translate an entire article at a time,
|
|
276
|
+
// they can instead translate paragraph-by-paragraph.That system
|
|
277
|
+
// doesn't understand blockquotes, so it will split up blockquotes into
|
|
278
|
+
// more than one paragraph. A way to solve this would be to make that
|
|
279
|
+
// system understand blockquotes, but then translators would have to
|
|
280
|
+
// translate an entire, multi-paragraph blockquote at a time. Instead,
|
|
281
|
+
// we choose to modify our blockquote matching to split up
|
|
282
|
+
// multi-paragraph blockquotes into multiple blockquotes.
|
|
283
|
+
//
|
|
284
|
+
// There is also precedence for doing this splitting up in other
|
|
285
|
+
// libraries, for instance CommonMark also splits up blockquotes with
|
|
286
|
+
// empty lines into multiple blockquotes:
|
|
287
|
+
// https://spec.commonmark.org/0.28/#example-205
|
|
288
|
+
match: SimpleMarkdown.blockRegex(/^ *>[^\n]+(\n( *>)?[^\n]+)*\n{2,}/)
|
|
289
|
+
}),
|
|
290
|
+
list: _objectSpread2(_objectSpread2({}, SimpleMarkdown.defaultRules.list), {}, {
|
|
291
|
+
match: (source, state, prevCapture) => {
|
|
292
|
+
// Since lists can contain double newlines and we have special
|
|
293
|
+
// handling of double newlines while parsing jipt content, just
|
|
294
|
+
// disable the list parser.
|
|
295
|
+
if (state.isJipt) {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return SimpleMarkdown.defaultRules.list.match(source, state, prevCapture);
|
|
300
|
+
}
|
|
301
|
+
}),
|
|
302
|
+
// The lint rule never actually matches anything.
|
|
303
|
+
// We check for lint after parsing, and, if we find any, we
|
|
304
|
+
// transform the tree to add lint nodes. This rule is here
|
|
305
|
+
// just for the react() function
|
|
306
|
+
lint: {
|
|
307
|
+
order: 1000,
|
|
308
|
+
match: s => null,
|
|
309
|
+
parse: (capture, _parse9, state) => ({})
|
|
310
|
+
}
|
|
311
|
+
}); // $FlowFixMe[incompatible-call]
|
|
312
|
+
|
|
313
|
+
var builtParser = SimpleMarkdown.parserFor(pureMarkdownRules);
|
|
314
|
+
var parse = (source, state) => {
|
|
315
|
+
var paragraphedSource = source + "\n\n";
|
|
316
|
+
return builtParser(paragraphedSource, _objectSpread2(_objectSpread2({}, state), {}, {
|
|
317
|
+
inline: false
|
|
318
|
+
}));
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
export { parse, pureMarkdownRules };
|
|
322
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.js"],"sourcesContent":["// @flow\n\n/**\n * Contains markdown related functions in pure javascript,\n * extracted from perseus-markdown.jsx\n * Note that this file may be used in stand alone nodejs, thus\n * do not import anything from Perseus\n */\nimport SimpleMarkdown from \"@khanacademy/simple-markdown\";\n\n/**\n * This match function matches math in `$`s, such as:\n *\n * $y = x + 1$\n *\n * It functions roughly like the following regex:\n * /\\$([^\\$]*)\\$/\n *\n * Unfortunately, math may have other `$`s inside it, as\n * long as they are inside `{` braces `}`, mostly for\n * `\\text{ $math$ }`.\n *\n * To parse this, we can't use a regex, since we\n * should support arbitrary nesting (even though\n * MathJax actually only supports two levels of nesting\n * here, which we *could* parse with a regex).\n *\n * Non-regex matchers like this are now a first-class\n * concept in simple-markdown. Yay!\n *\n * This can also match block-math, which is math alone in a paragraph.\n */\n\nconst rWidgetRule: RegExp = /^\\[\\[\\u2603 (([a-z-]+) ([0-9]+))\\]\\]/;\n\nconst mathMatcher = (source, state, isBlock) => {\n const length = source.length;\n let index = 0;\n\n // When looking for blocks, skip over leading spaces\n if (isBlock) {\n if (state.inline) {\n return null;\n }\n while (index < length && source[index] === \" \") {\n index++;\n }\n }\n\n // Our source must start with a \"$\"\n if (!(index < length && source[index] === \"$\")) {\n return null;\n }\n\n index++;\n const startIndex = index;\n let braceLevel = 0;\n\n // Loop through the source, looking for a closing '$'\n // closing '$'s only count if they are not escaped with\n // a `\\`, and we are not in nested `{}` braces.\n while (index < length) {\n const character = source[index];\n\n if (character === \"\\\\\") {\n // Consume both the `\\` and the escaped char as a single\n // token.\n // This is so that the second `$` in `$\\\\$` closes\n // the math expression, since the first `\\` is escaping\n // the second `\\`, but the second `\\` is not escaping\n // the second `$`.\n // This also handles the case of escaping `$`s or\n // braces `\\{`\n index++;\n } else if (braceLevel <= 0 && character === \"$\") {\n let endIndex = index + 1;\n if (isBlock) {\n // Look for two trailing newlines after the closing `$`\n const match = /^(?: *\\n){2,}/.exec(source.slice(endIndex));\n endIndex = match ? endIndex + match[0].length : null;\n }\n\n // Return an array that looks like the results of a\n // regex's .exec function:\n // capture[0] is the whole string\n // capture[1] is the first \"paren\" match, which is the\n // content of the math here, as if we wrote the regex\n // /\\$([^\\$]*)\\$/\n if (endIndex) {\n return [\n source.substring(0, endIndex),\n source.substring(startIndex, index),\n ];\n }\n return null;\n } else if (character === \"{\") {\n braceLevel++;\n } else if (character === \"}\") {\n braceLevel--;\n } else if (character === \"\\n\" && source[index - 1] === \"\\n\") {\n // This is a weird case we supported in the old\n // math implementation--double newlines break\n // math. I'm preserving it for now because content\n // creators might have questions with single '$'s\n // in paragraphs...\n return null;\n }\n\n index++;\n }\n\n // we didn't find a closing `$`\n return null;\n};\nconst mathMatch = (source: any, state: any): any =>\n mathMatcher(source, state, false);\nconst blockMathMatch = (source: any, state: any): any =>\n mathMatcher(source, state, true);\n\nconst TITLED_TABLE_REGEX = new RegExp(\n \"^\\\\|\\\\| +(.*) +\\\\|\\\\| *\\\\n\" +\n \"(\" +\n // The simple-markdown nptable regex, without\n // the leading `^`\n // $FlowFixMe[incompatible-use]\n SimpleMarkdown.defaultRules.nptable.match.regex.source.substring(1) +\n \")\",\n);\n\nconst crowdinJiptMatcher = SimpleMarkdown.blockRegex(/^(crwdns.*)\\n\\s*\\n/);\n\nexport const pureMarkdownRules = {\n ...SimpleMarkdown.defaultRules,\n\n // NOTE: basically ignored by JIPT. wraps everything at the outer layer\n columns: {\n order: -2,\n match: (SimpleMarkdown.blockRegex(\n /^([\\s\\S]*\\n\\n)={5,}\\n\\n([\\s\\S]*)/,\n ): any),\n parse: (capture: any, parse: any, state: any): any => {\n return {\n col1: parse(capture[1], state),\n col2: parse(capture[2], state),\n };\n },\n },\n crowdinId: {\n order: -1,\n match: (source: any, state: any, prevCapture: any): any => {\n // Only match on the just-in-place translation site\n if (state.isJipt) {\n return crowdinJiptMatcher(source, state, prevCapture);\n }\n return null;\n },\n parse: (capture: any, parse: any, state: any): any => ({\n id: capture[1],\n }),\n },\n // This is pretty much horrible, but we have a regex here to capture an\n // entire table + a title. capture[1] is the title. capture[2] of the\n // regex is a copy of the simple-markdown nptable regex. Then we turn\n // our capture[2] into tableCapture[0], and any further captures in\n // our table regex into tableCapture[1..], and we pass tableCapture to\n // our nptable regex\n titledTable: {\n // process immediately before nptables\n order: SimpleMarkdown.defaultRules.nptable.order - 0.5,\n match: (SimpleMarkdown.blockRegex(TITLED_TABLE_REGEX): any),\n parse: (capture: any, parse: any, state: any): any => {\n const title = SimpleMarkdown.parseInline(parse, capture[1], state);\n\n // Remove our [0] and [1] captures, and pass the rest to\n // the nptable parser\n const tableCapture = capture.slice(2);\n const table = SimpleMarkdown.defaultRules.nptable.parse(\n tableCapture,\n parse,\n state,\n );\n return {\n title: title,\n table: table,\n };\n },\n },\n widget: {\n order: SimpleMarkdown.defaultRules.link.order - 0.75,\n match: (SimpleMarkdown.inlineRegex(rWidgetRule): any),\n parse: (capture: any, parse: any, state: any): any => {\n return {\n id: capture[1],\n widgetType: capture[2],\n };\n },\n },\n blockMath: {\n order: (SimpleMarkdown.defaultRules.codeBlock.order + 0.5: any),\n match: blockMathMatch,\n parse: (capture: any, parse: any, state: any): any => {\n return {\n content: capture[1],\n };\n },\n },\n math: {\n order: SimpleMarkdown.defaultRules.link.order - 0.25,\n match: mathMatch,\n parse: (capture: any, parse: any, state: any): any => {\n return {\n content: capture[1],\n };\n },\n },\n unescapedDollar: {\n order: SimpleMarkdown.defaultRules.link.order - 0.24,\n match: (SimpleMarkdown.inlineRegex(/^(?!\\\\)\\$/): any),\n parse: (capture: any, parse: any, state: any): any => {\n return {};\n },\n },\n fence: {\n ...SimpleMarkdown.defaultRules.fence,\n parse: (capture: any, parse: any, state: any): any => {\n const node = SimpleMarkdown.defaultRules.fence.parse(\n capture,\n parse,\n state,\n );\n\n // support screenreader-only text with ```alt\n if (node.lang === \"alt\") {\n return {\n type: \"codeBlock\",\n lang: \"alt\",\n // default codeBlock parsing doesn't parse the contents.\n // We need to parse the contents for things like table\n // support :).\n // The \\n\\n is because the inside of the codeblock might\n // not end in double newlines for block rules, because\n // ordinarily we don't parse this :).\n content: parse(node.content + \"\\n\\n\", state),\n };\n }\n return node;\n },\n },\n blockQuote: {\n ...SimpleMarkdown.defaultRules.blockQuote,\n // Replace the less restrictive blockquote regex from SimpleMarkdown\n // with a more restrictive one. The only difference should be that\n //\n // > A blockquote\n //\n // > Another blockquote\n //\n // will now match as two different blockQuotes instead of a single\n // blockquote with some paragraph breaks in it.\n //\n // The main motivation for doing this is to provide better support for\n // translators translating blockquotes with multiple paragraphs in\n // them. When translating articles, we split up paragraphs, translate\n // them separately, and then recombine them. We do this so that\n // translators don't have to translate an entire article at a time,\n // they can instead translate paragraph-by-paragraph.That system\n // doesn't understand blockquotes, so it will split up blockquotes into\n // more than one paragraph. A way to solve this would be to make that\n // system understand blockquotes, but then translators would have to\n // translate an entire, multi-paragraph blockquote at a time. Instead,\n // we choose to modify our blockquote matching to split up\n // multi-paragraph blockquotes into multiple blockquotes.\n //\n // There is also precedence for doing this splitting up in other\n // libraries, for instance CommonMark also splits up blockquotes with\n // empty lines into multiple blockquotes:\n // https://spec.commonmark.org/0.28/#example-205\n match: (SimpleMarkdown.blockRegex(\n /^ *>[^\\n]+(\\n( *>)?[^\\n]+)*\\n{2,}/,\n ): any),\n },\n list: {\n ...SimpleMarkdown.defaultRules.list,\n match: (source: any, state: any, prevCapture: any): any => {\n // Since lists can contain double newlines and we have special\n // handling of double newlines while parsing jipt content, just\n // disable the list parser.\n if (state.isJipt) {\n return null;\n }\n return SimpleMarkdown.defaultRules.list.match(\n source,\n state,\n prevCapture,\n );\n },\n },\n // The lint rule never actually matches anything.\n // We check for lint after parsing, and, if we find any, we\n // transform the tree to add lint nodes. This rule is here\n // just for the react() function\n lint: {\n order: 1000,\n match: (s: any): any => null,\n parse: (capture: any, parse: any, state: any): any => ({}),\n },\n};\n\n// $FlowFixMe[incompatible-call]\nconst builtParser = SimpleMarkdown.parserFor(pureMarkdownRules);\n\nexport const parse = (source: string, state: $FlowFixMe): $FlowFixMe => {\n const paragraphedSource = source + \"\\n\\n\";\n\n return builtParser(paragraphedSource, {\n ...state,\n inline: false,\n });\n};\n"],"names":["rWidgetRule","mathMatcher","source","state","isBlock","length","index","inline","startIndex","braceLevel","character","endIndex","match","exec","slice","substring","mathMatch","blockMathMatch","TITLED_TABLE_REGEX","RegExp","SimpleMarkdown","defaultRules","nptable","regex","crowdinJiptMatcher","blockRegex","pureMarkdownRules","columns","order","parse","capture","col1","col2","crowdinId","prevCapture","isJipt","id","titledTable","title","parseInline","tableCapture","table","widget","link","inlineRegex","widgetType","blockMath","codeBlock","content","math","unescapedDollar","fence","node","lang","type","blockQuote","list","lint","s","builtParser","parserFor","paragraphedSource","_objectSpread"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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AAP,CAAA;AAKH,KAAA;AAdD,GAtJsB,CAAA;AAsK1B;AACA;AACA;AACA;AACAuB,EAAAA,IAAI,EAAE;AACF7B,IAAAA,KAAK,EAAE,IADL;AAEFhB,IAAAA,KAAK,EAAG8C,CAAD,IAAiB,IAFtB;AAGF7B,IAAAA,KAAK,EAAE,CAACC,OAAD,EAAeD,OAAf,EAA2B1B,KAA3B,MAAgD,EAAhD,CAAA;AAHL,GAAA;AA1KoB,CAAA;;AAkL9B,IAAMwD,WAAW,GAAGvC,cAAc,CAACwC,SAAf,CAAyBlC,iBAAzB,CAApB,CAAA;IAEaG,KAAK,GAAG,CAAC3B,MAAD,EAAiBC,KAAjB,KAAmD;AACpE,EAAA,IAAM0D,iBAAiB,GAAG3D,MAAM,GAAG,MAAnC,CAAA;AAEA,EAAA,OAAOyD,WAAW,CAACE,iBAAD,EAAAC,cAAA,CAAAA,cAAA,CAAA,EAAA,EACX3D,KADW,CAAA,EAAA,EAAA,EAAA;AAEdI,IAAAA,MAAM,EAAE,KAAA;AAFM,GAAlB,CAAA,CAAA,CAAA;AAIH;;;;"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var SimpleMarkdown = require('@khanacademy/simple-markdown');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var SimpleMarkdown__default = /*#__PURE__*/_interopDefaultLegacy(SimpleMarkdown);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Contains markdown related functions in pure javascript,
|
|
13
|
+
* extracted from perseus-markdown.jsx
|
|
14
|
+
* Note that this file may be used in stand alone nodejs, thus
|
|
15
|
+
* do not import anything from Perseus
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* This match function matches math in `$`s, such as:
|
|
19
|
+
*
|
|
20
|
+
* $y = x + 1$
|
|
21
|
+
*
|
|
22
|
+
* It functions roughly like the following regex:
|
|
23
|
+
* /\$([^\$]*)\$/
|
|
24
|
+
*
|
|
25
|
+
* Unfortunately, math may have other `$`s inside it, as
|
|
26
|
+
* long as they are inside `{` braces `}`, mostly for
|
|
27
|
+
* `\text{ $math$ }`.
|
|
28
|
+
*
|
|
29
|
+
* To parse this, we can't use a regex, since we
|
|
30
|
+
* should support arbitrary nesting (even though
|
|
31
|
+
* MathJax actually only supports two levels of nesting
|
|
32
|
+
* here, which we *could* parse with a regex).
|
|
33
|
+
*
|
|
34
|
+
* Non-regex matchers like this are now a first-class
|
|
35
|
+
* concept in simple-markdown. Yay!
|
|
36
|
+
*
|
|
37
|
+
* This can also match block-math, which is math alone in a paragraph.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
const rWidgetRule = /^\[\[\u2603 (([a-z-]+) ([0-9]+))\]\]/;
|
|
41
|
+
|
|
42
|
+
const mathMatcher = (source, state, isBlock) => {
|
|
43
|
+
const length = source.length;
|
|
44
|
+
let index = 0; // When looking for blocks, skip over leading spaces
|
|
45
|
+
|
|
46
|
+
if (isBlock) {
|
|
47
|
+
if (state.inline) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
while (index < length && source[index] === " ") {
|
|
52
|
+
index++;
|
|
53
|
+
}
|
|
54
|
+
} // Our source must start with a "$"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if (!(index < length && source[index] === "$")) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
index++;
|
|
62
|
+
const startIndex = index;
|
|
63
|
+
let braceLevel = 0; // Loop through the source, looking for a closing '$'
|
|
64
|
+
// closing '$'s only count if they are not escaped with
|
|
65
|
+
// a `\`, and we are not in nested `{}` braces.
|
|
66
|
+
|
|
67
|
+
while (index < length) {
|
|
68
|
+
const character = source[index];
|
|
69
|
+
|
|
70
|
+
if (character === "\\") {
|
|
71
|
+
// Consume both the `\` and the escaped char as a single
|
|
72
|
+
// token.
|
|
73
|
+
// This is so that the second `$` in `$\\$` closes
|
|
74
|
+
// the math expression, since the first `\` is escaping
|
|
75
|
+
// the second `\`, but the second `\` is not escaping
|
|
76
|
+
// the second `$`.
|
|
77
|
+
// This also handles the case of escaping `$`s or
|
|
78
|
+
// braces `\{`
|
|
79
|
+
index++;
|
|
80
|
+
} else if (braceLevel <= 0 && character === "$") {
|
|
81
|
+
let endIndex = index + 1;
|
|
82
|
+
|
|
83
|
+
if (isBlock) {
|
|
84
|
+
// Look for two trailing newlines after the closing `$`
|
|
85
|
+
const match = /^(?: *\n){2,}/.exec(source.slice(endIndex));
|
|
86
|
+
endIndex = match ? endIndex + match[0].length : null;
|
|
87
|
+
} // Return an array that looks like the results of a
|
|
88
|
+
// regex's .exec function:
|
|
89
|
+
// capture[0] is the whole string
|
|
90
|
+
// capture[1] is the first "paren" match, which is the
|
|
91
|
+
// content of the math here, as if we wrote the regex
|
|
92
|
+
// /\$([^\$]*)\$/
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if (endIndex) {
|
|
96
|
+
return [source.substring(0, endIndex), source.substring(startIndex, index)];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return null;
|
|
100
|
+
} else if (character === "{") {
|
|
101
|
+
braceLevel++;
|
|
102
|
+
} else if (character === "}") {
|
|
103
|
+
braceLevel--;
|
|
104
|
+
} else if (character === "\n" && source[index - 1] === "\n") {
|
|
105
|
+
// This is a weird case we supported in the old
|
|
106
|
+
// math implementation--double newlines break
|
|
107
|
+
// math. I'm preserving it for now because content
|
|
108
|
+
// creators might have questions with single '$'s
|
|
109
|
+
// in paragraphs...
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
index++;
|
|
114
|
+
} // we didn't find a closing `$`
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
return null;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const mathMatch = (source, state) => mathMatcher(source, state, false);
|
|
121
|
+
|
|
122
|
+
const blockMathMatch = (source, state) => mathMatcher(source, state, true);
|
|
123
|
+
|
|
124
|
+
const TITLED_TABLE_REGEX = new RegExp("^\\|\\| +(.*) +\\|\\| *\\n" + "(" + // The simple-markdown nptable regex, without
|
|
125
|
+
// the leading `^`
|
|
126
|
+
// $FlowFixMe[incompatible-use]
|
|
127
|
+
SimpleMarkdown__default["default"].defaultRules.nptable.match.regex.source.substring(1) + ")");
|
|
128
|
+
const crowdinJiptMatcher = SimpleMarkdown__default["default"].blockRegex(/^(crwdns.*)\n\s*\n/);
|
|
129
|
+
const pureMarkdownRules = { ...SimpleMarkdown__default["default"].defaultRules,
|
|
130
|
+
// NOTE: basically ignored by JIPT. wraps everything at the outer layer
|
|
131
|
+
columns: {
|
|
132
|
+
order: -2,
|
|
133
|
+
match: SimpleMarkdown__default["default"].blockRegex(/^([\s\S]*\n\n)={5,}\n\n([\s\S]*)/),
|
|
134
|
+
parse: (capture, parse, state) => {
|
|
135
|
+
return {
|
|
136
|
+
col1: parse(capture[1], state),
|
|
137
|
+
col2: parse(capture[2], state)
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
crowdinId: {
|
|
142
|
+
order: -1,
|
|
143
|
+
match: (source, state, prevCapture) => {
|
|
144
|
+
// Only match on the just-in-place translation site
|
|
145
|
+
if (state.isJipt) {
|
|
146
|
+
return crowdinJiptMatcher(source, state, prevCapture);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return null;
|
|
150
|
+
},
|
|
151
|
+
parse: (capture, parse, state) => ({
|
|
152
|
+
id: capture[1]
|
|
153
|
+
})
|
|
154
|
+
},
|
|
155
|
+
// This is pretty much horrible, but we have a regex here to capture an
|
|
156
|
+
// entire table + a title. capture[1] is the title. capture[2] of the
|
|
157
|
+
// regex is a copy of the simple-markdown nptable regex. Then we turn
|
|
158
|
+
// our capture[2] into tableCapture[0], and any further captures in
|
|
159
|
+
// our table regex into tableCapture[1..], and we pass tableCapture to
|
|
160
|
+
// our nptable regex
|
|
161
|
+
titledTable: {
|
|
162
|
+
// process immediately before nptables
|
|
163
|
+
order: SimpleMarkdown__default["default"].defaultRules.nptable.order - 0.5,
|
|
164
|
+
match: SimpleMarkdown__default["default"].blockRegex(TITLED_TABLE_REGEX),
|
|
165
|
+
parse: (capture, parse, state) => {
|
|
166
|
+
const title = SimpleMarkdown__default["default"].parseInline(parse, capture[1], state); // Remove our [0] and [1] captures, and pass the rest to
|
|
167
|
+
// the nptable parser
|
|
168
|
+
|
|
169
|
+
const tableCapture = capture.slice(2);
|
|
170
|
+
const table = SimpleMarkdown__default["default"].defaultRules.nptable.parse(tableCapture, parse, state);
|
|
171
|
+
return {
|
|
172
|
+
title: title,
|
|
173
|
+
table: table
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
widget: {
|
|
178
|
+
order: SimpleMarkdown__default["default"].defaultRules.link.order - 0.75,
|
|
179
|
+
match: SimpleMarkdown__default["default"].inlineRegex(rWidgetRule),
|
|
180
|
+
parse: (capture, parse, state) => {
|
|
181
|
+
return {
|
|
182
|
+
id: capture[1],
|
|
183
|
+
widgetType: capture[2]
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
blockMath: {
|
|
188
|
+
order: SimpleMarkdown__default["default"].defaultRules.codeBlock.order + 0.5,
|
|
189
|
+
match: blockMathMatch,
|
|
190
|
+
parse: (capture, parse, state) => {
|
|
191
|
+
return {
|
|
192
|
+
content: capture[1]
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
math: {
|
|
197
|
+
order: SimpleMarkdown__default["default"].defaultRules.link.order - 0.25,
|
|
198
|
+
match: mathMatch,
|
|
199
|
+
parse: (capture, parse, state) => {
|
|
200
|
+
return {
|
|
201
|
+
content: capture[1]
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
unescapedDollar: {
|
|
206
|
+
order: SimpleMarkdown__default["default"].defaultRules.link.order - 0.24,
|
|
207
|
+
match: SimpleMarkdown__default["default"].inlineRegex(/^(?!\\)\$/),
|
|
208
|
+
parse: (capture, parse, state) => {
|
|
209
|
+
return {};
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
fence: { ...SimpleMarkdown__default["default"].defaultRules.fence,
|
|
213
|
+
parse: (capture, parse, state) => {
|
|
214
|
+
const node = SimpleMarkdown__default["default"].defaultRules.fence.parse(capture, parse, state); // support screenreader-only text with ```alt
|
|
215
|
+
|
|
216
|
+
if (node.lang === "alt") {
|
|
217
|
+
return {
|
|
218
|
+
type: "codeBlock",
|
|
219
|
+
lang: "alt",
|
|
220
|
+
// default codeBlock parsing doesn't parse the contents.
|
|
221
|
+
// We need to parse the contents for things like table
|
|
222
|
+
// support :).
|
|
223
|
+
// The \n\n is because the inside of the codeblock might
|
|
224
|
+
// not end in double newlines for block rules, because
|
|
225
|
+
// ordinarily we don't parse this :).
|
|
226
|
+
content: parse(node.content + "\n\n", state)
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return node;
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
blockQuote: { ...SimpleMarkdown__default["default"].defaultRules.blockQuote,
|
|
234
|
+
// Replace the less restrictive blockquote regex from SimpleMarkdown
|
|
235
|
+
// with a more restrictive one. The only difference should be that
|
|
236
|
+
//
|
|
237
|
+
// > A blockquote
|
|
238
|
+
//
|
|
239
|
+
// > Another blockquote
|
|
240
|
+
//
|
|
241
|
+
// will now match as two different blockQuotes instead of a single
|
|
242
|
+
// blockquote with some paragraph breaks in it.
|
|
243
|
+
//
|
|
244
|
+
// The main motivation for doing this is to provide better support for
|
|
245
|
+
// translators translating blockquotes with multiple paragraphs in
|
|
246
|
+
// them. When translating articles, we split up paragraphs, translate
|
|
247
|
+
// them separately, and then recombine them. We do this so that
|
|
248
|
+
// translators don't have to translate an entire article at a time,
|
|
249
|
+
// they can instead translate paragraph-by-paragraph.That system
|
|
250
|
+
// doesn't understand blockquotes, so it will split up blockquotes into
|
|
251
|
+
// more than one paragraph. A way to solve this would be to make that
|
|
252
|
+
// system understand blockquotes, but then translators would have to
|
|
253
|
+
// translate an entire, multi-paragraph blockquote at a time. Instead,
|
|
254
|
+
// we choose to modify our blockquote matching to split up
|
|
255
|
+
// multi-paragraph blockquotes into multiple blockquotes.
|
|
256
|
+
//
|
|
257
|
+
// There is also precedence for doing this splitting up in other
|
|
258
|
+
// libraries, for instance CommonMark also splits up blockquotes with
|
|
259
|
+
// empty lines into multiple blockquotes:
|
|
260
|
+
// https://spec.commonmark.org/0.28/#example-205
|
|
261
|
+
match: SimpleMarkdown__default["default"].blockRegex(/^ *>[^\n]+(\n( *>)?[^\n]+)*\n{2,}/)
|
|
262
|
+
},
|
|
263
|
+
list: { ...SimpleMarkdown__default["default"].defaultRules.list,
|
|
264
|
+
match: (source, state, prevCapture) => {
|
|
265
|
+
// Since lists can contain double newlines and we have special
|
|
266
|
+
// handling of double newlines while parsing jipt content, just
|
|
267
|
+
// disable the list parser.
|
|
268
|
+
if (state.isJipt) {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return SimpleMarkdown__default["default"].defaultRules.list.match(source, state, prevCapture);
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
// The lint rule never actually matches anything.
|
|
276
|
+
// We check for lint after parsing, and, if we find any, we
|
|
277
|
+
// transform the tree to add lint nodes. This rule is here
|
|
278
|
+
// just for the react() function
|
|
279
|
+
lint: {
|
|
280
|
+
order: 1000,
|
|
281
|
+
match: s => null,
|
|
282
|
+
parse: (capture, parse, state) => ({})
|
|
283
|
+
}
|
|
284
|
+
}; // $FlowFixMe[incompatible-call]
|
|
285
|
+
|
|
286
|
+
const builtParser = SimpleMarkdown__default["default"].parserFor(pureMarkdownRules);
|
|
287
|
+
const parse = (source, state) => {
|
|
288
|
+
const paragraphedSource = source + "\n\n";
|
|
289
|
+
return builtParser(paragraphedSource, { ...state,
|
|
290
|
+
inline: false
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
exports.parse = parse;
|
|
295
|
+
exports.pureMarkdownRules = pureMarkdownRules;
|
|
296
|
+
//# sourceMappingURL=index.js.map
|