@khanacademy/simple-markdown 0.8.5 → 0.9.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/.eslintrc.js +7 -1
- package/CHANGELOG.md +13 -0
- package/dist/es/index.js +194 -280
- package/dist/es/index.js.map +1 -1
- package/dist/index.d.ts +187 -2
- package/dist/index.js +194 -244
- package/dist/index.js.flow +282 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/{simple-markdown_test.js → simple-markdown.test.ts} +39 -53
- package/src/{index.js → index.ts} +250 -187
- package/tsconfig.json +6 -68
- package/tsconfig.tsbuildinfo +1 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Javascript markdown parsing, made simple",
|
|
4
4
|
"author": "Khan Academy",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.9.0",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"module": "dist/es/index.js",
|
|
18
18
|
"main": "dist/index.js",
|
|
19
|
-
"source": "src/index.
|
|
19
|
+
"source": "src/index.ts",
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "bash -c 'yarn --silent --cwd \"../..\" test ${@:0} $($([[ ${@: -1} = -* ]] || [[ ${@: -1} = bash ]]) && echo $PWD)'"
|
|
22
22
|
},
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/react-dom": ">=16.0.0",
|
|
28
|
-
"perseus-build-settings": "^0.0.
|
|
28
|
+
"perseus-build-settings": "^0.0.5",
|
|
29
29
|
"size-limit": "^0.21.1",
|
|
30
30
|
"typescript": "^3.6.4",
|
|
31
31
|
"uglify-js": "^3.6.7"
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
/* eslint-disable no-var, no-unused-vars, no-console, import/no-commonjs, no-redeclare, no-useless-escape */
|
|
2
|
-
|
|
3
|
-
/* @ts-check */
|
|
4
|
-
import {render} from "@testing-library/react";
|
|
1
|
+
/* eslint-disable no-var, @typescript-eslint/no-unused-vars, no-console, import/no-commonjs, no-redeclare, no-useless-escape */
|
|
2
|
+
import assert from "assert";
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// So we use a /*::*/ hack to satisfy flow:
|
|
9
|
-
import SimpleMarkdown from "../index.js";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import * as ReactDOMServer from "react-dom/server";
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
import SimpleMarkdown from "../index";
|
|
12
8
|
|
|
13
|
-
|
|
14
|
-
var ReactDOMServer = require("react-dom/server");
|
|
9
|
+
import type {SingleASTNode} from "../index";
|
|
15
10
|
|
|
16
11
|
var inlineParse = SimpleMarkdown.defaultInlineParse;
|
|
17
12
|
var blockParse = SimpleMarkdown.defaultBlockParse;
|
|
@@ -39,7 +34,7 @@ var FLOW_IGNORE_COVARIANCE = {
|
|
|
39
34
|
*
|
|
40
35
|
* @param {SimpleMarkdown.ASTNode | Array<SimpleMarkdown.TableAlignment>} ast
|
|
41
36
|
*/
|
|
42
|
-
var prettyPrintAST = function (ast) {
|
|
37
|
+
var prettyPrintAST = function (ast: any) {
|
|
43
38
|
return JSON.stringify(ast, null, 4);
|
|
44
39
|
// // FIXME(aria): For debugging in more depth? This used to work?
|
|
45
40
|
// return nodeUtil.inspect(ast, {
|
|
@@ -77,7 +72,7 @@ var reactToHtml = function (reactElements) {
|
|
|
77
72
|
* @param {SimpleMarkdown.ASTNode} parsed
|
|
78
73
|
* @returns {string}
|
|
79
74
|
*/
|
|
80
|
-
var htmlThroughReact = function (parsed) {
|
|
75
|
+
var htmlThroughReact = function (parsed: Array<SingleASTNode>) {
|
|
81
76
|
var output = defaultReactOutput(parsed);
|
|
82
77
|
return reactToHtml(output);
|
|
83
78
|
};
|
|
@@ -104,7 +99,7 @@ var htmlFromMarkdown = function (source) {
|
|
|
104
99
|
* @param {string} source
|
|
105
100
|
* @param {string} html
|
|
106
101
|
*/
|
|
107
|
-
var assertParsesToReact = function (source, html) {
|
|
102
|
+
var assertParsesToReact = function (source: string, html: string) {
|
|
108
103
|
var actualHtml = htmlFromReactMarkdown(source);
|
|
109
104
|
assert.strictEqual(actualHtml, html);
|
|
110
105
|
};
|
|
@@ -113,7 +108,7 @@ var assertParsesToReact = function (source, html) {
|
|
|
113
108
|
* @param {string} source
|
|
114
109
|
* @param {string} html
|
|
115
110
|
*/
|
|
116
|
-
var assertParsesToHtml = function (source, html) {
|
|
111
|
+
var assertParsesToHtml = function (source: string, html) {
|
|
117
112
|
var actualHtml = htmlFromMarkdown(source);
|
|
118
113
|
assert.strictEqual(actualHtml, html);
|
|
119
114
|
};
|
|
@@ -3066,11 +3061,11 @@ describe("simple markdown", function () {
|
|
|
3066
3061
|
[{type: "text", content: "h2"}],
|
|
3067
3062
|
[{type: "text", content: "h3"}],
|
|
3068
3063
|
],
|
|
3069
|
-
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */
|
|
3064
|
+
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */ [
|
|
3070
3065
|
null,
|
|
3071
3066
|
null,
|
|
3072
3067
|
null,
|
|
3073
|
-
]
|
|
3068
|
+
],
|
|
3074
3069
|
cells: [
|
|
3075
3070
|
[
|
|
3076
3071
|
[{type: "text", content: "d1"}],
|
|
@@ -3129,11 +3124,11 @@ describe("simple markdown", function () {
|
|
|
3129
3124
|
},
|
|
3130
3125
|
],
|
|
3131
3126
|
],
|
|
3132
|
-
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */
|
|
3127
|
+
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */ [
|
|
3133
3128
|
null,
|
|
3134
3129
|
null,
|
|
3135
3130
|
null,
|
|
3136
|
-
]
|
|
3131
|
+
],
|
|
3137
3132
|
cells: [
|
|
3138
3133
|
[
|
|
3139
3134
|
[
|
|
@@ -3181,7 +3176,10 @@ describe("simple markdown", function () {
|
|
|
3181
3176
|
* @param {string} tableSrc
|
|
3182
3177
|
* @param {Array<SimpleMarkdown.TableAlignment>} expectedAligns
|
|
3183
3178
|
*/
|
|
3184
|
-
var validateAligns = function (
|
|
3179
|
+
var validateAligns = function (
|
|
3180
|
+
tableSrc,
|
|
3181
|
+
expectedAligns: Array<null> | Array<string>,
|
|
3182
|
+
) {
|
|
3185
3183
|
var parsed = blockParse(tableSrc + "\n");
|
|
3186
3184
|
assert.strictEqual(parsed[0].type, "table");
|
|
3187
3185
|
var actualAligns = parsed[0].align;
|
|
@@ -3192,11 +3190,11 @@ describe("simple markdown", function () {
|
|
|
3192
3190
|
"| h1 | h2 | h3 |\n" +
|
|
3193
3191
|
"| -- | -- | -- |\n" +
|
|
3194
3192
|
"| d1 | d2 | d3 |\n",
|
|
3195
|
-
/** @type {Array<SimpleMarkdown.TableAlignment>} */
|
|
3193
|
+
/** @type {Array<SimpleMarkdown.TableAlignment>} */ [
|
|
3196
3194
|
null,
|
|
3197
3195
|
null,
|
|
3198
3196
|
null,
|
|
3199
|
-
]
|
|
3197
|
+
],
|
|
3200
3198
|
);
|
|
3201
3199
|
|
|
3202
3200
|
validateAligns(
|
|
@@ -3235,16 +3233,16 @@ describe("simple markdown", function () {
|
|
|
3235
3233
|
var expected = [
|
|
3236
3234
|
{
|
|
3237
3235
|
type: "table",
|
|
3238
|
-
header: /** @type {any[][]} */
|
|
3239
|
-
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */
|
|
3236
|
+
header: /** @type {any[][]} */ [[], [], []],
|
|
3237
|
+
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */ [
|
|
3240
3238
|
null,
|
|
3241
3239
|
null,
|
|
3242
3240
|
null,
|
|
3243
|
-
]
|
|
3244
|
-
cells: /** @type {any[][]} */
|
|
3241
|
+
],
|
|
3242
|
+
cells: /** @type {any[][]} */ [
|
|
3245
3243
|
[[], [], []],
|
|
3246
3244
|
[[], [], []],
|
|
3247
|
-
]
|
|
3245
|
+
],
|
|
3248
3246
|
},
|
|
3249
3247
|
];
|
|
3250
3248
|
|
|
@@ -3283,10 +3281,10 @@ describe("simple markdown", function () {
|
|
|
3283
3281
|
{type: "text", content: "|"},
|
|
3284
3282
|
],
|
|
3285
3283
|
],
|
|
3286
|
-
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */
|
|
3284
|
+
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */ [
|
|
3287
3285
|
null,
|
|
3288
3286
|
null,
|
|
3289
|
-
]
|
|
3287
|
+
],
|
|
3290
3288
|
cells: [
|
|
3291
3289
|
[
|
|
3292
3290
|
[
|
|
@@ -3332,10 +3330,10 @@ describe("simple markdown", function () {
|
|
|
3332
3330
|
[{type: "text", content: "Attribute"}],
|
|
3333
3331
|
[{type: "text", content: "Type"}],
|
|
3334
3332
|
],
|
|
3335
|
-
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */
|
|
3333
|
+
align: /** @type {Array<SimpleMarkdown.TableAlignment>} */ [
|
|
3336
3334
|
null,
|
|
3337
3335
|
null,
|
|
3338
|
-
]
|
|
3336
|
+
],
|
|
3339
3337
|
cells: [
|
|
3340
3338
|
[
|
|
3341
3339
|
[{type: "inlineCode", content: "position"}],
|
|
@@ -3485,11 +3483,11 @@ describe("simple markdown", function () {
|
|
|
3485
3483
|
percentVar: percentVarRule,
|
|
3486
3484
|
});
|
|
3487
3485
|
|
|
3488
|
-
//
|
|
3486
|
+
// @ts-expect-error [FEI-5003] - TS2345 - Argument of type 'DefaultRules & { percentVar: { match: (source: any) => RegExpExecArray | null; order: number; parse: (capture: any, parse: any, state: any) => { content: any; }; }; }' is not assignable to parameter of type 'ParserRules'.
|
|
3489
3487
|
var rawBuiltParser = SimpleMarkdown.parserFor(rules);
|
|
3490
3488
|
|
|
3491
3489
|
/** @type {SimpleMarkdown.Parser} */
|
|
3492
|
-
var inlineParse = function (source) {
|
|
3490
|
+
var inlineParse = function (source: string) {
|
|
3493
3491
|
return rawBuiltParser(source, {inline: true});
|
|
3494
3492
|
};
|
|
3495
3493
|
|
|
@@ -3532,7 +3530,6 @@ describe("simple markdown", function () {
|
|
|
3532
3530
|
});
|
|
3533
3531
|
|
|
3534
3532
|
it("should sort rules by order", function () {
|
|
3535
|
-
// $FlowFixMe
|
|
3536
3533
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3537
3534
|
em1: Object.assign({}, emRule, {
|
|
3538
3535
|
order: 0,
|
|
@@ -3546,7 +3543,6 @@ describe("simple markdown", function () {
|
|
|
3546
3543
|
var parsed1 = parser1("_hi_", {inline: true});
|
|
3547
3544
|
validateParse(parsed1, [{content: "hi", type: "em1"}]);
|
|
3548
3545
|
|
|
3549
|
-
// $FlowFixMe
|
|
3550
3546
|
var parser2 = SimpleMarkdown.parserFor({
|
|
3551
3547
|
em1: Object.assign({}, emRule, {
|
|
3552
3548
|
order: 1,
|
|
@@ -3562,7 +3558,6 @@ describe("simple markdown", function () {
|
|
|
3562
3558
|
});
|
|
3563
3559
|
|
|
3564
3560
|
it("should allow fractional orders", function () {
|
|
3565
|
-
// $FlowFixMe
|
|
3566
3561
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3567
3562
|
em1: Object.assign({}, emRule, {
|
|
3568
3563
|
order: 1.4,
|
|
@@ -3576,7 +3571,6 @@ describe("simple markdown", function () {
|
|
|
3576
3571
|
var parsed1 = parser1("_hi_", {inline: true});
|
|
3577
3572
|
validateParse(parsed1, [{content: "hi", type: "em2"}]);
|
|
3578
3573
|
|
|
3579
|
-
// $FlowFixMe
|
|
3580
3574
|
var parser2 = SimpleMarkdown.parserFor({
|
|
3581
3575
|
em1: Object.assign({}, emRule, {
|
|
3582
3576
|
order: 0.5,
|
|
@@ -3592,7 +3586,6 @@ describe("simple markdown", function () {
|
|
|
3592
3586
|
});
|
|
3593
3587
|
|
|
3594
3588
|
it("should allow negative orders", function () {
|
|
3595
|
-
// $FlowFixMe
|
|
3596
3589
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3597
3590
|
em1: Object.assign({}, emRule, {
|
|
3598
3591
|
order: 0,
|
|
@@ -3606,7 +3599,6 @@ describe("simple markdown", function () {
|
|
|
3606
3599
|
var parsed1 = parser1("_hi_", {inline: true});
|
|
3607
3600
|
validateParse(parsed1, [{content: "hi", type: "em2"}]);
|
|
3608
3601
|
|
|
3609
|
-
// $FlowFixMe
|
|
3610
3602
|
var parser2 = SimpleMarkdown.parserFor({
|
|
3611
3603
|
em1: Object.assign({}, emRule, {
|
|
3612
3604
|
order: -2,
|
|
@@ -3622,7 +3614,6 @@ describe("simple markdown", function () {
|
|
|
3622
3614
|
});
|
|
3623
3615
|
|
|
3624
3616
|
it("should break ties by rule name", function () {
|
|
3625
|
-
// $FlowFixMe
|
|
3626
3617
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3627
3618
|
em1: Object.assign({}, emRule, {
|
|
3628
3619
|
order: 0,
|
|
@@ -3638,7 +3629,6 @@ describe("simple markdown", function () {
|
|
|
3638
3629
|
|
|
3639
3630
|
// ...regardless of their order in the
|
|
3640
3631
|
// original rule definition
|
|
3641
|
-
// $FlowFixMe
|
|
3642
3632
|
var parser2 = SimpleMarkdown.parserFor({
|
|
3643
3633
|
em2: Object.assign({}, emRule, {
|
|
3644
3634
|
order: 0,
|
|
@@ -3658,12 +3648,12 @@ describe("simple markdown", function () {
|
|
|
3658
3648
|
/** @type {any[]} */
|
|
3659
3649
|
var warnings = [];
|
|
3660
3650
|
/*::FLOW_IGNORE_COVARIANCE.*/ console.warn = function (
|
|
3661
|
-
/** @type {any} */ warning,
|
|
3651
|
+
/** @type {any} */ warning: any,
|
|
3662
3652
|
) {
|
|
3653
|
+
// @ts-expect-error [FEI-5003] - TS2345 - Argument of type 'any' is not assignable to parameter of type 'never'.
|
|
3663
3654
|
warnings.push(warning);
|
|
3664
3655
|
};
|
|
3665
3656
|
|
|
3666
|
-
// $FlowFixMe
|
|
3667
3657
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3668
3658
|
em1: Object.assign({}, emRule, {
|
|
3669
3659
|
order: 1 / 0 - 1 / 0,
|
|
@@ -3681,7 +3671,6 @@ describe("simple markdown", function () {
|
|
|
3681
3671
|
});
|
|
3682
3672
|
|
|
3683
3673
|
it("should break ties with quality", function () {
|
|
3684
|
-
// $FlowFixMe
|
|
3685
3674
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3686
3675
|
em1: Object.assign({}, emRule, {
|
|
3687
3676
|
order: 0,
|
|
@@ -3704,7 +3693,6 @@ describe("simple markdown", function () {
|
|
|
3704
3693
|
// ...regardless of their order in the
|
|
3705
3694
|
// original rule definition
|
|
3706
3695
|
|
|
3707
|
-
// $FlowFixMe
|
|
3708
3696
|
var parser2 = SimpleMarkdown.parserFor({
|
|
3709
3697
|
em2: Object.assign({}, emRule, {
|
|
3710
3698
|
order: 0,
|
|
@@ -3726,7 +3714,6 @@ describe("simple markdown", function () {
|
|
|
3726
3714
|
});
|
|
3727
3715
|
|
|
3728
3716
|
it("rules with quality should always win the tie", function () {
|
|
3729
|
-
// $FlowFixMe
|
|
3730
3717
|
var parser1 = SimpleMarkdown.parserFor({
|
|
3731
3718
|
em1: Object.assign({}, emRule, {
|
|
3732
3719
|
order: 0,
|
|
@@ -3745,7 +3732,6 @@ describe("simple markdown", function () {
|
|
|
3745
3732
|
|
|
3746
3733
|
// except if they don't match
|
|
3747
3734
|
|
|
3748
|
-
// $FlowFixMe
|
|
3749
3735
|
var parser2 = SimpleMarkdown.parserFor({
|
|
3750
3736
|
em: Object.assign({}, emRule, {
|
|
3751
3737
|
order: 0,
|
|
@@ -3857,7 +3843,7 @@ describe("simple markdown", function () {
|
|
|
3857
3843
|
}),
|
|
3858
3844
|
};
|
|
3859
3845
|
|
|
3860
|
-
//
|
|
3846
|
+
// @ts-expect-error [FEI-5003] - TS2345 - Argument of type '{ Array: { result: (arr: any, output: any, state: any) => any; }; word: { order: number; match: (source: any) => RegExpExecArray | null; parse: (capture: any, parse: any, state: any) => { ...; }; result: (node: any, output: any, state: any) => any; }; delimiter: SingleNodeParserRule & ... 2 more ... & { ...; }; }' is not assignable to parameter of type 'ParserRules'.
|
|
3861
3847
|
var parse = SimpleMarkdown.parserFor(rules, {wordCount: 0});
|
|
3862
3848
|
var output = SimpleMarkdown.outputFor(rules, "result", {
|
|
3863
3849
|
wordCount: 0,
|
|
@@ -3865,7 +3851,7 @@ describe("simple markdown", function () {
|
|
|
3865
3851
|
|
|
3866
3852
|
// test parsing
|
|
3867
3853
|
/** @type {{ wordCount?: number }} */
|
|
3868
|
-
var parseState = {};
|
|
3854
|
+
var parseState: Record<string, any> = {};
|
|
3869
3855
|
var ast1 = parse("hi here are some words", parseState);
|
|
3870
3856
|
assert.strictEqual(parseState.wordCount, 5);
|
|
3871
3857
|
// and repeated parsing
|
|
@@ -3875,7 +3861,7 @@ describe("simple markdown", function () {
|
|
|
3875
3861
|
|
|
3876
3862
|
// test output
|
|
3877
3863
|
/** @type {{ wordCount?: number }} */
|
|
3878
|
-
var outputState = {};
|
|
3864
|
+
var outputState: Record<string, any> = {};
|
|
3879
3865
|
var result1 = output(ast1, outputState);
|
|
3880
3866
|
assert.deepEqual(result1, ["hi", "here", "are", "some", "words"]);
|
|
3881
3867
|
assert.strictEqual(outputState.wordCount, 5);
|
|
@@ -4132,7 +4118,7 @@ describe("simple markdown", function () {
|
|
|
4132
4118
|
text: SimpleMarkdown.defaultRules.text,
|
|
4133
4119
|
};
|
|
4134
4120
|
|
|
4135
|
-
//
|
|
4121
|
+
// @ts-expect-error [FEI-5003] - TS2345 - Argument of type '{ Array: { readonly react: ArrayNodeOutput<ReactNode>; readonly html: ArrayNodeOutput<string>; }; spoiler: { order: number; match: (source: any) => RegExpExecArray | null; parse: (capture: any, parse: any) => { ...; }; html: (node: any, output: any) => string; }; text: TextInOutRule; }' is not assignable to parameter of type 'ParserRules'.
|
|
4136
4122
|
var parse = SimpleMarkdown.parserFor(rules, {inline: true});
|
|
4137
4123
|
var output = SimpleMarkdown.outputFor(rules, "html");
|
|
4138
4124
|
|
|
@@ -4475,7 +4461,6 @@ describe("simple markdown", function () {
|
|
|
4475
4461
|
}),
|
|
4476
4462
|
});
|
|
4477
4463
|
|
|
4478
|
-
// $FlowFixMe[incompatible-call]
|
|
4479
4464
|
var output = SimpleMarkdown.outputFor(rules, "react");
|
|
4480
4465
|
|
|
4481
4466
|
var parsed = SimpleMarkdown.defaultInlineParse("Hi! You! Are! <3!");
|
|
@@ -4961,10 +4946,11 @@ describe("simple markdown", function () {
|
|
|
4961
4946
|
match: (source, state, prevCapture) => true,
|
|
4962
4947
|
parse,
|
|
4963
4948
|
},
|
|
4964
|
-
};
|
|
4949
|
+
} as const;
|
|
4965
4950
|
|
|
4966
4951
|
expect(() =>
|
|
4967
4952
|
// $FlowFixMe[incompatible-call]
|
|
4953
|
+
// @ts-expect-error [FEI-5003] - TS2345 - Argument of type '{ readonly parseDoesntReturnCorrectType: { readonly order: 1; readonly match: (source: any, state: any, prevCapture: any) => boolean; readonly parse: Mock<any, any, any>; }; }' is not assignable to parameter of type 'ParserRules'.
|
|
4968
4954
|
SimpleMarkdown.parserFor(invalidRules)("some input"),
|
|
4969
4955
|
).toThrow();
|
|
4970
4956
|
expect(parse).toHaveBeenCalled();
|