@mtcute/markdown-parser 0.22.2 → 0.23.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.
Files changed (3) hide show
  1. package/index.cjs +38 -2
  2. package/index.js +38 -2
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -109,6 +109,8 @@ function parse(strings, ...sub) {
109
109
  let insideCode = false;
110
110
  let insidePre = false;
111
111
  let insideLink = false;
112
+ let currentBlockquoteStart = null;
113
+ let prevBlockquote = null;
112
114
  let insideLinkUrl = false;
113
115
  let pendingLinkUrl = "";
114
116
  function feed(text) {
@@ -298,10 +300,32 @@ function parse(strings, ...sub) {
298
300
  pos += nonWhitespace + 1;
299
301
  } else {
300
302
  pos = len;
301
- result = result.trimEnd();
303
+ }
304
+ if (currentBlockquoteStart != null) {
305
+ const prevEnd = prevBlockquote ? prevBlockquote.offset + prevBlockquote.length : Number.NaN;
306
+ if (currentBlockquoteStart - 1 === prevEnd) {
307
+ prevBlockquote.length += result.length - currentBlockquoteStart;
308
+ } else {
309
+ if (prevBlockquote) entities.push(prevBlockquote);
310
+ prevBlockquote = {
311
+ _: "messageEntityBlockquote",
312
+ offset: currentBlockquoteStart,
313
+ length: result.length - currentBlockquoteStart - 1
314
+ };
315
+ }
316
+ currentBlockquoteStart = null;
302
317
  }
303
318
  continue;
304
319
  }
320
+ if (c === ">" && (result.length === 0 || result[result.length - 1] === "\n")) {
321
+ currentBlockquoteStart = result.length;
322
+ pos += 1;
323
+ continue;
324
+ }
325
+ if (c === " " && currentBlockquoteStart === result.length) {
326
+ pos += 1;
327
+ continue;
328
+ }
305
329
  result += c;
306
330
  pos += 1;
307
331
  }
@@ -337,6 +361,7 @@ function parse(strings, ...sub) {
337
361
  }
338
362
  });
339
363
  feed(strings[strings.length - 1]);
364
+ if (prevBlockquote) entities.push(prevBlockquote);
340
365
  function adjustOffsets(from, by) {
341
366
  for (const ent of entities) {
342
367
  if (ent.offset >= from) ent.offset += by;
@@ -384,9 +409,20 @@ function parse(strings, ...sub) {
384
409
  }
385
410
  }
386
411
  }
412
+ const resultTrimmed = result.trimStart();
413
+ const numCharsTrimmed = result.length - resultTrimmed.length;
414
+ result = resultTrimmed;
415
+ adjustOffsets(0, -numCharsTrimmed);
416
+ result = result.trimEnd();
417
+ const finalEntities = [];
418
+ for (const ent of entities) {
419
+ const end = ent.offset + ent.length;
420
+ if (end > result.length) ent.length = result.length - ent.offset;
421
+ if (ent.length > 0) finalEntities.push(ent);
422
+ }
387
423
  return {
388
424
  text: result,
389
- entities
425
+ entities: finalEntities
390
426
  };
391
427
  }
392
428
  const md = Object.assign(parse, {
package/index.js CHANGED
@@ -102,6 +102,8 @@ function parse(strings, ...sub) {
102
102
  let insideCode = false;
103
103
  let insidePre = false;
104
104
  let insideLink = false;
105
+ let currentBlockquoteStart = null;
106
+ let prevBlockquote = null;
105
107
  let insideLinkUrl = false;
106
108
  let pendingLinkUrl = "";
107
109
  function feed(text) {
@@ -291,10 +293,32 @@ function parse(strings, ...sub) {
291
293
  pos += nonWhitespace + 1;
292
294
  } else {
293
295
  pos = len;
294
- result = result.trimEnd();
296
+ }
297
+ if (currentBlockquoteStart != null) {
298
+ const prevEnd = prevBlockquote ? prevBlockquote.offset + prevBlockquote.length : Number.NaN;
299
+ if (currentBlockquoteStart - 1 === prevEnd) {
300
+ prevBlockquote.length += result.length - currentBlockquoteStart;
301
+ } else {
302
+ if (prevBlockquote) entities.push(prevBlockquote);
303
+ prevBlockquote = {
304
+ _: "messageEntityBlockquote",
305
+ offset: currentBlockquoteStart,
306
+ length: result.length - currentBlockquoteStart - 1
307
+ };
308
+ }
309
+ currentBlockquoteStart = null;
295
310
  }
296
311
  continue;
297
312
  }
313
+ if (c === ">" && (result.length === 0 || result[result.length - 1] === "\n")) {
314
+ currentBlockquoteStart = result.length;
315
+ pos += 1;
316
+ continue;
317
+ }
318
+ if (c === " " && currentBlockquoteStart === result.length) {
319
+ pos += 1;
320
+ continue;
321
+ }
298
322
  result += c;
299
323
  pos += 1;
300
324
  }
@@ -330,6 +354,7 @@ function parse(strings, ...sub) {
330
354
  }
331
355
  });
332
356
  feed(strings[strings.length - 1]);
357
+ if (prevBlockquote) entities.push(prevBlockquote);
333
358
  function adjustOffsets(from, by) {
334
359
  for (const ent of entities) {
335
360
  if (ent.offset >= from) ent.offset += by;
@@ -377,9 +402,20 @@ function parse(strings, ...sub) {
377
402
  }
378
403
  }
379
404
  }
405
+ const resultTrimmed = result.trimStart();
406
+ const numCharsTrimmed = result.length - resultTrimmed.length;
407
+ result = resultTrimmed;
408
+ adjustOffsets(0, -numCharsTrimmed);
409
+ result = result.trimEnd();
410
+ const finalEntities = [];
411
+ for (const ent of entities) {
412
+ const end = ent.offset + ent.length;
413
+ if (end > result.length) ent.length = result.length - ent.offset;
414
+ if (ent.length > 0) finalEntities.push(ent);
415
+ }
380
416
  return {
381
417
  text: result,
382
- entities
418
+ entities: finalEntities
383
419
  };
384
420
  }
385
421
  const md = Object.assign(parse, {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@mtcute/markdown-parser",
3
3
  "type": "module",
4
- "version": "0.22.2",
4
+ "version": "0.23.0",
5
5
  "description": "Markdown entities parser for mtcute",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
- "@mtcute/core": "^0.22.2",
8
+ "@mtcute/core": "^0.23.0",
9
9
  "long": "5.2.3"
10
10
  },
11
11
  "exports": {