Skip to content

Commit 454d370

Browse files
committed
Fix the emphasis delimiter length for people that want to do stupid things.
1 parent 80a7d91 commit 454d370

File tree

5 files changed

+137
-25
lines changed

5 files changed

+137
-25
lines changed

package-lock.json

Lines changed: 2 additions & 2 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.15",
3+
"version": "0.0.1-beta.16",
44
"description": "PG language support for CodeMirror",
55
"author": "The WeBWorK Project",
66
"license": "MIT",

src/pgml-parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ export class PGMLParse {
379379
const type = BlockDefs[token.substring(0, 1)]?.type;
380380
let block = this.block;
381381
if (block && block.type === type) {
382-
block.to += 1;
382+
block.to += block.token?.length ?? 1;
383383
this.Terminate();
384384
return;
385385
}
386386
while (block?.type !== 'root') {
387387
if (block && block.prev?.type === type) {
388388
this.End(`end of ${type ?? ''}`, block);
389-
block.to += 1;
389+
block.to += block.token?.length ?? 1;
390390
this.Terminate();
391391
return;
392392
}

src/pgml.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ const pgmlFormat = (block: Item, offset: number): Element<Type>[] => {
332332
children.push(elt(Type.OptionMark, block.to - 1 + offset, block.to + offset));
333333
return [elt(Type.Option, block.from + offset, block.to + offset, children)];
334334
} else if (block.type === 'bold' || block.type === 'italic') {
335-
children.unshift(elt(Type.EmphasisMark, block.from + offset, block.from + 1 + offset));
335+
children.unshift(elt(Type.EmphasisMark, block.from + offset, block.from + (block.token?.length ?? 1) + offset));
336336
const lastChild = block.stack?.at(-1);
337337
if (lastChild instanceof Item && block.to === lastChild.to)
338338
children.push(elt(Type.PGMLError, block.to + offset, block.to + offset));
339-
else children.push(elt(Type.EmphasisMark, block.to - 1 + offset, block.to + offset));
339+
else children.push(elt(Type.EmphasisMark, block.to - (block.token?.length ?? 1) + offset, block.to + offset));
340340
return [
341341
elt(
342342
block.type === 'bold' ? Type.StrongEmphasis : Type.Emphasis,

test/pgml-formatting.txt

Lines changed: 130 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
BEGIN_PGML
44
The following is *bold text with math [`x + 3`]*
5+
6+
The following is bold with too many delimiters ***bold text****
57
END_PGML
68

79
==>
810

911
{
1012
"name": "Program",
1113
"from": 0,
12-
"to": 68,
14+
"to": 133,
1315
"children": [
1416
{
1517
"name": "PGMLBlock",
1618
"from": 0,
17-
"to": 68,
19+
"to": 133,
1820
"children": [
1921
{ "name": "BeginPG", "from": 0, "to": 10 },
2022
{
2123
"name": "PGMLContent",
2224
"from": 10,
23-
"to": 60,
25+
"to": 125,
2426
"children": [
2527
{
2628
"name": "Paragraph",
2729
"from": 10,
28-
"to": 60,
30+
"to": 59,
2931
"children": [
3032
{
3133
"name": "StrongEmphasis",
@@ -46,10 +48,26 @@ END_PGML
4648
]
4749
}
4850
]
51+
},
52+
{
53+
"name": "Paragraph",
54+
"from": 61,
55+
"to": 124,
56+
"children": [
57+
{
58+
"name": "StrongEmphasis",
59+
"from": 108,
60+
"to": 123,
61+
"children": [
62+
{ "name": "EmphasisMark", "from": 108, "to": 111 },
63+
{ "name": "EmphasisMark", "from": 120, "to": 123 }
64+
]
65+
}
66+
]
4967
}
5068
]
5169
},
52-
{ "name": "EndPG", "from": 60, "to": 68 }
70+
{ "name": "EndPG", "from": 125, "to": 133 }
5371
]
5472
}
5573
]
@@ -59,14 +77,14 @@ END_PGML
5977

6078
from: '0'
6179
parseAll: 'true'
62-
to: '49'
80+
to: '114'
6381
type: 'root'
6482
stack: [
6583
[ # 0
6684
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
6785
from: '0'
6886
indent: '0'
69-
to: '49'
87+
to: '48'
7088
type: 'indent'
7189
stack: [
7290
[ # 0
@@ -107,10 +125,48 @@ stack: [
107125
]
108126
]
109127
]
128+
]
129+
]
130+
[ # 1
131+
from: '48'
132+
noIndent: '1'
133+
to: '50'
134+
token: '\n\n'
135+
type: 'par'
136+
]
137+
[ # 2
138+
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
139+
from: '50'
140+
indent: '0'
141+
to: '113'
142+
type: 'indent'
143+
stack: [
144+
[ # 0
145+
combine: { text: 'type' }
146+
from: '50'
147+
to: '97'
148+
type: 'text'
149+
stack: ['The following is bold with too many delimiters ']
150+
]
151+
[ # 1
152+
from: '97'
153+
to: '112'
154+
token: '***'
155+
type: 'bold'
156+
stack: [
157+
[ # 0
158+
combine: { text: 'type' }
159+
from: '100'
160+
to: '109'
161+
type: 'text'
162+
stack: ['bold text']
163+
]
164+
]
165+
]
110166
[ # 2
111167
combine: { text: 'type' }
112-
from: '48'
113-
to: '49'
168+
from: '112'
169+
to: '113'
114170
type: 'text'
115171
stack: ['\n']
116172
]
@@ -122,30 +178,32 @@ stack: [
122178

123179
BEGIN_PGML
124180
The following is _italic text_
181+
182+
The following is poor taste ____italic text___
125183
END_PGML
126184

127185
==>
128186

129187
{
130188
"name": "Program",
131189
"from": 0,
132-
"to": 50,
190+
"to": 98,
133191
"children": [
134192
{
135193
"name": "PGMLBlock",
136194
"from": 0,
137-
"to": 50,
195+
"to": 98,
138196
"children": [
139197
{ "name": "BeginPG", "from": 0, "to": 10 },
140198
{
141199
"name": "PGMLContent",
142200
"from": 10,
143-
"to": 42,
201+
"to": 91,
144202
"children": [
145203
{
146204
"name": "Paragraph",
147205
"from": 10,
148-
"to": 42,
206+
"to": 41,
149207
"children": [
150208
{
151209
"name": "Emphasis",
@@ -157,10 +215,26 @@ END_PGML
157215
]
158216
}
159217
]
218+
},
219+
{
220+
"name": "Paragraph",
221+
"from": 43,
222+
"to": 91,
223+
"children": [
224+
{
225+
"name": "Emphasis",
226+
"from": 71,
227+
"to": 90,
228+
"children": [
229+
{ "name": "EmphasisMark", "from": 71, "to": 75 },
230+
{ "name": "EmphasisMark", "from": 86, "to": 90 }
231+
]
232+
}
233+
]
160234
}
161235
]
162236
},
163-
{ "name": "EndPG", "from": 42, "to": 50 }
237+
{ "name": "EndPG", "from": 90, "to": 98 }
164238
]
165239
}
166240
]
@@ -170,14 +244,14 @@ END_PGML
170244

171245
from: '0'
172246
parseAll: 'true'
173-
to: '31'
247+
to: '79'
174248
type: 'root'
175249
stack: [
176250
[ # 0
177251
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
178252
from: '0'
179253
indent: '0'
180-
to: '31'
254+
to: '30'
181255
type: 'indent'
182256
stack: [
183257
[ # 0
@@ -202,10 +276,48 @@ stack: [
202276
]
203277
]
204278
]
279+
]
280+
]
281+
[ # 1
282+
from: '30'
283+
noIndent: '1'
284+
to: '32'
285+
token: '\n\n'
286+
type: 'par'
287+
]
288+
[ # 2
289+
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
290+
from: '32'
291+
indent: '0'
292+
to: '80'
293+
type: 'indent'
294+
stack: [
295+
[ # 0
296+
combine: { text: 'type' }
297+
from: '32'
298+
to: '60'
299+
type: 'text'
300+
stack: ['The following is poor taste ']
301+
]
302+
[ # 1
303+
from: '60'
304+
to: '79'
305+
token: '____'
306+
type: 'italic'
307+
stack: [
308+
[ # 0
309+
combine: { text: 'type' }
310+
from: '64'
311+
to: '75'
312+
type: 'text'
313+
stack: ['italic text']
314+
]
315+
]
316+
]
205317
[ # 2
206318
combine: { text: 'type' }
207-
from: '30'
208-
to: '31'
319+
from: '79'
320+
to: '80'
209321
type: 'text'
210322
stack: ['\n']
211323
]

0 commit comments

Comments
 (0)