Skip to content

Commit 9d46db7

Browse files
committed
Fix a bug in the prevChar method of the PGML parser.
This was an error in conversion from the Perl `substr` method to the JavaScript `substring` method. This caused emphasis to not work correctly.
1 parent eca5ac6 commit 9d46db7

File tree

4 files changed

+74
-58
lines changed

4 files changed

+74
-58
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openwebwork/codemirror-lang-pg",
3-
"version": "0.0.1-beta.14",
3+
"version": "0.0.1-beta.15",
44
"description": "PG language support for CodeMirror",
55
"author": "The WeBWorK Project",
66
"license": "MIT",

src/pgml-parse.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,19 @@ export class PGMLParse {
110110
}
111111

112112
nextChar(defaultNext?: string) {
113-
if (typeof defaultNext === 'undefined') defaultNext = '';
114-
return (this.split[this.i] ?? this.split[this.i + 1] ?? defaultNext).substring(0, 1);
113+
return (
114+
this.split[this.i] ??
115+
this.split[this.i + 1] ??
116+
(typeof defaultNext === 'undefined' ? '' : defaultNext)
117+
).substring(0, 1);
115118
}
116119

117120
prevChar(defaultPrev?: string) {
118-
if (typeof defaultPrev === 'undefined') defaultPrev = '';
119-
let i2 = this.i - 2;
120-
if (i2 < 0) i2 = 0;
121-
let i3 = this.i - 3;
122-
if (i3 < 0) i3 = 0;
123-
return (this.split[i2] ?? this.split[i3] ?? defaultPrev).substring(-1, 1);
121+
return (
122+
this.split[this.i >= 2 ? this.i - 2 : 0] ??
123+
this.split[this.i >= 3 ? this.i - 3 : 0] ??
124+
(typeof defaultPrev === 'undefined' ? '' : defaultPrev)
125+
).slice(-1);
124126
}
125127

126128
Parse(split: (string | undefined)[]) {

test/pgml-formatting.txt

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
# bold text
22

33
BEGIN_PGML
4-
*bold text with math [`x + 3`]*
4+
The following is *bold text with math [`x + 3`]*
55
END_PGML
66

77
==>
88

99
{
1010
"name": "Program",
1111
"from": 0,
12-
"to": 51,
12+
"to": 68,
1313
"children": [
1414
{
1515
"name": "PGMLBlock",
1616
"from": 0,
17-
"to": 51,
17+
"to": 68,
1818
"children": [
1919
{ "name": "BeginPG", "from": 0, "to": 10 },
2020
{
2121
"name": "PGMLContent",
2222
"from": 10,
23-
"to": 43,
23+
"to": 60,
2424
"children": [
2525
{
2626
"name": "Paragraph",
2727
"from": 10,
28-
"to": 43,
28+
"to": 60,
2929
"children": [
3030
{
3131
"name": "StrongEmphasis",
32-
"from": 11,
33-
"to": 42,
32+
"from": 28,
33+
"to": 59,
3434
"children": [
35-
{ "name": "EmphasisMark", "from": 11, "to": 12 },
35+
{ "name": "EmphasisMark", "from": 28, "to": 29 },
3636
{
3737
"name": "MathMode",
38-
"from": 32,
39-
"to": 41,
38+
"from": 49,
39+
"to": 58,
4040
"children": [
41-
{ "name": "MathModeMark", "from": 32, "to": 34 },
42-
{ "name": "MathModeMark", "from": 39, "to": 41 }
41+
{ "name": "MathModeMark", "from": 49, "to": 51 },
42+
{ "name": "MathModeMark", "from": 56, "to": 58 }
4343
]
4444
},
45-
{ "name": "EmphasisMark", "from": 41, "to": 42 }
45+
{ "name": "EmphasisMark", "from": 58, "to": 59 }
4646
]
4747
}
4848
]
4949
}
5050
]
5151
},
52-
{ "name": "EndPG", "from": 43, "to": 51 }
52+
{ "name": "EndPG", "from": 60, "to": 68 }
5353
]
5454
}
5555
]
@@ -59,51 +59,58 @@ END_PGML
5959

6060
from: '0'
6161
parseAll: 'true'
62-
to: '32'
62+
to: '49'
6363
type: 'root'
6464
stack: [
6565
[ # 0
6666
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
6767
from: '0'
6868
indent: '0'
69-
to: '32'
69+
to: '49'
7070
type: 'indent'
7171
stack: [
7272
[ # 0
73+
combine: { text: 'type' }
7374
from: '0'
74-
to: '31'
75+
to: '17'
76+
type: 'text'
77+
stack: ['The following is ']
78+
]
79+
[ # 1
80+
from: '17'
81+
to: '48'
7582
token: '*'
7683
type: 'bold'
7784
stack: [
7885
[ # 0
7986
combine: { text: 'type' }
80-
from: '1'
81-
to: '21'
87+
from: '18'
88+
to: '38'
8289
type: 'text'
8390
stack: ['bold text with math ']
8491
]
8592
[ # 1
8693
children: [
8794
{
8895
combine: { text: 'type' }
89-
from: '23'
90-
to: '28'
96+
from: '40'
97+
to: '45'
9198
type: 'text'
9299
stack: ['x + 3']
93100
}
94101
]
95-
from: '21'
102+
from: '38'
96103
terminator: '`]'
97-
to: '30'
104+
to: '47'
98105
token: '[`'
99106
type: 'math'
100107
]
101108
]
102109
]
103-
[ # 1
110+
[ # 2
104111
combine: { text: 'type' }
105-
from: '31'
106-
to: '32'
112+
from: '48'
113+
to: '49'
107114
type: 'text'
108115
stack: ['\n']
109116
]
@@ -114,46 +121,46 @@ stack: [
114121
# italic text
115122

116123
BEGIN_PGML
117-
_italic text_
124+
The following is _italic text_
118125
END_PGML
119126

120127
==>
121128

122129
{
123130
"name": "Program",
124131
"from": 0,
125-
"to": 33,
132+
"to": 50,
126133
"children": [
127134
{
128135
"name": "PGMLBlock",
129136
"from": 0,
130-
"to": 33,
137+
"to": 50,
131138
"children": [
132139
{ "name": "BeginPG", "from": 0, "to": 10 },
133140
{
134141
"name": "PGMLContent",
135142
"from": 10,
136-
"to": 25,
143+
"to": 42,
137144
"children": [
138145
{
139146
"name": "Paragraph",
140147
"from": 10,
141-
"to": 25,
148+
"to": 42,
142149
"children": [
143150
{
144151
"name": "Emphasis",
145-
"from": 11,
146-
"to": 24,
152+
"from": 28,
153+
"to": 41,
147154
"children": [
148-
{ "name": "EmphasisMark", "from": 11, "to": 12 },
149-
{ "name": "EmphasisMark", "from": 23, "to": 24 }
155+
{ "name": "EmphasisMark", "from": 28, "to": 29 },
156+
{ "name": "EmphasisMark", "from": 40, "to": 41 }
150157
]
151158
}
152159
]
153160
}
154161
]
155162
},
156-
{ "name": "EndPG", "from": 25, "to": 33 }
163+
{ "name": "EndPG", "from": 42, "to": 50 }
157164
]
158165
}
159166
]
@@ -163,35 +170,42 @@ END_PGML
163170

164171
from: '0'
165172
parseAll: 'true'
166-
to: '14'
173+
to: '31'
167174
type: 'root'
168175
stack: [
169176
[ # 0
170177
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
171178
from: '0'
172179
indent: '0'
173-
to: '14'
180+
to: '31'
174181
type: 'indent'
175182
stack: [
176183
[ # 0
184+
combine: { text: 'type' }
177185
from: '0'
178-
to: '13'
186+
to: '17'
187+
type: 'text'
188+
stack: ['The following is ']
189+
]
190+
[ # 1
191+
from: '17'
192+
to: '30'
179193
token: '_'
180194
type: 'italic'
181195
stack: [
182196
[ # 0
183197
combine: { text: 'type' }
184-
from: '1'
185-
to: '12'
198+
from: '18'
199+
to: '29'
186200
type: 'text'
187201
stack: ['italic text']
188202
]
189203
]
190204
]
191-
[ # 1
205+
[ # 2
192206
combine: { text: 'type' }
193-
from: '13'
194-
to: '14'
207+
from: '30'
208+
to: '31'
195209
type: 'text'
196210
stack: ['\n']
197211
]

0 commit comments

Comments
 (0)